getchar()/gets() implemented

git-svn-id: http://picoc.googlecode.com/svn/trunk@205 21eae674-98b7-11dd-bd71-f92a316d2d60
This commit is contained in:
zik.saleeba 2009-03-15 09:57:19 +00:00
parent 70d48e75d7
commit a414de85e5
5 changed files with 30 additions and 3 deletions

1
TODO
View file

@ -27,6 +27,7 @@ Improvements:
Need test/debug: Need test/debug:
* all break/continue variations * all break/continue variations
* getchar()/gets() tests
Also: Also:
* Remove Var parameter from HeapPopStack() once we're certain it all works * Remove Var parameter from HeapPopStack() once we're certain it all works

View file

@ -161,12 +161,30 @@ void LibPrintf(struct ParseState *Parser, struct Value *ReturnValue, struct Valu
} }
} }
/* get a line of input. protected from buffer overrun */
void LibGets(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) void LibGets(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs)
{ {
struct Value *CharArray = Param[0]->Val->Pointer.Segment;
char *ReadBuffer = CharArray->Val->Array.Data + Param[0]->Val->Pointer.Offset;
int MaxLength = CharArray->Val->Array.Size - Param[0]->Val->Pointer.Offset;
char *Result;
ReturnValue->Val->Pointer.Segment = 0;
ReturnValue->Val->Pointer.Offset = 0;
if (Param[0]->Val->Pointer.Offset < 0 || MaxLength < 0)
return; /* no room for data */
Result = PlatformGetLine(ReadBuffer, MaxLength);
if (Result == NULL)
return;
ReturnValue->Val->Pointer = Param[0]->Val->Pointer;
} }
void LibGetc(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) void LibGetc(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs)
{ {
ReturnValue->Val->Integer = PlatformGetCharacter();
} }
/* list of all library functions and their prototypes */ /* list of all library functions and their prototypes */
@ -174,6 +192,6 @@ struct LibraryFunction CLibrary[] =
{ {
{ LibPrintf, "void printf(char *, ...)" }, { LibPrintf, "void printf(char *, ...)" },
{ LibGets, "void gets(char *, int)" }, { LibGets, "void gets(char *, int)" },
{ LibGetc, "int getc()" }, { LibGetc, "int getchar()" },
{ NULL, NULL } { NULL, NULL }
}; };

View file

@ -326,6 +326,7 @@ void LexFail(struct LexState *Lexer, const char *Message, ...);
void PlatformCleanup(); void PlatformCleanup();
void PlatformScanFile(const char *FileName); void PlatformScanFile(const char *FileName);
char *PlatformGetLine(char *Buf, int MaxLen); char *PlatformGetLine(char *Buf, int MaxLen);
int PlatformGetCharacter();
void PlatformPutc(unsigned char OutCh); void PlatformPutc(unsigned char OutCh);
void PlatformPrintf(const char *Format, ...); void PlatformPrintf(const char *Format, ...);
void PlatformVPrintf(const char *Format, va_list Args); void PlatformVPrintf(const char *Format, va_list Args);

View file

@ -37,8 +37,8 @@ void PlatformPutc(unsigned char OutCh)
putchar(OutCh); putchar(OutCh);
} }
/* write a character to the console */ /* read a character */
int PlatformGetc() int PlatformGetCharacter()
{ {
return getch(); return getch();
} }

View file

@ -17,6 +17,13 @@ char *PlatformGetLine(char *Buf, int MaxLen)
return fgets(Buf, MaxLen, stdin); return fgets(Buf, MaxLen, stdin);
} }
/* get a character of interactive input */
int PlatformGetCharacter()
{
fflush(stdout);
return getchar();
}
/* write a character to the console */ /* write a character to the console */
void PlatformPutc(unsigned char OutCh) void PlatformPutc(unsigned char OutCh)
{ {