static void test_fgetwln_single(void) { FILE *fp; size_t len; int i; fp = pipe_feed("%ls", (const void **)data_wide, DATA_LINES); for (i = 0; i < DATA_LINES; i++) { wchar_t *wstr; wstr = fgetwln(fp, &len); assert(wstr); assert(wmemcmp(data_wide[i], wstr, len) == 0); } assert(fgetwln(fp, &len) == NULL); pipe_close(fp); }
int main(int argc, char *argv[]) { const char *filename; wchar_t *p, *t; FILE *fp; size_t len; int ch, rval; setlocale(LC_ALL, ""); setprogname(argv[0]); while ((ch = getopt(argc, argv, "")) != -1) switch(ch) { case '?': default: usage(); } argc -= optind; argv += optind; fp = stdin; filename = "stdin"; rval = 0; do { if (*argv) { if ((fp = fopen(*argv, "r")) == NULL) { warn("%s", *argv); rval = 1; ++argv; continue; } filename = *argv++; } while ((p = fgetwln(fp, &len)) != NULL) { if (p[len - 1] == L'\n') --len; t = p + len - 1; for (t = p + len - 1; t >= p; --t) putwchar(*t); putwchar(L'\n'); } if (ferror(fp)) { warn("%s", filename); rval = 1; } (void)fclose(fp); } while(*argv); exit(rval); }
static void test_fgetwln_multi(void) { struct file files[FILE_COUNT]; int i, l; for (i = 0; i < FILE_COUNT; i++) { wchar_t *wstr; wstr = wcsdup(L"A\n"); wstr[0] += i; files[i].lines = reallocarray(NULL, LINE_COUNT, sizeof(char *)); files[i].lines[0] = wstr; files[i].lines[1] = wstr; files[i].fp = pipe_feed("%ls", files[i].lines, LINE_COUNT); } for (l = 0; l < LINE_COUNT; l++) { for (i = 0; i < FILE_COUNT; i++) { size_t len; wchar_t *wstr; wstr = fgetwln(files[i].fp, &len); assert(wstr); assert(len == LINE_LEN); files[i].got_len = len; files[i].got_buf = wstr; } for (i = 0; i < FILE_COUNT; i++) { assert(wmemcmp(files[i].lines[l], files[i].got_buf, files[i].got_len) == 0); } } for (i = 0; i < LINE_COUNT; i++) pipe_close(files[i].fp); }
/* * Allocate and read a binary string from file. * The strings are nl-ended or zero-ended, depending on the sort setting. */ struct bwstring * bwsfgetln(FILE *f, size_t *len, bool zero_ended, struct reader_buffer *rb) { wint_t eols; eols = zero_ended ? btowc('\0') : btowc('\n'); if (!zero_ended && (MB_CUR_MAX > 1)) { wchar_t *ret; ret = fgetwln(f, len); if (ret == NULL) { if (!feof(f)) err(2, NULL); return (NULL); } if (*len > 0) { if (ret[*len - 1] == (wchar_t)eols) --(*len); } return (bwssbdup(ret, *len)); } else if (!zero_ended && (MB_CUR_MAX == 1)) { char *ret; ret = fgetln(f, len); if (ret == NULL) { if (!feof(f)) err(2, NULL); return (NULL); } if (*len > 0) { if (ret[*len - 1] == '\n') --(*len); } return (bwscsbdup((unsigned char*)ret, *len)); } else { *len = 0; if (feof(f)) return (NULL); if (2 >= rb->fgetwln_z_buffer_size) { rb->fgetwln_z_buffer_size += 256; rb->fgetwln_z_buffer = sort_realloc(rb->fgetwln_z_buffer, sizeof(wchar_t) * rb->fgetwln_z_buffer_size); } rb->fgetwln_z_buffer[*len] = 0; if (MB_CUR_MAX == 1) while (!feof(f)) { int c; c = fgetc(f); if (c == EOF) { if (*len == 0) return (NULL); goto line_read_done; } if (c == eols) goto line_read_done; if (*len + 1 >= rb->fgetwln_z_buffer_size) { rb->fgetwln_z_buffer_size += 256; rb->fgetwln_z_buffer = sort_realloc(rb->fgetwln_z_buffer, SIZEOF_WCHAR_STRING(rb->fgetwln_z_buffer_size)); } rb->fgetwln_z_buffer[*len] = c; rb->fgetwln_z_buffer[++(*len)] = 0; } else while (!feof(f)) { wint_t c = 0; c = fgetwc(f); if (c == WEOF) { if (*len == 0) return (NULL); goto line_read_done; } if (c == eols) goto line_read_done; if (*len + 1 >= rb->fgetwln_z_buffer_size) { rb->fgetwln_z_buffer_size += 256; rb->fgetwln_z_buffer = sort_realloc(rb->fgetwln_z_buffer, SIZEOF_WCHAR_STRING(rb->fgetwln_z_buffer_size)); } rb->fgetwln_z_buffer[*len] = c; rb->fgetwln_z_buffer[++(*len)] = 0; } line_read_done: /* we do not count the last 0 */ return (bwssbdup(rb->fgetwln_z_buffer, *len)); } }