summaryrefslogtreecommitdiff
path: root/src/common/strlib.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/common/strlib.c')
-rw-r--r--src/common/strlib.c20
1 files changed, 9 insertions, 11 deletions
diff --git a/src/common/strlib.c b/src/common/strlib.c
index 7a6c134e7..019e2d629 100644
--- a/src/common/strlib.c
+++ b/src/common/strlib.c
@@ -12,7 +12,6 @@
#include <errno.h>
-
#define J_MAX_MALLOC_SIZE 65535
// escapes a string in-place (' -> \' , \ -> \\ , % -> _)
@@ -657,7 +656,7 @@ int sv_split(char* str, int len, int startoff, char delim, char** out_fields, in
end[0] = end[1] = '\0';
*out_fields = end + 2;
}
- else if( (opt&SV_TERMINATE_LF) && end[0] == '\r' )
+ else if( (opt&SV_TERMINATE_CR) && end[0] == '\r' )
{
if( !(opt&SV_KEEP_TERMINATOR) )
end[0] = '\0';
@@ -923,18 +922,12 @@ bool sv_readdb(const char* directory, const char* filename, char delim, int minc
FILE* fp;
int lines = 0;
int entries = 0;
- char* fields[64]; // room for 63 fields ([0] is reserved)
- int columns;
+ char** fields; // buffer for fields ([0] is reserved)
+ int columns, fields_length;
char path[1024], line[1024];
snprintf(path, sizeof(path), "%s/%s", directory, filename);
- if( maxcols > ARRAYLENGTH(fields)-1 )
- {
- ShowError("sv_readdb: Insufficient column storage in parser for file \"%s\" (want %d, have only %d). Increase the capacity in the source code please.\n", path, maxcols, ARRAYLENGTH(fields)-1);
- return false;
- }
-
// open file
fp = fopen(path, "r");
if( fp == NULL )
@@ -943,6 +936,10 @@ bool sv_readdb(const char* directory, const char* filename, char delim, int minc
return false;
}
+ // allocate enough memory for the maximum requested amount of columns plus the reserved one
+ fields_length = maxcols+1;
+ fields = aMalloc(fields_length*sizeof(char*));
+
// process rows one by one
while( fgets(line, sizeof(line), fp) )
{
@@ -954,7 +951,7 @@ bool sv_readdb(const char* directory, const char* filename, char delim, int minc
if( line[0] == '\0' || line[0] == '\n' || line[0] == '\r')
continue;
- columns = sv_split(line, strlen(line), 0, delim, fields, ARRAYLENGTH(fields), (e_svopt)(SV_TERMINATE_LF|SV_TERMINATE_CRLF));
+ columns = sv_split(line, strlen(line), 0, delim, fields, fields_length, (e_svopt)(SV_TERMINATE_LF|SV_TERMINATE_CRLF));
if( columns < mincols )
{
@@ -983,6 +980,7 @@ bool sv_readdb(const char* directory, const char* filename, char delim, int minc
entries++;
}
+ aFree(fields);
fclose(fp);
ShowStatus("Done reading '"CL_WHITE"%d"CL_RESET"' entries in '"CL_WHITE"%s"CL_RESET"'.\n", entries, path);