summaryrefslogtreecommitdiff
path: root/ini.c
diff options
context:
space:
mode:
authorAaron Ball <nullspoon@oper.io>2018-05-18 08:21:56 -0600
committerAaron Ball <nullspoon@oper.io>2018-05-18 08:24:35 -0600
commit9de9c5bb26bc73a5b45b43b1c55cff67b1be17af (patch)
tree47ad0da7332661836028f339e86e513e6ad37114 /ini.c
downloadcini-master.tar.gz
cini-master.tar.xz
Initial commitHEADmaster
Will return ini sections, with and without keys (--no-keys). Will also return all lines with matching keys, regardless of section. All source files contain GPLv3 license header
Diffstat (limited to 'ini.c')
-rw-r--r--ini.c83
1 files changed, 83 insertions, 0 deletions
diff --git a/ini.c b/ini.c
new file mode 100644
index 0000000..90f6a56
--- /dev/null
+++ b/ini.c
@@ -0,0 +1,83 @@
+// 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 "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;
+}

Generated by cgit