diff --git a/configfile.c b/configfile.c index e828a3b..c30fdb4 100644 --- a/configfile.c +++ b/configfile.c @@ -5,6 +5,7 @@ #include "containers/stringarray.h" #include "containers/sectionarray.h" +#include "containers/linearray.h" #include #include #include @@ -193,7 +194,7 @@ StringArray ConfigFileGetAll(CONFIGFILEPTR abstractConfigFile, CONST_STRPTR comp return result; } -VOID ConfigFileAdd(CONFIGFILEPTR abstractConfigFile, CONST_STRPTR compoundKey, CONST_STRPTR stringValue) +VOID ConfigFileAddVariable(CONFIGFILEPTR abstractConfigFile, CONST_STRPTR compoundKey, CONST_STRPTR stringValue) { struct ConfigFile* configFile = (struct ConfigFile*)abstractConfigFile; if( configFile != NULL ) @@ -218,15 +219,40 @@ VOID ConfigFileAdd(CONFIGFILEPTR abstractConfigFile, CONST_STRPTR compoundKey, C } } -VOID ConfigFileSet(CONFIGFILEPTR abstractConfigFile, CONST_STRPTR compoundKey, CONST_STRPTR stringValue) +BOOL ConfigFileSet(CONFIGFILEPTR abstractConfigFile, CONST_STRPTR compoundKey, CONST_STRPTR stringValue) { + BOOL result = FALSE; struct ConfigFile* configFile = (struct ConfigFile*)abstractConfigFile; if( configFile != NULL ) { - StringArray parts = ConfigFileSplitKeyCompletely(compoundKey); - + StringArray parts = ConfigFileSplitKeyForVar(compoundKey); + LineArray lines = SectionStoreFindLines(configFile->sectionStore, StringArrayValues(parts)[0], StringArrayValues(parts)[1]); + if( SizeOfArray(lines) == 0 ) + { + ConfigFileAddVariable(abstractConfigFile, compoundKey, stringValue); + result = TRUE; + } + else if( SizeOfArray(lines) == 1 ) + { + LINEPTR line = (LINEPTR)ArrayBackValue(LINEPTR, lines); + if( line ) + { + VARIABLEPTR var = VariableCreate(StringArrayValues(parts)[1], stringValue); + if( var ) + { + LineSetVariable(line, var); + result = TRUE; + } + } + } + else // more than one line, we error. + { + result = FALSE; + } StringArrayFree(parts, TRUE); + LineArrayFree(lines, FALSE); } + return result; } VOID ConfigFileReplaceAll(CONFIGFILEPTR abstractConfigFile, CONST_STRPTR compoundKey, CONST_STRPTR stringValue) diff --git a/configfile.h b/configfile.h index a21d3bf..f04e9dc 100644 --- a/configfile.h +++ b/configfile.h @@ -6,10 +6,10 @@ VOID ConfigFileFree(CONFIGFILEPTR configFile); StringArray ConfigFileGetAll(CONFIGFILEPTR abstractConfigFile, CONST_STRPTR compoundKey); STRPTR ConfigFileGet(CONFIGFILEPTR abstractConfigFile, CONST_STRPTR compoundKey); -VOID ConfigFileSet(CONFIGFILEPTR abstractConfigFile, CONST_STRPTR compoundKey, CONST_STRPTR stringValue); +BOOL ConfigFileSet(CONFIGFILEPTR abstractConfigFile, CONST_STRPTR compoundKey, CONST_STRPTR stringValue); // --add -VOID ConfigFileAdd(CONFIGFILEPTR abstractConfigFile, CONST_STRPTR compoundKey, CONST_STRPTR stringValue); +VOID ConfigFileAddVariable(CONFIGFILEPTR abstractConfigFile, CONST_STRPTR compoundKey, CONST_STRPTR stringValue); // --replace-all VOID ConfigFileReplaceAll(CONFIGFILEPTR abstractConfigFile, CONST_STRPTR compoundKey, CONST_STRPTR stringValue); diff --git a/configmodel.c b/configmodel.c index 3b4951e..e2c5022 100644 --- a/configmodel.c +++ b/configmodel.c @@ -149,18 +149,19 @@ VOID LineSetVariable(LINEPTR abstractLine, VARIABLEPTR abstractVariable) struct Line* line = (struct Line*)abstractLine; if( line != NULL ) { - if( line->rawText != NULL ) - { - // if we change the variable we remove any original text - FreeVec(line->rawText); - line->rawText = NULL; - } // we take ownership of the variable if( line->variable != NULL ) { VariableFree(line->variable); } line->variable = abstractVariable; + + if( line->rawText != NULL ) + { + // if we change the variable we remove any original text + FreeVec(line->rawText); + } + line->rawText = (STRPTR)VariableSerialize(line->variable); } } @@ -508,11 +509,11 @@ CONST_STRPTR VariableSerialize(VARIABLEPTR abstractVariable) { STRPTR result = NULL; struct Variable* variable = (struct Variable*)abstractVariable; - if( variable != NULL && variable->key != NULL && variable->value.stringValue != NULL ) + if( variable != NULL && variable->normalizedKey != NULL && variable->value.stringValue != NULL ) { - ULONG size = strlen(variable->key) + 3 + strlen(variable->value.stringValue) + 1; + ULONG size = strlen(variable->normalizedKey) + 3 + strlen(variable->value.stringValue) + 1; result = AllocVec(size, MEMF_CLEAR); - sprintf(result, "\t%s = %s\n", variable->key, variable->value.stringValue); + sprintf(result, "\t%s = %s\n", variable->normalizedKey, variable->value.stringValue); } return (CONST_STRPTR)result; } diff --git a/main.c b/main.c index 26b98ac..c8b9ab7 100644 --- a/main.c +++ b/main.c @@ -28,17 +28,18 @@ WORD DoTheWork(STRPTR filename) StringArray values = NULL; STRPTR var = NULL; - values = ConfigFileGetAll(config, "branch.main.remote"); +// values = ConfigFileGetAll(config, "branch.main.remote"); // StringArrayForEach(values, Printf("main %s\n", aString);); - StringArrayFree(values, TRUE); +// StringArrayFree(values, TRUE); - var = ConfigFileGet(config, "branch.config-file-parsing-from-book.remote"); +// var = ConfigFileGet(config, "branch.config-file-parsing-from-book.remote"); // Printf("parse %s\n\n\n", var); - FreeVec(var); + // FreeVec(var); // ConfigFileDump(config); - ConfigFileAdd(config, "branch.main.foob", "bar"); - ConfigFileAdd(config, "branch.alan.foob", "bar"); +// ConfigFileSet(config, "branch.main.foob", "bar"); + // ConfigFileSet(config, "branch.alan.foob", "bar"); + ConfigFileSet(config, "branch.main.remote", "testing"); ConfigFileDump(config); ConfigFileFree(config);