replace all works

main
Alan Francis 1 year ago
parent 199f9a988d
commit 60ea8cfdbc
  1. 6
      configfile.c
  2. 33
      configmodel.c
  3. 1
      configmodel.h
  4. 1
      main.c
  5. 10
      sectionstore.c
  6. 1
      sectionstore.h
  7. 3
      testconfig.cfg

@ -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);
}
}

@ -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;

@ -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);

@ -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);

@ -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();

@ -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);

@ -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

Loading…
Cancel
Save