Seems to hang together for getting values

main
Alan Francis 1 year ago
parent c03d0cb62f
commit e4037b112a
  1. 99
      configmodel.c
  2. 7
      configmodel.h
  3. 4
      containers/linearray.c
  4. 46
      containers/sectionarray.c
  5. 11
      containers/sectionarray.h
  6. 7
      containers/stringarray.c
  7. 2
      containers/stringarray.h
  8. 41
      main.c
  9. 47
      sectionstore.c
  10. 4
      sectionstore.h
  11. 1
      types.h

@ -4,11 +4,16 @@
#include "cregex/cregex.h" #include "cregex/cregex.h"
#include "containers/stringarray.h" #include "containers/stringarray.h"
#include "containers/linearray.h" #include "containers/linearray.h"
#include "containers/sectionarray.h"
#include <proto/exec.h> #include <proto/exec.h>
#include <proto/dos.h> #include <proto/dos.h>
#include <string.h> #include <string.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
STATIC VOID downcaseString(STRPTR string);
#define ZERO ((BPTR)0) #define ZERO ((BPTR)0)
@ -37,6 +42,7 @@ struct Variable
{ {
enum VariableType type; enum VariableType type;
CONST_STRPTR key; CONST_STRPTR key;
CONST_STRPTR normalizedKey;
union union
{ {
CONST_STRPTR stringValue; CONST_STRPTR stringValue;
@ -120,7 +126,26 @@ StringArray ConfigFileSplitKey(CONST_STRPTR key)
StringArrayAppend(result, sectionPart); // its been alloced so dont copy StringArrayAppend(result, sectionPart); // its been alloced so dont copy
varPart = (STRPTR)ArrayBackValue(STRPTR, parts); varPart = (STRPTR)ArrayBackValue(STRPTR, parts);
StringArrayAppendAndRetain(result, varPart); // its a reference so copy StringArrayAppendAndRetain(result, varPart); // its a reference so copy
StringArrayFree(parts); StringArrayFree(parts, TRUE);
return result;
}
StringArray ConfigFileGetAll(CONFIGFILEPTR abstractConfigFile, CONST_STRPTR compoundKey)
{
StringArray result = StringArrayNew();
ULONG index = 0;
struct ConfigFile* configFile = (struct ConfigFile*)abstractConfigFile;
if( configFile != NULL )
{
StringArray split = ConfigFileSplitKey(compoundKey);
VariableArray vars = SectionStoreGetAll(configFile->sectionStore, StringArrayValues(split)[0], StringArrayValues(split)[1]);
for( index = 0; index < SizeOfArray(vars); index++ )
{
StringArrayAppendAndRetain(result, VariableGetRawValue(VariableArrayValues(vars)[index]));
}
VariableArrayFree(vars, FALSE);
StringArrayFree(split, TRUE);
}
return result; return result;
} }
@ -302,6 +327,12 @@ VARIABLEPTR LineGetVariable(LINEPTR abstractLine)
return NULL; return NULL;
} }
BOOL LineHasVariable(LINEPTR line, CONST_STRPTR varKey)
{
VARIABLEPTR var = LineGetVariable(line);
return (BOOL)(var != NULL && VariableHasKey(var, varKey));
}
VOID LineFree(LINEPTR abstractLine) VOID LineFree(LINEPTR abstractLine)
{ {
struct Line* line = (struct Line*)abstractLine; struct Line* line = (struct Line*)abstractLine;
@ -382,6 +413,33 @@ VOID SectionAddLine(SECTIONPTR abstractSection, LINEPTR abstractLine)
} }
} }
VOID SectionCollectLinesForVariable(SECTIONPTR abstractSection, CONST_STRPTR varKey, LineArray collecting)
{
struct Section* section = (struct Section*)abstractSection;
if( section != NULL )
{
ULONG lineCount = SizeOfArray(section->lines);
ULONG index = 0;
STRPTR normalizedKey = AllocVec(strlen(varKey)+1, MEMF_CLEAR);
CopyMem(varKey, normalizedKey, strlen(varKey));
downcaseString(normalizedKey);
for( index = 0; index < lineCount; index++ )
{
LINEPTR line = LineArrayValues(section->lines)[index];
if( LineHasVariable(line, normalizedKey) )
{
LineArrayAppend(collecting, line);
}
}
FreeVec(normalizedKey);
}
else
{
Printf("null section\n");
}
}
VOID SectionDump(SECTIONPTR abstractSection) VOID SectionDump(SECTIONPTR abstractSection)
{ {
struct Section* section = (struct Section*)abstractSection; struct Section* section = (struct Section*)abstractSection;
@ -449,6 +507,9 @@ VARIABLEPTR VariableCreate(CONST_STRPTR key, CONST_STRPTR rawValue)
result = AllocVec(sizeof(struct Variable), MEMF_CLEAR); result = AllocVec(sizeof(struct Variable), MEMF_CLEAR);
result->key = AllocVec(length+1, MEMF_CLEAR); result->key = AllocVec(length+1, MEMF_CLEAR);
CopyMem(key, (STRPTR)result->key, length); CopyMem(key, (STRPTR)result->key, length);
result->normalizedKey = AllocVec(length+1, MEMF_CLEAR);
CopyMem(key, (STRPTR)result->normalizedKey, length);
downcaseString((STRPTR)result->normalizedKey);
} }
result->type = TypeString; result->type = TypeString;
if( rawValue != NULL ) if( rawValue != NULL )
@ -460,6 +521,29 @@ VARIABLEPTR VariableCreate(CONST_STRPTR key, CONST_STRPTR rawValue)
return result; return result;
} }
CONST_STRPTR VariableGetRawValue(VARIABLEPTR abstractVariable)
{
struct Variable* variable = (struct Variable*)abstractVariable;
if( variable != NULL )
{
return variable->value.stringValue;
}
return NULL;
}
BOOL VariableHasKey(VARIABLEPTR abstractVariable, CONST_STRPTR varKey)
{
struct Variable* variable = (struct Variable*)abstractVariable;
if( variable != NULL )
{
if( strcmp(varKey, variable->normalizedKey) == 0 )
{
return TRUE;
}
}
return FALSE;
}
VOID VariableFree(VARIABLEPTR abstractVariable) VOID VariableFree(VARIABLEPTR abstractVariable)
{ {
struct Variable* variable = (struct Variable*)abstractVariable; struct Variable* variable = (struct Variable*)abstractVariable;
@ -469,6 +553,10 @@ VOID VariableFree(VARIABLEPTR abstractVariable)
{ {
FreeVec((STRPTR)variable->key); FreeVec((STRPTR)variable->key);
} }
if( variable->normalizedKey != NULL )
{
FreeVec((STRPTR)variable->normalizedKey);
}
if( variable->type == TypeString && variable->value.stringValue != NULL ) if( variable->type == TypeString && variable->value.stringValue != NULL )
{ {
FreeVec((STRPTR)variable->value.stringValue); FreeVec((STRPTR)variable->value.stringValue);
@ -478,3 +566,12 @@ VOID VariableFree(VARIABLEPTR abstractVariable)
} }
} }
STATIC VOID downcaseString(STRPTR string)
{
BYTE* p = NULL;
for(p=string; *p; p++)
{
*p=tolower(*p);
}
}

