summaryrefslogtreecommitdiff
path: root/main.c
blob: 67a368fa11c087fab5dc3b684e09691a9cf63faa (plain)
    1 //  Cini is a command line tool to parse ini files
    2 //  Copyright (C) 2018  Aaron Ball <nullspoon@oper.io>
    3 // 
    4 //  This program is free software: you can redistribute it and/or modify
    5 //  it under the terms of the GNU General Public License as published by
    6 //  the Free Software Foundation, either version 3 of the License, or
    7 //  (at your option) any later version.
    8 // 
    9 //  This program is distributed in the hope that it will be useful,
   10 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
   11 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   12 //  GNU General Public License for more details.
   13 // 
   14 //  You should have received a copy of the GNU General Public License
   15 //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
   16 
   17 #include <stdio.h>
   18 #include <stdlib.h>
   19 #include <string.h>
   20 
   21 #include "str.h"
   22 #include "ini.h"
   23 
   24 
   25 int main(int argc, char* argv[]) {
   26   int i         = 1; // Yep, positive 1, skips action arg
   27   char* action  = "";
   28   char* file    = "";
   29   char* key     = "";
   30   char* section = "";
   31   int hidekeys  = 0;
   32 
   33 
   34   // Parse args
   35   for(; i<argc; i++) {
   36     if(strcmp(argv[i], "--no-keys") == 0) {
   37       hidekeys = 1;
   38     } else if(strcmp(argv[i], "-f") == 0 || strcmp(argv[i], "--file") == 0) {
   39       i++;
   40       file = argv[i];
   41     } else if(strcmp(argv[i], "-k") == 0 || strcmp(argv[i], "--key") == 0) {
   42       i++;
   43       key = argv[i];
   44     } else if(strcmp(argv[i], "-s") == 0 || strcmp(argv[i], "--section") == 0) {
   45       i++;
   46       section = argv[i];
   47     } else {
   48       action = argv[i];
   49     }
   50   }
   51 
   52   // Ensure action is specified
   53   if(action[0] == '\0') {
   54     fprintf(stderr, "Action required\n");
   55     return 1;
   56   }
   57 
   58   // Ensure ini file path is specified
   59   if(file[0] == '\0') {
   60     fprintf(stderr, "Path to ini file required (-f,--file)\n");
   61     return 1;
   62   }
   63 
   64   if(strcmp(action, "section") == 0) {
   65     if(section[0] == '\0') {
   66       printf("Section name required (-s,--section)\n");
   67       return 1;
   68     }
   69     return ini_getsection(file, section, hidekeys);
   70   } else if(strcmp(action, "key") == 0) {
   71     if(key[0] == '\0') {
   72       printf("Key required (-k,--key)\n");
   73       return 1;
   74     }
   75     return ini_getbykey(file, key);
   76   }
   77 
   78   fprintf(stderr, "Unknown option '%s'\n", action);
   79   return 1;
   80 }

Generated by cgit