return 0;
}
+
+int collecty_fwalk(FILE* f, collecty_fwalk_callback callback, void* data) {
+ char* line = NULL;
+ size_t length = 0;
+ size_t l = 0;
+ int r;
+
+ // Start at the beginning of the stream
+ r = fseek(f, 0L, SEEK_SET);
+ if (r < 0)
+ return -errno;
+
+ // Walk through all lines
+ for (;;) {
+ // Fetch the next line
+ r = getline(&line, &length, f);
+ if (r < 0) {
+ // Reset r if have reached the end of the file
+ if (feof(f))
+ r = 0;
+
+ break;
+ }
+
+ // Remove the trailing newline
+ collecty_string_rstrip(line);
+
+ // Determine the length of the line
+ l = strlen(line);
+
+ // Call the callback
+ r = callback(line, l, data);
+ if (r < 0)
+ break;
+ }
+
+ // Cleanup
+ if (line)
+ free(line);
+
+ return r;
+}
#define COLLECTY_UTIL_H
#include <stdint.h>
+#include <stdio.h>
#include <stdlib.h>
// Preprocessor macro to make something a string
int collecty_format_title(char** title, const char* format, ...)
__attribute__((format(printf, 2, 3)));
+typedef int (*collecty_fwalk_callback)(const char* line, const size_t length, void* data);
+
+int collecty_fwalk(FILE* f, collecty_fwalk_callback callback, void* data);
+
#endif /* COLLECTY_UTIL_H */