From b6b95f58edd0d30180faa88bc86e6c29c1b631e9 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sun, 14 Feb 2021 17:07:26 +0100 Subject: [PATCH] import the inifile builder from iniparser repo --- src/tools/inifile.c | 81 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 src/tools/inifile.c diff --git a/src/tools/inifile.c b/src/tools/inifile.c new file mode 100644 index 000000000..e8db63740 --- /dev/null +++ b/src/tools/inifile.c @@ -0,0 +1,81 @@ +#include +#include +#include + +#include + +const char *progname = "inifile"; + +void usage(void) { + fprintf(stdout, "Usage: %s file.ini [add|del] \n", progname); +} + +int main(int argc, char ** argv) { + const char *inifile; + const char *command; + dictionary *ini; + FILE *F; + + inifile = (argc<2) ? 0 : argv[1]; + command = (argc<3) ? 0 : argv[2]; + + if (!inifile) { + fputs("Missing filename.\n", stderr); + usage(); + return 1; + } + if (!command) { + fputs("Missing command.\n", stderr); + usage(); + return 1; + } + ini = iniparser_load(inifile); + if (!ini) { + fprintf(stderr, "could not open %s.\n", inifile); + return 1; + } + if (strcmp(command, "add")==0) { + if (argc==4) { + iniparser_set(ini, argv[3], ""); + } + else if (argc==5) { + iniparser_set(ini, argv[3], argv[4]); + } + else { + fputs("set needs one or two arguments.\n", stderr); + return 1; + } + } + else if (strcmp(command, "get")==0) { + const char * str; + if (argc==4) { + str = iniparser_getstring(ini, argv[3], ""); + } + else if (argc==5) { + str = iniparser_getstring(ini, argv[3], argv[4]); + } + else { + fputs("set needs one or two arguments.\n", stderr); + return 1; + } + fprintf(stdout, "%s\n", str); + } + else if (strcmp(command, "del")==0) { + if (argc<4) { + fputs("del needs one argument.\n", stderr); + return 1; + } + iniparser_unset(ini, argv[3]); + } else { + fprintf(stderr, "unknown command %s.\n", command); + usage(); + return 1; + } + + if ((F=fopen(inifile, "wt"))!=0) { + iniparser_dump_ini(ini, F); + fclose(F); + } + iniparser_freedict(ini); + return 0; +}