@ -7,7 +7,7 @@ CONFIGFILEPTR ConfigFileRead(CONST_STRPTR filename);
VOID ConfigFileFree(CONFIGFILEPTR configFile); VOID ConfigFileFree(CONFIGFILEPTR configFile);
VOID ConfigFileDump(CONFIGFILEPTR configFile); VOID ConfigFileDump(CONFIGFILEPTR configFile);
StringArray ConfigFileSplitKey(CONST_STRPTR key); StringArray ConfigFileSplitKey(CONST_STRPTR key);
StringArray ConfigFileGetAll(CONFIGFILEPTR abstractConfigFile, CONST_STRPTR compoundKey);
LINEPTR LineNew(CONST_STRPTR buffer, ULONG size); LINEPTR LineNew(CONST_STRPTR buffer, ULONG size);
VOID LineFree(LINEPTR abstractLine); VOID LineFree(LINEPTR abstractLine);
@ -19,6 +19,7 @@ SECTIONPTR LineGetSection(LINEPTR line);
VARIABLEPTR LineGetVariable(LINEPTR line); VARIABLEPTR LineGetVariable(LINEPTR line);
VOID LineSetInitialVariable(LINEPTR line, VARIABLEPTR variable); VOID LineSetInitialVariable(LINEPTR line, VARIABLEPTR variable);
VOID LineSetVariable(LINEPTR line, VARIABLEPTR variable); VOID LineSetVariable(LINEPTR line, VARIABLEPTR variable);
BOOL LineHasVariable(LINEPTR line, CONST_STRPTR varKey);
VOID LineDump(LINEPTR abstractLine); VOID LineDump(LINEPTR abstractLine);
@ -30,7 +31,11 @@ VOID SectionAddLine(SECTIONPTR section, LINEPTR line);
CONST_STRPTR SectionCanonicalName(SECTIONPTR section); CONST_STRPTR SectionCanonicalName(SECTIONPTR section);
VOID SectionDump(SECTIONPTR section); VOID SectionDump(SECTIONPTR section);
VOID SectionCollectLinesForVariable(SECTIONPTR section, CONST_STRPTR varKey, LineArray collecting);
VARIABLEPTR VariableCreate(CONST_STRPTR key, CONST_STRPTR rawValue); VARIABLEPTR VariableCreate(CONST_STRPTR key, CONST_STRPTR rawValue);
BOOL VariableHasKey(VARIABLEPTR variable, CONST_STRPTR varKey);
CONST_STRPTR VariableGetRawValue(VARIABLEPTR variable);
VOID VariableFree(VARIABLEPTR variable); VOID VariableFree(VARIABLEPTR variable);
#endif #endif

