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.h"
18
19 /**
20 * Creates a new empty note object since no filename was specified.
21 */
22 void note_new(note* n, char* path) {
23 strcpy(n->path, path);
24 basename(path, n->name);
25 }
26
27
28 /**
29 * Opens a new note using the specified editor.
30 *
31 * @param string editor Path to the editor binary to use for editing.
32 *
33 * @return int Success or failure of the command
34 */
35 int note_create(note* n, char* editor) {
36 return file_edit(n->path, editor);
37 }
38
39
40 /**
41 * Prints the contents of the specified note.
42 *
43 * @return int Status of the cat operation.
44 * 0 Success
45 * -1 File could not be opened
46 */
47 int note_cat(note* n) {
48 FILE* f = fopen(n->path, "r");
49 // Return early if the file could not be opened
50 if(!f) { return -1; }
51
52 char c;
53 // Print the contents of the note
54 while((c = fgetc(f)) != EOF) {
55 printf("%c", c);
56 }
57
58 // Close up shop
59 fclose(f);
60
61 printf("\n");
62
63 return 0;
64 }
65
66
67 /**
68 * Deletes the current note instance from the filesystem.
69 *
70 * @return int Success or failure
71 */
72 int note_rm(note* n) {
73 // Verify file exists.
74 FILE* f = fopen(n->path, "r");
75 if(f) {
76 return remove(n->path);
77 fclose(f);
78 } else {
79 return 1;
80 }
81 }
82
83
84 /**
85 * TODO
86 */
87 int note_search(note* n, char* term, int case_insensitive) {
88 FILE* f = fopen(n->path, "r");
89
90 char buf[file_width];
91 int line_num = 1;
92 while((fgets(buf, file_width, f)) != NULL) {
93 if(str_contains(buf, term, case_insensitive) == 1) {
94 printf("%s: % 4d: %s", n->name, line_num, buf);
95 }
96 line_num++;
97 }
98 fclose(f);
99 return 0;
100 }
|