diff --git a/testapp/configfile.h b/testapp/configfile.h new file mode 100644 index 0000000..265bc4a --- /dev/null +++ b/testapp/configfile.h @@ -0,0 +1,36 @@ +#ifndef __CONFIGFILE_H +#define __CONFIGFILE_H +#include +#include +#include +typedef APTR CONFIGFILEPTR; + +CONFIGFILEPTR ConfigFileRead(CONST_STRPTR filename); +VOID ConfigFileFree(CONFIGFILEPTR configFile); + +Array ConfigFileGetAll(CONFIGFILEPTR abstractConfigFile, CONST_STRPTR compoundKey); +STRPTR ConfigFileGet(CONFIGFILEPTR abstractConfigFile, CONST_STRPTR compoundKey); +BOOL ConfigFileSet(CONFIGFILEPTR abstractConfigFile, CONST_STRPTR compoundKey, CONST_STRPTR stringValue); + +// --add +VOID ConfigFileAddVariable(CONFIGFILEPTR abstractConfigFile, CONST_STRPTR compoundKey, CONST_STRPTR stringValue); + +// --replace-all +VOID ConfigFileReplaceAll(CONFIGFILEPTR abstractConfigFile, CONST_STRPTR compoundKey, CONST_STRPTR stringValue); + +// --unset +VOID ConfigFileUnset(CONFIGFILEPTR abstractConfigFile, CONST_STRPTR compoundKey, CONST_STRPTR stringValue); + +// --unset-all +VOID ConfigFileUnsetAll(CONFIGFILEPTR abstractConfigFile, CONST_STRPTR compoundKey, CONST_STRPTR stringValue); + +// --remove-section +VOID ConfigFileRemoveSection(CONFIGFILEPTR abstractConfigFile, CONST_STRPTR canonicalName); + +VOID ConfigFileDump(CONFIGFILEPTR configFile); +VOID ConfigFileSave(CONFIGFILEPTR configFile); +VOID ConfigFileWrite(CONFIGFILEPTR abstractConfigFile, BPTR file); + +Array ConfigFileSubsectionsForSection(CONFIGFILEPTR abstractConfigFile, CONST_STRPTR primarySection); + +#endif \ No newline at end of file diff --git a/testapp/main.c b/testapp/main.c new file mode 100644 index 0000000..b9b8f49 --- /dev/null +++ b/testapp/main.c @@ -0,0 +1,102 @@ +// Printf("running (%ld bytes avail)\n", AvailMem(0)); +#define __CLIB_PRAGMA_LIBCALL +#include +#include +#define __NOLIBBASE__ +// #include "containers/stringarray.h" +// #include "containers/linearray.h" +#include "configfile.h" +//#include "configmodel.h" +#include + +WORD DoTheWork(STRPTR filename); +VOID ProcessFile(BPTR configFile); + +char *vers="\0$VER: ConfigReader (dd.mm.yyyy)"; +char *stacksize = "$STACK:8192"; // only works when started from CLI + +struct Library *ContainerkitBase; + +WORD DoTheWork(STRPTR filename) +{ + WORD result = RETURN_OK; + if (ContainerkitBase) + { + CONFIGFILEPTR config = ConfigFileRead(filename); + if( config != NULL ) + { + Array values = NULL; + STRPTR var = NULL; + +// values = ConfigFileGetAll(config, "branch.main.remote"); +// StringArrayForEach(values, Printf("main %s\n", aString);); +// StringArrayFree(values, TRUE); + +// var = ConfigFileGet(config, "branch.config-file-parsing-from-book.remote"); +// Printf("parse %s\n\n\n", var); + // FreeVec(var); + + // ConfigFileDump(config); +// ConfigFileSet(config, "branch.main.foob", "bar"); + // ConfigFileSet(config, "branch.alan.foob", "bar"); + ConfigFileSet(config, "branch.main.remote", "testing"); + ConfigFileSet(config, "branch.main.second", "testing"); +// ConfigFileSet(config, "branch.main.third", "testing"); +// ConfigFileReplaceAll(config, "branch.main.alan", "hello"); +// ConfigFileAddVariable(config, "branch.main.newvar", "alan"); +// ConfigFileAddVariable(config, "branch.main.newvar", "alan2"); +// ConfigFileAddVariable(config, "branch.main.newvar", "alan3"); +// ConfigFileReplaceAll(config, "branch.main.newvar", "goodbye"); +// ConfigFileAddVariable(config, "branch.main.newvar1", "yay"); + + ConfigFileAddVariable(config, "foo1", "alan"); + ConfigFileAddVariable(config, "test.foo1", "alan2"); + ConfigFileAddVariable(config, "test.foo.foo1", "alan3"); + ConfigFileAddVariable(config, "test.foo.bar.foo1", "alan4"); + ConfigFileAddVariable(config, "test.foo.bar.baz.foo1", "alan5"); + ConfigFileAddVariable(config, "test.foo.bar.baz.bat.foo1", "alan6"); + ConfigFileAddVariable(config, "test.section.jane.foo3", "alan3"); + values = ConfigFileSubsectionsForSection(config, "branch"); + if( values ) + { + ArrayForEach(STRPTR, aString, values, Printf("branch:[%s]\n",aString);); + ArrayForEach(STRPTR, aString, values, FreeVec(aString);); + DeleteArray(values); + } + ConfigFileSave(config); + + ConfigFileFree(config); + } + else + { + Printf("could not read the file\n"); + result = RETURN_ERROR; + } + } + return result; +} + +WORD main(WORD argc, STRPTR *argv) +{ + WORD result = RETURN_OK; + + // this does nothing but the first call to Print drops a bunch of memory, + // I assume because of opening some resource so this means my start and + // end markers are "clean" and I can ensure I'm not leaking. + Printf("\n"); + + ContainerkitBase = OpenLibrary("containerkit.library", 1); + if( ContainerkitBase ) + { + Printf("\n\nrunning (%ld bytes avail)\n\n", AvailMem(0)); + result = DoTheWork(argv[1]); + Printf("\n\ndone (%ld bytes avail)\n\n", AvailMem(0)); + CloseLibrary(ContainerkitBase); + } + else + { + Printf("failed to open library\n"); + result = RETURN_ERROR; + } + return result; +} diff --git a/testapp/smakefile b/testapp/smakefile new file mode 100644 index 0000000..a9c3aab --- /dev/null +++ b/testapp/smakefile @@ -0,0 +1,22 @@ +# +# :ts=8 +# + +############################################################################### + +NAME = testapp + +LFLAGS = addsym smallcode smalldata noicons batch +LIBS = lib:sc.lib lib:amiga.lib lib:debug.lib + +############################################################################### + +$(NAME) : main.o configfile.lib configfile.h + slink lib:c.o main.o to $(NAME) noicons lib $(LIBS) configfile.lib $(LFLAGS) + +clean: + delete \#?.o $(NAME) ALL QUIET + +############################################################################### + +main.o : main.c diff --git a/testapp/testapp b/testapp/testapp new file mode 100644 index 0000000..337564a Binary files /dev/null and b/testapp/testapp differ diff --git a/testapp/testconfig.cfg b/testapp/testconfig.cfg new file mode 100644 index 0000000..367d98b --- /dev/null +++ b/testapp/testconfig.cfg @@ -0,0 +1,20 @@ +#this is a comment +[core] + repositoryformatversion = 0 + precomposeunicode = true + +[remote "origin.foo"] #this is also a coment + url = git@git.alancfrancis.com:acf/AmigaGit2.git + fetch = +refs/heads/*:refs/remotes/origin/* + +[branch "main"] + remote = origin + merge = refs/heads/main + alan = yes + alan = no + alan = sure + +[branch "config-file-parsing-from-book"] + remote = origin + merge = refs/heads/config-file-parsing-from-book +