set basically works

main
Alan Francis 1 year ago
parent d1d26c96d1
commit 199f9a988d
  1. 34
      configfile.c
  2. 4
      configfile.h
  3. 19
      configmodel.c
  4. 13
      main.c

@ -5,6 +5,7 @@
#include "containers/stringarray.h"
#include "containers/sectionarray.h"
#include "containers/linearray.h"
#include <proto/exec.h>
#include <proto/dos.h>
#include <string.h>
@ -193,7 +194,7 @@ StringArray ConfigFileGetAll(CONFIGFILEPTR abstractConfigFile, CONST_STRPTR comp
return result;
}
VOID ConfigFileAdd(CONFIGFILEPTR abstractConfigFile, CONST_STRPTR compoundKey, CONST_STRPTR stringValue)
VOID ConfigFileAddVariable(CONFIGFILEPTR abstractConfigFile, CONST_STRPTR compoundKey, CONST_STRPTR stringValue)
{
struct ConfigFile* configFile = (struct ConfigFile*)abstractConfigFile;
if( configFile != NULL )
@ -218,15 +219,40 @@ VOID ConfigFileAdd(CONFIGFILEPTR abstractConfigFile, CONST_STRPTR compoundKey, C
}
}
VOID ConfigFileSet(CONFIGFILEPTR abstractConfigFile, CONST_STRPTR compoundKey, CONST_STRPTR stringValue)
BOOL ConfigFileSet(CONFIGFILEPTR abstractConfigFile, CONST_STRPTR compoundKey, CONST_STRPTR stringValue)
{
BOOL result = FALSE;
struct ConfigFile* configFile = (struct ConfigFile*)abstractConfigFile;
if( configFile != NULL )
{
StringArray parts = ConfigFileSplitKeyCompletely(compoundKey);
StringArray parts = ConfigFileSplitKeyForVar(compoundKey);
LineArray lines = SectionStoreFindLines(configFile->sectionStore, StringArrayValues(parts)[0], StringArrayValues(parts)[1]);
if( SizeOfArray(lines) == 0 )
{
ConfigFileAddVariable(abstractConfigFile, compoundKey, stringValue);
result = TRUE;
}
else if( SizeOfArray(lines) == 1 )
{
LINEPTR line = (LINEPTR)ArrayBackValue(LINEPTR, lines);
if( line )
{
VARIABLEPTR var = VariableCreate(StringArrayValues(parts)[1], stringValue);
if( var )
{
LineSetVariable(line, var);
result = TRUE;
}
}
}
else // more than one line, we error.
{
result = FALSE;
}
StringArrayFree(parts, TRUE);
LineArrayFree(lines, FALSE);
}
return result;
}
VOID ConfigFileReplaceAll(CONFIGFILEPTR abstractConfigFile, CONST_STRPTR compoundKey, CONST_STRPTR stringValue)

@ -6,10 +6,10 @@ VOID ConfigFileFree(CONFIGFILEPTR configFile);
StringArray ConfigFileGetAll(CONFIGFILEPTR abstractConfigFile, CONST_STRPTR compoundKey);
STRPTR ConfigFileGet(CONFIGFILEPTR abstractConfigFile, CONST_STRPTR compoundKey);
VOID ConfigFileSet(CONFIGFILEPTR abstractConfigFile, CONST_STRPTR compoundKey, CONST_STRPTR stringValue);
BOOL ConfigFileSet(CONFIGFILEPTR abstractConfigFile, CONST_STRPTR compoundKey, CONST_STRPTR stringValue);
// --add
VOID ConfigFileAdd(CONFIGFILEPTR abstractConfigFile, CONST_STRPTR compoundKey, CONST_STRPTR stringValue);
VOID ConfigFileAddVariable(CONFIGFILEPTR abstractConfigFile, CONST_STRPTR compoundKey, CONST_STRPTR stringValue);
// --replace-all
VOID ConfigFileReplaceAll(CONFIGFILEPTR abstractConfigFile, CONST_STRPTR compoundKey, CONST_STRPTR stringValue);

@ -149,18 +149,19 @@ VOID LineSetVariable(LINEPTR abstractLine, VARIABLEPTR abstractVariable)
struct Line* line = (struct Line*)abstractLine;
if( line != NULL )
{
if( line->rawText != NULL )
{
// if we change the variable we remove any original text
FreeVec(line->rawText);
line->rawText = NULL;
}
// we take ownership of the variable
if( line->variable != NULL )
{
VariableFree(line->variable);
}
line->variable = abstractVariable;
if( line->rawText != NULL )
{
// if we change the variable we remove any original text
FreeVec(line->rawText);
}
line->rawText = (STRPTR)VariableSerialize(line->variable);
}
}
@ -508,11 +509,11 @@ CONST_STRPTR VariableSerialize(VARIABLEPTR abstractVariable)
{
STRPTR result = NULL;
struct Variable* variable = (struct Variable*)abstractVariable;
if( variable != NULL && variable->key != NULL && variable->value.stringValue != NULL )
if( variable != NULL && variable->normalizedKey != NULL && variable->value.stringValue != NULL )
{
ULONG size = strlen(variable->key) + 3 + strlen(variable->value.stringValue) + 1;
ULONG size = strlen(variable->normalizedKey) + 3 + strlen(variable->value.stringValue) + 1;
result = AllocVec(size, MEMF_CLEAR);
sprintf(result, "\t%s = %s\n", variable->key, variable->value.stringValue);
sprintf(result, "\t%s = %s\n", variable->normalizedKey, variable->value.stringValue);
}
return (CONST_STRPTR)result;
}

@ -28,17 +28,18 @@ WORD DoTheWork(STRPTR filename)
StringArray values = NULL;
STRPTR var = NULL;
values = ConfigFileGetAll(config, "branch.main.remote");
// values = ConfigFileGetAll(config, "branch.main.remote");
// StringArrayForEach(values, Printf("main %s\n", aString););
StringArrayFree(values, TRUE);
// StringArrayFree(values, TRUE);
var = ConfigFileGet(config, "branch.config-file-parsing-from-book.remote");
// var = ConfigFileGet(config, "branch.config-file-parsing-from-book.remote");
// Printf("parse %s\n\n\n", var);
FreeVec(var);
// FreeVec(var);
// ConfigFileDump(config);
ConfigFileAdd(config, "branch.main.foob", "bar");
ConfigFileAdd(config, "branch.alan.foob", "bar");
// ConfigFileSet(config, "branch.main.foob", "bar");
// ConfigFileSet(config, "branch.alan.foob", "bar");
ConfigFileSet(config, "branch.main.remote", "testing");
ConfigFileDump(config);
ConfigFileFree(config);

Loading…
Cancel
Save