/** * A class to help with parsing standard config files * * Copyright (C) 2021 Aaron Ball * * Noteless 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. * * Noteless 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 noteless. If not, see . */ #include "config.h" void config_init(struct config *c) { c->extension[0] = 0; c->editor[0] = 0; c->notepath[0] = 0; } int config_read(struct config* c, char* path) { char linebuf[1024] = {0}; char keybuf[1024] = {0}; FILE* fd; fd = fopen(path, "r"); if(!fd) return -2; while(fgets(linebuf, 1024, fd) != NULL) { if(linebuf[0] == '#') continue; if(config_linekey(linebuf, keybuf) == 1) { if(strcmp(keybuf, "extension") == 0) { strcpy(c->extension, config_lineval(linebuf)); } else if(strcmp(keybuf, "path") == 0) { strcpy(c->notepath, config_lineval(linebuf)); } else if(strcmp(keybuf, "editor") == 0) { strcpy(c->editor, config_lineval(linebuf)); } } } return 1; } int config_linekey(char* line, char* outbuf) { char* keyend = strchr(line, ' '); if(!keyend) return -1; strncpy(outbuf, line, keyend - line); outbuf[keyend - line] = '\0'; return 1; } char* config_lineval(char* line) { char* start = strstr(line, " "); while(start[0] == ' ') { start++; } // If we hit the end of the line, no value was specified if(start[0] == '\0' || start[0] == '\n') return NULL; return trim(start); }