diff --git a/configmodel.c b/configmodel.c index a4b6bca..e1200a5 100644 --- a/configmodel.c +++ b/configmodel.c @@ -1,4 +1,5 @@ #include "configmodel.h" +#include "sectionstore.h" #include "cregex/cregex.h" #include "containers/stringarray.h" @@ -43,6 +44,7 @@ struct ConfigFile { CONST_STRPTR filename; LineArray lines; + SECTIONSTOREPTR sectionStore; }; struct Section @@ -88,6 +90,10 @@ VOID ConfigFileFree(CONFIGFILEPTR abstractConfigFile) { LineArrayFree(configFile->lines, TRUE); //also frees lines } + if( configFile->sectionStore != NULL ) + { + SectionStoreFree(configFile->sectionStore); + } FreeVec(configFile); } } @@ -99,7 +105,8 @@ CONFIGFILEPTR ConfigFileRead(CONST_STRPTR filename) if( configFile != ZERO ) { LINEPTR line = NULL; - SECTIONPTR currentSection = NULL; + SECTIONPTR currentSection = NULL;//SectionCreateWithName(""); // initial empty section + result->sectionStore = SectionStoreNew(); result->lines = LineArrayNew(); InitialisePatterns(); while( (line = LineReadIncludingContinuation(configFile, currentSection)) != NULL ) diff --git a/configmodel.h b/configmodel.h index 0a7f311..61512da 100644 --- a/configmodel.h +++ b/configmodel.h @@ -19,4 +19,12 @@ VOID ReleasePatterns(VOID); LINEPTR LineReadIncludingContinuation(BPTR file, SECTIONPTR currentSection); VOID LineFree(LINEPTR abstractLine); CONST_STRPTR LineGetRawText(LINEPTR line); + +SECTIONPTR SectionCreateWithName(CONST_STRPTR primary); +SECTIONPTR SectionCreateWithNameAndSubname(CONST_STRPTR primary, CONST_STRPTR secondary); +VOID SectionFree(SECTIONPTR abstractSection); + +VARIABLEPTR VariableCreate(CONST_STRPTR key, CONST_STRPTR rawValue); +VOID VariableFree(VARIABLEPTR abstractVariable); + #endif \ No newline at end of file diff --git a/containers/linemap.h b/containers/linemap.h index e9682e8..df4bdf2 100644 --- a/containers/linemap.h +++ b/containers/linemap.h @@ -8,6 +8,6 @@ #define LineMap Map LineMap LineMapNew(VOID); - VOID LineMapFree(LineMap map); + #endif \ No newline at end of file diff --git a/containers/sectionarray.c b/containers/sectionarray.c new file mode 100644 index 0000000..6e5dfe7 --- /dev/null +++ b/containers/sectionarray.c @@ -0,0 +1,28 @@ +#include "sectionarray.h" +#include +#include + +Array SectionArrayNew(VOID) +{ + #define SIZE_STRPTR 2 + return NewArray(SIZE_STRPTR); +} + +VOID SectionArrayAppend(SectionArray array, SECTIONPTR value) +{ + AppendToArray(CONST_STRPTR, array, value); +} + +VOID SectionArrayFree(SectionArray array) +{ + if( array != NULL ) + { + SectionArrayForEach(array, SectionFree(aSection);); + DeleteArray(array); + } +} + +SECTIONPTR* SectionArrayValues(SectionArray array) +{ + return ArrayValues(SECTIONPTR, array); +} \ No newline at end of file diff --git a/containers/sectionarray.h b/containers/sectionarray.h new file mode 100644 index 0000000..883b2d2 --- /dev/null +++ b/containers/sectionarray.h @@ -0,0 +1,17 @@ +#ifndef __SECTIONARRAY_H +#define __SECTIONARRAY_H + +#include +#include +#include "configmodel.h" +#define SectionArray Array + +SectionArray SectionArrayNew(VOID); +VOID SectionArrayAppend(SectionArray array, CONST_STRPTR value); +VOID SectionArrayFree(SectionArray array); +SECTIONPTR* SectionArrayValues(SectionArray array); + +#define SectionArrayForEach(array, block) do {SECTIONPTR *afe_123_p = (*(SECTIONPTR **)array); SECTIONPTR aSection = *afe_123_p; int afe_123_c = (((ULONG *)array)[1]);\ + for (; afe_123_c--; aSection = *(++afe_123_p)) block} while (0); + +#endif \ No newline at end of file diff --git a/containers/sectionmap.c b/containers/sectionmap.c new file mode 100644 index 0000000..edfb790 --- /dev/null +++ b/containers/sectionmap.c @@ -0,0 +1,45 @@ +#include "sectionmap.h" +#include "configmodel.h" + +SectionMap SectionMapNew() +{ + SectionMap result = NewMap(CNTKIT_KEY_STRING, + CNTKIT_CAPACITY, 8, + CNTKIT_VALUESIZE, 4, + TAG_DONE); + + return result; +} + +VOID SectionMapSet(SectionMap map, CONST_STRPTR canonicalName, SECTIONPTR section) +{ + if( canonicalName != NULL ) + { + if( section == NULL ) + { + RemoveStrKey(map, (STRPTR)canonicalName); + } + else + { + MapStrToValue(map, (STRPTR)canonicalName, (ULONG)section); + } + } +} + +SECTIONPTR SectionMapGet(SectionMap map, CONST_STRPTR canonicalName) +{ + if( canonicalName != NULL ) + { + return (SECTIONPTR)ValueOfStrKey(map, (STRPTR)canonicalName); + } + else + { + return NULL; + } +} + +VOID SectionMapFree(SectionMap map) +{ + // doesnt seem to delete contents + DeleteMap(map); +} diff --git a/containers/sectionmap.h b/containers/sectionmap.h new file mode 100644 index 0000000..f3035bc --- /dev/null +++ b/containers/sectionmap.h @@ -0,0 +1,14 @@ +#ifndef __SECTIONMAP_H +#define __SECTIONMAP_H + +#include +#include +#include "configmodel.h" + +#define SectionMap Map // a map of string (canonical name) to Section + +SectionMap SectionMapNew(VOID); +VOID SectionMapSet(SectionMap map, CONST_STRPTR canonicalName, SECTIONPTR section); +SECTIONPTR SectionMapGet(SectionMap map, CONST_STRPTR canonicalName); +VOID SectionMapFree(SectionMap map); +#endif \ No newline at end of file diff --git a/containers/stringarray.c b/containers/stringarray.c index 38c68b0..000b5d2 100644 --- a/containers/stringarray.c +++ b/containers/stringarray.c @@ -1,4 +1,5 @@ #include "stringarray.h" +#include "configmodel.h" #include #include diff --git a/sectionstore.c b/sectionstore.c new file mode 100644 index 0000000..ce13c21 --- /dev/null +++ b/sectionstore.c @@ -0,0 +1,48 @@ +#include "sectionstore.h" +#include "containers/sectionmap.h" +#include "containers/sectionarray.h" + +#include + +struct SectionStore +{ + SectionMap map; + SectionArray array; +}; + +SECTIONSTOREPTR SectionStoreNew(VOID) +{ + struct SectionStore* result = AllocVec(sizeof(struct SectionStore), MEMF_CLEAR); + result->map = SectionMapNew(); + result->array = SectionArrayNew(); + + return result; +} + +VOID SectionStoreFree(SECTIONSTOREPTR abstractSectionStore) +{ + if( abstractSectionStore != NULL ) + { + struct SectionStore* store = (struct SectionStore*)abstractSectionStore; + if( store->map != NULL ) SectionMapFree( store->map ); + if( store->array != NULL ) SectionArrayFree( store->array ); + FreeVec(store); + } +} + +VOID SectionStoreAddSection(SECTIONPTR section) +{ +} + +VOID SectionStoreRemoveSection(SECTIONPTR section) +{ +} + +VOID SectionStoreRemoveSectionByName(CONST_STRPTR canonicalName) +{ +} + +SECTIONPTR SectionStoreGetSection(CONST_STRPTR canonicalName) +{ + return NULL; +} diff --git a/sectionstore.h b/sectionstore.h new file mode 100644 index 0000000..cd9f3c4 --- /dev/null +++ b/sectionstore.h @@ -0,0 +1,18 @@ +#ifndef __SECTIONSTORE_H +#define __SECTIONSTORE_H + +#include + +typedef APTR SECTIONSTOREPTR; + +#include "configmodel.h" + +SECTIONSTOREPTR SectionStoreNew(VOID); +VOID SectionStoreFree(SECTIONSTOREPTR sectionStore); + +VOID SectionStoreAddSection(SECTIONPTR section); +VOID SectionStoreRemoveSection(SECTIONPTR section); +VOID SectionStoreRemoveSectionByName(CONST_STRPTR canonicalName); + +SECTIONPTR SectionStoreGetSection(CONST_STRPTR canonicalName); +#endif \ No newline at end of file diff --git a/smakefile b/smakefile index 032d25a..375404a 100644 --- a/smakefile +++ b/smakefile @@ -11,14 +11,14 @@ LIBS = lib:sc.lib lib:amiga.lib lib:debug.lib ############################################################################### -$(NAME) : main.o configmodel.o cregex/cregex.lib containers/containers.lib - slink lib:c.o main.o configmodel.o to $(NAME) noicons lib $(LIBS) cregex/cregex.lib containers/containers.lib $(LFLAGS) +$(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) cregex/cregex.lib : cregex/cregex_compile.o cregex/cregex_parse.o cregex/cregex_vm.o JOIN cregex/cregex_compile.o cregex/cregex_parse.o cregex/cregex_vm.o AS cregex/cregex.lib -containers/containers.lib : containers/stringarray.o containers/linearray.o containers/linemap.o - JOIN containers/stringarray.o containers/linearray.o containers/linemap.o AS containers/containers.lib +containers/containers.lib : containers/stringarray.o containers/linearray.o containers/linemap.o containers/sectionarray.o containers/sectionmap.o + JOIN containers/stringarray.o containers/linearray.o containers/linemap.o containers/sectionarray.o containers/sectionmap.o AS containers/containers.lib clean: delete \#?.o $(NAME) ALL QUIET @@ -30,6 +30,7 @@ cleanlibs: main.o : main.c configmodel.o : configmodel.c configmodel.h +sectionstore.o : sectionstore.c sectionstore.h cregex/cregex_compile.o : cregex/cregex_compile.c cregex/cregex.h cregex/cregex_parse.o : cregex/cregex_parse.c cregex/cregex.h @@ -38,3 +39,5 @@ cregex/cregex_vm.o : cregex/cregex_vm.c cregex/cregex.h containers/stringarray.o : containers/stringarray.c containers/stringarray.h containers/linearray.o : containers/linearray.c containers/linearray.h containers/linemap.o : containers/linemap.c containers/linemap.h +containers/sectionarray.o : containers/sectionarray.c containers/sectionarray.h +containers/sectionmap.o : containers/sectionmap.c containers/sectionmap.h