split any passed key into section and variable parts

main
Alan Francis 1 year ago
parent 0c35672ca0
commit c03d0cb62f
  1. 44
      configmodel.c
  2. 1
      configmodel.h
  3. 43
      containers/stringarray.c
  4. 3
      containers/stringarray.h
  5. 33
      main.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;

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

@ -4,6 +4,8 @@
#include <proto/exec.h>
#include <string.h>
#define MIN(x,y) (x<y?x:y)
StringArray StringArrayNew(VOID)
{
#define SIZE_STRPTR 2
@ -31,6 +33,47 @@ VOID StringArrayFree(StringArray array)
}
}
STRPTR StringArrayJoined(StringArray array, BYTE linkCharacter)
{
return StringArrayJoinedParts(array, linkCharacter, SizeOfArray(array));
}
STRPTR StringArrayJoinedParts(StringArray array, BYTE linkCharacter, ULONG maxParts)
{
ULONG requiredSize = 0;
ULONG index = 0;
ULONG cursor = 0;
STRPTR result = NULL;
ULONG howManyParts = MIN(maxParts, SizeOfArray(array));
if( howManyParts <= 0 )
{
return (STRPTR)AllocVec(1, MEMF_CLEAR); //return an empty string
}
for( index = 0; index < howManyParts; index++ )
{
requiredSize += strlen(StringArrayValues(array)[index]);
}
requiredSize += (howManyParts - 1); // add the joins
requiredSize += 1; // trailing NULL
result = AllocVec(requiredSize, MEMF_CLEAR);
for( index = 0; index < howManyParts; index++ )
{
CONST_STRPTR entry = StringArrayValues(array)[index];
CopyMem((STRPTR)entry, result+cursor, strlen(entry));
cursor += strlen(entry);
result[cursor] = linkCharacter;
cursor += 1;
}
result[requiredSize-1] = '\0';
return result;
}
CONST_STRPTR* StringArrayValues(StringArray array)
{
return ArrayValues(CONST_STRPTR, array);

@ -9,6 +9,9 @@ VOID StringArrayAppendAndRetain(StringArray array, CONST_STRPTR value);
VOID StringArrayFree(StringArray array);
CONST_STRPTR* StringArrayValues(StringArray array);
STRPTR StringArrayJoined(StringArray array, BYTE linkCharacter);
STRPTR StringArrayJoinedParts(StringArray array, BYTE linkCharacter, ULONG maxParts);
#define StringArrayForEach(array, block) do {STRPTR *afe_123_p = (*(STRPTR **)array); STRPTR aString = *afe_123_p; int afe_123_c = (((ULONG *)array)[1]);\
for (; afe_123_c--; aString = *(++afe_123_p)) block} while (0);

@ -19,20 +19,29 @@ struct Library *ContainerkitBase;
WORD DoTheWork(STRPTR filename)
{
WORD result = RETURN_OK;
STRPTR joined = NULL;
if (ContainerkitBase)
{
CONFIGFILEPTR config = ConfigFileRead(filename);
if( config != NULL )
{
Printf("XXXXXXX\n");
ConfigFileDump(config);
Printf("XXXXXXX\n");
ConfigFileFree(config);
}
else
{
result = RETURN_ERROR;
}
StringArray parts = ConfigFileSplitKey("foo.bar.baz.blah");
StringArrayForEach(parts, Printf("[%s]\n",aString););
// joined = StringArrayJoined(parts, '>');
// 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;
}

Loading…
Cancel
Save