portable getline replacement (closes #182)

This commit is contained in:
Steven G. Johnson 2020-03-28 09:36:58 -04:00
parent 0890a538bf
commit d588d7097c
3 changed files with 17 additions and 5 deletions

View File

@ -1,15 +1,16 @@
#include "tests.h" #include "tests.h"
#include "simple_getline.h"
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
char *buf = NULL; char buf[8192];
size_t bufsize = 0; size_t bufsize = 0;
FILE *f = argc > 1 ? fopen(argv[1], "r") : NULL; FILE *f = argc > 1 ? fopen(argv[1], "r") : NULL;
utf8proc_uint8_t src[1024]; utf8proc_uint8_t src[1024];
int len; int len;
check(f != NULL, "error opening GraphemeBreakTest.txt"); check(f != NULL, "error opening GraphemeBreakTest.txt");
while (getline(&buf, &bufsize, f) > 0) { while (simple_getline(buf, f) > 0) {
size_t bi = 0, si = 0; size_t bi = 0, si = 0;
lineno += 1; lineno += 1;

View File

@ -1,4 +1,5 @@
#include "tests.h" #include "tests.h"
#include "simple_getline.h"
#define CHECK_NORM(NRM, norm, src) { \ #define CHECK_NORM(NRM, norm, src) { \
char *src_norm = (char*) utf8proc_ ## NRM((utf8proc_uint8_t*) src); \ char *src_norm = (char*) utf8proc_ ## NRM((utf8proc_uint8_t*) src); \
@ -9,13 +10,12 @@
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
char *buf = NULL; char buf[8192];
size_t bufsize = 0;
FILE *f = argc > 1 ? fopen(argv[1], "r") : NULL; FILE *f = argc > 1 ? fopen(argv[1], "r") : NULL;
char source[1024], NFC[1024], NFD[1024], NFKC[1024], NFKD[1024]; char source[1024], NFC[1024], NFD[1024], NFKC[1024], NFKD[1024];
check(f != NULL, "error opening NormalizationTest.txt"); check(f != NULL, "error opening NormalizationTest.txt");
while (getline(&buf, &bufsize, f) > 0) { while (simple_getline(buf, f) > 0) {
size_t offset; size_t offset;
lineno += 1; lineno += 1;

11
test/simple_getline.h Normal file
View File

@ -0,0 +1,11 @@
/* simplistic, portable replacement for getline, sufficient for our tests */
static size_t simple_getline(char buf[8192], FILE *f) {
size_t i = 0;
while (i < 1023) {
int c = getc(f);
if (c == EOF || c == '\n') break;
buf[i++] = (char) c;
}
buf[i] = 0;
return i;
}