From f86126bfc3fefac726dcb613be91012a656f7c6f Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Sun, 1 Oct 2017 17:24:43 +0200 Subject: [SERVER] Helper function for loading line based space separated data from file --- src/server/fileutil.c | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) (limited to 'src/server/fileutil.c') diff --git a/src/server/fileutil.c b/src/server/fileutil.c index 58ac0e9..d68649e 100644 --- a/src/server/fileutil.c +++ b/src/server/fileutil.c @@ -1,4 +1,5 @@ #include "fileutil.h" +#include "helper.h" #include #include @@ -80,3 +81,35 @@ time_t file_lastModification(const char * const file) if ( stat( file, &st ) != 0 ) return 0; return st.st_mtime; } + +int file_loadLineBased(const char * const file, int minFields, int maxFields, void (*cb)(int argc, char **argv, void *data), void *data) +{ + char buffer[1000], *line; + char *items[20]; + int count = 0, itemCount; + + if ( file == NULL || cb == NULL ) return -1; + FILE *fp = fopen( file, "r" ); + if ( fp == NULL ) return -1; + while ( fgets( buffer, sizeof(buffer), fp ) != NULL ) { + itemCount = 0; + for (line = buffer; *line != '\0' && itemCount < 20; ) { // Trim left and scan for "-" prefix + while ( *line == ' ' || *line == '\t' ) ++line; + if ( *line == '\r' || *line == '\n' || *line == '\0' ) break; // Ignore empty lines + items[itemCount++] = line; + if ( itemCount >= maxFields ) { + trim_right( line ); + break; + } + while ( *line != '\0' && *line != ' ' && *line != '\t' && *line != '\r' && *line != '\n' ) ++line; + if ( *line != '\0' ) *line++ = '\0'; + } + if ( itemCount >= minFields ) { + cb( itemCount, items, data ); + count++; + } + } + fclose( fp ); + return count; +} + -- cgit v1.2.3-55-g7522