1 /**
2 * Copyright (C) 2016 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 "note_list.h"
18
19 /**
20 * Constructor
21 *
22 * @param list List stream to be instantiated
23 * @param path Path to the treasure... er... notes
24 * @param ext Extension of the notes to pay attention to
25 */
26 int note_list_new(note_list* list, char* path, char* ext) {
27 list->cursor = 0;
28 // Open the dir fd
29 list->dirp = opendir(path);
30 if(list->dirp == NULL) {
31 printf("Error: Could not open path '%s'.\n", path);
32 return 1;
33 }
34 list->noteent = malloc(sizeof(note));
35 strcpy(list->path, path);
36 strcpy(list->ext, ext);
37
38 // Skip the first two, as they are always . and ..
39 readdir(list->dirp);
40 readdir(list->dirp);
41
42 return 0;
43 }
44
45
46 void note_list_free(note_list* list) {
47 closedir(list->dirp);
48 }
49
50
51 note* note_list_read(note_list* list) {
52 struct dirent* de;
53 char fullpath[256];
54 char fext[64];
55
56 // Iterrate over dir entities
57 while(1) {
58 // Return null if readdir also returns null
59 if((de = readdir(list->dirp)) == NULL) { return NULL; }
60 // Skip any non-regular files (DT_REG == 8)
61 if(de->d_type != 8) { continue; }
62 // Skip hidden files
63 if(de->d_name[0] == '.') { continue; }
64
65 // Skip files that don't match the specified extension
66 get_extension(de->d_name, fext);
67 if(strcmp(fext, list->ext) != 0) { continue; }
68
69 break;
70 }
71
72 // Construct the note full path
73 sprintf(fullpath, "%s/%s", list->path, de->d_name);
74 // Create the note entity
75 note_new(list->noteent, fullpath);
76
77 // Increment the file counter
78 ++list->cursor;
79 return list->noteent;
80 }
81
82
83 /**
84 * Rewinds the note_list directory descriptior to zero. Basically executes
85 * rewinddir and reset the note cursor.
86 *
87 * @param list Note list to rewind
88 */
89 void note_list_rewind(note_list* list) {
90 // Reset to dir fd to index 0
91 rewinddir(list->dirp);
92 // Set list cursor to 0
93 list->cursor = 0;
94 }
95
96
97 /**
98 * Seeks the note with the specified index (starting at 1).
99 *
100 * @param list Note list to seek through
101 * @param index Integer index to seek to
102 */
103 note* note_list_seek(note_list* list, int index) {
104 // Reset dir to 0
105 note_list_rewind(list);
106
107 while(list->cursor < index) {
108 list->noteent = note_list_read(list);
109 }
110 return list->noteent;
111 }
112
113
114 /**
115 * Opens the specified note for editing
116 *
117 * @param list Note list the note-to-be-edited belongs to
118 * @param editor Path to the editor to use for editing
119 * @param term Search term
120 */
121 int note_list_edit(note_list* list, char* editor, char* term) {
122 note* n;
123
124 while(1) {
125 n = note_list_read(list);
126 if(n == NULL) { break; }
127
128 if(strncmp(n->name, term, strlen(term)) == 0) {
129 file_edit(n->path, editor);
130 return 0;
131 }
132 }
133
134 printf("No notes exist that match the search term '%s'.\n", term);
135 return 1;
136 }
137
138
139 /**
140 * Opens a new note for editing
141 *
142 * @param string editor Path to the editor to use for editing
143 * @param string note_name Name of the note to be created and edited
144 *
145 * @return int Success or failure (always success for now)
146 */
147 int note_list_create_note(note_list* list, char* editor, char* name) {
148 char fullname[128];
149 char fullpath[256];
150 note* n;
151
152 // Create full file name
153 sprintf(fullname, "%s.%s", name, list->ext);
154
155 // Check if note already exists
156 while((n = note_list_read(list)) != NULL) {
157 if(strcmp(n->name, fullname) == 0) { return 1; }
158 }
159
160 // Construct full note path for the new note
161 sprintf(fullpath, "%s/%s", list->path, fullname);
162 file_edit(fullpath, editor);
163 return 0;
164 }
165
166
167 /**
168 * Returns the contents of the requested note
169 *
170 * @param string note_name Name of the note to get the contents for
171 *
172 * @return vector<string> Contents of the note, broken per line into a vector.
173 */
174 int note_list_cat_note(note_list* list, char* term) {
175 note* n;
176 // Check for the requested note
177 while((n = note_list_read(list)) != NULL) {
178 if(strncmp(n->name, term, strlen(term)) == 0) {
179 note_cat(n);
180 return 0;
181 }
182 }
183 return 1;
184 }
185
186
187 /**
188 * Searches each note in the list for the given search term.
189 *
190 * @return int Number of instances found in all notes
191 * TODO: This needs to be written still
192 */
193 int note_list_search(note_list* list, char* term, int case_insensitive) {
194 note* n;
195
196 while((n = note_list_read(list)) != NULL) {
197 note_search(n, term, case_insensitive);
198 }
199 return 0;
200 }
201
202
203 /**
204 * Deletes the specified note
205 *
206 * @param string note_name Name of the note to be deleted
207 *
208 * @return int Exit code (1 = error, 0 = success)
209 */
210 int note_list_rm_note(note_list* list, char* name) {
211 note* n;
212
213 while((n = note_list_read(list)) != NULL) {
214 if(strncmp(n->name, name, strlen(name)) == 0) {
215 return note_rm(n);
216 }
217 }
218 return 1;
219 }
|