formatting
This commit is contained in:
parent
a61bd97d9b
commit
c5384cb514
|
@ -54,14 +54,11 @@ void BasicIOInit(Picoc *pc)
|
||||||
/* output a single character to either a FILE * or a string */
|
/* output a single character to either a FILE * or a string */
|
||||||
void StdioOutPutc(int OutCh, StdOutStream *Stream)
|
void StdioOutPutc(int OutCh, StdOutStream *Stream)
|
||||||
{
|
{
|
||||||
if (Stream->FilePtr != NULL)
|
if (Stream->FilePtr != NULL) {
|
||||||
{
|
|
||||||
/* output to stdio stream */
|
/* output to stdio stream */
|
||||||
putc(OutCh, Stream->FilePtr);
|
putc(OutCh, Stream->FilePtr);
|
||||||
Stream->CharCount++;
|
Stream->CharCount++;
|
||||||
}
|
} else if (Stream->StrOutLen < 0 || Stream->StrOutLen > 1) {
|
||||||
else if (Stream->StrOutLen < 0 || Stream->StrOutLen > 1)
|
|
||||||
{
|
|
||||||
/* output to a string */
|
/* output to a string */
|
||||||
*Stream->StrOutPtr = OutCh;
|
*Stream->StrOutPtr = OutCh;
|
||||||
Stream->StrOutPtr++;
|
Stream->StrOutPtr++;
|
||||||
|
@ -76,18 +73,13 @@ void StdioOutPutc(int OutCh, StdOutStream *Stream)
|
||||||
/* output a string to either a FILE * or a string */
|
/* output a string to either a FILE * or a string */
|
||||||
void StdioOutPuts(const char *Str, StdOutStream *Stream)
|
void StdioOutPuts(const char *Str, StdOutStream *Stream)
|
||||||
{
|
{
|
||||||
if (Stream->FilePtr != NULL)
|
if (Stream->FilePtr != NULL) {
|
||||||
{
|
|
||||||
/* output to stdio stream */
|
/* output to stdio stream */
|
||||||
fputs(Str, Stream->FilePtr);
|
fputs(Str, Stream->FilePtr);
|
||||||
}
|
} else {
|
||||||
else
|
|
||||||
{
|
|
||||||
/* output to a string */
|
/* output to a string */
|
||||||
while (*Str != '\0')
|
while (*Str != '\0') {
|
||||||
{
|
if (Stream->StrOutLen < 0 || Stream->StrOutLen > 1) {
|
||||||
if (Stream->StrOutLen < 0 || Stream->StrOutLen > 1)
|
|
||||||
{
|
|
||||||
/* output to a string */
|
/* output to a string */
|
||||||
*Stream->StrOutPtr = *Str;
|
*Stream->StrOutPtr = *Str;
|
||||||
Str++;
|
Str++;
|
||||||
|
@ -107,9 +99,7 @@ void StdioFprintfWord(StdOutStream *Stream, const char *Format, unsigned long Va
|
||||||
{
|
{
|
||||||
if (Stream->FilePtr != NULL)
|
if (Stream->FilePtr != NULL)
|
||||||
Stream->CharCount += fprintf(Stream->FilePtr, Format, Value);
|
Stream->CharCount += fprintf(Stream->FilePtr, Format, Value);
|
||||||
|
else if (Stream->StrOutLen >= 0) {
|
||||||
else if (Stream->StrOutLen >= 0)
|
|
||||||
{
|
|
||||||
#ifndef WIN32
|
#ifndef WIN32
|
||||||
int CCount = snprintf(Stream->StrOutPtr, Stream->StrOutLen, Format, Value);
|
int CCount = snprintf(Stream->StrOutPtr, Stream->StrOutLen, Format, Value);
|
||||||
#else
|
#else
|
||||||
|
@ -118,9 +108,7 @@ void StdioFprintfWord(StdOutStream *Stream, const char *Format, unsigned long Va
|
||||||
Stream->StrOutPtr += CCount;
|
Stream->StrOutPtr += CCount;
|
||||||
Stream->StrOutLen -= CCount;
|
Stream->StrOutLen -= CCount;
|
||||||
Stream->CharCount += CCount;
|
Stream->CharCount += CCount;
|
||||||
}
|
} else {
|
||||||
else
|
|
||||||
{
|
|
||||||
int CCount = sprintf(Stream->StrOutPtr, Format, Value);
|
int CCount = sprintf(Stream->StrOutPtr, Format, Value);
|
||||||
Stream->CharCount += CCount;
|
Stream->CharCount += CCount;
|
||||||
Stream->StrOutPtr += CCount;
|
Stream->StrOutPtr += CCount;
|
||||||
|
@ -132,9 +120,7 @@ void StdioFprintfFP(StdOutStream *Stream, const char *Format, double Value)
|
||||||
{
|
{
|
||||||
if (Stream->FilePtr != NULL)
|
if (Stream->FilePtr != NULL)
|
||||||
Stream->CharCount += fprintf(Stream->FilePtr, Format, Value);
|
Stream->CharCount += fprintf(Stream->FilePtr, Format, Value);
|
||||||
|
else if (Stream->StrOutLen >= 0) {
|
||||||
else if (Stream->StrOutLen >= 0)
|
|
||||||
{
|
|
||||||
#ifndef WIN32
|
#ifndef WIN32
|
||||||
int CCount = snprintf(Stream->StrOutPtr, Stream->StrOutLen, Format, Value);
|
int CCount = snprintf(Stream->StrOutPtr, Stream->StrOutLen, Format, Value);
|
||||||
#else
|
#else
|
||||||
|
@ -143,9 +129,7 @@ void StdioFprintfFP(StdOutStream *Stream, const char *Format, double Value)
|
||||||
Stream->StrOutPtr += CCount;
|
Stream->StrOutPtr += CCount;
|
||||||
Stream->StrOutLen -= CCount;
|
Stream->StrOutLen -= CCount;
|
||||||
Stream->CharCount += CCount;
|
Stream->CharCount += CCount;
|
||||||
}
|
} else {
|
||||||
else
|
|
||||||
{
|
|
||||||
int CCount = sprintf(Stream->StrOutPtr, Format, Value);
|
int CCount = sprintf(Stream->StrOutPtr, Format, Value);
|
||||||
Stream->CharCount += CCount;
|
Stream->CharCount += CCount;
|
||||||
Stream->StrOutPtr += CCount;
|
Stream->StrOutPtr += CCount;
|
||||||
|
@ -157,9 +141,7 @@ void StdioFprintfPointer(StdOutStream *Stream, const char *Format, void *Value)
|
||||||
{
|
{
|
||||||
if (Stream->FilePtr != NULL)
|
if (Stream->FilePtr != NULL)
|
||||||
Stream->CharCount += fprintf(Stream->FilePtr, Format, Value);
|
Stream->CharCount += fprintf(Stream->FilePtr, Format, Value);
|
||||||
|
else if (Stream->StrOutLen >= 0) {
|
||||||
else if (Stream->StrOutLen >= 0)
|
|
||||||
{
|
|
||||||
#ifndef WIN32
|
#ifndef WIN32
|
||||||
int CCount = snprintf(Stream->StrOutPtr, Stream->StrOutLen, Format, Value);
|
int CCount = snprintf(Stream->StrOutPtr, Stream->StrOutLen, Format, Value);
|
||||||
#else
|
#else
|
||||||
|
@ -168,9 +150,7 @@ void StdioFprintfPointer(StdOutStream *Stream, const char *Format, void *Value)
|
||||||
Stream->StrOutPtr += CCount;
|
Stream->StrOutPtr += CCount;
|
||||||
Stream->StrOutLen -= CCount;
|
Stream->StrOutLen -= CCount;
|
||||||
Stream->CharCount += CCount;
|
Stream->CharCount += CCount;
|
||||||
}
|
} else {
|
||||||
else
|
|
||||||
{
|
|
||||||
int CCount = sprintf(Stream->StrOutPtr, Format, Value);
|
int CCount = sprintf(Stream->StrOutPtr, Format, Value);
|
||||||
Stream->CharCount += CCount;
|
Stream->CharCount += CCount;
|
||||||
Stream->StrOutPtr += CCount;
|
Stream->StrOutPtr += CCount;
|
||||||
|
@ -198,20 +178,16 @@ int StdioBasePrintf(struct ParseState *Parser, FILE *Stream, char *StrOut, int S
|
||||||
SOStream.StrOutLen = StrOutLen;
|
SOStream.StrOutLen = StrOutLen;
|
||||||
SOStream.CharCount = 0;
|
SOStream.CharCount = 0;
|
||||||
|
|
||||||
while (*FPos != '\0')
|
while (*FPos != '\0') {
|
||||||
{
|
if (*FPos == '%') {
|
||||||
if (*FPos == '%')
|
|
||||||
{
|
|
||||||
/* work out what type we're printing */
|
/* work out what type we're printing */
|
||||||
FPos++;
|
FPos++;
|
||||||
ShowType = NULL;
|
ShowType = NULL;
|
||||||
OneFormatBuf[0] = '%';
|
OneFormatBuf[0] = '%';
|
||||||
OneFormatCount = 1;
|
OneFormatCount = 1;
|
||||||
|
|
||||||
do
|
do {
|
||||||
{
|
switch (*FPos) {
|
||||||
switch (*FPos)
|
|
||||||
{
|
|
||||||
case 'd': case 'i': ShowType = &pc->IntType; break; /* integer decimal */
|
case 'd': case 'i': ShowType = &pc->IntType; break; /* integer decimal */
|
||||||
case 'o': case 'u': case 'x': case 'X': ShowType = &pc->IntType; break; /* integer base conversions */
|
case 'o': case 'u': case 'x': case 'X': ShowType = &pc->IntType; break; /* integer base conversions */
|
||||||
#ifndef NO_FP
|
#ifndef NO_FP
|
||||||
|
@ -234,10 +210,8 @@ int StdioBasePrintf(struct ParseState *Parser, FILE *Stream, char *StrOut, int S
|
||||||
OneFormatCount++;
|
OneFormatCount++;
|
||||||
|
|
||||||
/* do special actions depending on the conversion type */
|
/* do special actions depending on the conversion type */
|
||||||
if (ShowType == &pc->VoidType)
|
if (ShowType == &pc->VoidType) {
|
||||||
{
|
switch (*FPos) {
|
||||||
switch (*FPos)
|
|
||||||
{
|
|
||||||
case 'm': StdioOutPuts(strerror(errno), &SOStream); break;
|
case 'm': StdioOutPuts(strerror(errno), &SOStream); break;
|
||||||
case '%': StdioOutPutc(*FPos, &SOStream); break;
|
case '%': StdioOutPutc(*FPos, &SOStream); break;
|
||||||
case '\0': OneFormatBuf[OneFormatCount] = '\0'; StdioOutPutc(*FPos, &SOStream); break;
|
case '\0': OneFormatBuf[OneFormatCount] = '\0'; StdioOutPutc(*FPos, &SOStream); break;
|
||||||
|
@ -253,19 +227,16 @@ int StdioBasePrintf(struct ParseState *Parser, FILE *Stream, char *StrOut, int S
|
||||||
|
|
||||||
} while (ShowType == NULL && OneFormatCount < MAX_FORMAT);
|
} while (ShowType == NULL && OneFormatCount < MAX_FORMAT);
|
||||||
|
|
||||||
if (ShowType != &pc->VoidType)
|
if (ShowType != &pc->VoidType) {
|
||||||
{
|
|
||||||
if (ArgCount >= Args->NumArgs)
|
if (ArgCount >= Args->NumArgs)
|
||||||
StdioOutPuts("XXX", &SOStream);
|
StdioOutPuts("XXX", &SOStream);
|
||||||
else
|
else {
|
||||||
{
|
|
||||||
/* null-terminate the buffer */
|
/* null-terminate the buffer */
|
||||||
OneFormatBuf[OneFormatCount] = '\0';
|
OneFormatBuf[OneFormatCount] = '\0';
|
||||||
|
|
||||||
/* print this argument */
|
/* print this argument */
|
||||||
ThisArg = (struct Value *)((char *)ThisArg + MEM_ALIGN(sizeof(struct Value) + TypeStackSizeValue(ThisArg)));
|
ThisArg = (struct Value *)((char *)ThisArg + MEM_ALIGN(sizeof(struct Value) + TypeStackSizeValue(ThisArg)));
|
||||||
if (ShowType == &pc->IntType)
|
if (ShowType == &pc->IntType) {
|
||||||
{
|
|
||||||
/* show a signed integer */
|
/* show a signed integer */
|
||||||
if (IS_NUMERIC_COERCIBLE(ThisArg))
|
if (IS_NUMERIC_COERCIBLE(ThisArg))
|
||||||
StdioFprintfWord(&SOStream, OneFormatBuf, ExpressionCoerceUnsignedInteger(ThisArg));
|
StdioFprintfWord(&SOStream, OneFormatBuf, ExpressionCoerceUnsignedInteger(ThisArg));
|
||||||
|
@ -273,8 +244,7 @@ int StdioBasePrintf(struct ParseState *Parser, FILE *Stream, char *StrOut, int S
|
||||||
StdioOutPuts("XXX", &SOStream);
|
StdioOutPuts("XXX", &SOStream);
|
||||||
}
|
}
|
||||||
#ifndef NO_FP
|
#ifndef NO_FP
|
||||||
else if (ShowType == &pc->FPType)
|
else if (ShowType == &pc->FPType) {
|
||||||
{
|
|
||||||
/* show a floating point number */
|
/* show a floating point number */
|
||||||
if (IS_NUMERIC_COERCIBLE(ThisArg))
|
if (IS_NUMERIC_COERCIBLE(ThisArg))
|
||||||
StdioFprintfFP(&SOStream, OneFormatBuf, ExpressionCoerceFP(ThisArg));
|
StdioFprintfFP(&SOStream, OneFormatBuf, ExpressionCoerceFP(ThisArg));
|
||||||
|
@ -282,25 +252,18 @@ int StdioBasePrintf(struct ParseState *Parser, FILE *Stream, char *StrOut, int S
|
||||||
StdioOutPuts("XXX", &SOStream);
|
StdioOutPuts("XXX", &SOStream);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
else if (ShowType == pc->CharPtrType)
|
else if (ShowType == pc->CharPtrType) {
|
||||||
{
|
|
||||||
if (ThisArg->Typ->Base == TypePointer)
|
if (ThisArg->Typ->Base == TypePointer)
|
||||||
StdioFprintfPointer(&SOStream, OneFormatBuf, ThisArg->Val->Pointer);
|
StdioFprintfPointer(&SOStream, OneFormatBuf, ThisArg->Val->Pointer);
|
||||||
|
|
||||||
else if (ThisArg->Typ->Base == TypeArray && ThisArg->Typ->FromType->Base == TypeChar)
|
else if (ThisArg->Typ->Base == TypeArray && ThisArg->Typ->FromType->Base == TypeChar)
|
||||||
StdioFprintfPointer(&SOStream, OneFormatBuf, &ThisArg->Val->ArrayMem[0]);
|
StdioFprintfPointer(&SOStream, OneFormatBuf, &ThisArg->Val->ArrayMem[0]);
|
||||||
|
|
||||||
else
|
else
|
||||||
StdioOutPuts("XXX", &SOStream);
|
StdioOutPuts("XXX", &SOStream);
|
||||||
}
|
} else if (ShowType == pc->VoidPtrType) {
|
||||||
else if (ShowType == pc->VoidPtrType)
|
|
||||||
{
|
|
||||||
if (ThisArg->Typ->Base == TypePointer)
|
if (ThisArg->Typ->Base == TypePointer)
|
||||||
StdioFprintfPointer(&SOStream, OneFormatBuf, ThisArg->Val->Pointer);
|
StdioFprintfPointer(&SOStream, OneFormatBuf, ThisArg->Val->Pointer);
|
||||||
|
|
||||||
else if (ThisArg->Typ->Base == TypeArray)
|
else if (ThisArg->Typ->Base == TypeArray)
|
||||||
StdioFprintfPointer(&SOStream, OneFormatBuf, &ThisArg->Val->ArrayMem[0]);
|
StdioFprintfPointer(&SOStream, OneFormatBuf, &ThisArg->Val->ArrayMem[0]);
|
||||||
|
|
||||||
else
|
else
|
||||||
StdioOutPuts("XXX", &SOStream);
|
StdioOutPuts("XXX", &SOStream);
|
||||||
}
|
}
|
||||||
|
@ -308,9 +271,7 @@ int StdioBasePrintf(struct ParseState *Parser, FILE *Stream, char *StrOut, int S
|
||||||
ArgCount++;
|
ArgCount++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
} else {
|
||||||
else
|
|
||||||
{
|
|
||||||
/* just output a normal character */
|
/* just output a normal character */
|
||||||
StdioOutPutc(*FPos, &SOStream);
|
StdioOutPutc(*FPos, &SOStream);
|
||||||
FPos++;
|
FPos++;
|
||||||
|
@ -334,16 +295,13 @@ int StdioBaseScanf(struct ParseState *Parser, FILE *Stream, char *StrIn, char *F
|
||||||
if (Args->NumArgs > MAX_SCANF_ARGS)
|
if (Args->NumArgs > MAX_SCANF_ARGS)
|
||||||
ProgramFail(Parser, "too many arguments to scanf() - %d max", MAX_SCANF_ARGS);
|
ProgramFail(Parser, "too many arguments to scanf() - %d max", MAX_SCANF_ARGS);
|
||||||
|
|
||||||
for (ArgCount = 0; ArgCount < Args->NumArgs; ArgCount++)
|
for (ArgCount = 0; ArgCount < Args->NumArgs; ArgCount++) {
|
||||||
{
|
|
||||||
ThisArg = (struct Value *)((char *)ThisArg + MEM_ALIGN(sizeof(struct Value) + TypeStackSizeValue(ThisArg)));
|
ThisArg = (struct Value *)((char *)ThisArg + MEM_ALIGN(sizeof(struct Value) + TypeStackSizeValue(ThisArg)));
|
||||||
|
|
||||||
if (ThisArg->Typ->Base == TypePointer)
|
if (ThisArg->Typ->Base == TypePointer)
|
||||||
ScanfArg[ArgCount] = ThisArg->Val->Pointer;
|
ScanfArg[ArgCount] = ThisArg->Val->Pointer;
|
||||||
|
|
||||||
else if (ThisArg->Typ->Base == TypeArray)
|
else if (ThisArg->Typ->Base == TypeArray)
|
||||||
ScanfArg[ArgCount] = &ThisArg->Val->ArrayMem[0];
|
ScanfArg[ArgCount] = &ThisArg->Val->ArrayMem[0];
|
||||||
|
|
||||||
else
|
else
|
||||||
ProgramFail(Parser, "non-pointer argument to scanf() - argument %d after format", ArgCount+1);
|
ProgramFail(Parser, "non-pointer argument to scanf() - argument %d after format", ArgCount+1);
|
||||||
}
|
}
|
||||||
|
@ -507,8 +465,7 @@ void StdioPuts(struct ParseState *Parser, struct Value *ReturnValue, struct Valu
|
||||||
void StdioGets(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs)
|
void StdioGets(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs)
|
||||||
{
|
{
|
||||||
ReturnValue->Val->Pointer = fgets(Param[0]->Val->Pointer, GETS_MAXValue, stdin);
|
ReturnValue->Val->Pointer = fgets(Param[0]->Val->Pointer, GETS_MAXValue, stdin);
|
||||||
if (ReturnValue->Val->Pointer != NULL)
|
if (ReturnValue->Val->Pointer != NULL) {
|
||||||
{
|
|
||||||
char *EOLPos = strchr(Param[0]->Val->Pointer, '\n');
|
char *EOLPos = strchr(Param[0]->Val->Pointer, '\n');
|
||||||
if (EOLPos != NULL)
|
if (EOLPos != NULL)
|
||||||
*EOLPos = '\0';
|
*EOLPos = '\0';
|
||||||
|
|
|
@ -62,10 +62,8 @@ char *PlatformReadFile(Picoc *pc, const char *FileName)
|
||||||
ReadText[BytesRead] = '\0';
|
ReadText[BytesRead] = '\0';
|
||||||
fclose(InFile);
|
fclose(InFile);
|
||||||
|
|
||||||
if ((ReadText[0] == '#') && (ReadText[1] == '!'))
|
if ((ReadText[0] == '#') && (ReadText[1] == '!')) {
|
||||||
{
|
for (p = ReadText; (*p != '\r') && (*p != '\n'); ++p) {
|
||||||
for (p = ReadText; (*p != '\r') && (*p != '\n'); ++p)
|
|
||||||
{
|
|
||||||
*p = ' ';
|
*p = ' ';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -22,8 +22,7 @@ char *PlatformGetLine(char *Buf, int MaxLen, const char *Prompt)
|
||||||
|
|
||||||
// If the first character is \n or \r, eat it
|
// If the first character is \n or \r, eat it
|
||||||
ch = getch();
|
ch = getch();
|
||||||
if (ch == '\n' || ch == '\r')
|
if (ch == '\n' || ch == '\r') {
|
||||||
{
|
|
||||||
// And get the next character
|
// And get the next character
|
||||||
ch = getch();
|
ch = getch();
|
||||||
}
|
}
|
||||||
|
|
|
@ -39,8 +39,7 @@ void PlatformCleanup(Picoc *pc)
|
||||||
char *PlatformGetLine(char *Buf, int MaxLen, const char *Prompt)
|
char *PlatformGetLine(char *Buf, int MaxLen, const char *Prompt)
|
||||||
{
|
{
|
||||||
#ifdef USE_READLINE
|
#ifdef USE_READLINE
|
||||||
if (Prompt != NULL)
|
if (Prompt != NULL) {
|
||||||
{
|
|
||||||
/* use GNU readline to read the line */
|
/* use GNU readline to read the line */
|
||||||
char *InLine = readline(Prompt);
|
char *InLine = readline(Prompt);
|
||||||
if (InLine == NULL)
|
if (InLine == NULL)
|
||||||
|
@ -105,10 +104,8 @@ char *PlatformReadFile(Picoc *pc, const char *FileName)
|
||||||
ReadText[BytesRead] = '\0';
|
ReadText[BytesRead] = '\0';
|
||||||
fclose(InFile);
|
fclose(InFile);
|
||||||
|
|
||||||
if ((ReadText[0] == '#') && (ReadText[1] == '!'))
|
if ((ReadText[0] == '#') && (ReadText[1] == '!')) {
|
||||||
{
|
for (p = ReadText; (*p != '\r') && (*p != '\n'); ++p) {
|
||||||
for (p = ReadText; (*p != '\r') && (*p != '\n'); ++p)
|
|
||||||
{
|
|
||||||
*p = ' ';
|
*p = ' ';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -122,8 +119,7 @@ void PicocPlatformScanFile(Picoc *pc, const char *FileName)
|
||||||
char *SourceStr = PlatformReadFile(pc, FileName);
|
char *SourceStr = PlatformReadFile(pc, FileName);
|
||||||
|
|
||||||
/* ignore "#!/path/to/picoc" .. by replacing the "#!" with "//" */
|
/* ignore "#!/path/to/picoc" .. by replacing the "#!" with "//" */
|
||||||
if (SourceStr != NULL && SourceStr[0] == '#' && SourceStr[1] == '!')
|
if (SourceStr != NULL && SourceStr[0] == '#' && SourceStr[1] == '!') {
|
||||||
{
|
|
||||||
SourceStr[0] = '/';
|
SourceStr[0] = '/';
|
||||||
SourceStr[1] = '/';
|
SourceStr[1] = '/';
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue