summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/common.c113
-rw-r--r--src/common.h2
-rw-r--r--src/config.c234
-rw-r--r--src/config.h43
-rw-r--r--src/main.c57
5 files changed, 86 insertions, 363 deletions
diff --git a/src/common.c b/src/common.c
index b7abb03..c8b8d9a 100644
--- a/src/common.c
+++ b/src/common.c
@@ -17,6 +17,16 @@
#include "common.h"
+char* trim(char* str) {
+ char* end = &str[strlen(str)];
+ while(str[0] == ' ' || str[0] == '\t')
+ str++;
+ while(end[0] == ' ' || end[0] == '\t' || end[0] == '\n' || end[0] == '\0')
+ end--;
+ end[1] = '\0';
+ return str;
+}
+
/**
* Determins the text editor that should be used. Checks the environmental
* variable EDITOR for this.
@@ -34,108 +44,6 @@ void get_user_editor(char* editor) {
}
}
-
-/**
- * Converts an integer to its equivelant char array
- *
- * @param i The integer to be converted
- * @param out Char array pointer to the output variable. Must be large enough
- * to hold the output or unpredictable behavior may result.
- *
- * @return int The char count, not including the terminating null byte char
- */
-int itoc(int num, char* out) {
- int count = 0;
-
- int tmp_num = num;
- // Get a char count. This is necessary becasue the numbers come out
- // backwards. We need to start from the end and work back.
- while(tmp_num > 0) {
- //int n = tmp_num%10;
- tmp_num /= 10;
- count++;
- }
-
- int out_count = count;
-
- // Set the last char as a null byte
- out[count] = '\0';
- count--;
-
- while(count >= 0) {
- int n = num%10;
-
- // Add 48 to the int to get its char equivelant ascii index
- out[count] = n + 48;
-
- num /= 10;
- count--;
- }
-
- return out_count;
-}
-
-
-/**
- * Returns the current localtime in format YYYYMMDD.HHmmss. Useful for "random"
- * non-conflicting filenames.
- *
- * @param date Output variable for the date string. The output variable must
- * have space for the date to be stored, otherwise unexpected
- * behavior may occur.
- */
-void get_current_date_time(char* date) {
- time_t rawtime;
- time(&rawtime);
- struct tm* now = localtime(&rawtime);
-
- // Date
- // Year
- int year = now->tm_year + 1900;
- char c_year[5];
- itoc(year, c_year);
- strcpy(date, c_year);
-
- // Month
- int mon = now->tm_mon + 1;
- char c_mon[3];
- itoc(mon, c_mon);
- if(mon < 10) { strcat(date, "0"); }
- strcat(date, c_mon);
-
- // Day
- int day = now->tm_mday;
- char c_day[3];
- if(day < 10) { strcat(date, "0"); }
- strcat(date, c_day);
-
- // A delimiter to make it easier for humans to read.
- strcat(date, ".");
-
- // Time
- // Hour
- int hour = now->tm_hour;
- char c_hour[3];
- itoc(hour, c_hour);
- if(hour < 10) { strcat(date, "0"); }
- strcat(date, c_hour);
-
- // Minute
- int min = now->tm_min;
- char c_min[3];
- itoc(min, c_min);
- if(min < 10) { strcat(date, "0"); }
- strcat(date, c_min);
-
- // Second
- int sec = now->tm_sec;
- char c_sec[3];
- itoc(sec, c_sec);
- if(sec < 10) { strcat(date, "0"); }
- strcat(date, c_sec);
-}
-
-
/**
* TODO: Write this function description
*
@@ -325,4 +233,3 @@ int strip_extension(char* filename) {
}
return -1;
}
-
diff --git a/src/common.h b/src/common.h
index 1d5632f..0b63ef7 100644
--- a/src/common.h
+++ b/src/common.h
@@ -22,6 +22,8 @@
#ifndef noteless_common
#define noteless_common
+char* trim(char*);
+
void get_user_editor(char*);
int itoc(int, char*);
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);
}
diff --git a/src/config.h b/src/config.h
index 36e535e..9f8072d 100644
--- a/src/config.h
+++ b/src/config.h
@@ -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>
*
* 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
@@ -18,37 +18,20 @@
*/
#ifndef NOTELESS_CONFIG_H
#define NOTELESS_CONFIG_H
-#define config_line_len 120
-#define config_max 256
-
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
-
-/**
- * Represents a config file in memory. Provides a friendly interface for
- * checking configuration variables without having to parse multiple times.
- */
-typedef struct config {
- int count;
- char path[256];
- char extension[16];
- char* keys[config_max];
- char* values[config_max];
-} config_t;
-
-int config_load(config_t*, char*);
-
-void config_free(config_t*);
-
-int config_isset(config_t*, char*);
-
-char* config_get(config_t*, char*);
-
-int config_set(config_t*, char*, char*);
-
-int config_valid_line_count(char*);
-
-int config_add(config_t*, char*, char*);
+#include "common.h"
+
+struct config {
+ char notepath[512];
+ char editor[512];
+ char extension[32];
+};
+
+void config_init(struct config*);
+int config_read(struct config*, char*);
+int config_linekey(char*, char*);
+char* config_lineval(char*);
#endif
diff --git a/src/main.c b/src/main.c
index aa51cdb..119bdac 100644
--- a/src/main.c
+++ b/src/main.c
@@ -78,36 +78,25 @@ void list_notes(note_list* list) {
}
+
/**
- * Sets up environment variables.
+ * Set up environment variables.
* Handles loading in configuration defaults before loading the config file
* values if one is present.
*/
-void config_setup(config_t* c) {
- // Get the home dir
- char home_path[strlen(getenv("HOME"))];
- strcpy(home_path, getenv("HOME"));
+void config_setup(struct config *c) {
+ char cpath[1024];
+ config_init(c);
// Set default note path
- char tmp_note_path[strlen(home_path) + 17];
- strcpy(tmp_note_path, home_path);
- strcat(tmp_note_path, "/Documents/Notes");
- config_set(c, "note_path", tmp_note_path);
-
+ sprintf(c->notepath, "%s/%s", getenv("HOME"), "/Documents/Notes");
// Set the default note extension
- config_set(c, "extension", "mdown");
-
- // Set editor default
- char tmp_editor[128];
- get_user_editor(tmp_editor);
- config_set(c, "editor", tmp_editor);
-
- // Assemble the path to the conf file
- char conf_path[strlen(home_path) + 23];
- strcpy(conf_path, home_path);
- strcat(conf_path, "/.config/noteless.conf");
- // Load the actual config file to override any defaults
- config_load(c, conf_path);
+ strcpy(c->extension, "adoc");
+ // Set the default note extension
+ strcpy(c->editor, "vim");
+
+ sprintf(cpath, "%s/.config/noteless.conf", getenv("HOME"));
+ config_read(c, cpath);
}
@@ -119,33 +108,25 @@ int main(int argc, char* argv []) {
return 1;
}
- config_t c;
+ struct config c;
note_list list;
-
- //
- // Config setup
- //
- config_setup(&c);
- note_list_new(
- &list,
- config_get(&c, "note_path"),
- config_get(&c, "extension")
- );
+ config_setup(&c);
+ note_list_new(&list, c.notepath, c.extension);
if(errno != 0) { return 1; }
if(strcmp(argv[1], "ls") == 0 || strcmp(argv[1], "list") == 0) {
list_notes(&list);
} else if(strcmp(argv[1], "new") == 0) {
- int create_status = note_list_create_note(&list, config_get(&c, "editor"), argv[2]);
+ int create_status = note_list_create_note(&list, c.editor, argv[2]);
// Notify user that a note already exists by the specified name
if(create_status == 1) {
printf("A note by the name %s already exists.\n", argv[2]);
return create_status;
}
} else if(strcmp(argv[1], "edit") == 0) {
- return note_list_edit(&list, config_get(&c, "editor"), argv[2]);
+ return note_list_edit(&list, c.editor, argv[2]);
} else if(strcmp(argv[1], "rm") == 0) {
int rm_status = note_list_rm_note(&list, argv[2]);
if(rm_status == 0) {
@@ -163,7 +144,7 @@ int main(int argc, char* argv []) {
} else if(strcmp(argv[1], "help") == 0) {
get_help();
} else {
- int err = note_list_edit(&list, config_get(&c, "editor"), argv[1]);
+ int err = note_list_edit(&list, c.editor, argv[1]);
if(err == 1) {
printf("%s is also not a valid command.\n", argv[1]);
return 1;
@@ -172,7 +153,5 @@ int main(int argc, char* argv []) {
// Clean up
note_list_free(&list);
- config_free(&c);
-
return 0;
}

Generated by cgit