From b06089a99a5873a4e1c61fc7eaf2271678aaa9dd Mon Sep 17 00:00:00 2001 From: Alan Francis Date: Sat, 18 Nov 2023 23:06:24 +0000 Subject: [PATCH] reading some structure --- configmodel.c | 172 +++++++++++++++++++++++++++++++++++++++++++------ configmodel.h | 2 +- testconfig.cfg | 3 - 3 files changed, 152 insertions(+), 25 deletions(-) diff --git a/configmodel.c b/configmodel.c index a5f84eb..9bd570f 100644 --- a/configmodel.c +++ b/configmodel.c @@ -14,6 +14,11 @@ cregex_program_t* InitialisePattern(CONST_STRPTR pattern); Array RunPattern(CONST_STRPTR text, cregex_program_t* patternProgram); + +SECTIONPTR LineGetSection(LINEPTR abstractLine); +VARIABLEPTR LineGetVariable(LINEPTR abstractLine); +VOID LineDump(LINEPTR abstractLine); + SECTIONPTR SectionCreateWithName(CONST_STRPTR primary); SECTIONPTR SectionCreateWithNameAndSubname(CONST_STRPTR primary, CONST_STRPTR secondary); VOID SectionFree(SECTIONPTR abstractSection); @@ -42,8 +47,8 @@ struct ConfigFile struct Section { - CONST_STRPTR primary_key; - CONST_STRPTR secondary_key; + CONST_STRPTR primary; + CONST_STRPTR secondary; LineArray lines; }; @@ -57,7 +62,7 @@ enum VariableType struct Variable { enum VariableType type; - CONST_STRPTR name; + CONST_STRPTR key; union { CONST_STRPTR stringValue; @@ -94,12 +99,19 @@ CONFIGFILEPTR ConfigFileRead(CONST_STRPTR filename) if( configFile != ZERO ) { LINEPTR line = NULL; + SECTIONPTR currentSection = NULL; result->lines = LineArrayNew(); InitialisePatterns(); - while( (line = LineReadIncludingContinuation(configFile)) != NULL ) + while( (line = LineReadIncludingContinuation(configFile, currentSection)) != NULL ) { -// Printf("successfully read line {%s}\n", LineGetRawText(line)); + // add it to the flat list of lines LineArrayAppend(result->lines, line); + LineDump(line); + + if( LineGetSection(line) != NULL ) + { + currentSection = LineGetSection(line); + } } ReleasePatterns(); Close(configFile); @@ -120,23 +132,19 @@ LINEPTR LineCreate(CONST_STRPTR buffer, ULONG size) if( size > 0 ) { result = AllocVec(sizeof(struct Line), MEMF_CLEAR); - result->rawText = AllocVec(size+1, MEMF_CLEAR); - CopyMem(buffer, result->rawText, size); + result->rawText = AllocVec(size, MEMF_CLEAR); + CopyMem(buffer, result->rawText, size-1); // drop the \\n if( matches = RunPattern(result->rawText, sectionPatternProgram) ) { if( SizeOfArray(matches) == 3 ) { - Printf("\nsection {%s}\n", StringArrayValues(matches)[1]); result->section = SectionCreateWithName(StringArrayValues(matches)[1]); } else if( SizeOfArray(matches) == 5 ) { - Printf("\nsection {%s.%s}\n", StringArrayValues(matches)[1], StringArrayValues(matches)[3]); result->section = SectionCreateWithNameAndSubname(StringArrayValues(matches)[1], StringArrayValues(matches)[3]); } -// Printf("\nsection size=%ld\n", SizeOfArray(stringArray)); -// StringArrayForEach(stringArray, Printf("{{%s}}",aString);); StringArrayFree(matches); } else @@ -144,8 +152,6 @@ LINEPTR LineCreate(CONST_STRPTR buffer, ULONG size) if( matches = RunPattern(result->rawText, variablePatternProgram) ) { - Printf("\nvariable {%s} = {%s}\n", StringArrayValues(matches)[1], StringArrayValues(matches)[2]); -// StringArrayForEach(stringArray, Printf("{{%s}}",aString);); result->variable = VariableCreate(StringArrayValues(matches)[1], StringArrayValues(matches)[2]); StringArrayFree(matches); } @@ -153,22 +159,54 @@ LINEPTR LineCreate(CONST_STRPTR buffer, ULONG size) { if( matches = RunPattern(result->rawText, blankPatternProgram) ) { - Printf("\nYY %s\n", result->rawText); StringArrayFree(matches); } else { - Printf("\nXX %s\n", result->rawText); + Printf("\nDIDNT MATCH ANYTHING %s\n", result->rawText); } } } } return result; - } -LINEPTR LineReadIncludingContinuation(BPTR file) +VOID LineDump(LINEPTR abstractLine) +{ + struct Line* line = (struct Line*)abstractLine; + if( line != NULL ) + { + Printf("> %s\n", line->rawText); + if( line->variable == NULL && line->section == NULL ) + { + Printf("(blank)\n"); + } + else if( line->variable == NULL && line->section != NULL ) + { + struct Section* section = (struct Section*)LineGetSection(abstractLine); + Printf("(new section %s", section->primary); + if( section->secondary ) + { + Printf("[%s]", section->secondary); + } + Printf(")\n"); + } + else + { + struct Section* section = (struct Section*)LineGetSection(abstractLine); + struct Variable* var = (struct Variable*)LineGetVariable(abstractLine); + Printf("(variable in section %s", section->primary); + if( section->secondary ) + { + Printf("[%s]\n", section->secondary); + } + Printf(") %s = %s\n", var->key, var->value.stringValue); + } + } +} + +LINEPTR LineReadIncludingContinuation(BPTR file, SECTIONPTR currentSection) { UBYTE* buffer = AllocVec(512, MEMF_CLEAR); ULONG bufLength = 512; @@ -188,12 +226,36 @@ LINEPTR LineReadIncludingContinuation(BPTR file) if( bytesReadTotal > 0 ) { result = LineCreate(buffer, bytesReadTotal); + if( result->variable != NULL && result->section == NULL ) + { + result->section = currentSection; + } } FreeVec(buffer); return result; } +SECTIONPTR LineGetSection(LINEPTR abstractLine) +{ + struct Line* line = (struct Line*)abstractLine; + if( line != NULL ) + { + return line->section; + } + return NULL; +} + +VARIABLEPTR LineGetVariable(LINEPTR abstractLine) +{ + struct Line* line = (struct Line*)abstractLine; + if( line != NULL ) + { + return line->variable; + } + return NULL; +} + VOID LineFree(LINEPTR abstractLine) { struct Line* line = (struct Line*)abstractLine; @@ -203,8 +265,10 @@ VOID LineFree(LINEPTR abstractLine) { FreeVec(line->rawText); } - if( line->section != NULL ) + if( line->section != NULL && line->variable == NULL ) { + //only free the section in a section line, + //not a variable line as thats just a weak ref SectionFree(line->section); } if( line->variable != NULL ) @@ -231,25 +295,91 @@ CONST_STRPTR LineGetRawText(LINEPTR abstractLine) SECTIONPTR SectionCreateWithName(CONST_STRPTR primary) { - return NULL; + return SectionCreateWithNameAndSubname(primary, NULL); } SECTIONPTR SectionCreateWithNameAndSubname(CONST_STRPTR primary, CONST_STRPTR secondary) { - return NULL; + struct Section* result = NULL; + + if( primary != NULL ) + { + ULONG length = strlen(primary); + result = AllocVec(sizeof(struct Section), MEMF_CLEAR); + result->primary = AllocVec(length+1, MEMF_CLEAR); + CopyMem(primary, (STRPTR)result->primary, length); + + if( secondary != NULL ) + { + ULONG length = strlen(secondary); + result->secondary = AllocVec(length+1, MEMF_CLEAR); + CopyMem(secondary, (STRPTR)result->secondary, length); + } + + result->lines = LineArrayNew(); + } + + return result; } VOID SectionFree(SECTIONPTR abstractSection) { + struct Section* section = (struct Section*)abstractSection; + if( section != NULL ) + { + if( section->primary != NULL ) + { + FreeVec((STRPTR)section->primary); + } + if( section->secondary != NULL ) + { + FreeVec((STRPTR)section->secondary); + } + if( section->lines != NULL ) + { + LineArrayFree(section->lines, FALSE); // dont free the lines they are only weak + } + FreeVec(section); + } } VARIABLEPTR VariableCreate(CONST_STRPTR key, CONST_STRPTR rawValue) { - return NULL; + struct Variable* result = NULL; + if( key != NULL ) + { + ULONG length = strlen(key); + result = AllocVec(sizeof(struct Variable), MEMF_CLEAR); + result->key = AllocVec(length+1, MEMF_CLEAR); + CopyMem(key, (STRPTR)result->key, length); + } + result->type = TypeString; + if( rawValue != NULL ) + { + ULONG length = strlen(rawValue); + result->value.stringValue = AllocVec(length+1, MEMF_CLEAR); + CopyMem(rawValue, (STRPTR)result->value.stringValue, length); + } + return result; } VOID VariableFree(VARIABLEPTR abstractVariable) { + struct Variable* variable = (struct Variable*)abstractVariable; + if( variable != NULL ) + { + if( variable->key != NULL ) + { + FreeVec((STRPTR)variable->key); + } + if( variable->type == TypeString && variable->value.stringValue != NULL ) + { + FreeVec((STRPTR)variable->value.stringValue); + } + FreeVec(variable); + + } + } diff --git a/configmodel.h b/configmodel.h index feea203..0a7f311 100644 --- a/configmodel.h +++ b/configmodel.h @@ -16,7 +16,7 @@ VOID ConfigFileSave(CONFIGFILEPTR config); VOID InitialisePatterns(VOID); VOID ReleasePatterns(VOID); -LINEPTR LineReadIncludingContinuation(BPTR file); +LINEPTR LineReadIncludingContinuation(BPTR file, SECTIONPTR currentSection); VOID LineFree(LINEPTR abstractLine); CONST_STRPTR LineGetRawText(LINEPTR line); #endif \ No newline at end of file diff --git a/testconfig.cfg b/testconfig.cfg index 8cdc8fe..2989ba2 100644 --- a/testconfig.cfg +++ b/testconfig.cfg @@ -1,9 +1,6 @@ #this is a comment [core] repositoryformatversion = 0 - filemode = true - bare = false - logallrefupdates = true ignorecase = true precomposeunicode = true