diff options
author | Chris Robinson <[email protected]> | 2014-02-23 14:07:09 -0800 |
---|---|---|
committer | Chris Robinson <[email protected]> | 2014-02-23 14:07:09 -0800 |
commit | 46b2115bfbf514648331401312f50b15395af1fa (patch) | |
tree | 73de61e4003583bee6c329cedb4493c37c31e7c8 | |
parent | 0626a44eb1df787c075d0e4ea208802f82acd1e0 (diff) |
Avoid using a global static buffer for config loading
-rw-r--r-- | Alc/alcConfig.c | 54 |
1 files changed, 51 insertions, 3 deletions
diff --git a/Alc/alcConfig.c b/Alc/alcConfig.c index 42aff560..a3c8aa1e 100644 --- a/Alc/alcConfig.c +++ b/Alc/alcConfig.c @@ -32,6 +32,7 @@ #include <stdio.h> #include <ctype.h> #include <string.h> +#include <limits.h> #include "alMain.h" @@ -39,6 +40,14 @@ #include <shlobj.h> #endif +#ifndef PATH_MAX +#ifdef MAX_PATH +#define PATH_MAX MAX_PATH +#else +#define PATH_MAX 4096 +#endif +#endif + typedef struct ConfigEntry { char *key; char *value; @@ -48,10 +57,8 @@ typedef struct ConfigBlock { ConfigEntry *entries; unsigned int entryCount; } ConfigBlock; - static ConfigBlock cfgBlock; -static char buffer[1024]; static char *lstrip(char *line) { @@ -69,12 +76,50 @@ static char *rstrip(char *line) return line; } +static int readline(FILE *f, char **output, size_t *maxlen) +{ + size_t len = 0; + int c; + + while((c=fgetc(f)) != EOF && (c == '\r' || c == '\n')) + ; + if(c == EOF) + return 0; + + do { + if(len+1 >= *maxlen) + { + void *temp = NULL; + size_t newmax; + + newmax = (*maxlen ? (*maxlen)<<1 : 32); + if(newmax > *maxlen) + temp = realloc(*output, newmax); + if(!temp) + { + ERR("Failed to realloc %lu bytes from %lu!\n", newmax, *maxlen); + return 0; + } + + *output = temp; + *maxlen = newmax; + } + (*output)[len++] = c; + (*output)[len] = '\0'; + } while((c=fgetc(f)) != EOF && c != '\r' && c != '\n'); + + return 1; +} + + static void LoadConfigFromFile(FILE *f) { char curSection[128] = ""; + char *buffer = NULL; + size_t maxlen = 0; ConfigEntry *ent; - while(fgets(buffer, sizeof(buffer), f)) + while(readline(f, &buffer, &maxlen)) { char *line, *comment; char key[256] = ""; @@ -175,10 +220,13 @@ static void LoadConfigFromFile(FILE *f) TRACE("found '%s' = '%s'\n", ent->key, ent->value); } + + free(buffer); } void ReadALConfig(void) { + char buffer[PATH_MAX]; const char *str; FILE *f; |