From 380a0d7c0ee2cf3d4dc23c70af4fbf4e065d45a9 Mon Sep 17 00:00:00 2001 From: Konstantin8105 Date: Fri, 1 Mar 2019 09:33:02 +0300 Subject: [PATCH 1/3] CSTD: stdio.h: ungetc: prepare --- noarch/stdio.go | 4 ++++ program/definition_function.go | 1 + tests/stdio.c | 24 +++++++++++++++++++++++- 3 files changed, 28 insertions(+), 1 deletion(-) diff --git a/noarch/stdio.go b/noarch/stdio.go index 265a0d56..2150e4c9 100644 --- a/noarch/stdio.go +++ b/noarch/stdio.go @@ -502,6 +502,10 @@ func Getchar() int { return getc(Stdin.OsFile) } +func Ungetc(ch int, f *File) int { + return -1 +} + // Fseek handles fseek(). // // Sets the position indicator associated with the stream to a new position. diff --git a/program/definition_function.go b/program/definition_function.go index 8bb99e65..6a0fc5f0 100644 --- a/program/definition_function.go +++ b/program/definition_function.go @@ -228,6 +228,7 @@ var builtInFunctionDefinitions = map[string][]string{ "int fgetc(FILE*) -> noarch.Fgetc", "int fputc(int, FILE*) -> noarch.Fputc", "int getc(FILE*) -> noarch.Fgetc", + "int ungetc(int , FILE *) -> noarch.Ungetc", "char * gets(char*) -> noarch.Gets", "int getchar() -> noarch.Getchar", "int putc(int, FILE*) -> noarch.Fputc", diff --git a/tests/stdio.c b/tests/stdio.c index 88cb0046..699b8eef 100644 --- a/tests/stdio.c +++ b/tests/stdio.c @@ -566,9 +566,30 @@ void test_sscanf() is_streq(str, "Rudolph"); } +void test_ungetc() +{ + FILE * pFile; + int c; + char buffer [256]; + + pFile = fopen (test_file,"r"); + is_not_null(pFile); + while (!feof (pFile)) { + c=getc (pFile); + if (c == EOF) break; + if (c == 'T') { + ungetc ('@',pFile); + } else { + ungetc (c,pFile); + } + // fgets (buffer,10,pFile); + // printf("%s\n",buffer); + } +} + int main() { - plan(65); + plan(66); START_TEST(putchar) START_TEST(puts) @@ -604,6 +625,7 @@ int main() START_TEST(eof) START_TEST(getline) START_TEST(sscanf) + START_TEST(ungetc) // that test must be last test START_TEST(perror) From 4ce79c1f7c4b4b307fcbd01c13b08b4d35e01e66 Mon Sep 17 00:00:00 2001 From: Konstantin8105 Date: Tue, 5 Mar 2019 09:40:02 +0300 Subject: [PATCH 2/3] add test --- noarch/stdio.go | 4 ---- program/definition_function.go | 1 - 2 files changed, 5 deletions(-) diff --git a/noarch/stdio.go b/noarch/stdio.go index 2150e4c9..265a0d56 100644 --- a/noarch/stdio.go +++ b/noarch/stdio.go @@ -502,10 +502,6 @@ func Getchar() int { return getc(Stdin.OsFile) } -func Ungetc(ch int, f *File) int { - return -1 -} - // Fseek handles fseek(). // // Sets the position indicator associated with the stream to a new position. diff --git a/program/definition_function.go b/program/definition_function.go index 6a0fc5f0..8bb99e65 100644 --- a/program/definition_function.go +++ b/program/definition_function.go @@ -228,7 +228,6 @@ var builtInFunctionDefinitions = map[string][]string{ "int fgetc(FILE*) -> noarch.Fgetc", "int fputc(int, FILE*) -> noarch.Fputc", "int getc(FILE*) -> noarch.Fgetc", - "int ungetc(int , FILE *) -> noarch.Ungetc", "char * gets(char*) -> noarch.Gets", "int getchar() -> noarch.Getchar", "int putc(int, FILE*) -> noarch.Fputc", From 63f6e1ddb0cb9ab40bdbeef0351f6f30ced03bf8 Mon Sep 17 00:00:00 2001 From: Konstantin8105 Date: Mon, 1 Apr 2019 22:48:51 +0300 Subject: [PATCH 3/3] fix test --- tests/stdio.c | 29 +++++++++++++++-------------- tests/stdio_ungetc.txt | 3 +++ 2 files changed, 18 insertions(+), 14 deletions(-) create mode 100644 tests/stdio_ungetc.txt diff --git a/tests/stdio.c b/tests/stdio.c index 7d261bf6..d6862f2f 100644 --- a/tests/stdio.c +++ b/tests/stdio.c @@ -568,23 +568,24 @@ void test_sscanf() void test_ungetc() { - FILE * pFile; + FILE * fp; int c; char buffer [256]; - pFile = fopen (test_file,"r"); - is_not_null(pFile); - while (!feof (pFile)) { - c=getc (pFile); - if (c == EOF) break; - if (c == 'T') { - ungetc ('@',pFile); - } else { - ungetc (c,pFile); - } - // fgets (buffer,10,pFile); - // printf("%s\n",buffer); - } + fp = fopen ("./tests/stdio_ungetc.txt","r"); + is_not_null(fp); + + while(!feof(fp)) { + c = getc (fp); + /* replace ! with + */ + if( c == '!' ) { + ungetc ('+', fp); + } else { + ungetc(c, fp); + } + fgets(buffer, 255, fp); + fputs(buffer, stdout); + } } void test_FILE() diff --git a/tests/stdio_ungetc.txt b/tests/stdio_ungetc.txt new file mode 100644 index 00000000..1711d837 --- /dev/null +++ b/tests/stdio_ungetc.txt @@ -0,0 +1,3 @@ +this is tutorials point +!c standard library +!library functions and macros