From 60ea8cfdbcb2cb1072921c109dcb31524468e5db Mon Sep 17 00:00:00 2001 From: Alan Francis Date: Sat, 2 Dec 2023 10:09:11 +0000 Subject: [PATCH] replace all works --- configfile.c | 6 ++++-- configmodel.c | 33 +++++++++++++++++++++++++++++++++ configmodel.h | 1 + main.c | 1 + sectionstore.c | 10 ++++++++++ sectionstore.h | 1 + testconfig.cfg | 3 +++ 7 files changed, 53 insertions(+), 2 deletions(-) diff --git a/configfile.c b/configfile.c index c30fdb4..f7ee9ae 100644 --- a/configfile.c +++ b/configfile.c @@ -260,8 +260,9 @@ VOID ConfigFileReplaceAll(CONFIGFILEPTR abstractConfigFile, CONST_STRPTR compoun struct ConfigFile* configFile = (struct ConfigFile*)abstractConfigFile; if( configFile != NULL ) { - StringArray parts = ConfigFileSplitKeyCompletely(compoundKey); - + StringArray parts = ConfigFileSplitKeyForVar(compoundKey); + SectionStoreRemoveLines(configFile->sectionStore, StringArrayValues(parts)[0], StringArrayValues(parts)[1]); + ConfigFileAddVariable( abstractConfigFile, compoundKey, stringValue); StringArrayFree(parts, TRUE); } } @@ -273,6 +274,7 @@ VOID ConfigFileUnset(CONFIGFILEPTR abstractConfigFile, CONST_STRPTR compoundKey, { StringArray parts = ConfigFileSplitKeyCompletely(compoundKey); + StringArrayFree(parts, TRUE); } } diff --git a/configmodel.c b/configmodel.c index e2c5022..079da53 100644 --- a/configmodel.c +++ b/configmodel.c @@ -346,6 +346,39 @@ VOID SectionCollectLinesForVariable(SECTIONPTR abstractSection, CONST_STRPTR var } } +VOID SectionRemoveLinesForVariable(SECTIONPTR abstractSection, CONST_STRPTR varKey) +{ + struct Section* section = (struct Section*)abstractSection; + if( section != NULL ) + { + ULONG lineCount = SizeOfArray(section->lines); + ULONG index = 0; + LineArray newLineArray = LineArrayNew(); + STRPTR normalizedKey = AllocVec(strlen(varKey)+1, MEMF_CLEAR); + CopyMem(varKey, normalizedKey, strlen(varKey)); + downcaseString(normalizedKey); + + for( index = 0; index < lineCount; index++ ) + { + // instead of compacting, we save lines that dont match, and free lines that do + LINEPTR line = LineArrayValues(section->lines)[index]; + if( LineHasVariable(line, normalizedKey) ) + { + LineFree(line); + LineArrayValues(section->lines)[index] = NULL; // dont crash when we cann LineArrayFree + } + else + { + LineArrayAppend(newLineArray, line); + } + } + LineArrayFree(section->lines, FALSE);//dont free the lines, the newArray will do that later + section->lines = newLineArray; + FreeVec(normalizedKey); + } +} + + VOID SectionDump(SECTIONPTR abstractSection) { struct Section* section = (struct Section*)abstractSection; diff --git a/configmodel.h b/configmodel.h index ec6b714..1aaa115 100644 --- a/configmodel.h +++ b/configmodel.h @@ -27,6 +27,7 @@ VOID SectionDump(SECTIONPTR section); CONST_STRPTR SectionSerialize(SECTIONPTR section); VOID SectionCollectLinesForVariable(SECTIONPTR section, CONST_STRPTR varKey, LineArray collecting); +VOID SectionRemoveLinesForVariable(SECTIONPTR section, CONST_STRPTR varKey); VARIABLEPTR VariableCreate(CONST_STRPTR key, CONST_STRPTR rawValue); BOOL VariableHasKey(VARIABLEPTR variable, CONST_STRPTR varKey); diff --git a/main.c b/main.c index c8b9ab7..37b267a 100644 --- a/main.c +++ b/main.c @@ -40,6 +40,7 @@ WORD DoTheWork(STRPTR filename) // ConfigFileSet(config, "branch.main.foob", "bar"); // ConfigFileSet(config, "branch.alan.foob", "bar"); ConfigFileSet(config, "branch.main.remote", "testing"); + ConfigFileReplaceAll(config, "branch.main.alan", "hello"); ConfigFileDump(config); ConfigFileFree(config); diff --git a/sectionstore.c b/sectionstore.c index 80331f3..7da92be 100644 --- a/sectionstore.c +++ b/sectionstore.c @@ -98,6 +98,16 @@ SECTIONPTR SectionStoreCurrentSection(SECTIONSTOREPTR abstractSectionStore) return result; } +VOID SectionStoreRemoveLines(SECTIONSTOREPTR sectionStore, CONST_STRPTR canonicalSectionName, CONST_STRPTR varKey) +{ + SECTIONPTR section = SectionStoreGetSection(sectionStore, canonicalSectionName); + if( section != NULL ) + { + SectionRemoveLinesForVariable(section, varKey); + } + +} + LineArray SectionStoreFindLines(SECTIONSTOREPTR sectionStore, CONST_STRPTR canonicalSectionName, CONST_STRPTR varKey) { LineArray result = LineArrayNew(); diff --git a/sectionstore.h b/sectionstore.h index 8cabdfb..1215eef 100644 --- a/sectionstore.h +++ b/sectionstore.h @@ -11,6 +11,7 @@ SECTIONPTR SectionStoreCurrentSection(SECTIONSTOREPTR sectionStore); SECTIONPTR SectionStoreGetSection(SECTIONSTOREPTR sectionStore, CONST_STRPTR canonicalName); VOID SectionStoreAddLineToCurrentSection(SECTIONSTOREPTR sectionStore, LINEPTR line); +VOID SectionStoreRemoveLines(SECTIONSTOREPTR sectionStore, CONST_STRPTR canonicalSectionName, CONST_STRPTR varKey); LineArray SectionStoreFindLines(SECTIONSTOREPTR sectionStore, CONST_STRPTR canonicalSectionName, CONST_STRPTR varKey); VariableArray SectionStoreGetAll(SECTIONSTOREPTR sectionStore, CONST_STRPTR canonicalSectionName, CONST_STRPTR varKey); diff --git a/testconfig.cfg b/testconfig.cfg index 8be879c..367d98b 100644 --- a/testconfig.cfg +++ b/testconfig.cfg @@ -10,6 +10,9 @@ [branch "main"] remote = origin merge = refs/heads/main + alan = yes + alan = no + alan = sure [branch "config-file-parsing-from-book"] remote = origin