X-Git-Url: http://git.pk910.de/?p=NeonServV5.git;a=blobdiff_plain;f=src%2FConfigParser.c;h=050d6b48d77fbc11fe6a328df76eb5ce0861393c;hp=775e062fef399dc4385bd2c06e3e9499d9a48cb1;hb=HEAD;hpb=be180588e158f3b1297ffaf3a715577fb3d73f85 diff --git a/src/ConfigParser.c b/src/ConfigParser.c index 775e062..050d6b4 100644 --- a/src/ConfigParser.c +++ b/src/ConfigParser.c @@ -1,5 +1,5 @@ -/* ConfigParser.c - NeonServ v5.2 - * Copyright (C) 2011 Philipp Kreil (pk910) +/* ConfigParser.c - NeonServ v5.6 + * Copyright (C) 2011-2012 Philipp Kreil (pk910) * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -16,6 +16,7 @@ */ #include "ConfigParser.h" +#include "tools.h" #define ENTRYTYPE_BLOCK 1 #define ENTRYTYPE_STRING 2 @@ -46,13 +47,21 @@ int loadConfig(const char *filename) { // allocate memory to contain the whole file: char *buffer = (char*) malloc (sizeof(char)*lSize + 1); - if (buffer == NULL) return 0; + if (buffer == NULL) { + fclose(f); + return 0; + } // copy the file into the buffer: size_t result = fread (buffer, 1, lSize, f); - if (result != lSize) return 0; + if (result != lSize) { + fclose(f); + return 0; + } buffer[lSize] = '\0'; + fclose(f); + // now parse the config file... if(root_entry) { free_loaded_config(); @@ -148,9 +157,15 @@ static char *parse_config_recursive(struct ConfigEntry *centry, char *buffer, in } if(flags & PARSER_FLAG_COMMAND) { int found_command = 0; + char *tmp_buffer; switch(*buffer) { case '/': - buffer = strstr(buffer, "\n"); + tmp_buffer = buffer; + buffer = strchr(buffer, '\r'); + if(!buffer) + buffer = strchr(tmp_buffer, '\n'); + if(!buffer) + buffer = tmp_buffer + strlen(tmp_buffer)-1; found_command = 1; break; case '*': @@ -291,6 +306,52 @@ char *get_string_field(char *field_path) { return NULL; } +char **get_all_fieldnames(char *block_path) { + struct ConfigEntry *centry = root_entry; + char *a, *b = block_path; + struct ConfigEntry *subentry; + while((a = strstr(b, ".")) && centry) { + if(centry->type == ENTRYTYPE_BLOCK) { + int found = 0; + for(subentry = centry->value; subentry; subentry = subentry->next) { + if(!stricmplen(subentry->name, b, a-b)) { + centry = subentry; + found = 1; + break; + } + } + if(!found) + return NULL; + } else + return NULL; + b = a+1; + } + if(centry->type == ENTRYTYPE_BLOCK) { + int found = 0; + for(subentry = centry->value; subentry; subentry = subentry->next) { + if(!stricmp(subentry->name, b)) { + centry = subentry; + found = 1; + break; + } + } + if(!found) + return NULL; + } else + return NULL; + if(centry->type != ENTRYTYPE_BLOCK) return NULL; + int count = 0; + for(subentry = centry->value; subentry; subentry = subentry->next) { + count++; + } + char **fieldnames = calloc(count+1, sizeof(char *)); + count = 0; + for(subentry = centry->value; subentry; subentry = subentry->next) { + fieldnames[count++] = subentry->name; + } + return fieldnames; +} + void free_loaded_config() { if(root_entry) { free_entry_rekursiv(root_entry, 1);