/* * Append a copy of `other' to `f'. */ void eif_file_append(FILE *f, FILE *other, EIF_INTEGER l) { size_t amount; char buffer[512]; int bufsize = 512; errno = 0; if (fseek(other, 0, FS_START) != 0) { esys(); } if (fseek(f, 0, FS_END) != 0) { esys(); } while (l > 0) { amount = l; if (l < bufsize) amount = l; else amount = bufsize; if (amount != fread(buffer, sizeof(char), amount, other)) { eise_io("FILE: unable to read appended file."); break; } l -= (EIF_INTEGER) amount; if (amount != fwrite(buffer, sizeof(char), amount, f)) { eise_io("FILE: unable to write appended file."); break; } } }
/* * Look ahead one character. If EOF, return 0. */ EIF_CHARACTER_8 eif_file_lh(FILE *f) { int c; errno = 0; c = getc(f); if (c == EOF && ferror(f)) eise_io("FILE: error when reading a character ahead."); if (c != EOF && EOF == ungetc(c, f)) eise_io("FILE: error when reading a character ahead."); return (EIF_CHARACTER_8) (c == EOF ? (char) 0 : (char) c); }
/* * Get a string from `f' and fill it into `s' (at most `bound' characters), * with `start' being the amount of bytes already stored within s. This * means we really have to read (bound - start) characters. */ EIF_INTEGER eif_file_gs(FILE *f, char *s, EIF_INTEGER bound, EIF_INTEGER start) { EIF_INTEGER amount; int c = '\0'; EIF_INTEGER read; amount = bound - start; s += start; errno = 0; read = 0; while (amount-- > 0) { if ((c = getc(f)) == '\n' || c == EOF) break; *s++ = c; read++; } if (c == EOF && ferror(f)) { /* An I/O error occurred */ /* Raise exception */ eise_io("FILE: unable to read current line."); } if (c == EOF || c == '\n') #ifdef EIF_WINDOWS if ((read > 0) && (*(s-1) == '\r')) return read - 1; else return read; #else return read; #endif if (amount == -1) return (read + 1); return bound - start + 1; }
rt_private int run_idr_read (IDR *bu) { RT_GET_CONTEXT register char * ptr = bu->i_buf; int32 read_size; long amount_left; register int part_read; register int total_read = 0; if ((char_read_func ((char *)(&read_size), sizeof (int32))) < sizeof (int32)) eise_io("Independent retrieve: unable to read buffer size."); read_size = ntohl (read_size); #ifdef DEBUG if (read_size > idrs_size (bu)) print_err_msg(stderr, "Too large %d info for %d buffer\n", read_size, idrs_size (bu)); #endif amount_left = read_size; while (total_read < read_size) { if ((part_read = char_read_func (ptr, amount_left)) <= 0) { /* If we read 0 bytes, it means that we reached the end of file, * so we are missing something, instead of going further we stop */ eio(); } total_read += part_read; ptr += part_read; amount_left -= part_read; } return total_read; }
/* * Write string `str' on `f'. */ void eif_file_ps(FILE *f, char *str, EIF_INTEGER len) { errno = 0; if (len == 0) { /* Nothing to be done. */ } else if (fwrite(str, sizeof(char) * len, 1, f) != 1) { eise_io("FILE: unable to write STRING object."); } }
/* * Get a real from `f'. */ EIF_REAL_32 eif_file_grb(FILE* f) { EIF_REAL_32 r; errno = 0; if (fread (&r, sizeof (EIF_REAL_32), 1, f) != 1) { eise_io("FILE: unable to read REAL_32 value."); } return r; }
/* * Get a double from `f'. */ EIF_REAL_64 eif_file_gdb(FILE* f) { EIF_REAL_64 d; errno = 0; if (fread (&d, sizeof(EIF_REAL_64), 1, f) != 1) { eise_io("FILE: unable to read REAL_64 value."); } return d; }
/* * Get an integer from `f'. */ EIF_INTEGER eif_file_gib(FILE* f) { EIF_INTEGER i; errno = 0; if (fread (&i, sizeof (EIF_INTEGER), 1, f) != 1) { eise_io("FILE: unable to read INTEGER value."); } return i; }
/* * Get a word from `f' and fill it into `s' (at most `bound' characters), * with `start' being the amount of bytes already stored within s. This * means we really have to read (bound - start) characters. Any leading * spaces are skipped. */ EIF_INTEGER eif_file_gw(FILE* f, char* s, EIF_INTEGER bound, EIF_INTEGER start) { EIF_INTEGER amount; /* Amount of bytes to be read */ int c = 0; /* Last char read */ amount = bound - start; /* Characters to be read */ s += start; /* Where read characters are written */ errno = 0; /* No error, a priori */ if (start == 0) { /* First call */ while ((c = getc(f)) != EOF) if (!isspace(c)) break; if (c == EOF && ferror(f)) /* An I/O error occurred */ eise_io("FILE: unable to read word."); /* Raise exception */ if (c == EOF) return (EIF_INTEGER) 0; /* Reached EOF before word */ else if (EOF == ungetc(c, f)) eise_io("FILE: unable to read word."); } while (amount-- > 0) { c = getc(f); if (c == EOF) break; if (isspace(c)) { if (EOF == ungetc(c, f)) eise_io("FILE: unable to read word."); break; } *s++ = (char) c; } if (c == EOF && ferror(f)) /* An I/O error occurred */ eise_io("FILE: unable to read word."); /* Raise exception */ /* If we managed to get the whole string, return the number of characters * read. Otherwise, return (bound - start + 1) to indicate an error * condition. */ if (c == EOF || isspace(c)) return bound - start - amount - 1; /* Number of characters read */ return bound - start + 1; /* Error condition */ }
/* * Read upto next input line. */ void eif_file_tnil(FILE *f) { int c; errno = 0; while ((c = getc(f)) != '\n' && c != EOF) ; if (c == EOF && ferror(f)) eise_io("FILE: error during reading the end of the file."); }
/* * Get a real from `f'. */ EIF_REAL_32 eif_file_gr(FILE *f) { EIF_REAL_32 r; errno = 0; if (fscanf(f, "%f", &r) < 0) { eise_io("FILE: unable to read REAL value."); } rt_swallow_nl(f); return r; }
/* * Read min (bound, remaining bytes in file) characters into `s' and * return the number of characters read. */ EIF_INTEGER eif_file_gss(FILE* f, char* s, EIF_INTEGER bound) { size_t amount = fread(s, sizeof(char), (size_t) bound, f); if (ferror(f)) { /* An I/O error occurred */ eise_io("FILE: unable to read stream."); /* Raise exception */ } return (EIF_INTEGER) amount; /* Number of characters read */ }
/* * Get a character from `f'. */ EIF_CHARACTER_8 eif_file_gc(FILE *f) { int c; errno = 0; c = getc(f); if (c == EOF && ferror(f)) { eise_io("FILE: unable to read CHARACTER value."); } return (EIF_CHARACTER_8)c; }
/* * Get a double from `f'. */ EIF_REAL_64 eif_file_gd(FILE *f) { EIF_REAL_64 d; errno = 0; if (fscanf(f, "%lf", &d) < 0) { eise_io("FILE: unable to read DOUBLE value."); } rt_swallow_nl(f); return d; }
/* * Get an integer from `f'. */ EIF_INTEGER eif_file_gi(FILE *f) { EIF_INTEGER i; errno = 0; if (fscanf(f, "%d", &i) < 0) { eise_io("FILE: unable to read INTEGER value."); } rt_swallow_nl(f); return i; }
/* * Swallow next character if it is a new line. */ static void rt_swallow_nl(FILE *f) { /* getc() cannot be used as it doesn't set the EOF flag */ if (f != stdin) { if (fscanf (f, "\n") == EOF && ferror(f)) { eise_io ("FILE: error during reading the end of the file,"); } } else { int c; errno = 0; c = getc(f); if (c == EOF && ferror(f)) eise_io("FILE: error during reading the end of the file."); if (c != '\n' && EOF == ungetc(c, f)) eise_io("FILE: End of file."); } }
/* * Write `number' on `f'. */ void eif_file_pr(FILE *f, EIF_REAL_32 number) { int res; errno = 0; if (number != number) { res = fprintf (f, "%s", "NaN"); } else if (GE_real_32_is_negative_infinity(number)) { res = fprintf (f, "%s", "-Infinity"); } else if (GE_real_32_is_positive_infinity(number)) { res = fprintf (f, "%s", "Infinity"); } else { res = fprintf (f, "%g", number); } if (res < 0) { eise_io("FILE: unable to write REAL_32 value."); } }
/* * Write double `val' onto `f'. */ void eif_file_pd(FILE *f, EIF_REAL_64 val) { int res; errno = 0; if (val != val) { res = fprintf (f, "%s", "NaN"); } else if (GE_real_64_is_negative_infinity(val)) { res = fprintf (f, "%s", "-Infinity"); } else if (GE_real_64_is_positive_infinity(val)) { res = fprintf (f, "%s", "Infinity"); } else { res = fprintf(f, "%.17g", val); } if (res < 0) { eise_io("FILE: unable to write REAL_64 value."); } }
/* * Write `number' on `f'. */ void eif_file_prb(FILE* f, EIF_REAL_32 number) { errno = 0; if (fwrite(&number, sizeof(EIF_REAL_32),1, f) != 1) { eise_io("FILE: unable to write REAL_32 value."); } }
/* * Write character `c' on `f'. */ void eif_file_pc(FILE *f, char c) { errno = 0; if (putc(c, f) == EOF) { eise_io("FILE: unable to write CHARACTER value."); } }
/* * Write `number' on `f'. */ void eif_file_pi(FILE *f, EIF_INTEGER number) { errno = 0; if (fprintf(f, "%d", number) < 0) { eise_io("FILE: unable to write INTEGER value."); } }
/* * Write double `val' onto `f'. */ void eif_file_pdb(FILE* f, EIF_REAL_64 val) { errno = 0; if (fwrite (&val, sizeof(EIF_REAL_64), 1, f) != 1) { eise_io("FILE: unable to write REAL_64 value."); } }
/* * Write `number' on `f'. */ void eif_file_pib(FILE* f, EIF_INTEGER number) { errno = 0; if (fwrite(&number, sizeof(EIF_INTEGER),1, f) != 1) { eise_io("FILE: unable to write INTEGER value."); } }
/* * Put new_line onto `f'. */ void eif_file_tnwl(FILE *f) { errno = 0; if (putc('\n', f) == EOF) { eise_io("FILE: unable to write new line."); } }