@ -11,8 +11,8 @@
LineArray LineArrayNew(VOID) LineArray LineArrayNew(VOID)
{ {
#define SIZE_LINEPTR 2 #define SIZE_APTR 2
return NewArray(SIZE_LINEPTR); return NewArray(SIZE_APTR);
} }
VOID LineArrayAppend(LineArray array, LINEPTR value) VOID LineArrayAppend(LineArray array, LINEPTR value)

@ -2,22 +2,25 @@
#include <proto/exec.h> #include <proto/exec.h>
#include <string.h> #include <string.h>
Array SectionArrayNew(VOID) #define SIZE_APTR 2
SectionArray SectionArrayNew(VOID)
{ {
#define SIZE_STRPTR 2 return NewArray(SIZE_APTR);
return NewArray(SIZE_STRPTR);
} }
VOID SectionArrayAppend(SectionArray array, SECTIONPTR value) VOID SectionArrayAppend(SectionArray array, SECTIONPTR value)
{ {
AppendToArray(CONST_STRPTR, array, value); AppendToArray(SECTIONPTR, array, value);
} }
VOID SectionArrayFree(SectionArray array) VOID SectionArrayFree(SectionArray array, BOOL freeSections)
{ {
if( array != NULL ) if( array != NULL )
{ {
SectionArrayForEach(array, SectionFree(aSection);); if( freeSections == TRUE )
{
SectionArrayForEach(array, SectionFree(aSection););
}
DeleteArray(array); DeleteArray(array);
} }
} }
@ -25,4 +28,33 @@ VOID SectionArrayFree(SectionArray array)
SECTIONPTR* SectionArrayValues(SectionArray array) SECTIONPTR* SectionArrayValues(SectionArray array)
{ {
return ArrayValues(SECTIONPTR, array); return ArrayValues(SECTIONPTR, array);
} }
VariableArray VariableArrayNew(VOID)
{
return NewArray(SIZE_APTR);
}
VOID VariableArrayAppend(VariableArray array, VARIABLEPTR value)
{
AppendToArray(VARIABLEPTR, array, value);
}
VOID VariableArrayFree(VariableArray array, BOOL freeVars)
{
if( array != NULL )
{
if( freeVars == TRUE )
{
VariableArrayForEach(array, VariableFree(aVariable););
}
DeleteArray(array);
}
}
VARIABLEPTR* VariableArrayValues(VariableArray array)
{
return ArrayValues(VARIABLEPTR, array);
}

