summaryrefslogtreecommitdiff
path: root/src/config.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/config.c')
-rw-r--r--src/config.c234
1 files changed, 43 insertions, 191 deletions
diff --git a/src/config.c b/src/config.c
index 2661f4c..111a8b4 100644
--- a/src/config.c
+++ b/src/config.c
@@ -1,7 +1,7 @@
/**
* A class to help with parsing standard config files
*
- * Copyright (C) 2016 Aaron Ball <nullspoon@oper.io>
+ * Copyright (C) 2021 Aaron Ball <nullspoon@oper.io>
*
* Noteless is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@@ -18,203 +18,55 @@
*/
#include "config.h"
-/**
- * Constructor for the config class. Reads config file into a vector (one line
- * per index), then parses each line into a vector size 2x.
- *
- * @param const char* p Path to the config file to be read in
- */
-int config_load(config_t* conf, char* p) {
- // TODO
- strcpy(conf->path, p);
-
- int line_count = config_valid_line_count(p);
-
- // The file couldn't be opened or no valid lines found, return -1
- if(line_count == 0) { return -1; }
-
- FILE* f;
- f = fopen(conf->path, "r");
-
- // Verify the file could be opened
- if(!f) {
- // printf("Could not open config file at %s.\n", conf->path);
- return -1;
- }
-
- // Set the maximum read width and read buffer array
- int len = config_line_len;
- char buf[config_line_len] = "";
-
- char key[32];
- char value[256];
-
- while(fgets(buf, len, f) != NULL) {
- int start = 0;
- // This loop allows for lines to not start at the beginning
- while(buf[start] != '\0') {
- if(buf[start] != ' ') {
- break;
- } else {
- start++;
- }
- }
-
- // Skip all lines starting with a comment char
- if(buf[start] == '#') { continue; }
- // Skip empty lines
- if(buf[start] == '\n') { continue; }
-
- int end = start;
- while(buf[end] != '\0') {
- // This allows for inline comment chars. The line ends when the first
- // comment char is found.
- if(buf[end] == '#' || buf[end] == '\n') { break; }
- end++;
- }
-
- char tmp_line[config_line_len] = "";
- // Temp variable to store the text between index start and index end
- strncpy(tmp_line, &buf[start], end - start + 1);
- // Make sure it has a trailing newline so sscanf works
- strcat(tmp_line, "\n");
-
- // Parse the line into temp variables
- sscanf(tmp_line, "%s %s", key, value);
-
- // Set the config key/value
- config_set(conf, key, value);
- }
-
- fclose(f);
-
- return 0;
+void config_init(struct config *c) {
+ c->extension[0] = 0;
+ c->editor[0] = 0;
+ c->notepath[0] = 0;
}
-
-/**
- * Destructor for config struct.
- *
- * @param conf Config_t pointer to be freed from memory
- */
-void config_free(config_t* conf) {
- for(int i = 0; i < conf->count; i++) {
- free(conf->keys[i]);
- free(conf->values[i]);
- }
-}
-
-
-/**
- * Performs a line count of valid lines in the specified config file. This is
- * useful for determining the size of an array to store keys or values in.
- * A valid line is considered to be a line that is not empty or commented out.
- *
- * @param path Path to the config file to be parsed
- *
- * @return int Count of lines that are not commented out or empty
- */
-int config_valid_line_count(char* path) {
- int count = 0;
-
- FILE* f;
- f = fopen(path, "r");
- if(!f) { return -1; }
-
- int len = config_line_len;
- char buf[config_line_len];
-
- while(fgets(buf, len, f) != NULL) {
- int start = 0;
-
- // Find the first char
- while(buf[start] == ' ') { start++; }
-
- // Confirm the line is not a comment or empty
- if(buf[start] != '#' && buf[start] != '\n') { count++; }
- }
- fclose(f);
-
- return count;
-}
-
-
-/**
- * Checks the configuration struct for the given key's value.
- *
- * @param conf Config_t instance pointer containing keys and values to check
- * @param key Key to check if exists
- *
- * @return int Key value exists (1) or not (0)
- */
-int config_isset(config_t* conf, char* key) {
- for(int i = 0; i < conf->count; i++) {
- if(strcmp(conf->keys[i], key) == 0) {
- return 1;
+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 0;
+ }
+ return 1;
}
-
-/**
- * TODO
- */
-char* config_get(config_t* conf, char* key) {
- for(int i = 0; i < conf->count; i++) {
- if(strcmp(conf->keys[i], key) == 0) {
- return conf->values[i];
- }
- }
- return NULL;
+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, " ");
-/**
- * Sets the value of the specified key. If the key exists, its value is reset
- * to the value that is passed in. If the key does not exist, the key and value
- * are appended to the end of the key/value arrays.
- *
- * @param conf The config object containing the keys and values
- * @param key Key to set the value for
- * @param value Value to be set for the given key
- *
- * @return Index of the key/value that has been set
- */
-int config_set(config_t* conf, char* key, char* value) {
- for(int i = 0; i < conf->count; i++) {
- if(strcmp(conf->keys[i], key) == 0) {
- // Key exists, reset its value
- strcpy(conf->values[i], value);
- return i;
- }
+ while(start[0] == ' ') {
+ start++;
}
- // Key does not exist, create and add
- char* h_key = malloc(strlen(key) + 1);
- char* h_value = malloc(strlen(value) + 1);
- strcpy(h_key, key);
- strcpy(h_value, value);
- conf->keys[conf->count] = h_key;
- conf->values[conf->count] = h_value;
- conf->count++;
- return (conf->count - 1);
-}
-
-
-/**
- * TODO
- */
-int config_add(config_t* conf, char* key, char* value) {
- // Allocate the new key and value memory
- char* h_key = malloc(strlen(key) + 1);
- char* h_value = malloc(strlen(value) + 1);
- // Assign
- strcpy(h_key, key);
- strcpy(h_value, value);
-
- conf->count++;
- conf->keys[conf->count] = h_key;
- conf->values[conf->count] = h_value;
-
- return conf->count;
+ // If we hit the end of the line, no value was specified
+ if(start[0] == '\0' || start[0] == '\n')
+ return NULL;
+ return trim(start);
}

Generated by cgit