diff options
Diffstat (limited to 'main.c')
-rw-r--r-- | main.c | 80 |
1 files changed, 80 insertions, 0 deletions
@@ -0,0 +1,80 @@ +// Cini is a command line tool to parse ini files +// Copyright (C) 2018 Aaron Ball <nullspoon@oper.io> +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see <http://www.gnu.org/licenses/>. + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include "str.h" +#include "ini.h" + + +int main(int argc, char* argv[]) { + int i = 1; // Yep, positive 1, skips action arg + char* action = ""; + char* file = ""; + char* key = ""; + char* section = ""; + int hidekeys = 0; + + + // Parse args + for(; i<argc; i++) { + if(strcmp(argv[i], "--no-keys") == 0) { + hidekeys = 1; + } else if(strcmp(argv[i], "-f") == 0 || strcmp(argv[i], "--file") == 0) { + i++; + file = argv[i]; + } else if(strcmp(argv[i], "-k") == 0 || strcmp(argv[i], "--key") == 0) { + i++; + key = argv[i]; + } else if(strcmp(argv[i], "-s") == 0 || strcmp(argv[i], "--section") == 0) { + i++; + section = argv[i]; + } else { + action = argv[i]; + } + } + + // Ensure action is specified + if(action[0] == '\0') { + fprintf(stderr, "Action required\n"); + return 1; + } + + // Ensure ini file path is specified + if(file[0] == '\0') { + fprintf(stderr, "Path to ini file required (-f,--file)\n"); + return 1; + } + + if(strcmp(action, "section") == 0) { + if(section[0] == '\0') { + printf("Section name required (-s,--section)\n"); + return 1; + } + return ini_getsection(file, section, hidekeys); + } else if(strcmp(action, "key") == 0) { + if(key[0] == '\0') { + printf("Key required (-k,--key)\n"); + return 1; + } + return ini_getbykey(file, key); + } + + fprintf(stderr, "Unknown option '%s'\n", action); + return 1; +} |