Create filestream_getline
This commit is contained in:
parent
e9cf351c67
commit
d40eade52c
|
@ -65,6 +65,8 @@ int filestream_read_file(const char *path, void **buf, ssize_t *len);
|
||||||
|
|
||||||
char *filestream_gets(RFILE *stream, char *s, size_t len);
|
char *filestream_gets(RFILE *stream, char *s, size_t len);
|
||||||
|
|
||||||
|
char *filestream_getline(RFILE *stream);
|
||||||
|
|
||||||
int filestream_getc(RFILE *stream);
|
int filestream_getc(RFILE *stream);
|
||||||
|
|
||||||
bool filestream_write_file(const char *path, const void *data, ssize_t size);
|
bool filestream_write_file(const char *path, const void *data, ssize_t size);
|
||||||
|
|
|
@ -243,6 +243,41 @@ error:
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
char *filestream_getline(RFILE *stream)
|
||||||
|
{
|
||||||
|
char* newline = (char*)malloc(9);
|
||||||
|
char* newline_tmp = NULL;
|
||||||
|
size_t cur_size = 8;
|
||||||
|
size_t idx = 0;
|
||||||
|
int in = filestream_getc(stream);
|
||||||
|
|
||||||
|
if (!newline)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
while (in != EOF && in != '\n')
|
||||||
|
{
|
||||||
|
if (idx == cur_size)
|
||||||
|
{
|
||||||
|
cur_size *= 2;
|
||||||
|
newline_tmp = (char*)realloc(newline, cur_size + 1);
|
||||||
|
|
||||||
|
if (!newline_tmp)
|
||||||
|
{
|
||||||
|
free(newline);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
newline = newline_tmp;
|
||||||
|
}
|
||||||
|
|
||||||
|
newline[idx++] = in;
|
||||||
|
in = filestream_getc(stream);
|
||||||
|
}
|
||||||
|
|
||||||
|
newline[idx] = '\0';
|
||||||
|
return newline;
|
||||||
|
}
|
||||||
|
|
||||||
char *filestream_gets(RFILE *stream, char *s, size_t len)
|
char *filestream_gets(RFILE *stream, char *s, size_t len)
|
||||||
{
|
{
|
||||||
if (!stream)
|
if (!stream)
|
||||||
|
|
Loading…
Reference in New Issue