@ -7,10 +7,19 @@
SectionArray SectionArrayNew(VOID); SectionArray SectionArrayNew(VOID);
VOID SectionArrayAppend(SectionArray array, SECTIONPTR value); VOID SectionArrayAppend(SectionArray array, SECTIONPTR value);
VOID SectionArrayFree(SectionArray array); VOID SectionArrayFree(SectionArray array, BOOL freeSections);
SECTIONPTR* SectionArrayValues(SectionArray array); SECTIONPTR* SectionArrayValues(SectionArray array);
#define SectionArrayForEach(array, block) do {SECTIONPTR *afe_123_p = (*(SECTIONPTR **)array); SECTIONPTR aSection = *afe_123_p; int afe_123_c = (((ULONG *)array)[1]);\ #define SectionArrayForEach(array, block) do {SECTIONPTR *afe_123_p = (*(SECTIONPTR **)array); SECTIONPTR aSection = *afe_123_p; int afe_123_c = (((ULONG *)array)[1]);\
for (; afe_123_c--; aSection = *(++afe_123_p)) block} while (0); for (; afe_123_c--; aSection = *(++afe_123_p)) block} while (0);
VariableArray VariableArrayNew(VOID);
VOID VariableArrayAppend(VariableArray array, VARIABLEPTR value);
VOID VariableArrayFree(VariableArray array, BOOL freeVars);
VARIABLEPTR* VariableArrayValues(VariableArray array);
#define VariableArrayForEach(array, block) do {VARIABLEPTR *afe_123_p = (*(VARIABLEPTR **)array); SECTIONPTR aVariable = *afe_123_p; int afe_123_c = (((ULONG *)array)[1]);\
for (; afe_123_c--; aVariable = *(++afe_123_p)) block} while (0);
#endif #endif

@ -24,11 +24,14 @@ VOID StringArrayAppendAndRetain(StringArray array, CONST_STRPTR value)
StringArrayAppend(array, localCopy); StringArrayAppend(array, localCopy);
} }
VOID StringArrayFree(StringArray array) VOID StringArrayFree(StringArray array, BOOL freeStrings)
{ {
if( array != NULL ) if( array != NULL )
{ {
StringArrayForEach(array, FreeVec(aString);); if( freeStrings )
{
StringArrayForEach(array, FreeVec(aString););
}
DeleteArray(array); DeleteArray(array);
} }
} }

@ -6,7 +6,7 @@
StringArray StringArrayNew(VOID); StringArray StringArrayNew(VOID);
VOID StringArrayAppend(StringArray array, CONST_STRPTR value); VOID StringArrayAppend(StringArray array, CONST_STRPTR value);
VOID StringArrayAppendAndRetain(StringArray array, CONST_STRPTR value); VOID StringArrayAppendAndRetain(StringArray array, CONST_STRPTR value);
VOID StringArrayFree(StringArray array); VOID StringArrayFree(StringArray array, BOOL freeStrings);
CONST_STRPTR* StringArrayValues(StringArray array); CONST_STRPTR* StringArrayValues(StringArray array);
STRPTR StringArrayJoined(StringArray array, BYTE linkCharacter); STRPTR StringArrayJoined(StringArray array, BYTE linkCharacter);

