diff --git a/include/quakeio.h b/include/quakeio.h index 285a2cc..d43d822 100644 --- a/include/quakeio.h +++ b/include/quakeio.h @@ -63,6 +63,6 @@ int Qseek(QFile *file, long offset, int whence); long Qtell(QFile *file); int Qflush(QFile *file); int Qeof(QFile *file); - +char *Qgetline(QFile *file); #endif /*_QUAKEIO_H*/ diff --git a/source/quakeio.c b/source/quakeio.c index 30e2b38..8138f4f 100644 --- a/source/quakeio.c +++ b/source/quakeio.c @@ -339,3 +339,37 @@ Qeof(QFile *file) return -1; #endif } + +/* + + Qgetline + + Dynamic lenght version of Qgets. DO NOT free the buffer. + +*/ +char * +Qgetline(QFile *file) +{ + static int size = 256; + static char *buf = 0; + int len; + + if (!buf) + buf = malloc (size); + + if (!Qgets(file, buf, size)) + return 0; + + len = strlen (buf); + while (buf[len - 1] != '\n') { + char *t = realloc (buf, size + 256); + if (!t) + return 0; + buf = t; + size += 256; + if (!Qgets(file, buf+len, size - len)) + break; + len = strlen (buf); + } + return buf; +}