From a4ed111518522384e6e81569a66ea4f64fefce7d Mon Sep 17 00:00:00 2001 From: Alan C Francis Date: Sun, 31 Dec 2023 23:14:06 +0000 Subject: [PATCH] save a file --- configfile.c | 35 ++++++++++++++++++++++++++++++++++- configfile.h | 2 ++ configmodel.c | 24 ++++++++++++++++++++---- configmodel.h | 2 ++ types.h | 1 + 5 files changed, 59 insertions(+), 5 deletions(-) diff --git a/configfile.c b/configfile.c index 6f7e19e..645eab1 100644 --- a/configfile.c +++ b/configfile.c @@ -21,7 +21,7 @@ STATIC LINEPTR configFileReadLine(BPTR file); // - STRUCTS ----------------------------------------------------------------------------- struct ConfigFile { - CONST_STRPTR filename; + STRPTR filename; SECTIONSTOREPTR sectionStore; }; @@ -38,6 +38,10 @@ VOID ConfigFileFree(CONFIGFILEPTR abstractConfigFile) { SectionStoreFree(configFile->sectionStore); } + if( configFile->filename != NULL ) + { + FreeVec(configFile->filename); + } FreeVec(configFile); } } @@ -52,6 +56,8 @@ CONFIGFILEPTR ConfigFileRead(CONST_STRPTR filename) LINEPTR line = NULL; result = AllocVec(sizeof(struct ConfigFile), MEMF_CLEAR); result->sectionStore = SectionStoreNew(); + result->filename = AllocVec(strlen(filename)+1, MEMF_CLEAR); + CopyMem(filename, result->filename, strlen(filename)); while( (line = configFileReadLine(configFile)) != NULL ) { SectionStoreAddLineToCurrentSection(result->sectionStore, line); @@ -63,6 +69,33 @@ CONFIGFILEPTR ConfigFileRead(CONST_STRPTR filename) return result; } +VOID ConfigFileSave(CONFIGFILEPTR abstractConfigFile) +{ + struct ConfigFile* configFile = (struct ConfigFile*)abstractConfigFile; + BPTR file = Open(configFile->filename, MODE_NEWFILE); + if( file != 0 ) + { + ConfigFileWrite(abstractConfigFile, file); + Close(file); + } +} + +VOID ConfigFileWrite(CONFIGFILEPTR abstractConfigFile, BPTR file) +{ + ULONG index = 0; + ULONG count = 0; + struct ConfigFile* configFile = (struct ConfigFile*)abstractConfigFile; + if( configFile != NULL ) + { + count = SectionStoreSectionCount(configFile->sectionStore); + for( index = 0; index < count; index++ ) + { + SECTIONPTR section = SectionStoreSectionAt(configFile->sectionStore, index); + SectionWrite(section, file); + } + } +} + StringArray ConfigFileSubsectionsForSection(CONFIGFILEPTR abstractConfigFile, CONST_STRPTR primarySection) { diff --git a/configfile.h b/configfile.h index 72caa4a..8f73de2 100644 --- a/configfile.h +++ b/configfile.h @@ -24,6 +24,8 @@ VOID ConfigFileUnsetAll(CONFIGFILEPTR abstractConfigFile, CONST_STRPTR compoundK VOID ConfigFileRemoveSection(CONFIGFILEPTR abstractConfigFile, CONST_STRPTR canonicalName); VOID ConfigFileDump(CONFIGFILEPTR configFile); +VOID ConfigFileSave(CONFIGFILEPTR configFile); +VOID ConfigFileWrite(CONFIGFILEPTR abstractConfigFile, BPTR file); StringArray ConfigFileSplitKeyForVar(CONST_STRPTR key); StringArray ConfigFileSplitKeyCompletely(CONST_STRPTR key); diff --git a/configmodel.c b/configmodel.c index 5257703..00307e6 100644 --- a/configmodel.c +++ b/configmodel.c @@ -50,18 +50,26 @@ LINEPTR LineNew(CONST_STRPTR buffer) STATIC VOID lineDumpRecreate(LINEPTR abstractLine); -VOID LineDump(LINEPTR abstractLine) +VOID LineWrite(LINEPTR abstractLine, BPTR file) { struct Line* line = (struct Line*)abstractLine; if( line != NULL ) { if( line->rawText != NULL ) { - Printf("%s", line->rawText); + Write(file, line->rawText, strlen(line->rawText)); } - else + } +} + +VOID LineDump(LINEPTR abstractLine) +{ + struct Line* line = (struct Line*)abstractLine; + if( line != NULL ) + { + if( line->rawText != NULL ) { - lineDumpRecreate(line); + Printf("%s", line->rawText); } } } @@ -377,6 +385,14 @@ VOID SectionRemoveLinesForVariable(SECTIONPTR abstractSection, CONST_STRPTR varK } } +VOID SectionWrite(SECTIONPTR abstractSection, BPTR file) +{ + struct Section* section = (struct Section*)abstractSection; + if( section != NULL && section->lines != NULL ) + { + ArrayForEach(LINEPTR, aLine, section->lines, LineWrite(aLine,file);); + } +} VOID SectionDump(SECTIONPTR abstractSection) { diff --git a/configmodel.h b/configmodel.h index 926b1ed..526c401 100644 --- a/configmodel.h +++ b/configmodel.h @@ -14,6 +14,7 @@ VOID LineSetInitialVariable(LINEPTR line, VARIABLEPTR variable); VOID LineSetVariable(LINEPTR line, VARIABLEPTR variable); BOOL LineHasVariable(LINEPTR line, CONST_STRPTR varKey); BOOL LineIsBlank(LINEPTR line); +VOID LineWrite(LINEPTR abstractLine, BPTR file); VOID LineDump(LINEPTR abstractLine); @@ -24,6 +25,7 @@ VOID SectionAddSectionLine(SECTIONPTR section, LINEPTR line); VOID SectionAddLine(SECTIONPTR abstractSection, LINEPTR abstractLine, BOOL addBeforeBlanks); CONST_STRPTR SectionCanonicalName(SECTIONPTR section); VOID SectionDump(SECTIONPTR section); +VOID SectionWrite(SECTIONPTR section, BPTR file); CONST_STRPTR SectionSerialize(SECTIONPTR section); BOOL SectionHasName(SECTIONPTR section, CONST_STRPTR primary); CONST_STRPTR SectionGetRawSubsectionName(SECTIONPTR section); diff --git a/types.h b/types.h index 896e760..da9d7cd 100644 --- a/types.h +++ b/types.h @@ -2,6 +2,7 @@ #define __TYPES_H #include +#include typedef APTR SECTIONSTOREPTR;