char* mio_get_line(const MFP* mfp) { int size = 1; int pos; char* buf = NULL; char* s; char* t; assert(mfp_is_valid(mfp)); do { pos = size - 1; size += BUF_SIZE_INC - 1; buf = (buf == NULL) ? malloc((size_t)size) : realloc(buf, (size_t)size); t = &buf[pos]; buf[size - 1] = '@'; s = mio_gets(mfp, t, BUF_SIZE_INC); } while(s != NULL && buf[size - 1] == '\0' && buf[size - 2] != '\n'); /* nothing read at all ? */ if (s == NULL && size == BUF_SIZE_INC) { free(buf); buf = NULL; } return buf; }
static const char *selectByLines (MIO *input, const char* (* lineTaster) (const char *, void *), const char* defaultLang, void *userData) { char line[0x800]; while (mio_gets(input, line, sizeof(line))) { const char *lang = lineTaster (line, userData); if (lang) return lang; } return defaultLang; }