// Cini is a command line tool to parse ini files // Copyright (C) 2018 Aaron Ball // // 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 . #include "ini.h" int ini_getbykey(char* path, char* reqkey) { FILE* fd; // File descriptor for ini file char buf[1024]; char key[512]; fd = fopen(path, "r"); while(fgets(buf, 1024, fd) != NULL) { trim(buf, buf); // Don't care about empty lines or section headers if(strcmp(buf, "") == 0 || buf[0] == '[') continue; split(buf, ' ', 0, key); if(strcmp(key, reqkey) == 0) { printf("key: %s\n", key); } } fclose(fd); return 0; } int ini_getsection(char* path, char* reqsection, int hidekeys) { FILE* fd; // File descriptor for ini file char buf[512]; char section[512] = ""; int linelen = 0; char print[512]; fd = fopen(path, "r"); if(!fd) { fprintf(stderr, "Could not open file %s\n", path); return 1; } while(fgets(buf, 512, fd) != NULL) { trim(buf, buf); if(strcmp(buf, "") == 0) continue; linelen = strlen(buf); if(buf[0] == '[') { strncpy(section, &buf[1], linelen - 2); section[linelen - 2] = '\0'; continue; } if(strcmp(reqsection, section) == 0) { if(hidekeys == 1) { // Split by =, grab col 1 (not 0) split(buf, '=', 1, print); // Trim leading and trailing whitespace trim(print, print); printf("%s\n", print); } else { printf("%s\n", buf); } } } fclose(fd); return 0; }