1 /**
2 * Copyright (C) 2021 Aaron Ball <nullspoon@oper.io>
3 *
4 * Noteless is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * Noteless is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with Noteless. If not, see <http://www.gnu.org/licenses/>.
16 */
17 #include "common.h"
18
19
20 char* trim(char* str) {
21 char* end = &str[strlen(str)];
22 while(str[0] == ' ' || str[0] == '\t')
23 str++;
24 while(end[0] == ' ' || end[0] == '\t' || end[0] == '\n' || end[0] == '\0')
25 end--;
26 end[1] = '\0';
27 return str;
28 }
29
30 /**
31 * Determins the text editor that should be used. Checks the environmental
32 * variable EDITOR for this.
33 *
34 * @return char* Path to the editor binary to be used (stored in heap)
35 */
36 void get_user_editor(char* editor) {
37 if(getenv("EDITOR") != NULL) {
38 /* If the EDITOR variable is set, use that */
39 //int editor_len = strlen(getenv("EDITOR"));
40 // *editor = calloc(editor_len, sizeof(char));
41 strcpy(editor, getenv("EDITOR"));
42 } else {
43 strcpy(editor, "vi");
44 }
45 }
46
47 /**
48 * TODO: Write this function description
49 *
50 * @return int Extension char length
51 */
52 int get_extension(char* path, char* extension) {
53 int path_len = strlen(path);
54 // Start from the right and move left
55 for(int i = path_len; i > 0; i--) {
56 // Copy if an extension delimiter (a period) is found
57 if(path[i] == '.') {
58 int ext_len = path_len - i;
59 strncpy(extension, &path[i+1], ext_len);
60 return ext_len;
61 }
62 }
63 return -1;
64 }
65
66
67 /**
68 * Opens a note using the specified editor
69 *
70 * @param string editor Path to the editor binary to use for editing.
71 *
72 * @return int Success or failure of the command
73 */
74 int file_edit(char* path, char* editor) {
75 char cmd[strlen(editor) + 3 + strlen(path)];
76 sprintf(cmd, "%s %s", editor, path);
77 return system(cmd);
78 }
79
80 /**
81 * Just a wrapper function to call the specified line search function for case
82 * insensitive or case sensitive matching.
83 *
84 * @param line Line to check for the search term
85 * @param term Search term to find in line
86 *
87 * @return int Search term found (1) or not (0)
88 */
89 int str_contains(char* line, char* term, int case_insensitive) {
90 if(case_insensitive == 1) {
91 if(strcasestr(line, term) != NULL)
92 return 1;
93 } else {
94 if(strstr(line, term) != NULL)
95 return 1;
96 }
97 return 0;
98 }
99
100 /**
101 * Removes the extension from the given string filename
102 *
103 * @param filename Filename/path to strip extension from
104 *
105 * @return int Result of the strip operation
106 */
107 int strip_extension(char* filename) {
108 for(int i = strlen(filename); i > 0; i--) {
109 if(filename[i] == '.') {
110 filename[i] = '\0';
111 return 0;
112 }
113 }
114 return -1;
115 }
|