mirror of
https://git.code.sf.net/p/quake/newtree
synced 2024-11-25 21:41:57 +00:00
Add Qgetline for Mercury. Safely read in a random lenght line from a file.
This commit is contained in:
parent
bdb02ef735
commit
21787a552f
2 changed files with 35 additions and 1 deletions
|
@ -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*/
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue