diff --git a/configmodel.c b/configmodel.c index 2818dcb..bc36223 100644 --- a/configmodel.c +++ b/configmodel.c @@ -109,7 +109,7 @@ CONFIGFILEPTR ConfigFileRead(CONST_STRPTR filename) result->sectionStore = SectionStoreNew(); result->lines = LineArrayNew(); InitialisePatterns(); - while( (line = LineReadIncludingContinuation(configFile, currentSection)) != NULL ) + while( (line = LineReadIncludingContinuation(configFile, result->sectionStore, currentSection)) != NULL ) { // add it to the flat list of lines LineArrayAppend(result->lines, line); @@ -131,7 +131,7 @@ VOID ConfigFileSave(CONFIGFILEPTR config) { } -LINEPTR LineCreate(CONST_STRPTR buffer, ULONG size) +LINEPTR LineCreate(CONST_STRPTR buffer, ULONG size, SECTIONSTOREPTR sectionStore) { struct Line* result = NULL; StringArray matches = NULL; @@ -144,13 +144,19 @@ LINEPTR LineCreate(CONST_STRPTR buffer, ULONG size) if( matches = RunPattern(result->rawText, sectionPatternProgram) ) { + SECTIONPTR section = NULL; if( SizeOfArray(matches) == 3 ) { - result->section = SectionCreateWithName(StringArrayValues(matches)[1]); + section = SectionCreateWithName(StringArrayValues(matches)[1]); } else if( SizeOfArray(matches) == 5 ) { - result->section = SectionCreateWithNameAndSubname(StringArrayValues(matches)[1], StringArrayValues(matches)[3]); + section = SectionCreateWithNameAndSubname(StringArrayValues(matches)[1], StringArrayValues(matches)[3]); + } + if( section != NULL ) + { + SectionStoreAddSection(sectionStore, section); + result->section = section; } StringArrayFree(matches); } @@ -217,7 +223,7 @@ VOID LineDump(LINEPTR abstractLine) } } -LINEPTR LineReadIncludingContinuation(BPTR file, SECTIONPTR currentSection) +LINEPTR LineReadIncludingContinuation(BPTR file, SECTIONSTOREPTR sectionStore, SECTIONPTR currentSection) { UBYTE* buffer = AllocVec(512, MEMF_CLEAR); ULONG bufLength = 512; @@ -236,7 +242,7 @@ LINEPTR LineReadIncludingContinuation(BPTR file, SECTIONPTR currentSection) // make a line if( bytesReadTotal > 0 ) { - result = LineCreate(buffer, bytesReadTotal); + result = LineCreate(buffer, bytesReadTotal, sectionStore); if( result->variable != NULL && result->section == NULL ) { result->section = currentSection; @@ -276,12 +282,7 @@ VOID LineFree(LINEPTR abstractLine) { FreeVec(line->rawText); } - if( line->section != NULL && line->variable == NULL ) - { - //only free the section in a section line, - //not a variable line as thats just a weak ref - SectionFree(line->section); - } + // we dont free the section as its stored in the section store if( line->variable != NULL ) { VariableFree(line->variable); diff --git a/configmodel.h b/configmodel.h index f43fb9b..92e2127 100644 --- a/configmodel.h +++ b/configmodel.h @@ -1,13 +1,8 @@ #ifndef __CONFIGMODEL_H #define __CONFIGMODEL_H -#include +#include "types.h" #include -typedef APTR LINEPTR; -typedef APTR SECTIONPTR; -typedef APTR VARIABLEPTR; -typedef APTR CONFIGFILEPTR; - CONFIGFILEPTR ConfigFileRead(CONST_STRPTR filename); VOID ConfigFileFree(CONFIGFILEPTR abstractConfigFile); @@ -16,7 +11,7 @@ VOID ConfigFileSave(CONFIGFILEPTR config); VOID InitialisePatterns(VOID); VOID ReleasePatterns(VOID); -LINEPTR LineReadIncludingContinuation(BPTR file, SECTIONPTR currentSection); +LINEPTR LineReadIncludingContinuation(BPTR file, SECTIONPTR currentSection, SECTIONSTOREPTR sectionStore); VOID LineFree(LINEPTR abstractLine); CONST_STRPTR LineGetRawText(LINEPTR line); diff --git a/containers/sectionarray.h b/containers/sectionarray.h index 883b2d2..b07981c 100644 --- a/containers/sectionarray.h +++ b/containers/sectionarray.h @@ -7,7 +7,7 @@ #define SectionArray Array SectionArray SectionArrayNew(VOID); -VOID SectionArrayAppend(SectionArray array, CONST_STRPTR value); +VOID SectionArrayAppend(SectionArray array, SECTIONPTR value); VOID SectionArrayFree(SectionArray array); SECTIONPTR* SectionArrayValues(SectionArray array); diff --git a/sectionstore.c b/sectionstore.c index ce13c21..b7c983a 100644 --- a/sectionstore.c +++ b/sectionstore.c @@ -30,19 +30,29 @@ VOID SectionStoreFree(SECTIONSTOREPTR abstractSectionStore) } } -VOID SectionStoreAddSection(SECTIONPTR section) -{ -} - -VOID SectionStoreRemoveSection(SECTIONPTR section) -{ -} - -VOID SectionStoreRemoveSectionByName(CONST_STRPTR canonicalName) +VOID SectionStoreAddSection(SECTIONSTOREPTR abstractSectionStore, SECTIONPTR section) { + struct SectionStore* store = (struct SectionStore*)abstractSectionStore; + if( store != NULL && section != NULL ) + { + CONST_STRPTR canonicalName = SectionCanonicalName(section); + if( canonicalName != NULL ) + { + // the array acts as storage as it can free its contents + SectionArrayAppend(store->array, section); + // the map will copy the string key and free it, but not the section + SectionMapSet(store->map, canonicalName, section); + //free the canonical name as the map has copied it. + FreeVec((STRPTR)canonicalName); + } + } } -SECTIONPTR SectionStoreGetSection(CONST_STRPTR canonicalName) +SECTIONPTR SectionStoreGetSection(SECTIONSTOREPTR abstractSectionStore, CONST_STRPTR canonicalName) { - return NULL; + struct SectionStore* store = (struct SectionStore*)abstractSectionStore; + if( store != NULL && canonicalName != NULL ) + { + return SectionMapGet(store->map, canonicalName); + } } diff --git a/sectionstore.h b/sectionstore.h index cd9f3c4..a0b5e51 100644 --- a/sectionstore.h +++ b/sectionstore.h @@ -1,18 +1,13 @@ #ifndef __SECTIONSTORE_H #define __SECTIONSTORE_H -#include +#include "types.h" -typedef APTR SECTIONSTOREPTR; +SECTIONPTR SectionStoreGetSection(SECTIONSTOREPTR sectionStore, CONST_STRPTR canonicalName); -#include "configmodel.h" +VOID SectionStoreAddSection(SECTIONSTOREPTR sectionStore, SECTIONPTR section); 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); +VOID SectionStoreFree(SECTIONSTOREPTR sectionStore); #endif \ No newline at end of file diff --git a/smakefile b/smakefile index 375404a..14da6ba 100644 --- a/smakefile +++ b/smakefile @@ -29,15 +29,15 @@ cleanlibs: ############################################################################### main.o : main.c -configmodel.o : configmodel.c configmodel.h -sectionstore.o : sectionstore.c sectionstore.h +configmodel.o : configmodel.c configmodel.h types.h +sectionstore.o : sectionstore.c sectionstore.h types.h cregex/cregex_compile.o : cregex/cregex_compile.c cregex/cregex.h cregex/cregex_parse.o : cregex/cregex_parse.c cregex/cregex.h 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 +containers/stringarray.o : containers/stringarray.c containers/stringarray.h types.h +containers/linearray.o : containers/linearray.c containers/linearray.h types.h +containers/linemap.o : containers/linemap.c containers/linemap.h types.h +containers/sectionarray.o : containers/sectionarray.c containers/sectionarray.h types.h +containers/sectionmap.o : containers/sectionmap.c containers/sectionmap.h types.h diff --git a/types.h b/types.h new file mode 100644 index 0000000..3485f2d --- /dev/null +++ b/types.h @@ -0,0 +1,14 @@ +#ifndef __TYPES_H +#define __TYPES_H + +#include + +typedef APTR LINEPTR; +typedef APTR SECTIONPTR; +typedef APTR VARIABLEPTR; +typedef APTR CONFIGFILEPTR; +typedef APTR SECTIONSTOREPTR; + + + +#endif \ No newline at end of file