Add Qgetline for Mercury. Safely read in a random lenght line from a file.

This commit is contained in:
Bill Currie 2000-10-06 07:05:22 +00:00
parent bdb02ef735
commit 21787a552f
2 changed files with 35 additions and 1 deletions

View File

@ -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*/

View File

@ -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;
}