From 69fa45e1baaec25d2dd3f0d8bf0842198f98b568 Mon Sep 17 00:00:00 2001 From: Alan Francis Date: Sat, 18 Nov 2023 16:13:11 +0000 Subject: [PATCH] restructure --- linearray.c => arraytypes/linearray.c | 7 ++- linearray.h => arraytypes/linearray.h | 2 +- stringarray.c => arraytypes/stringarray.c | 0 stringarray.h => arraytypes/stringarray.h | 0 configmodel.c | 53 +++++++++++++++++++-- configmodel.h | 6 +++ cregex.h => cregex/cregex.h | 0 cregex_compile.c => cregex/cregex_compile.c | 0 cregex_parse.c => cregex/cregex_parse.c | 0 cregex_vm.c => cregex/cregex_vm.c | 0 main.c | 19 +++----- smakefile | 31 +++++++----- 12 files changed, 86 insertions(+), 32 deletions(-) rename linearray.c => arraytypes/linearray.c (81%) rename linearray.h => arraytypes/linearray.h (85%) rename stringarray.c => arraytypes/stringarray.c (100%) rename stringarray.h => arraytypes/stringarray.h (100%) rename cregex.h => cregex/cregex.h (100%) rename cregex_compile.c => cregex/cregex_compile.c (100%) rename cregex_parse.c => cregex/cregex_parse.c (100%) rename cregex_vm.c => cregex/cregex_vm.c (100%) diff --git a/linearray.c b/arraytypes/linearray.c similarity index 81% rename from linearray.c rename to arraytypes/linearray.c index 1c28467..ea8d1aa 100644 --- a/linearray.c +++ b/arraytypes/linearray.c @@ -20,9 +20,12 @@ VOID LineArrayAppend(LineArray array, LINEPTR value) AppendToArray(LINEPTR, array, value); } -VOID LineArrayFree(LineArray array) +VOID LineArrayFree(LineArray array, BOOL freeLines) { - ArrayForEach(LINEPTR, aLine, array, LineFree(aLine);); + if( freeLines ) + { + ArrayForEach(LINEPTR, aLine, array, LineFree(aLine);); + } DeleteArray(array); } diff --git a/linearray.h b/arraytypes/linearray.h similarity index 85% rename from linearray.h rename to arraytypes/linearray.h index 7a6b4c3..5e5d59b 100644 --- a/linearray.h +++ b/arraytypes/linearray.h @@ -10,7 +10,7 @@ LineArray LineArrayNew(VOID); VOID LineArrayAppend(LineArray array, LINEPTR value); -VOID LineArrayFree(LineArray array); +VOID LineArrayFree(LineArray array, BOOL freeLines); LINEPTR* LineArrayValues(LineArray array); #endif diff --git a/stringarray.c b/arraytypes/stringarray.c similarity index 100% rename from stringarray.c rename to arraytypes/stringarray.c diff --git a/stringarray.h b/arraytypes/stringarray.h similarity index 100% rename from stringarray.h rename to arraytypes/stringarray.h diff --git a/configmodel.c b/configmodel.c index 476100a..c351baa 100644 --- a/configmodel.c +++ b/configmodel.c @@ -1,13 +1,16 @@ #include "configmodel.h" -#include "cregex.h" -#include "stringarray.h" +#include "cregex/cregex.h" +#include "arraytypes/stringarray.h" +#include "arraytypes/linearray.h" #include #include #include #include +#define ZERO ((BPTR)0) + cregex_program_t* InitialisePattern(CONST_STRPTR pattern); Array RunPattern(CONST_STRPTR text, cregex_program_t* patternProgram); @@ -23,9 +26,16 @@ STATIC cregex_program_t* variablePatternProgram = NULL; STATIC cregex_program_t* blankPatternProgram = NULL; STATIC cregex_program_t* integerPatternProgram = NULL; +struct ConfigFile +{ + CONST_STRPTR filename; + LineArray lines; +}; + struct Section { - StringArray names; + StringArray nameComponents; + LineArray lines; }; enum VariableType @@ -58,6 +68,43 @@ struct Line object; }; + +VOID ConfigFileFree(CONFIGFILEPTR abstractConfigFile) +{ + struct ConfigFile* configFile = (struct ConfigFile*)abstractConfigFile; + if( configFile != NULL ) + { + if( configFile->lines != NULL ) + { + LineArrayFree(configFile->lines, TRUE); //also frees lines + } + FreeVec(configFile); + } +} + +CONFIGFILEPTR ConfigFileRead(CONST_STRPTR filename) +{ + struct ConfigFile* result = AllocVec(sizeof(struct ConfigFile), MEMF_CLEAR); + BPTR configFile = Open(filename, MODE_OLDFILE); + if( configFile != ZERO ) + { + LINEPTR line = NULL; + result->lines = LineArrayNew(); + InitialisePatterns(); + while( (line = LineReadIncludingContinuation(configFile)) != NULL ) + { +// Printf("successfully read line {%s}\n", LineGetRawText(line)); + LineArrayAppend(result->lines, line); + } + ReleasePatterns(); + Close(configFile); + } + + return result; +} + +VOID ConfigFileSave(CONFIGFILEPTR config); + VOID InitialisePatterns(VOID) { sectionPatternProgram = InitialisePattern(RX_SECTION_LINE); diff --git a/configmodel.h b/configmodel.h index 6e27560..feea203 100644 --- a/configmodel.h +++ b/configmodel.h @@ -6,6 +6,12 @@ typedef APTR LINEPTR; typedef APTR SECTIONPTR; typedef APTR VARIABLEPTR; +typedef APTR CONFIGFILEPTR; + +CONFIGFILEPTR ConfigFileRead(CONST_STRPTR filename); +VOID ConfigFileFree(CONFIGFILEPTR abstractConfigFile); + +VOID ConfigFileSave(CONFIGFILEPTR config); VOID InitialisePatterns(VOID); VOID ReleasePatterns(VOID); diff --git a/cregex.h b/cregex/cregex.h similarity index 100% rename from cregex.h rename to cregex/cregex.h diff --git a/cregex_compile.c b/cregex/cregex_compile.c similarity index 100% rename from cregex_compile.c rename to cregex/cregex_compile.c diff --git a/cregex_parse.c b/cregex/cregex_parse.c similarity index 100% rename from cregex_parse.c rename to cregex/cregex_parse.c diff --git a/cregex_vm.c b/cregex/cregex_vm.c similarity index 100% rename from cregex_vm.c rename to cregex/cregex_vm.c diff --git a/main.c b/main.c index d763d50..317a065 100644 --- a/main.c +++ b/main.c @@ -3,15 +3,11 @@ #include #include #define __NOLIBBASE__ -#include "stringarray.h" -#include "linearray.h" +#include "arraytypes/stringarray.h" +#include "arraytypes/linearray.h" #include "configmodel.h" #include -#include "cregex.h" - -#define ZERO ((BPTR)0) - WORD DoTheWork(STRPTR filename); VOID ProcessFile(BPTR configFile); @@ -34,7 +30,7 @@ VOID ProcessFile(BPTR configFile) LineArrayAppend(lineArray, line); line = LineReadIncludingContinuation(configFile); } - LineArrayFree(lineArray); + LineArrayFree(lineArray, TRUE); ReleasePatterns(); } @@ -43,16 +39,13 @@ WORD DoTheWork(STRPTR filename) WORD result = RETURN_OK; if (ContainerkitBase) { - BPTR configFile = ZERO; - configFile = Open(filename, MODE_OLDFILE); - if( configFile != ZERO ) + CONFIGFILEPTR config = ConfigFileRead(filename); + if( config != NULL ) { - ProcessFile(configFile); - Close(configFile); + ConfigFileFree(config); } else { - Printf("file open failed!\n"); result = RETURN_ERROR; } } diff --git a/smakefile b/smakefile index 89aadc2..24fe566 100644 --- a/smakefile +++ b/smakefile @@ -11,24 +11,29 @@ LIBS = lib:sc.lib lib:amiga.lib lib:debug.lib ############################################################################### -$(NAME) : main.o configmodel.o cregex.lib arraytypes.lib - slink lib:c.o main.o configmodel.o to $(NAME) noicons lib $(LIBS) cregex.lib arraytypes.lib $(LFLAGS) +$(NAME) : main.o configmodel.o cregex/cregex.lib arraytypes/arraytypes.lib + slink lib:c.o main.o configmodel.o to $(NAME) noicons lib $(LIBS) cregex/cregex.lib arraytypes/arraytypes.lib $(LFLAGS) -cregex.lib : cregex_compile.o cregex_parse.o cregex_vm.o - JOIN cregex_compile.o cregex_parse.o cregex_vm.o AS cregex.lib +cregex/cregex.lib : cregex/cregex_compile.o cregex/cregex_parse.o cregex/cregex_vm.o + JOIN cregex/cregex_compile.o cregex/cregex_parse.o cregex/cregex_vm.o AS cregex/cregex.lib -arraytypes.lib : stringarray.o linearray.o - JOIN stringarray.o linearray.o AS arraytypes.lib +arraytypes/arraytypes.lib : arraytypes/stringarray.o arraytypes/linearray.o + JOIN arraytypes/stringarray.o arraytypes/linearray.o AS arraytypes/arraytypes.lib clean: - delete \#?.o \#?.lib $(NAME) ALL + delete \#?.o $(NAME) ALL QUIET + +cleanlibs: + delete arraytypes/arraytypes.lib arraytypes/\#?.o cregex/cregex.lib cregex/\#?.o ALL QUIET ############################################################################### -main.o : main.c stringarray.h -stringarray.o : stringarray.c stringarray.h -linearray.o : linearray.c linearray.h +main.o : main.c configmodel.o : configmodel.c configmodel.h -cregex_compile.o : cregex_compile.c cregex.h -cregex_parse.o : cregex_parse.c cregex.h -cregex_vm.o : cregex_vm.c cregex.h + +cregex/cregex_compile.o : cregex/cregex_compile.c cregex/cregex.h +cregex/cregex_parse.o : cregex/cregex_parse.c cregex/cregex.h +cregex/cregex_vm.o : cregex/cregex_vm.c cregex/cregex.h + +arraytypes/stringarray.o : arraytypes/stringarray.c arraytypes/stringarray.h +arraytypes/linearray.o : arraytypes/linearray.c arraytypes/linearray.h