#include <errno.h>
#include <stdarg.h>
-#include <stdint.h>
#include <stdio.h>
#include "string.h"
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;
-}
-
-int collecty_fwalk_buffer(const char* buffer, const size_t length,
- collecty_fwalk_callback callback, void* data) {
- FILE* f = NULL;
- int r;
-
- // Open as a file handle
- f = fmemopen((char*)buffer, length, "r");
- if (!f) {
- r = -errno;
- goto ERROR;
- }
-
- // Walk through all lines
- r = collecty_fwalk(f, callback, data);
-
-ERROR:
- if (f)
- fclose(f);
-
- return r;
-}
#ifndef COLLECTY_UTIL_H
#define COLLECTY_UTIL_H
-#include <stdint.h>
-#include <stdio.h>
-#include <stdlib.h>
+#include <stddef.h>
// Preprocessor macro to make something a string
#define STRINGIFY(x) #x
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);
-
-int collecty_fwalk_buffer(const char* buffer, const size_t length,
- collecty_fwalk_callback callback, void* data);
-
#endif /* COLLECTY_UTIL_H */