save a file

main
Alan Francis 1 year ago
parent 8f06578a85
commit a4ed111518
  1. 35
      configfile.c
  2. 2
      configfile.h
  3. 24
      configmodel.c
  4. 2
      configmodel.h
  5. 1
      types.h

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

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

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

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

@ -2,6 +2,7 @@
#define __TYPES_H
#include <exec/types.h>
#include <dos/dos.h>
typedef APTR SECTIONSTOREPTR;

Loading…
Cancel
Save