diff options
-rw-r--r-- | src/common.c | 241 | ||||
-rw-r--r-- | src/common.h | 16 | ||||
-rw-r--r-- | src/config.c | 234 | ||||
-rw-r--r-- | src/config.h | 43 | ||||
-rw-r--r-- | src/main.c | 58 | ||||
-rw-r--r-- | src/note.c | 4 | ||||
-rw-r--r-- | src/note.h | 2 | ||||
-rw-r--r-- | src/note_list.c | 6 | ||||
-rw-r--r-- | src/note_list.h | 2 |
9 files changed, 102 insertions, 504 deletions
diff --git a/src/common.c b/src/common.c index b7abb03..258edd1 100644 --- a/src/common.c +++ b/src/common.c @@ -1,5 +1,5 @@ /** - * 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 @@ -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 * @@ -169,28 +77,6 @@ int file_edit(char* path, char* editor) { return system(cmd); } - -/** - * Yup, wrote my own. - * TODO: Make this work with escaped path delimiters - * TODO: Write this function description - * - * @return int Basename path length - */ -int basename(char* path, char* out) { - int path_len = strlen(path); - // Start from the right and move left - for(int i = path_len; i > 0; i--) { - // Copy if an extension delimiter (a period) is found - if(path[i] == '/') { - strcpy(out, &path[i + 1]); - return i; - } - } - return -1; -} - - /** * Just a wrapper function to call the specified line search function for case * insensitive or case sensitive matching. @@ -202,113 +88,15 @@ int basename(char* path, char* out) { */ int str_contains(char* line, char* term, int case_insensitive) { if(case_insensitive == 1) { - return str_contains_case_insensitive(line, term); + if(strcasestr(line, term) != NULL) + return 1; } else { - return str_contains_case_sensitive(line, term); - } -} - - -/** - * Performs a case insensitive search in the given line for the specified term. - * - * Note that for the sake of efficiency, this function is fairly confusing - * (sorry). Learning how chars and ints relate in c will help tremendously - * to understand this function. - * - * @param str String to check for the search term - * @param term Search term to find in line - * - * @return int Search term found (1) or not (0) - */ -int str_contains_case_insensitive(char* str, char* term) { - // String char index - int si = 0; - // Term char index - int ti = 0; - int term_len = strlen(term); - - // Compare one char at a time - while(str[si] != '\0') { - // Current str char - char lc; - // Current term char - char tc; - - // Welcome to the confusing part (see function description) - - // Convert current term char to lowercase - if(term[ti] >= 97 && term[ti] <= 122) { - // Already lower case - tc = term[ti]; - } else if(term[ti] >= 65 && term[ti] <= 90) { - // Uppercase. Add 32 to get lowercase equivelant. - tc = term[ti] + 32; - } else { - // Term char isn't a letter. Must be a symbol or something ELSE... - // Take it as it is. - tc = term[ti]; - } - - // Convert current str char to lowercase - if(str[si] >= 97 && str[si] <= 122) { - // Already lower case - lc = str[si]; - } else if(str[si] >= 65 && str[si] <= 90) { - // Uppercase. Add 32 to get lowercase equivelant. - lc = str[si] + 32; - } else { - // Term char isn't a letter. Must be a symbol or something ELSE... - // Take it as it is. - lc = str[si]; - } - - // Compare the temp lowercase version of the current str and term chars - if(lc == tc) { - ti++; - } else { - ti = 0; - } - - // Return if we've matched the entire search term - if(ti == term_len) { return 1; } - - si++; - } - return 0; -} - - -/** - * Performs a case sensitive search in the given string for the specified term. - * - * @param str String to check for the search term - * @param term Search term to find in str - * - * @return int Search term found (1) or not (0) - */ -int str_contains_case_sensitive(char* str, char* term) { - int s = 0; - int t = 0; - int term_len = strlen(term); - - // Compare one char at a time - while(str[s] != '\0') { - if(t == term_len) { + if(strstr(line, term) != NULL) return 1; - } else if(term[t] == str[s]) { - // Found a single char match. Increment to the next term char - t++; - } else { - // Reset the match counter if a partial or no match was made - t = 0; - } - s++; } return 0; } - /** * Removes the extension from the given string filename * @@ -325,4 +113,3 @@ int strip_extension(char* filename) { } return -1; } - diff --git a/src/common.h b/src/common.h index 1d5632f..37c900e 100644 --- a/src/common.h +++ b/src/common.h @@ -1,5 +1,5 @@ /** - * 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 @@ -14,13 +14,17 @@ * You should have received a copy of the GNU General Public License * along with noteless. If not, see <http://www.gnu.org/licenses/>. */ +#ifndef noteless_common +#define noteless_common + +#define _GNU_SOURCE #include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> +#include <libgen.h> -#ifndef noteless_common -#define noteless_common +char* trim(char*); void get_user_editor(char*); @@ -32,14 +36,8 @@ int get_extension(char*, char*); int file_edit(char*, char*); -int basename(char*, char*); - int str_contains(char*, char*, int); -int str_contains_case_sensitive(char*, char*); - -int str_contains_case_insensitive(char*, char*); - int strip_extension(char*); #endif 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 @@ -1,5 +1,5 @@ /** - * 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 @@ -79,35 +79,23 @@ 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 + strcpy(c->extension, "adoc"); // 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->editor, "vim"); + + sprintf(cpath, "%s/.config/noteless.conf", getenv("HOME")); + config_read(c, cpath); } @@ -119,33 +107,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 +143,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 +152,5 @@ int main(int argc, char* argv []) { // Clean up note_list_free(&list); - config_free(&c); - return 0; } @@ -1,5 +1,5 @@ /** - * 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 @@ -21,7 +21,7 @@ */ void note_new(note* n, char* path) { strcpy(n->path, path); - basename(path, n->name); + strcpy(n->name, basename(path)); // Get file stats struct stat s; @@ -1,5 +1,5 @@ /** - * 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 diff --git a/src/note_list.c b/src/note_list.c index f043866..aa29eb2 100644 --- a/src/note_list.c +++ b/src/note_list.c @@ -1,5 +1,5 @@ /** - * 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 @@ -50,7 +50,7 @@ void note_list_free(note_list* list) { note* note_list_read(note_list* list) { struct dirent* de; - char fullpath[256]; + char fullpath[512]; char fext[64]; // Iterrate over dir entities @@ -146,7 +146,7 @@ int note_list_edit(note_list* list, char* editor, char* term) { */ int note_list_create_note(note_list* list, char* editor, char* name) { char fullname[128]; - char fullpath[256]; + char fullpath[512]; note* n; // Create full file name diff --git a/src/note_list.h b/src/note_list.h index ddbb3e7..ee5221b 100644 --- a/src/note_list.h +++ b/src/note_list.h @@ -1,5 +1,5 @@ /** - * 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 |