diff --git a/configfile.c b/configfile.c index b3dd04b..6f7e19e 100644 --- a/configfile.c +++ b/configfile.c @@ -64,6 +64,13 @@ CONFIGFILEPTR ConfigFileRead(CONST_STRPTR filename) } +StringArray ConfigFileSubsectionsForSection(CONFIGFILEPTR abstractConfigFile, CONST_STRPTR primarySection) +{ + struct ConfigFile* configFile = (struct ConfigFile*)abstractConfigFile; + return SectionStoreSubsectionsForSection(configFile->sectionStore, primarySection); +} + + // 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. diff --git a/configfile.h b/configfile.h index f04e9dc..72caa4a 100644 --- a/configfile.h +++ b/configfile.h @@ -26,4 +26,7 @@ VOID ConfigFileRemoveSection(CONFIGFILEPTR abstractConfigFile, CONST_STRPTR cano VOID ConfigFileDump(CONFIGFILEPTR configFile); StringArray ConfigFileSplitKeyForVar(CONST_STRPTR key); StringArray ConfigFileSplitKeyCompletely(CONST_STRPTR key); + +StringArray ConfigFileSubsectionsForSection(CONFIGFILEPTR abstractConfigFile, CONST_STRPTR primarySection); + #endif \ No newline at end of file diff --git a/configmodel.c b/configmodel.c index e5a9944..5257703 100644 --- a/configmodel.c +++ b/configmodel.c @@ -252,6 +252,19 @@ SECTIONPTR SectionCreateWithNameAndSubname(CONST_STRPTR primary, CONST_STRPTR se return result; } +BOOL SectionHasName(SECTIONPTR abstractSection, CONST_STRPTR primary) +{ + struct Section* section = (struct Section*)abstractSection; + return (BOOL)(strcmp(primary, section->primary) == 0); +} + +CONST_STRPTR SectionGetRawSubsectionName(SECTIONPTR abstractSection) +{ + struct Section* section = (struct Section*)abstractSection; + return section->secondary; +} + + VOID SectionAddSectionLine(SECTIONPTR abstractSection, LINEPTR abstractLine) { // sets self onto line and then adds line to the collection diff --git a/configmodel.h b/configmodel.h index da7183c..926b1ed 100644 --- a/configmodel.h +++ b/configmodel.h @@ -25,6 +25,8 @@ VOID SectionAddLine(SECTIONPTR abstractSection, LINEPTR abstractLine, BOOL addBe CONST_STRPTR SectionCanonicalName(SECTIONPTR section); VOID SectionDump(SECTIONPTR section); CONST_STRPTR SectionSerialize(SECTIONPTR section); +BOOL SectionHasName(SECTIONPTR section, CONST_STRPTR primary); +CONST_STRPTR SectionGetRawSubsectionName(SECTIONPTR section); VOID SectionCollectLinesForVariable(SECTIONPTR section, CONST_STRPTR varKey, LineArray collecting); VOID SectionRemoveLinesForVariable(SECTIONPTR section, CONST_STRPTR varKey); diff --git a/main.c b/main.c index 749d282..ca517d6 100644 --- a/main.c +++ b/main.c @@ -56,6 +56,12 @@ WORD DoTheWork(STRPTR filename) // ConfigFileAddVariable(config, "test.foo.bar.baz.foo1", "alan5"); // ConfigFileAddVariable(config, "test.foo.bar.baz.bat.foo1", "alan6"); // ConfigFileAddVariable(config, "test.section.jane.foo3", "alan3"); +// values = ConfigFileSubsectionsForSection(config, "branch"); +// if( values ) +// { +// StringArrayForEach(values, Printf("branch:[%s]\n",aString);); +// StringArrayFree(values, TRUE); +// } ConfigFileDump(config); // ConfigFileFree(config); diff --git a/sectionstore.c b/sectionstore.c index 150bd11..e3f99b7 100644 --- a/sectionstore.c +++ b/sectionstore.c @@ -212,6 +212,27 @@ SECTIONPTR SectionStoreSectionAt(SECTIONSTOREPTR abstractSectionStore, ULONG ind return result; } +StringArray SectionStoreSubsectionsForSection(SECTIONSTOREPTR abstractSectionStore, CONST_STRPTR primarySection) +{ + struct SectionStore* store = (struct SectionStore*)abstractSectionStore; + StringArray result = StringArrayNew(); + ULONG index = 0; + + for( index = 0; index < SizeOfArray(store->array); index++) + { + SECTIONPTR section = SectionArrayValues(store->array)[index]; + if( SectionHasName(section, primarySection) ) + { + CONST_STRPTR secondary = SectionGetRawSubsectionName(section); + if( secondary != NULL && strlen(secondary) > 0 ) + { + StringArrayAppendAndRetain(result, secondary); + } + } + } + return result; +} + SECTIONPTR SectionStoreFindOrCreateSection(SECTIONSTOREPTR sectionStore, CONST_STRPTR compoundKey) { // now get the section diff --git a/sectionstore.h b/sectionstore.h index 65424b8..61ea2b6 100644 --- a/sectionstore.h +++ b/sectionstore.h @@ -22,4 +22,6 @@ VARIABLEPTR SectionStoreGet(SECTIONSTOREPTR sectionStore, CONST_STRPTR canonical ULONG SectionStoreSectionCount(SECTIONSTOREPTR sectionStore); SECTIONPTR SectionStoreSectionAt(SECTIONSTOREPTR sectionStore, ULONG index); +StringArray SectionStoreSubsectionsForSection(SECTIONSTOREPTR sectionStore, CONST_STRPTR primarySection); + #endif \ No newline at end of file