@ -22,26 +22,27 @@ WORD DoTheWork(STRPTR filename)
STRPTR joined = NULL; STRPTR joined = NULL;
if (ContainerkitBase) if (ContainerkitBase)
{ {
StringArray parts = ConfigFileSplitKey("foo.bar.baz.blah"); CONFIGFILEPTR config = ConfigFileRead(filename);
StringArrayForEach(parts, Printf("[%s]\n",aString);); if( config != NULL )
{
// joined = StringArrayJoined(parts, '>'); StringArray values = NULL;
// Printf("{%s}", joined);
// FreeVec(joined); values = ConfigFileGetAll(config, "branch.main.remote");
StringArrayForEach(values, Printf("main %s\n", aString););
StringArrayFree(parts); StringArrayFree(values, TRUE);
// CONFIGFILEPTR config = ConfigFileRead(filename);
// if( config != NULL ) values = ConfigFileGetAll(config, "branch.config-file-parsing-from-book.remote");
// { StringArrayForEach(values, Printf("parse %s\n", aString););
// Printf("XXXXXXX\n"); StringArrayFree(values, TRUE);
// ConfigFileDump(config);
// Printf("XXXXXXX\n");
// ConfigFileFree(config);
// } ConfigFileFree(config);
// else }
// { else
// result = RETURN_ERROR; {
// } result = RETURN_ERROR;
}
} }
return result; return result;
} }

@ -2,6 +2,7 @@
#include "containers/sectionmap.h" #include "containers/sectionmap.h"
#include "containers/sectionarray.h" #include "containers/sectionarray.h"
#include "containers/stringarray.h" #include "containers/stringarray.h"
#include "containers/linearray.h"
#include "cregex/pattern.h" #include "cregex/pattern.h"
#include <proto/exec.h> #include <proto/exec.h>
@ -50,7 +51,7 @@ VOID SectionStoreFree(SECTIONSTOREPTR abstractSectionStore)
{ {
struct SectionStore* store = (struct SectionStore*)abstractSectionStore; struct SectionStore* store = (struct SectionStore*)abstractSectionStore;
if( store->map != NULL ) SectionMapFree( store->map ); if( store->map != NULL ) SectionMapFree( store->map );
if( store->array != NULL ) SectionArrayFree( store->array ); if( store->array != NULL ) SectionArrayFree( store->array, TRUE );//free teh sections
if( store->sectionPatternProgram != NULL ) PatternFree(store->sectionPatternProgram); if( store->sectionPatternProgram != NULL ) PatternFree(store->sectionPatternProgram);
if( store->variablePatternProgram != NULL ) PatternFree(store->variablePatternProgram); if( store->variablePatternProgram != NULL ) PatternFree(store->variablePatternProgram);
if( store->blankPatternProgram != NULL ) PatternFree(store->blankPatternProgram); if( store->blankPatternProgram != NULL ) PatternFree(store->blankPatternProgram);
@ -97,6 +98,42 @@ SECTIONPTR SectionStoreCurrentSection(SECTIONSTOREPTR abstractSectionStore)
return result; return result;
} }
LineArray SectionStoreFindLines(SECTIONSTOREPTR sectionStore, CONST_STRPTR canonicalSectionName, CONST_STRPTR varKey)
{
LineArray result = LineArrayNew();
SECTIONPTR section = SectionStoreGetSection(sectionStore, canonicalSectionName);
if( section != NULL )
{
SectionCollectLinesForVariable(section, varKey, result);
}
return result;
}
VariableArray SectionStoreGetAll(SECTIONSTOREPTR sectionStore, CONST_STRPTR canonicalSectionName, CONST_STRPTR varKey)
{
VariableArray result = VariableArrayNew();
LineArray lines = SectionStoreFindLines(sectionStore, canonicalSectionName, varKey);
ULONG index = 0;
for( index = 0; index < SizeOfArray(lines); index++)
{
LINEPTR line = LineArrayValues(lines)[index];
VariableArrayAppend(result, LineGetVariable(line));
}
LineArrayFree(lines, FALSE);
return result;
}
VARIABLEPTR SectionStoreGet(SECTIONSTOREPTR sectionStore, CONST_STRPTR canonicalSectionName, CONST_STRPTR varKey)
{
VARIABLEPTR result = NULL;
LineArray lines = SectionStoreFindLines(sectionStore, canonicalSectionName, varKey);
result = LineGetVariable((LINEPTR)ArrayBackValue(LINEPTR, lines));
LineArrayFree(lines, FALSE);
return result;
}
VOID SectionStoreAddLine(SECTIONSTOREPTR abstractSectionStore, LINEPTR line) VOID SectionStoreAddLine(SECTIONSTOREPTR abstractSectionStore, LINEPTR line)
{ {
struct SectionStore* store = (struct SectionStore*)abstractSectionStore; struct SectionStore* store = (struct SectionStore*)abstractSectionStore;
@ -180,7 +217,7 @@ STATIC VARIABLEPTR parseVariable(LINEPTR line, PATTERNPTR variablePatternProgram
if( matches = PatternRun(LineGetRawText(line), variablePatternProgram) ) if( matches = PatternRun(LineGetRawText(line), variablePatternProgram) )
{ {
result = VariableCreate(StringArrayValues(matches)[1], StringArrayValues(matches)[2]); result = VariableCreate(StringArrayValues(matches)[1], StringArrayValues(matches)[2]);
StringArrayFree(matches); StringArrayFree(matches, TRUE);
} }
return result; return result;
} }
@ -199,7 +236,7 @@ STATIC SECTIONPTR parseSection(LINEPTR line, PATTERNPTR sectionPatternProgram)
{ {
result = SectionCreateWithNameAndSubname(StringArrayValues(matches)[1], StringArrayValues(matches)[3]); result = SectionCreateWithNameAndSubname(StringArrayValues(matches)[1], StringArrayValues(matches)[3]);
} }
StringArrayFree(matches); StringArrayFree(matches, TRUE);
} }
return result; return result;
} }
@ -226,7 +263,7 @@ STATIC BOOL parseIntegerValue(CONST_STRPTR value, LONG* outInt, PATTERNPTR integ
if( matches = PatternRun(value, integerProgram) ) if( matches = PatternRun(value, integerProgram) )
{ {
StringArrayFree(matches); StringArrayFree(matches, TRUE);
*outInt = atol(value); *outInt = atol(value);
result = TRUE; result = TRUE;
} }
@ -241,7 +278,7 @@ STATIC BOOL parseBlank(LINEPTR line, PATTERNPTR blankPatternProgram)
if( matches = PatternRun(LineGetRawText(line), blankPatternProgram) ) if( matches = PatternRun(LineGetRawText(line), blankPatternProgram) )
{ {
result = TRUE; result = TRUE;
StringArrayFree(matches); StringArrayFree(matches, TRUE);
} }
return result; return result;
} }

