1 #include "system/stacktrace.h"
8 #include "line_stream.h"
10 #include "system/nth_alloc.h"
11 #include "system/log.h"
22 static void fclose_lt(void* file)
27 LineStream *create_line_stream(const char *filename,
31 trace_assert(filename);
36 LineStream *line_stream = PUSH_LT(
38 nth_calloc(1, sizeof(LineStream)),
40 if (line_stream == NULL) {
45 line_stream->stream = PUSH_LT(
47 fopen(filename, mode),
49 if (line_stream->stream == NULL) {
50 log_fail("Could not open file '%s': %s\n", filename, strerror(errno));
54 line_stream->buffer = PUSH_LT(
56 nth_calloc(1, sizeof(char) * capacity),
58 if (line_stream->buffer == NULL) {
62 line_stream->capacity = capacity;
63 line_stream->unfinished = false;
68 void destroy_line_stream(LineStream *line_stream)
70 trace_assert(line_stream);
72 RETURN_LT0(line_stream->lt);
76 const char *line_stream_next_chunk(LineStream *line_stream)
78 trace_assert(line_stream);
80 const char *s = fgets(line_stream->buffer,
81 (int) line_stream->capacity,
87 line_stream->unfinished = s[n - 1] != '\n';
92 const char *line_stream_next(LineStream *line_stream)
94 trace_assert(line_stream);
96 while (line_stream->unfinished) {
97 line_stream_next_chunk(line_stream);
100 return line_stream_next_chunk(line_stream);