summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAaron Ball <nullspoon@oper.io>2021-08-18 12:42:00 -0600
committerAaron Ball <nullspoon@oper.io>2021-08-18 20:38:29 -0600
commit436d26d45c92425b5dfcae32bc7ae3828c2715fe (patch)
tree55ce5b8e853112853efc977df9714025d17bbefd
parent2228b04e946babd30bd27f1241e6aa12ed68048c (diff)
downloadnoteless-436d26d45c92425b5dfcae32bc7ae3828c2715fe.tar.gz
noteless-436d26d45c92425b5dfcae32bc7ae3828c2715fe.tar.xz
Refactor common note searching
This replaces the custom "str contains str [insensitive|sensitive]" functions with the standard `strstr` and `strcasestr` (removing over 100 lines of code). This also replaces the custom basename function in common with the standard basename in libgen.h.
-rw-r--r--src/common.c126
-rw-r--r--src/common.h14
-rw-r--r--src/main.c1
-rw-r--r--src/note.c2
4 files changed, 9 insertions, 134 deletions
diff --git a/src/common.c b/src/common.c
index c8b8d9a..2a9232b 100644
--- a/src/common.c
+++ b/src/common.c
@@ -77,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.
@@ -110,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
*
diff --git a/src/common.h b/src/common.h
index 0b63ef7..138ea96 100644
--- a/src/common.h
+++ b/src/common.h
@@ -14,13 +14,15 @@
* 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>
-
-#ifndef noteless_common
-#define noteless_common
+#include <libgen.h>
char* trim(char*);
@@ -34,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/main.c b/src/main.c
index 119bdac..33825d1 100644
--- a/src/main.c
+++ b/src/main.c
@@ -78,7 +78,6 @@ void list_notes(note_list* list) {
}
-
/**
* Set up environment variables.
* Handles loading in configuration defaults before loading the config file
diff --git a/src/note.c b/src/note.c
index 6b42ca4..2f2d2d3 100644
--- a/src/note.c
+++ b/src/note.c
@@ -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;

Generated by cgit