@ -12,6 +12,10 @@ SECTIONPTR SectionStoreGetSection(SECTIONSTOREPTR sectionStore, CONST_STRPTR can
VOID SectionStoreAddLine(SECTIONSTOREPTR sectionStore, LINEPTR line); VOID SectionStoreAddLine(SECTIONSTOREPTR sectionStore, LINEPTR line);
LineArray SectionStoreFindLines(SECTIONSTOREPTR sectionStore, CONST_STRPTR canonicalSectionName, CONST_STRPTR varKey);
VariableArray SectionStoreGetAll(SECTIONSTOREPTR sectionStore, CONST_STRPTR canonicalSectionName, CONST_STRPTR varKey);
VARIABLEPTR SectionStoreGet(SECTIONSTOREPTR sectionStore, CONST_STRPTR canonicalSectionName, CONST_STRPTR varKey);
ULONG SectionStoreSectionCount(SECTIONSTOREPTR sectionStore); ULONG SectionStoreSectionCount(SECTIONSTOREPTR sectionStore);
SECTIONPTR SectionStoreSectionAt(SECTIONSTOREPTR sectionStore, ULONG index); SECTIONPTR SectionStoreSectionAt(SECTIONSTOREPTR sectionStore, ULONG index);

@ -15,6 +15,7 @@ typedef APTR PATTERNPTR;
typedef APTR StringArray; typedef APTR StringArray;
typedef APTR LineArray; typedef APTR LineArray;
typedef APTR SectionArray; typedef APTR SectionArray;
typedef APTR VariableArray;
typedef APTR SectionMap; // a map of string (canonical name) to Section typedef APTR SectionMap; // a map of string (canonical name) to Section

Loading…
Cancel
Save