From d1d26c96d113ea036511ad2d5e40da70900badbd Mon Sep 17 00:00:00 2001 From: Alan Francis Date: Fri, 1 Dec 2023 16:15:27 +0000 Subject: [PATCH] some tidying --- configfile.c | 331 ++++++++++++++++++++++++++++++++++++++ configfile.h | 10 +- configmodel.c | 327 +------------------------------------ configmodel.h | 16 -- containers/linearray.c | 1 + containers/linearray.h | 4 +- containers/sectionarray.c | 2 + containers/sectionarray.h | 4 +- containers/stringarray.h | 2 + main.c | 9 +- smakefile | 6 +- 11 files changed, 361 insertions(+), 351 deletions(-) create mode 100644 configfile.c diff --git a/configfile.c b/configfile.c new file mode 100644 index 0000000..e828a3b --- /dev/null +++ b/configfile.c @@ -0,0 +1,331 @@ +#include "types.h" +#include "configfile.h" +#include "configmodel.h" +#include "sectionstore.h" + +#include "containers/stringarray.h" +#include "containers/sectionarray.h" +#include +#include +#include +// #include +// #include +// #include + +#define ZERO ((BPTR)0) + +STATIC SECTIONPTR findOrCreateSection(SECTIONSTOREPTR sectionStore, CONST_STRPTR compoundKey); +STATIC LINEPTR configFileReadLine(BPTR file); + + +// - STRUCTS ----------------------------------------------------------------------------- +struct ConfigFile +{ + CONST_STRPTR filename; + SECTIONSTOREPTR sectionStore; +}; + +// --------------------------------------------------------------------------------------- +// - CONFIG FILE ------------------------------------------------------------------------- +// --------------------------------------------------------------------------------------- + +VOID ConfigFileFree(CONFIGFILEPTR abstractConfigFile) +{ + struct ConfigFile* configFile = (struct ConfigFile*)abstractConfigFile; + if( configFile != NULL ) + { + if( configFile->sectionStore != NULL ) + { + SectionStoreFree(configFile->sectionStore); + } + FreeVec(configFile); + } +} + +CONFIGFILEPTR ConfigFileRead(CONST_STRPTR filename) +{ + struct ConfigFile* result = NULL; + BPTR configFile = Open(filename, MODE_OLDFILE); + if( configFile != ZERO ) + { + + LINEPTR line = NULL; + result = AllocVec(sizeof(struct ConfigFile), MEMF_CLEAR); + result->sectionStore = SectionStoreNew(); + while( (line = configFileReadLine(configFile)) != NULL ) + { + SectionStoreAddLineToCurrentSection(result->sectionStore, line); + //LineDump(line); + } + Close(configFile); + } + + return result; +} + + +// a key is "foo.bar.baz.blah" where "foo" is the primary section name +// "bar.baz" is the secondary section name and "blah" is the variable name. +// this splits foo.bar.baz.blah into foo, bar.baz amd blah. +// section, subsection and variable. +// we'll get empty strings for the parts that arent present +StringArray ConfigFileSplitKeyCompletely(CONST_STRPTR key) +{ + StringArray result = StringArrayNew(); + StringArray parts = StringArrayNew(); + ULONG numberOfParts = 0; + + STRPTR token = NULL; + // we need to make a copy of the key because strtok modifies it. + STRPTR keyCopy = AllocVec(strlen(key)+1, MEMF_CLEAR); + CopyMem(key, keyCopy, strlen(key)); + + token = strtok(keyCopy, "."); + while (token != NULL) + { + StringArrayAppendAndRetain(parts, token); + token = strtok(NULL, "."); + } + FreeVec(keyCopy); + + // now we join all but the first and last part + numberOfParts = SizeOfArray(parts); + if( numberOfParts == 1 ) //just a variable ["","","var"] + { + StringArrayAppendAndRetain(result, ""); + StringArrayAppendAndRetain(result, ""); + StringArrayAppendAndRetain(result, (STRPTR)ArrayBackValue(STRPTR, parts)); + } + else if( numberOfParts == 2 ) // section and variable ["section","","var"] + { + StringArrayAppendAndRetain(result, StringArrayValues(parts)[0]); + StringArrayAppendAndRetain(result, ""); + StringArrayAppendAndRetain(result, StringArrayValues(parts)[1]); + } + else if( numberOfParts == 3 ) // section and subsection and variable ["section","subsec","var"] + { + StringArrayAppendAndRetain(result, StringArrayValues(parts)[0]); + StringArrayAppendAndRetain(result, StringArrayValues(parts)[1]); + StringArrayAppendAndRetain(result, StringArrayValues(parts)[2]); + } + else if( numberOfParts > 3 ) // subsections needs dotted ["section", "subsec1.subsec2", "var""] + { + StringArrayAppendAndRetain(result, StringArrayValues(parts)[0]); //section + + // start at index 1, and add (size -2 (start+end)) parts + StringArrayAppend(result, StringArrayJoinedParts(parts, '.', 1, SizeOfArray(parts)-2)); + + StringArrayAppendAndRetain(result, (STRPTR)ArrayBackValue(STRPTR, parts)); // variable + } + StringArrayFree(parts, TRUE); + return result; +} + +StringArray ConfigFileSplitKeyForVar(CONST_STRPTR key) +{ + // a key is "foo.bar.baz.blah" where "foo" is the primary section name + // "bar.baz" is the secondary section name and "blah" is the variable name. + // In this case we'll just break it down into the canonical section name, + // foo.bar.baz and the variable name blah. + + StringArray result = StringArrayNew(); + StringArray parts = StringArrayNew(); + STRPTR sectionPart = NULL; + STRPTR varPart = NULL; + + STRPTR token = NULL; + // we need to make a copy of the key because strtok modifies it. + STRPTR keyCopy = AllocVec(strlen(key)+1, MEMF_CLEAR); + CopyMem(key, keyCopy, strlen(key)); + + token = strtok(keyCopy, "."); + while (token != NULL) + { + StringArrayAppendAndRetain(parts, token); + token = strtok(NULL, "."); + } + FreeVec(keyCopy); + + // now we join all but the last part + sectionPart = StringArrayJoinedParts(parts, '.', 0, SizeOfArray(parts)-1); + StringArrayAppend(result, sectionPart); // its been alloced so dont copy + varPart = (STRPTR)ArrayBackValue(STRPTR, parts); + StringArrayAppendAndRetain(result, varPart); // its a reference so copy + StringArrayFree(parts, TRUE); + return result; +} + +STRPTR ConfigFileGet(CONFIGFILEPTR abstractConfigFile, CONST_STRPTR compoundKey) +{ + STRPTR result = NULL; + struct ConfigFile* configFile = (struct ConfigFile*)abstractConfigFile; + if( configFile != NULL ) + { + StringArray split = ConfigFileSplitKeyForVar(compoundKey); + VARIABLEPTR var = SectionStoreGet(configFile->sectionStore, StringArrayValues(split)[0], StringArrayValues(split)[1]); + if( var != NULL ) + { + CONST_STRPTR value = VariableGetRawValue(var); + result = AllocVec(strlen(value)+1, MEMF_CLEAR); + CopyMem(value, result, strlen(value)); + } + StringArrayFree(split, TRUE); + } + return result; +} + +StringArray ConfigFileGetAll(CONFIGFILEPTR abstractConfigFile, CONST_STRPTR compoundKey) +{ + StringArray result = StringArrayNew(); + ULONG index = 0; + struct ConfigFile* configFile = (struct ConfigFile*)abstractConfigFile; + if( configFile != NULL ) + { + StringArray split = ConfigFileSplitKeyForVar(compoundKey); + VariableArray vars = SectionStoreGetAll(configFile->sectionStore, StringArrayValues(split)[0], StringArrayValues(split)[1]); + for( index = 0; index < SizeOfArray(vars); index++ ) + { + StringArrayAppendAndRetain(result, VariableGetRawValue(VariableArrayValues(vars)[index])); + } + VariableArrayFree(vars, FALSE); + StringArrayFree(split, TRUE); + } + return result; +} + +VOID ConfigFileAdd(CONFIGFILEPTR abstractConfigFile, CONST_STRPTR compoundKey, CONST_STRPTR stringValue) +{ + struct ConfigFile* configFile = (struct ConfigFile*)abstractConfigFile; + if( configFile != NULL ) + { + SECTIONPTR section = NULL; + VARIABLEPTR var = NULL; + CONST_STRPTR varLine = NULL; + LINEPTR line = NULL; + + StringArray canonicalParts = ConfigFileSplitKeyForVar(compoundKey); + var = VariableCreate(StringArrayValues(canonicalParts)[1], stringValue); + varLine = VariableSerialize(var); + line = LineNew(varLine); + LineSetInitialVariable(line, var); + FreeVec((STRPTR)varLine); + StringArrayFree(canonicalParts, TRUE); + + // now get the section + section = findOrCreateSection(configFile->sectionStore, compoundKey); + // add the line to the section + SectionAddLine(section, line); + } +} + +VOID ConfigFileSet(CONFIGFILEPTR abstractConfigFile, CONST_STRPTR compoundKey, CONST_STRPTR stringValue) +{ + struct ConfigFile* configFile = (struct ConfigFile*)abstractConfigFile; + if( configFile != NULL ) + { + StringArray parts = ConfigFileSplitKeyCompletely(compoundKey); + + StringArrayFree(parts, TRUE); + } +} + +VOID ConfigFileReplaceAll(CONFIGFILEPTR abstractConfigFile, CONST_STRPTR compoundKey, CONST_STRPTR stringValue) +{ + struct ConfigFile* configFile = (struct ConfigFile*)abstractConfigFile; + if( configFile != NULL ) + { + StringArray parts = ConfigFileSplitKeyCompletely(compoundKey); + + StringArrayFree(parts, TRUE); + } +} + +VOID ConfigFileUnset(CONFIGFILEPTR abstractConfigFile, CONST_STRPTR compoundKey, CONST_STRPTR stringValue) +{ + struct ConfigFile* configFile = (struct ConfigFile*)abstractConfigFile; + if( configFile != NULL ) + { + StringArray parts = ConfigFileSplitKeyCompletely(compoundKey); + + StringArrayFree(parts, TRUE); + } +} + +VOID ConfigFileUnsetAll(CONFIGFILEPTR abstractConfigFile, CONST_STRPTR compoundKey, CONST_STRPTR stringValue) +{ + struct ConfigFile* configFile = (struct ConfigFile*)abstractConfigFile; + if( configFile != NULL ) + { + StringArray parts = ConfigFileSplitKeyCompletely(compoundKey); + + StringArrayFree(parts, TRUE); + } +} + +VOID ConfigFileDump(CONFIGFILEPTR abstractConfigFile) +{ + ULONG index = 0; + ULONG count = 0; + struct ConfigFile* configFile = (struct ConfigFile*)abstractConfigFile; + if( configFile != NULL ) + { + count = SectionStoreSectionCount(configFile->sectionStore); + for( index = 0; index < count; index++ ) + { + SECTIONPTR section = SectionStoreSectionAt(configFile->sectionStore, index); + SectionDump(section); + } + } +} + +STATIC LINEPTR configFileReadLine(BPTR file) +{ + UBYTE* buffer = AllocVec(512, MEMF_CLEAR); + ULONG bufLength = 512; + ULONG bytesReadTotal = 0; + UBYTE* read = NULL; + struct Line* result = NULL; + + // read the whole line including continuation + do + { + read = FGets(file, &(buffer[bytesReadTotal]), bufLength-bytesReadTotal); + bytesReadTotal = strlen(buffer); + } + while( read != NULL && bytesReadTotal >= 2 && bytesReadTotal < bufLength && buffer[bytesReadTotal-1] == '\n' && buffer[bytesReadTotal-2] == '\\' ); + + + // make a line + if( bytesReadTotal > 0 ) + { + buffer[bytesReadTotal] = '\0'; + result = LineNew(buffer); + } + + FreeVec(buffer); + return result; +} + +STATIC SECTIONPTR findOrCreateSection(SECTIONSTOREPTR sectionStore, CONST_STRPTR compoundKey) +{ + // now get the section + StringArray canonicalParts = ConfigFileSplitKeyForVar(compoundKey); + SECTIONPTR section = section = SectionStoreGetSection(sectionStore, StringArrayValues(canonicalParts)[0]); + if( section == NULL ) + { + StringArray separateParts = ConfigFileSplitKeyCompletely(compoundKey); + CONST_STRPTR sectionLineText = NULL; + LINEPTR sectionLine = NULL; + + section = SectionCreateWithNameAndSubname(StringArrayValues(separateParts)[0], StringArrayValues(separateParts)[1]); + sectionLineText = SectionSerialize(section); + sectionLine = LineNew(sectionLineText); + SectionAddSectionLine(section, sectionLine); + FreeVec((STRPTR)sectionLineText); + SectionStoreAddSection(sectionStore, section); //section store will free it for us + StringArrayFree(separateParts, TRUE); + } + StringArrayFree(canonicalParts, TRUE); + return section; +} + diff --git a/configfile.h b/configfile.h index 0cda07c..a21d3bf 100644 --- a/configfile.h +++ b/configfile.h @@ -1,3 +1,6 @@ +#ifndef __CONFIGFILE_H +#define __CONFIGFILE_H +#include "types.h" CONFIGFILEPTR ConfigFileRead(CONST_STRPTR filename); VOID ConfigFileFree(CONFIGFILEPTR configFile); @@ -18,4 +21,9 @@ VOID ConfigFileUnset(CONFIGFILEPTR abstractConfigFile, CONST_STRPTR compoundKey, VOID ConfigFileUnsetAll(CONFIGFILEPTR abstractConfigFile, CONST_STRPTR compoundKey, CONST_STRPTR stringValue); // --remove-section -VOID ConfigFileRemoveSection(CONFIGFILEPTR abstractConfigFile, CONST_STRPTR canonicalName); \ No newline at end of file +VOID ConfigFileRemoveSection(CONFIGFILEPTR abstractConfigFile, CONST_STRPTR canonicalName); + +VOID ConfigFileDump(CONFIGFILEPTR configFile); +StringArray ConfigFileSplitKeyForVar(CONST_STRPTR key); +StringArray ConfigFileSplitKeyCompletely(CONST_STRPTR key); +#endif \ No newline at end of file diff --git a/configmodel.c b/configmodel.c index cc74702..3b4951e 100644 --- a/configmodel.c +++ b/configmodel.c @@ -1,29 +1,15 @@ #include "configmodel.h" -#include "sectionstore.h" -#include "cregex/cregex.h" -#include "containers/stringarray.h" #include "containers/linearray.h" #include "containers/sectionarray.h" - #include #include #include #include -#include #include STATIC VOID downcaseString(STRPTR string); -#define ZERO ((BPTR)0) - -// - STRUCTS ----------------------------------------------------------------------------- -struct ConfigFile -{ - CONST_STRPTR filename; - SECTIONSTOREPTR sectionStore; -}; - struct Section { CONST_STRPTR primary; @@ -59,311 +45,6 @@ struct Line struct Section* section; }; -// --------------------------------------------------------------------------------------- -// - CONFIG FILE ------------------------------------------------------------------------- -// --------------------------------------------------------------------------------------- - -VOID ConfigFileFree(CONFIGFILEPTR abstractConfigFile) -{ - struct ConfigFile* configFile = (struct ConfigFile*)abstractConfigFile; - if( configFile != NULL ) - { - if( configFile->sectionStore != NULL ) - { - SectionStoreFree(configFile->sectionStore); - } - FreeVec(configFile); - } -} - -STATIC LINEPTR configFileReadLine(BPTR file); - -CONFIGFILEPTR ConfigFileRead(CONST_STRPTR filename) -{ - struct ConfigFile* result = NULL; - BPTR configFile = Open(filename, MODE_OLDFILE); - if( configFile != ZERO ) - { - - LINEPTR line = NULL; - result = AllocVec(sizeof(struct ConfigFile), MEMF_CLEAR); - result->sectionStore = SectionStoreNew(); - while( (line = configFileReadLine(configFile)) != NULL ) - { - SectionStoreAddLineToCurrentSection(result->sectionStore, line); - //LineDump(line); - } - Close(configFile); - } - - return result; -} - - -// a key is "foo.bar.baz.blah" where "foo" is the primary section name -// "bar.baz" is the secondary section name and "blah" is the variable name. -// this splits foo.bar.baz.blah into foo, bar.baz amd blah. -// section, subsection and variable. -// we'll get empty strings for the parts that arent present -StringArray ConfigFileSplitKeyCompletely(CONST_STRPTR key) -{ - StringArray result = StringArrayNew(); - StringArray parts = StringArrayNew(); - ULONG numberOfParts = 0; - - STRPTR token = NULL; - // we need to make a copy of the key because strtok modifies it. - STRPTR keyCopy = AllocVec(strlen(key)+1, MEMF_CLEAR); - CopyMem(key, keyCopy, strlen(key)); - - token = strtok(keyCopy, "."); - while (token != NULL) - { - StringArrayAppendAndRetain(parts, token); - token = strtok(NULL, "."); - } - FreeVec(keyCopy); - - // now we join all but the first and last part - numberOfParts = SizeOfArray(parts); - if( numberOfParts == 1 ) //just a variable ["","","var"] - { - StringArrayAppendAndRetain(result, ""); - StringArrayAppendAndRetain(result, ""); - StringArrayAppendAndRetain(result, (STRPTR)ArrayBackValue(STRPTR, parts)); - } - else if( numberOfParts == 2 ) // section and variable ["section","","var"] - { - StringArrayAppendAndRetain(result, StringArrayValues(parts)[0]); - StringArrayAppendAndRetain(result, ""); - StringArrayAppendAndRetain(result, StringArrayValues(parts)[1]); - } - else if( numberOfParts == 3 ) // section and subsection and variable ["section","subsec","var"] - { - StringArrayAppendAndRetain(result, StringArrayValues(parts)[0]); - StringArrayAppendAndRetain(result, StringArrayValues(parts)[1]); - StringArrayAppendAndRetain(result, StringArrayValues(parts)[2]); - } - else if( numberOfParts > 3 ) // subsections needs dotted ["section", "subsec1.subsec2", "var""] - { - StringArrayAppendAndRetain(result, StringArrayValues(parts)[0]); //section - - // start at index 1, and add (size -2 (start+end)) parts - StringArrayAppend(result, StringArrayJoinedParts(parts, '.', 1, SizeOfArray(parts)-2)); - - StringArrayAppendAndRetain(result, (STRPTR)ArrayBackValue(STRPTR, parts)); // variable - } - StringArrayFree(parts, TRUE); - return result; -} - -StringArray ConfigFileSplitKeyForVar(CONST_STRPTR key) -{ - // a key is "foo.bar.baz.blah" where "foo" is the primary section name - // "bar.baz" is the secondary section name and "blah" is the variable name. - // In this case we'll just break it down into the canonical section name, - // foo.bar.baz and the variable name blah. - - StringArray result = StringArrayNew(); - StringArray parts = StringArrayNew(); - STRPTR sectionPart = NULL; - STRPTR varPart = NULL; - - STRPTR token = NULL; - // we need to make a copy of the key because strtok modifies it. - STRPTR keyCopy = AllocVec(strlen(key)+1, MEMF_CLEAR); - CopyMem(key, keyCopy, strlen(key)); - - token = strtok(keyCopy, "."); - while (token != NULL) - { - StringArrayAppendAndRetain(parts, token); - token = strtok(NULL, "."); - } - FreeVec(keyCopy); - - // now we join all but the last part - sectionPart = StringArrayJoinedParts(parts, '.', 0, SizeOfArray(parts)-1); - StringArrayAppend(result, sectionPart); // its been alloced so dont copy - varPart = (STRPTR)ArrayBackValue(STRPTR, parts); - StringArrayAppendAndRetain(result, varPart); // its a reference so copy - StringArrayFree(parts, TRUE); - return result; -} - -STRPTR ConfigFileGet(CONFIGFILEPTR abstractConfigFile, CONST_STRPTR compoundKey) -{ - STRPTR result = NULL; - struct ConfigFile* configFile = (struct ConfigFile*)abstractConfigFile; - if( configFile != NULL ) - { - StringArray split = ConfigFileSplitKeyForVar(compoundKey); - VARIABLEPTR var = SectionStoreGet(configFile->sectionStore, StringArrayValues(split)[0], StringArrayValues(split)[1]); - if( var != NULL ) - { - CONST_STRPTR value = VariableGetRawValue(var); - result = AllocVec(strlen(value)+1, MEMF_CLEAR); - CopyMem(value, result, strlen(value)); - } - StringArrayFree(split, TRUE); - } - return result; -} - -StringArray ConfigFileGetAll(CONFIGFILEPTR abstractConfigFile, CONST_STRPTR compoundKey) -{ - StringArray result = StringArrayNew(); - ULONG index = 0; - struct ConfigFile* configFile = (struct ConfigFile*)abstractConfigFile; - if( configFile != NULL ) - { - StringArray split = ConfigFileSplitKeyForVar(compoundKey); - VariableArray vars = SectionStoreGetAll(configFile->sectionStore, StringArrayValues(split)[0], StringArrayValues(split)[1]); - for( index = 0; index < SizeOfArray(vars); index++ ) - { - StringArrayAppendAndRetain(result, VariableGetRawValue(VariableArrayValues(vars)[index])); - } - VariableArrayFree(vars, FALSE); - StringArrayFree(split, TRUE); - } - return result; -} - -VOID ConfigFileAdd(CONFIGFILEPTR abstractConfigFile, CONST_STRPTR compoundKey, CONST_STRPTR stringValue) -{ - struct ConfigFile* configFile = (struct ConfigFile*)abstractConfigFile; - if( configFile != NULL ) - { - SECTIONPTR section = NULL; - VARIABLEPTR var = NULL; - CONST_STRPTR varLine = NULL; - LINEPTR line = NULL; - StringArray canonicalParts = NULL; - - // get canonical section name and var name - canonicalParts = ConfigFileSplitKeyForVar(compoundKey); - - // build a var line - var = VariableCreate(StringArrayValues(canonicalParts)[1], stringValue); - varLine = VariableSerialize(var); - line = LineNew(varLine); - FreeVec((STRPTR)varLine); - LineSetInitialVariable(line, var); - - // now get the section - section = SectionStoreGetSection(configFile->sectionStore, StringArrayValues(canonicalParts)[0]); - if( section == NULL ) - { - StringArray separateParts = ConfigFileSplitKeyCompletely(compoundKey); - CONST_STRPTR sectionLineText = NULL; - LINEPTR sectionLine = NULL; - - section = SectionCreateWithNameAndSubname(StringArrayValues(separateParts)[0], StringArrayValues(separateParts)[1]); - sectionLineText = SectionSerialize(section); - sectionLine = LineNew(sectionLineText); - SectionAddSectionLine(section, sectionLine); - FreeVec((STRPTR)sectionLineText); - SectionDump(section); - SectionStoreAddSection(configFile->sectionStore, section); //section store will free it for us - SectionDump(section); - StringArrayFree(separateParts, TRUE); - } - // add the line to the section - SectionAddLine(section, line); - SectionDump(section); - - StringArrayFree(canonicalParts, TRUE); - } -} - -VOID ConfigFileSet(CONFIGFILEPTR abstractConfigFile, CONST_STRPTR compoundKey, CONST_STRPTR stringValue) -{ - struct ConfigFile* configFile = (struct ConfigFile*)abstractConfigFile; - if( configFile != NULL ) - { - StringArray parts = ConfigFileSplitKeyCompletely(compoundKey); - - StringArrayFree(parts, TRUE); - } -} - -VOID ConfigFileReplaceAll(CONFIGFILEPTR abstractConfigFile, CONST_STRPTR compoundKey, CONST_STRPTR stringValue) -{ - struct ConfigFile* configFile = (struct ConfigFile*)abstractConfigFile; - if( configFile != NULL ) - { - StringArray parts = ConfigFileSplitKeyCompletely(compoundKey); - - StringArrayFree(parts, TRUE); - } -} - -VOID ConfigFileUnset(CONFIGFILEPTR abstractConfigFile, CONST_STRPTR compoundKey, CONST_STRPTR stringValue) -{ - struct ConfigFile* configFile = (struct ConfigFile*)abstractConfigFile; - if( configFile != NULL ) - { - StringArray parts = ConfigFileSplitKeyCompletely(compoundKey); - - StringArrayFree(parts, TRUE); - } -} - -VOID ConfigFileUnsetAll(CONFIGFILEPTR abstractConfigFile, CONST_STRPTR compoundKey, CONST_STRPTR stringValue) -{ - struct ConfigFile* configFile = (struct ConfigFile*)abstractConfigFile; - if( configFile != NULL ) - { - StringArray parts = ConfigFileSplitKeyCompletely(compoundKey); - - StringArrayFree(parts, TRUE); - } -} - -VOID ConfigFileDump(CONFIGFILEPTR abstractConfigFile) -{ - ULONG index = 0; - ULONG count = 0; - struct ConfigFile* configFile = (struct ConfigFile*)abstractConfigFile; - if( configFile != NULL ) - { - count = SectionStoreSectionCount(configFile->sectionStore); - for( index = 0; index < count; index++ ) - { - SECTIONPTR section = SectionStoreSectionAt(configFile->sectionStore, index); - SectionDump(section); - } - } -} - -STATIC LINEPTR configFileReadLine(BPTR file) -{ - UBYTE* buffer = AllocVec(512, MEMF_CLEAR); - ULONG bufLength = 512; - ULONG bytesReadTotal = 0; - UBYTE* read = NULL; - struct Line* result = NULL; - - // read the whole line including continuation - do - { - read = FGets(file, &(buffer[bytesReadTotal]), bufLength-bytesReadTotal); - bytesReadTotal = strlen(buffer); - } - while( read != NULL && bytesReadTotal >= 2 && bytesReadTotal < bufLength && buffer[bytesReadTotal-1] == '\n' && buffer[bytesReadTotal-2] == '\\' ); - - - // make a line - if( bytesReadTotal > 0 ) - { - buffer[bytesReadTotal] = '\0'; - result = LineNew(buffer); - } - - FreeVec(buffer); - return result; -} - // --------------------------------------------------------------------------------------- // - LINE -------------------------------------------------------------------------------- // --------------------------------------------------------------------------------------- @@ -413,16 +94,16 @@ STATIC VOID lineDumpRecreate(LINEPTR abstractLine) { // section struct Section* section = (struct Section*)LineGetSection(abstractLine); - STRPTR sectionText = SectionSerialize(section); + CONST_STRPTR sectionText = SectionSerialize(section); Printf(sectionText); - FreeVec(sectionText); + FreeVec((STRPTR)sectionText); } else { struct Variable* var = (struct Variable*)LineGetVariable(abstractLine); - STRPTR varText = VariableSerialize(var); + CONST_STRPTR varText = VariableSerialize(var); Printf(varText); - FreeVec(varText); + FreeVec((STRPTR)varText); } } } diff --git a/configmodel.h b/configmodel.h index 14a9d63..ec6b714 100644 --- a/configmodel.h +++ b/configmodel.h @@ -1,22 +1,6 @@ #ifndef __CONFIGMODEL_H #define __CONFIGMODEL_H #include "types.h" -#include - -CONFIGFILEPTR ConfigFileRead(CONST_STRPTR filename); -VOID ConfigFileFree(CONFIGFILEPTR configFile); - -StringArray ConfigFileGetAll(CONFIGFILEPTR abstractConfigFile, CONST_STRPTR compoundKey); -STRPTR ConfigFileGet(CONFIGFILEPTR abstractConfigFile, CONST_STRPTR compoundKey); -VOID ConfigFileAdd(CONFIGFILEPTR abstractConfigFile, CONST_STRPTR compoundKey, CONST_STRPTR stringValue); -VOID ConfigFileSet(CONFIGFILEPTR abstractConfigFile, CONST_STRPTR compoundKey, CONST_STRPTR stringValue); -VOID ConfigFileReplaceAll(CONFIGFILEPTR abstractConfigFile, CONST_STRPTR compoundKey, CONST_STRPTR stringValue); -VOID ConfigFileUnset(CONFIGFILEPTR abstractConfigFile, CONST_STRPTR compoundKey, CONST_STRPTR stringValue); -VOID ConfigFileUnsetAll(CONFIGFILEPTR abstractConfigFile, CONST_STRPTR compoundKey, CONST_STRPTR stringValue); - -VOID ConfigFileDump(CONFIGFILEPTR configFile); -StringArray ConfigFileSplitKeyForVar(CONST_STRPTR key); -StringArray ConfigFileSplitKeyCompletely(CONST_STRPTR key); LINEPTR LineNew(CONST_STRPTR buffer); VOID LineFree(LINEPTR abstractLine); diff --git a/containers/linearray.c b/containers/linearray.c index 8ae0294..ba1e503 100644 --- a/containers/linearray.c +++ b/containers/linearray.c @@ -2,6 +2,7 @@ #include #include #include "linearray.h" +#include "configmodel.h" // ----------------------------- diff --git a/containers/linearray.h b/containers/linearray.h index 04557ac..fea74f4 100644 --- a/containers/linearray.h +++ b/containers/linearray.h @@ -2,9 +2,9 @@ #define __LINEARRAY_H #include +#include "types.h" #include -#include -#include "configmodel.h" +#include LineArray LineArrayNew(VOID); VOID LineArrayAppend(LineArray array, LINEPTR value); diff --git a/containers/sectionarray.c b/containers/sectionarray.c index f6de047..5e7fbd9 100644 --- a/containers/sectionarray.c +++ b/containers/sectionarray.c @@ -1,6 +1,8 @@ #include "sectionarray.h" #include #include +#include "configmodel.h" + #define SIZE_APTR 2 SectionArray SectionArrayNew(VOID) diff --git a/containers/sectionarray.h b/containers/sectionarray.h index 237d2c5..eef808e 100644 --- a/containers/sectionarray.h +++ b/containers/sectionarray.h @@ -1,9 +1,9 @@ #ifndef __SECTIONARRAY_H #define __SECTIONARRAY_H -#include +#include "types.h" #include -#include "configmodel.h" +#include SectionArray SectionArrayNew(VOID); VOID SectionArrayAppend(SectionArray array, SECTIONPTR value); diff --git a/containers/stringarray.h b/containers/stringarray.h index 7b4b704..9a637c9 100644 --- a/containers/stringarray.h +++ b/containers/stringarray.h @@ -2,6 +2,8 @@ #define __STRINGARRAY_H #include "types.h" +#include +#include StringArray StringArrayNew(VOID); VOID StringArrayAppend(StringArray array, CONST_STRPTR value); diff --git a/main.c b/main.c index 8192c21..26b98ac 100644 --- a/main.c +++ b/main.c @@ -5,6 +5,7 @@ #define __NOLIBBASE__ #include "containers/stringarray.h" #include "containers/linearray.h" +#include "configfile.h" #include "configmodel.h" #include @@ -19,7 +20,6 @@ struct Library *ContainerkitBase; WORD DoTheWork(STRPTR filename) { WORD result = RETURN_OK; - STRPTR joined = NULL; if (ContainerkitBase) { CONFIGFILEPTR config = ConfigFileRead(filename); @@ -29,16 +29,17 @@ WORD DoTheWork(STRPTR filename) STRPTR var = NULL; values = ConfigFileGetAll(config, "branch.main.remote"); - StringArrayForEach(values, Printf("main %s\n", aString);); +// StringArrayForEach(values, Printf("main %s\n", aString);); StringArrayFree(values, TRUE); var = ConfigFileGet(config, "branch.config-file-parsing-from-book.remote"); - Printf("parse %s\n\n\n", var); +// Printf("parse %s\n\n\n", var); FreeVec(var); // ConfigFileDump(config); + ConfigFileAdd(config, "branch.main.foob", "bar"); ConfigFileAdd(config, "branch.alan.foob", "bar"); - // ConfigFileDump(config); + ConfigFileDump(config); ConfigFileFree(config); } diff --git a/smakefile b/smakefile index 522070d..9e8ea86 100644 --- a/smakefile +++ b/smakefile @@ -11,8 +11,8 @@ LIBS = lib:sc.lib lib:amiga.lib lib:debug.lib ############################################################################### -$(NAME) : main.o configmodel.o sectionstore.o cregex/cregex.lib containers/containers.lib - slink lib:c.o main.o configmodel.o sectionstore.o to $(NAME) noicons lib $(LIBS) cregex/cregex.lib containers/containers.lib $(LFLAGS) +$(NAME) : main.o configfile.o configmodel.o sectionstore.o cregex/cregex.lib containers/containers.lib + slink lib:c.o main.o configfile.o configmodel.o sectionstore.o to $(NAME) noicons lib $(LIBS) cregex/cregex.lib containers/containers.lib $(LFLAGS) cregex/cregex.lib : cregex/pattern.o cregex/cregex_compile.o cregex/cregex_parse.o cregex/cregex_vm.o JOIN cregex/pattern.o cregex/cregex_compile.o cregex/cregex_parse.o cregex/cregex_vm.o AS cregex/cregex.lib @@ -30,7 +30,7 @@ cleanlibs: main.o : main.c configmodel.o : configmodel.c configmodel.h types.h -configregex.o : configregex.c configregex.h types.h +configfile.o : configfile.c configfile.h types.h sectionstore.o : sectionstore.c sectionstore.h types.h cregex/cregex_compile.o : cregex/cregex_compile.c cregex/cregex.h