|
|
|
@ -21,7 +21,7 @@ STATIC LINEPTR configFileReadLine(BPTR file); |
|
|
|
|
// - STRUCTS -----------------------------------------------------------------------------
|
|
|
|
|
struct ConfigFile |
|
|
|
|
{ |
|
|
|
|
CONST_STRPTR filename; |
|
|
|
|
STRPTR filename; |
|
|
|
|
SECTIONSTOREPTR sectionStore; |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
@ -38,121 +38,88 @@ VOID ConfigFileFree(CONFIGFILEPTR abstractConfigFile) |
|
|
|
|
{ |
|
|
|
|
SectionStoreFree(configFile->sectionStore); |
|
|
|
|
} |
|
|
|
|
if( configFile->filename != NULL ) |
|
|
|
|
{ |
|
|
|
|
FreeVec(configFile->filename); |
|
|
|
|
} |
|
|
|
|
FreeVec(configFile); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
CONFIGFILEPTR ConfigFileRead(CONST_STRPTR filename) |
|
|
|
|
CONFIGFILEPTR ConfigFileReadName(CONST_STRPTR filename) |
|
|
|
|
{ |
|
|
|
|
struct ConfigFile* result = NULL; |
|
|
|
|
BPTR configFile = Open(filename, MODE_OLDFILE); |
|
|
|
|
if( configFile != ZERO ) |
|
|
|
|
{ |
|
|
|
|
result = ConfigFileRead(configFile); |
|
|
|
|
Close(configFile); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
LINEPTR line = NULL; |
|
|
|
|
result = AllocVec(sizeof(struct ConfigFile), MEMF_CLEAR); |
|
|
|
|
result->sectionStore = SectionStoreNew(); |
|
|
|
|
while( (line = configFileReadLine(configFile)) != NULL ) |
|
|
|
|
return result; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
CONFIGFILEPTR ConfigFileRead(BPTR configFile) |
|
|
|
|
{
|
|
|
|
|
struct ConfigFile* result = NULL; |
|
|
|
|
LINEPTR line = NULL; |
|
|
|
|
if( configFile != 0 ) |
|
|
|
|
{ |
|
|
|
|
UBYTE buffer[256]; |
|
|
|
|
if(NameFromFH(configFile, buffer, 256))// if we opened with a filename this is a waste, if not its necessary
|
|
|
|
|
{ |
|
|
|
|
SectionStoreAddLineToCurrentSection(result->sectionStore, line); |
|
|
|
|
//LineDump(line);
|
|
|
|
|
result = AllocVec(sizeof(struct ConfigFile), MEMF_CLEAR); |
|
|
|
|
result->sectionStore = SectionStoreNew(); |
|
|
|
|
result->filename = AllocVec(strlen(buffer)+1, MEMF_CLEAR); |
|
|
|
|
CopyMem(buffer, result->filename, strlen(buffer)); |
|
|
|
|
while( (line = configFileReadLine(configFile)) != NULL ) |
|
|
|
|
{ |
|
|
|
|
SectionStoreAddLineToCurrentSection(result->sectionStore, line); |
|
|
|
|
//LineDump(line);
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
Close(configFile); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return result; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
VOID ConfigFileSaveOverwrite(CONFIGFILEPTR abstractConfigFile) |
|
|
|
|
{ |
|
|
|
|
struct ConfigFile* configFile = (struct ConfigFile*)abstractConfigFile; |
|
|
|
|
ConfigFileSaveCopyTo(abstractConfigFile, configFile->filename); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// a key is "foo.bar.baz.blah" where "foo" is the primary section name
|
|
|
|
|
// "bar.baz" is the secondary section name and "blah" is the variable name.
|
|
|
|
|
// this splits foo.bar.baz.blah into foo, bar.baz amd blah.
|
|
|
|
|
// section, subsection and variable.
|
|
|
|
|
// we'll get empty strings for the parts that arent present
|
|
|
|
|
StringArray ConfigFileSplitKeyCompletely(CONST_STRPTR key) |
|
|
|
|
VOID ConfigFileSaveCopyTo(CONFIGFILEPTR abstractConfigFile, STRPTR newFilename) |
|
|
|
|
{ |
|
|
|
|
StringArray result = StringArrayNew(); |
|
|
|
|
StringArray parts = StringArrayNew(); |
|
|
|
|
ULONG numberOfParts = 0; |
|
|
|
|
|
|
|
|
|
STRPTR token = NULL; |
|
|
|
|
// we need to make a copy of the key because strtok modifies it.
|
|
|
|
|
STRPTR keyCopy = AllocVec(strlen(key)+1, MEMF_CLEAR); |
|
|
|
|
CopyMem(key, keyCopy, strlen(key)); |
|
|
|
|
|
|
|
|
|
token = strtok(keyCopy, "."); |
|
|
|
|
while (token != NULL) |
|
|
|
|
{ |
|
|
|
|
StringArrayAppendAndRetain(parts, token); |
|
|
|
|
token = strtok(NULL, "."); |
|
|
|
|
} |
|
|
|
|
FreeVec(keyCopy); |
|
|
|
|
|
|
|
|
|
// now we join all but the first and last part
|
|
|
|
|
numberOfParts = SizeOfArray(parts); |
|
|
|
|
if( numberOfParts == 1 ) //just a variable ["","","var"]
|
|
|
|
|
BPTR file = Open(newFilename, MODE_NEWFILE); |
|
|
|
|
if( file != 0 ) |
|
|
|
|
{ |
|
|
|
|
StringArrayAppendAndRetain(result, ""); |
|
|
|
|
StringArrayAppendAndRetain(result, ""); |
|
|
|
|
StringArrayAppendAndRetain(result, (STRPTR)ArrayBackValue(STRPTR, parts)); |
|
|
|
|
ConfigFileWrite(abstractConfigFile, file); |
|
|
|
|
Close(file); |
|
|
|
|
} |
|
|
|
|
else if( numberOfParts == 2 ) // section and variable ["section","","var"]
|
|
|
|
|
{ |
|
|
|
|
StringArrayAppendAndRetain(result, StringArrayValues(parts)[0]); |
|
|
|
|
StringArrayAppendAndRetain(result, ""); |
|
|
|
|
StringArrayAppendAndRetain(result, StringArrayValues(parts)[1]); |
|
|
|
|
} |
|
|
|
|
else if( numberOfParts == 3 ) // section and subsection and variable ["section","subsec","var"]
|
|
|
|
|
{ |
|
|
|
|
StringArrayAppendAndRetain(result, StringArrayValues(parts)[0]); |
|
|
|
|
StringArrayAppendAndRetain(result, StringArrayValues(parts)[1]); |
|
|
|
|
StringArrayAppendAndRetain(result, StringArrayValues(parts)[2]); |
|
|
|
|
} |
|
|
|
|
else if( numberOfParts > 3 ) // subsections needs dotted ["section", "subsec1.subsec2", "var""]
|
|
|
|
|
{ |
|
|
|
|
StringArrayAppendAndRetain(result, StringArrayValues(parts)[0]); //section
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// start at index 1, and add (size -2 (start+end)) parts
|
|
|
|
|
StringArrayAppend(result, StringArrayJoinedParts(parts, '.', 1, SizeOfArray(parts)-2)); |
|
|
|
|
|
|
|
|
|
StringArrayAppendAndRetain(result, (STRPTR)ArrayBackValue(STRPTR, parts)); // variable
|
|
|
|
|
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); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
StringArrayFree(parts, TRUE); |
|
|
|
|
return result; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
StringArray ConfigFileSplitKeyForVar(CONST_STRPTR key) |
|
|
|
|
|
|
|
|
|
StringArray ConfigFileSubsectionsForSection(CONFIGFILEPTR abstractConfigFile, CONST_STRPTR primarySection) |
|
|
|
|
{ |
|
|
|
|
// a key is "foo.bar.baz.blah" where "foo" is the primary section name
|
|
|
|
|
// "bar.baz" is the secondary section name and "blah" is the variable name.
|
|
|
|
|
// In this case we'll just break it down into the canonical section name,
|
|
|
|
|
// foo.bar.baz and the variable name blah.
|
|
|
|
|
|
|
|
|
|
StringArray result = StringArrayNew(); |
|
|
|
|
StringArray parts = StringArrayNew(); |
|
|
|
|
STRPTR sectionPart = NULL; |
|
|
|
|
STRPTR varPart = NULL; |
|
|
|
|
|
|
|
|
|
STRPTR token = NULL; |
|
|
|
|
// we need to make a copy of the key because strtok modifies it.
|
|
|
|
|
STRPTR keyCopy = AllocVec(strlen(key)+1, MEMF_CLEAR); |
|
|
|
|
CopyMem(key, keyCopy, strlen(key)); |
|
|
|
|
|
|
|
|
|
token = strtok(keyCopy, "."); |
|
|
|
|
while (token != NULL) |
|
|
|
|
{ |
|
|
|
|
StringArrayAppendAndRetain(parts, token); |
|
|
|
|
token = strtok(NULL, "."); |
|
|
|
|
} |
|
|
|
|
FreeVec(keyCopy); |
|
|
|
|
|
|
|
|
|
// now we join all but the last part
|
|
|
|
|
sectionPart = StringArrayJoinedParts(parts, '.', 0, SizeOfArray(parts)-1); |
|
|
|
|
StringArrayAppend(result, sectionPart); // its been alloced so dont copy
|
|
|
|
|
varPart = (STRPTR)ArrayBackValue(STRPTR, parts); |
|
|
|
|
StringArrayAppendAndRetain(result, varPart); // its a reference so copy
|
|
|
|
|
StringArrayFree(parts, TRUE); |
|
|
|
|
return result; |
|
|
|
|
struct ConfigFile* configFile = (struct ConfigFile*)abstractConfigFile; |
|
|
|
|
return SectionStoreSubsectionsForSection(configFile->sectionStore, primarySection); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
STRPTR ConfigFileGet(CONFIGFILEPTR abstractConfigFile, CONST_STRPTR compoundKey) |
|
|
|
@ -161,7 +128,7 @@ STRPTR ConfigFileGet(CONFIGFILEPTR abstractConfigFile, CONST_STRPTR compoundKey) |
|
|
|
|
struct ConfigFile* configFile = (struct ConfigFile*)abstractConfigFile; |
|
|
|
|
if( configFile != NULL ) |
|
|
|
|
{ |
|
|
|
|
StringArray split = ConfigFileSplitKeyForVar(compoundKey); |
|
|
|
|
StringArray split = CompoundKeySplitKeyForVar(compoundKey); |
|
|
|
|
VARIABLEPTR var = SectionStoreGet(configFile->sectionStore, StringArrayValues(split)[0], StringArrayValues(split)[1]); |
|
|
|
|
if( var != NULL ) |
|
|
|
|
{ |
|
|
|
@ -181,7 +148,7 @@ StringArray ConfigFileGetAll(CONFIGFILEPTR abstractConfigFile, CONST_STRPTR comp |
|
|
|
|
struct ConfigFile* configFile = (struct ConfigFile*)abstractConfigFile; |
|
|
|
|
if( configFile != NULL ) |
|
|
|
|
{ |
|
|
|
|
StringArray split = ConfigFileSplitKeyForVar(compoundKey); |
|
|
|
|
StringArray split = CompoundKeySplitKeyForVar(compoundKey); |
|
|
|
|
VariableArray vars = SectionStoreGetAll(configFile->sectionStore, StringArrayValues(split)[0], StringArrayValues(split)[1]); |
|
|
|
|
for( index = 0; index < SizeOfArray(vars); index++ ) |
|
|
|
|
{ |
|
|
|
@ -203,7 +170,7 @@ VOID ConfigFileAddVariable(CONFIGFILEPTR abstractConfigFile, CONST_STRPTR compou |
|
|
|
|
CONST_STRPTR varLine = NULL; |
|
|
|
|
LINEPTR line = NULL; |
|
|
|
|
|
|
|
|
|
StringArray canonicalParts = ConfigFileSplitKeyForVar(compoundKey); |
|
|
|
|
StringArray canonicalParts = CompoundKeySplitKeyForVar(compoundKey); |
|
|
|
|
var = VariableCreate(StringArrayValues(canonicalParts)[1], stringValue); |
|
|
|
|
varLine = VariableSerialize(var); |
|
|
|
|
line = LineNew(varLine); |
|
|
|
@ -224,7 +191,7 @@ BOOL ConfigFileSet(CONFIGFILEPTR abstractConfigFile, CONST_STRPTR compoundKey, C |
|
|
|
|
struct ConfigFile* configFile = (struct ConfigFile*)abstractConfigFile; |
|
|
|
|
if( configFile != NULL ) |
|
|
|
|
{ |
|
|
|
|
StringArray parts = ConfigFileSplitKeyForVar(compoundKey); |
|
|
|
|
StringArray parts = CompoundKeySplitKeyForVar(compoundKey); |
|
|
|
|
LineArray lines = SectionStoreFindLines(configFile->sectionStore, StringArrayValues(parts)[0], StringArrayValues(parts)[1]); |
|
|
|
|
if( SizeOfArray(lines) == 0 ) |
|
|
|
|
{ |
|
|
|
@ -259,7 +226,7 @@ VOID ConfigFileReplaceAll(CONFIGFILEPTR abstractConfigFile, CONST_STRPTR compoun |
|
|
|
|
struct ConfigFile* configFile = (struct ConfigFile*)abstractConfigFile; |
|
|
|
|
if( configFile != NULL ) |
|
|
|
|
{ |
|
|
|
|
StringArray parts = ConfigFileSplitKeyForVar(compoundKey); |
|
|
|
|
StringArray parts = CompoundKeySplitKeyForVar(compoundKey); |
|
|
|
|
SectionStoreRemoveLines(configFile->sectionStore, StringArrayValues(parts)[0], StringArrayValues(parts)[1]); |
|
|
|
|
ConfigFileAddVariable( abstractConfigFile, compoundKey, stringValue); |
|
|
|
|
StringArrayFree(parts, TRUE); |
|
|
|
@ -271,7 +238,7 @@ VOID ConfigFileUnset(CONFIGFILEPTR abstractConfigFile, CONST_STRPTR compoundKey, |
|
|
|
|
struct ConfigFile* configFile = (struct ConfigFile*)abstractConfigFile; |
|
|
|
|
if( configFile != NULL ) |
|
|
|
|
{ |
|
|
|
|
StringArray parts = ConfigFileSplitKeyCompletely(compoundKey); |
|
|
|
|
StringArray parts = CompoundKeySplitKeyCompletely(compoundKey); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
StringArrayFree(parts, TRUE); |
|
|
|
@ -283,7 +250,7 @@ VOID ConfigFileUnsetAll(CONFIGFILEPTR abstractConfigFile, CONST_STRPTR compoundK |
|
|
|
|
struct ConfigFile* configFile = (struct ConfigFile*)abstractConfigFile; |
|
|
|
|
if( configFile != NULL ) |
|
|
|
|
{ |
|
|
|
|
StringArray parts = ConfigFileSplitKeyCompletely(compoundKey); |
|
|
|
|
StringArray parts = CompoundKeySplitKeyCompletely(compoundKey); |
|
|
|
|
|
|
|
|
|
StringArrayFree(parts, TRUE); |
|
|
|
|
} |
|
|
|
|