summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAaron Ball <nullspoon@iohq.net>2015-04-25 13:20:16 -0600
committerAaron Ball <nullspoon@iohq.net>2015-04-25 13:20:16 -0600
commit966fd29cedccd9de3f8cc2670e9d77c182c5ee6a (patch)
tree9240cfca07711a6a49d3e6a2d26b17e17fd75b12
parentd1ab070dd1b7902356b74f192eb0fbecb6437b51 (diff)
downloadnoteless-966fd29cedccd9de3f8cc2670e9d77c182c5ee6a.tar.gz
noteless-966fd29cedccd9de3f8cc2670e9d77c182c5ee6a.tar.xz
Rewrote the innards of the config struct
Previously, a nasty hybrid of stack variables in main() and heap variables in the config struct, chained together with a series of "if isset" statements, was being used to gather the config environment. This change allows everything to be stored in the config object in heap, using a simple set function which handles all of the "isset" logic in the config struct. Updated the main function to work with the new config method Cleaned up much commented out code Removed a few config-related errors Fixed the config free, as it was causing segfaults trying to free memory allocated in the stack
-rw-r--r--src/config.c119
-rw-r--r--src/config.h12
-rw-r--r--src/main.c90
3 files changed, 90 insertions, 131 deletions
diff --git a/src/config.c b/src/config.c
index c3509cc..3453fa7 100644
--- a/src/config.c
+++ b/src/config.c
@@ -24,21 +24,21 @@
*
* @param const char* p Path to the config file to be read in
*/
-int config_new(config_t* conf, char* p) {
+int config_load(config_t* conf, char* p) {
// TODO
strcpy(conf->path, p);
- conf->count = config_valid_line_count(p);
+ int line_count = config_valid_line_count(p);
- conf->keys = malloc(sizeof(char*) * conf->count);
- conf->values = malloc(sizeof(char*) * conf->count);
+ // 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);
+ // printf("Could not open config file at %s.\n", conf->path);
return -1;
}
@@ -49,7 +49,6 @@ int config_new(config_t* conf, char* p) {
char key[32];
char value[256];
- int i = 0;
while(fgets(buf, len, f) != NULL) {
int start = 0;
// This loop allows for lines to not start at the beginning
@@ -74,7 +73,6 @@ int config_new(config_t* conf, char* p) {
end++;
}
- //char tmp_line[end - start + 10];
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);
@@ -84,19 +82,8 @@ int config_new(config_t* conf, char* p) {
// Parse the line into temp variables
sscanf(tmp_line, "%s %s", key, value);
- // Allocate heap memory for values since these need to persist.
- // Note that here we are using the temp variables to establish string
- // length for when we allocate heap memory.
- conf->keys[i] = malloc(sizeof(char) * (strlen(key) + 1));
- conf->values[i] = malloc(sizeof(char) * (strlen(value) + 1));
-
- // Copy in the temp values
- strcpy(conf->keys[i], key);
- strcpy(conf->values[i], value);
-
- // Increment the counter for tracking keys and values in their respective
- // arrays
- i++;
+ // Set the config key/value
+ config_set(conf, key, value);
}
fclose(f);
@@ -115,8 +102,6 @@ void config_free(config_t* conf) {
free(conf->keys[i]);
free(conf->values[i]);
}
- free(conf->keys);
- free(conf->values);
}
@@ -134,6 +119,7 @@ int config_valid_line_count(char* path) {
FILE* f;
f = fopen(path, "r");
+ if(!f) { return -1; }
int len = config_line_len;
char buf[config_line_len];
@@ -170,6 +156,7 @@ int config_isset(config_t* conf, char* key) {
return 0;
}
+
/**
* TODO
*/
@@ -184,50 +171,50 @@ char* config_get(config_t* conf, char* key) {
/**
- * Parses one line of the config file. Strips illegal characters first, then
- * sets key to the text on the left of the equals sign and value to the text on
- * the right.
+ * 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.
*
- * string line - Config file line to be parsed and added to the config object
+ * @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 int Success or failure
+ * @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;
+ }
+ }
+ // 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::parse_line( string line ) {
-// // The two vars that will be appended to the key value vector
-// string key;
-// string value;
-// int split;
-//
-// // Clean spaces and other illegal chars
-// string clean_line;
-// for( int i = 0; i < line.length(); i++ ) {
-// if( line[i] == ' ' ) {
-// // Ignore, it's a space
-// continue;
-// } else {
-// clean_line += line[i];
-// }
-// }
-//
-// // Line was empty, skip 'er
-// if( clean_line.length() == 0 ) { return 0; }
-//
-// // One character at a time
-// for( int i = 0; i < clean_line.length(); i++ ) {
-// if( clean_line[i] == '=' ) {
-// // Set the key
-// key = clean_line.substr( 0, i );
-// split = i;
-// break;
-// }
-// }
-// // Set the value
-// value = clean_line.substr( split + 1, clean_line.length() );
-//
-// // Append to class global values
-// values.push_back( key );
-// values.push_back( value );
-//
-// // Success!
-// return 0;
-// }
+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;
+}
diff --git a/src/config.h b/src/config.h
index 295969d..d0cc9cb 100644
--- a/src/config.h
+++ b/src/config.h
@@ -19,6 +19,7 @@
#ifndef NOTELESS_CONFIG_H
#define NOTELESS_CONFIG_H
#define config_line_len 120
+#define config_max 128
#include <stdlib.h>
#include <stdio.h>
@@ -31,13 +32,12 @@
typedef struct config {
int count;
char path[256];
- char note_path[256];
char extension[16];
- char** keys;
- char** values;
+ char* keys[config_max];
+ char* values[config_max];
} config_t;
-int config_new(config_t*, char*);
+int config_load(config_t*, char*);
void config_free(config_t*);
@@ -45,8 +45,10 @@ 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 parse_line(char*);
+int config_add(config_t*, char*, char*);
#endif
diff --git a/src/main.c b/src/main.c
index f5558ff..f24a4bd 100644
--- a/src/main.c
+++ b/src/main.c
@@ -20,8 +20,6 @@
#include "common.h"
#include "config.h"
#include "note_list.h"
-// #include "note.h"
-// #include "path.h"
/**
* Prints the standard help text
@@ -63,20 +61,8 @@ void list_notes(note_list_t* list) {
/**
- * Searches all note contents for the specified text
+ * Ye olde main function
*/
-// int search_notes( note_list list, string term ) {
-// vector<string> matches = list.find( true, term );
-// for( int i = 0; i < matches.size(); i++ ) {
-// cout << matches[i] << endl;
-// }
-// if( matches.size() == 0 ) {
-// return 1;
-// }
-// return 0;
-// }
-
-
int main(int argc, char* argv[]) {
// Print helptext if no commands specified
if(argc == 1) {
@@ -85,59 +71,43 @@ int main(int argc, char* argv[]) {
return 1;
}
- /**
- * Config variables
- */
+ // Get the home dir
char home_path[strlen(getenv("HOME"))];
strcpy(home_path, getenv("HOME"));
- /* Default path to the note store */
- char* note_path = NULL;
-
- /* Default note extension */
- char* note_ext = "mdown";
-
- /* A little editor detection */
- char* editor = NULL;
-
/**
* Config file overrides
*/
- char conf_path[strlen(home_path) + 23];
- strcpy(conf_path, home_path);
- strcat(conf_path, "/.config/noteless.conf");
-
config_t c;
- int config_status = config_new(&c, conf_path);
- if(config_status == -1) {
- printf("Config file could not be found.\n");
- }
- // Override where notes are to be stored
- // The local version of note path, in case it isn't set in the config
+ // 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);
- if(config_isset(&c, "path") == 1) {
- note_path = config_get(&c, "path");
- } else {
- strcpy(tmp_note_path, home_path);
- strcat(tmp_note_path, "/Documents/Notes");
- note_path = tmp_note_path;
- }
-
- // Override the extension used by the notes
- if(config_isset(&c, "extension")) {
- note_ext = config_get(&c, "extension");
- }
+ // Set the default note extension
+ config_set(&c, "extension", "mdown");
- // Override editor settings
+ // Set editor default
char tmp_editor[128];
- if(config_isset(&c, "editor")) {
- editor = config_get(&c, "editor");
- } else {
- get_user_editor(tmp_editor);
- editor = tmp_editor;
- }
+ 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);
+
+ // Just some literal code comments
+ // int config_status = config_load(&c, conf_path);
+ // if(config_status == -1) {
+ // printf("Config file could not be opened or does not exist.\n");
+ // printf("Assuming defaults.\n");
+ // }
+
// // If the init command was passed, we want to init before checking if the
// // note path exists, otherwise the error will display and the store will
@@ -162,19 +132,19 @@ int main(int argc, char* argv[]) {
note_list_t list;
- note_list_new(&list, note_path, note_ext);
+ note_list_new(&list, config_get(&c, "note_path"), config_get(&c, "extension"));
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, editor, argv[2]);
+ int create_status = note_list_create_note(&list, config_get(&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, editor, argv[2]);
+ return note_list_edit(&list, config_get(&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) {
@@ -193,7 +163,7 @@ int main(int argc, char* argv[]) {
get_help();
} else if(note_list_get_note_id(&list, argv[1]) != -1 ) {
// Try to open the note if it exists
- return note_list_edit(&list, editor, argv[1]);
+ return note_list_edit(&list, config_get(&c, "editor"), argv[1]);
} else {
printf("Error: Unknown command or note name '%s'.\n", argv[1]);
return 1;

Generated by cgit