From c03d0cb62fbc4429b8dde7278711193b780bd53f Mon Sep 17 00:00:00 2001 From: Alan Francis Date: Fri, 24 Nov 2023 20:32:46 +0000 Subject: [PATCH] split any passed key into section and variable parts --- configmodel.c | 44 +++++++++++++++++++++++++++++++++++----- configmodel.h | 1 + containers/stringarray.c | 43 +++++++++++++++++++++++++++++++++++++++ containers/stringarray.h | 3 +++ main.c | 33 +++++++++++++++++++----------- 5 files changed, 107 insertions(+), 17 deletions(-) diff --git a/configmodel.c b/configmodel.c index 87bfeb5..c24177e 100644 --- a/configmodel.c +++ b/configmodel.c @@ -90,13 +90,38 @@ CONFIGFILEPTR ConfigFileRead(CONST_STRPTR filename) return result; } -VOID SectionDump(SECTIONPTR abstractSection) +StringArray ConfigFileSplitKey(CONST_STRPTR key) { - struct Section* section = (struct Section*)abstractSection; - if( section != NULL && section->lines != NULL ) - { - ArrayForEach(LINEPTR, aLine, section->lines, LineDump(aLine);); + // 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. + // In this case we'll just break it down into the canonical section name, + // foo.bar.baz and the variable name blah. + + StringArray result = StringArrayNew(); + StringArray parts = StringArrayNew(); + STRPTR sectionPart = NULL; + STRPTR varPart = NULL; + + STRPTR token = NULL; + // we need to make a copy of the key because strtok modifies it. + STRPTR keyCopy = AllocVec(strlen(key)+1, MEMF_CLEAR); + CopyMem(key, keyCopy, strlen(key)); + + token = strtok(keyCopy, "."); + while (token != NULL) + { + StringArrayAppendAndRetain(parts, token); + token = strtok(NULL, "."); } + FreeVec(keyCopy); + + // now we join all but the last part + sectionPart = StringArrayJoinedParts(parts, '.', SizeOfArray(parts)-1); + StringArrayAppend(result, sectionPart); // its been alloced so dont copy + varPart = (STRPTR)ArrayBackValue(STRPTR, parts); + StringArrayAppendAndRetain(result, varPart); // its a reference so copy + StringArrayFree(parts); + return result; } VOID ConfigFileDump(CONFIGFILEPTR abstractConfigFile) @@ -357,6 +382,15 @@ VOID SectionAddLine(SECTIONPTR abstractSection, LINEPTR abstractLine) } } +VOID SectionDump(SECTIONPTR abstractSection) +{ + struct Section* section = (struct Section*)abstractSection; + if( section != NULL && section->lines != NULL ) + { + ArrayForEach(LINEPTR, aLine, section->lines, LineDump(aLine);); + } +} + VOID SectionFree(SECTIONPTR abstractSection) { struct Section* section = (struct Section*)abstractSection; diff --git a/configmodel.h b/configmodel.h index 27430bb..06fb5d8 100644 --- a/configmodel.h +++ b/configmodel.h @@ -6,6 +6,7 @@ CONFIGFILEPTR ConfigFileRead(CONST_STRPTR filename); VOID ConfigFileFree(CONFIGFILEPTR configFile); VOID ConfigFileDump(CONFIGFILEPTR configFile); +StringArray ConfigFileSplitKey(CONST_STRPTR key); LINEPTR LineNew(CONST_STRPTR buffer, ULONG size); diff --git a/containers/stringarray.c b/containers/stringarray.c index e2303ee..b19ec18 100644 --- a/containers/stringarray.c +++ b/containers/stringarray.c @@ -4,6 +4,8 @@ #include #include + +#define MIN(x,y) (x'); +// Printf("{%s}", joined); +// FreeVec(joined); + + StringArrayFree(parts); +// CONFIGFILEPTR config = ConfigFileRead(filename); +// if( config != NULL ) +// { +// Printf("XXXXXXX\n"); +// ConfigFileDump(config); +// Printf("XXXXXXX\n"); +// ConfigFileFree(config); +// } +// else +// { +// result = RETURN_ERROR; +// } } return result; }