diff --git a/configfile.c b/configfile.c index 2edd781..269eb97 100644 --- a/configfile.c +++ b/configfile.c @@ -46,26 +46,39 @@ VOID ConfigFileFree(CONFIGFILEPTR abstractConfigFile) } } -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(); - result->filename = AllocVec(strlen(filename)+1, MEMF_CLEAR); - CopyMem(filename, result->filename, strlen(filename)); - 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; } diff --git a/configfile.h b/configfile.h index 265bc4a..29c39e9 100644 --- a/configfile.h +++ b/configfile.h @@ -5,7 +5,11 @@ #include typedef APTR CONFIGFILEPTR; -CONFIGFILEPTR ConfigFileRead(CONST_STRPTR filename); +// added the BPTR version so we can use WorkspaceOpenFile. +// We still need a file name to be able to save though I should probably +// get that from the file +CONFIGFILEPTR ConfigFileRead(BPTR configFile); +CONFIGFILEPTR ConfigFileReadName(CONST_STRPTR filename); VOID ConfigFileFree(CONFIGFILEPTR configFile); Array ConfigFileGetAll(CONFIGFILEPTR abstractConfigFile, CONST_STRPTR compoundKey); diff --git a/main.c b/main.c index e49ac19..00f5144 100644 --- a/main.c +++ b/main.c @@ -22,7 +22,7 @@ WORD DoTheWork(STRPTR filename) WORD result = RETURN_OK; if (ContainerkitBase) { - CONFIGFILEPTR config = ConfigFileRead(filename); + CONFIGFILEPTR config = ConfigFileReadName(filename); if( config != NULL ) { StringArray values = NULL; diff --git a/testapp/configfile.h b/testapp/configfile.h index 265bc4a..e988f47 100644 --- a/testapp/configfile.h +++ b/testapp/configfile.h @@ -5,7 +5,8 @@ #include typedef APTR CONFIGFILEPTR; -CONFIGFILEPTR ConfigFileRead(CONST_STRPTR filename); +CONFIGFILEPTR ConfigFileRead(BPTR configFile, CONST_STRPTR saveName); +CONFIGFILEPTR ConfigFileReadName(CONST_STRPTR filename); VOID ConfigFileFree(CONFIGFILEPTR configFile); Array ConfigFileGetAll(CONFIGFILEPTR abstractConfigFile, CONST_STRPTR compoundKey); diff --git a/testapp/containerkit.library b/testapp/containerkit.library new file mode 100644 index 0000000..1bcac3f Binary files /dev/null and b/testapp/containerkit.library differ diff --git a/testapp/main.c b/testapp/main.c index b9b8f49..6dcf0d8 100644 --- a/testapp/main.c +++ b/testapp/main.c @@ -12,7 +12,7 @@ WORD DoTheWork(STRPTR filename); VOID ProcessFile(BPTR configFile); -char *vers="\0$VER: ConfigReader (dd.mm.yyyy)"; +char *vers="\0$VER: testapp (dd.mm.yyyy)"; char *stacksize = "$STACK:8192"; // only works when started from CLI struct Library *ContainerkitBase; @@ -22,7 +22,8 @@ WORD DoTheWork(STRPTR filename) WORD result = RETURN_OK; if (ContainerkitBase) { - CONFIGFILEPTR config = ConfigFileRead(filename); + BPTR file = Open(filename, MODE_READWRITE); + CONFIGFILEPTR config = ConfigFileRead(file); if( config != NULL ) { Array values = NULL; diff --git a/testapp/testconfig.cfg b/testapp/testconfig.cfg index 367d98b..b73753d 100644 --- a/testapp/testconfig.cfg +++ b/testapp/testconfig.cfg @@ -1,4 +1,6 @@ #this is a comment + foo1 = alan + foo1 = alan [core] repositoryformatversion = 0 precomposeunicode = true @@ -8,13 +10,32 @@ fetch = +refs/heads/*:refs/remotes/origin/* [branch "main"] - remote = origin + remote = testing merge = refs/heads/main alan = yes alan = no alan = sure + second = testing [branch "config-file-parsing-from-book"] remote = origin merge = refs/heads/config-file-parsing-from-book +[test] + foo1 = alan2 + foo1 = alan2 +[test "foo"] + foo1 = alan3 + foo1 = alan3 +[test "foo.bar"] + foo1 = alan4 + foo1 = alan4 +[test "foo.bar.baz"] + foo1 = alan5 + foo1 = alan5 +[test "foo.bar.baz.bat"] + foo1 = alan6 + foo1 = alan6 +[test "section.jane"] + foo3 = alan3 + foo3 = alan3