diff --git a/ase/awk/StdAwk.cpp b/ase/awk/StdAwk.cpp index 46a71b8f..d8d15b13 100644 --- a/ase/awk/StdAwk.cpp +++ b/ase/awk/StdAwk.cpp @@ -266,7 +266,11 @@ StdAwk::ssize_t StdAwk::readPipe (Pipe& io, char_t* buf, size_t len) while (n < (ssize_t)len) { ase_cint_t c = ase_fgetc (fp); - if (c == ASE_CHAR_EOF) break; + if (c == ASE_CHAR_EOF) + { + if (ase_ferror(fp)) n = -1; + break; + } buf[n++] = c; if (c == ASE_T('\n')) break; @@ -369,7 +373,11 @@ StdAwk::ssize_t StdAwk::readFile (File& io, char_t* buf, size_t len) while (n < (ssize_t)len) { ase_cint_t c = ase_fgetc (fp); - if (c == ASE_CHAR_EOF) break; + if (c == ASE_CHAR_EOF) + { + if (ase_ferror(fp)) n = -1; + break; + } buf[n++] = c; if (c == ASE_T('\n')) break; diff --git a/ase/test/awk/Awk.cpp b/ase/test/awk/Awk.cpp index b967338b..316d832a 100644 --- a/ase/test/awk/Awk.cpp +++ b/ase/test/awk/Awk.cpp @@ -301,7 +301,11 @@ protected: while (n < (ssize_t)len) { ase_cint_t c = ase_fgetc (fp); - if (c == ASE_CHAR_EOF) break; + if (c == ASE_CHAR_EOF) + { + if (ase_ferror(fp)) n = -1; + break; + } buf[n++] = c; if (c == ASE_T('\n')) break; @@ -424,6 +428,7 @@ protected: ase_cint_t c = ase_fgetc (fp); if (c == ASE_CHAR_EOF) { + if (ase_ferror(fp)) return -1; if (t->nextConIdx >= numConInFiles) break; const char_t* fn = conInFile[t->nextConIdx]; diff --git a/ase/test/awk/awk.c b/ase/test/awk/awk.c index fbc8b305..e3f3936b 100644 --- a/ase/test/awk/awk.c +++ b/ase/test/awk/awk.c @@ -239,11 +239,19 @@ static ase_ssize_t awk_srcio_in ( } else if (cmd == ASE_AWK_IO_READ) { - if (size <= 0) return -1; - c = ase_fgetc ((FILE*)src_io->input_handle); - if (c == ASE_CHAR_EOF) return 0; - *data = (ase_char_t)c; - return 1; + ase_ssize_t n = 0; + FILE* fp = (FILE*)src_io->input_handle; + while (!ase_feof(fp) && n < size) + { + ase_cint_t c = ase_fgetc (fp); + if (c == ASE_CHAR_EOF) + { + if (ase_ferror(fp)) n = -1; + break; + } + data[n++] = c; + } + return n; } return -1; @@ -321,6 +329,7 @@ static ase_ssize_t awk_extio_pipe ( case ASE_AWK_IO_READ: { + /* int chunk = (size > ASE_TYPE_MAX(int))? ASE_TYPE_MAX(int): (int)size; if (ase_fgets (data, chunk, (FILE*)epa->handle) == ASE_NULL) { @@ -328,6 +337,20 @@ static ase_ssize_t awk_extio_pipe ( return 0; } return ase_strlen(data); + */ + ase_ssize_t n = 0; + FILE* fp = (FILE*)epa->handle; + while (!ase_feof(fp) && n < size) + { + ase_cint_t c = ase_fgetc (fp); + if (c == ASE_CHAR_EOF) + { + if (ase_ferror(fp)) n = -1; + break; + } + data[n++] = c; + } + return n; } case ASE_AWK_IO_WRITE: @@ -443,6 +466,7 @@ static ase_ssize_t awk_extio_file ( case ASE_AWK_IO_READ: { + /* int chunk = (size > ASE_TYPE_MAX(int))? ASE_TYPE_MAX(int): (int)size; if (ase_fgets (data, chunk, (FILE*)epa->handle) == ASE_NULL) { @@ -450,6 +474,20 @@ static ase_ssize_t awk_extio_file ( return 0; } return ase_strlen(data); + */ + ase_ssize_t n = 0; + FILE* fp = (FILE*)epa->handle; + while (!ase_feof(fp) && n < size) + { + ase_cint_t c = ase_fgetc (fp); + if (c == ASE_CHAR_EOF) + { + if (ase_ferror(fp)) n = -1; + break; + } + data[n++] = c; + } + return n; } case ASE_AWK_IO_WRITE: @@ -503,6 +541,23 @@ static const ase_char_t* infiles[1000] = ASE_NULL }; +static ase_ssize_t getdata (ase_char_t* data, ase_size_t size, FILE* fp) +{ + ase_ssize_t n = 0; + while (!ase_feof(fp) && n < size) + { + ase_cint_t c = ase_fgetc (fp); + if (c == ASE_CHAR_EOF) + { + if (ase_ferror(fp)) n = -1; + break; + } + data[n++] = c; + if (c == ASE_T('\n')) break; + } + return n; +} + static ase_ssize_t awk_extio_console ( int cmd, void* arg, ase_char_t* data, ase_size_t size) { @@ -518,12 +573,15 @@ static ase_ssize_t awk_extio_console ( } else if (cmd == ASE_AWK_IO_READ) { - int chunk = (size > ASE_TYPE_MAX(int))? ASE_TYPE_MAX(int): (int)size; + ase_ssize_t n; + /*int chunk = (size > ASE_TYPE_MAX(int))? ASE_TYPE_MAX(int): (int)size; while (ase_fgets (data, chunk, (FILE*)epa->handle) == ASE_NULL) { - if (ferror((FILE*)epa->handle)) return -1; + if (ferror((FILE*)epa->handle)) return -1;*/ + while ((n = getdata(data,size,(FILE*)epa->handle)) == 0) + { /* it has reached the end of the current file. * open the next file if available */ if (infiles[infile_no] == ASE_NULL) @@ -611,7 +669,8 @@ static ase_ssize_t awk_extio_console ( infile_no++; } - return ase_strlen(data); + /*return ase_strlen(data);*/ + return n; } else if (cmd == ASE_AWK_IO_WRITE) { diff --git a/ase/utl/stdio.h b/ase/utl/stdio.h index e2c27116..291dba1b 100644 --- a/ase/utl/stdio.h +++ b/ase/utl/stdio.h @@ -20,22 +20,26 @@ #define ase_fprintf _ftprintf #define ase_vfprintf _vftprintf - #define ase_fgets _fgetts - #define ase_fgetc _fgettc - #define ase_fputs _fputts - #define ase_fputc _fputtc + #define ase_fgets(x,y,s) _fgetts(x,y,s) + #define ase_fgetc(x) _fgettc(x) + #define ase_fputs(x,s) _fputts(x,s) + #define ase_fputc(x,s) _fputtc(x,s) #elif defined(ASE_CHAR_IS_MCHAR) - #define ase_fgets fgets - #define ase_fgetc fgetc - #define ase_fputs fputs - #define ase_fputc fputc + #define ase_fgets(x,y,s) fgets(x,y,s) + #define ase_fgetc(x) fgetc(x) + #define ase_fputs(x,s) fputs(x,s) + #define ase_fputc(x,s) fputc(x,s) #else - #define ase_fgets fgetws - #define ase_fgetc fgetwc - #define ase_fputs fputws - #define ase_fputc fputwc + #define ase_fgets(x,y,s) fgetws(x,y,s) + #define ase_fgetc(x) fgetwc(x) + #define ase_fputs(x,s) fputws(x,s) + #define ase_fputc(x,s) fputwc(x,s) #endif +#define ase_feof(s) feof(s) +#define ase_ferror(s) ferror(s) +#define ase_clearerr(s) clearerr(s) + #ifdef __cplusplus extern "C" { #endif