1 /**
2 * Copyright (C) 2014 Aaron Ball <nullspoon@iohq.net>
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_t* note, char* path) {
23 strcpy(note->path, path);
24 basename(path, note->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_t* note, char* editor) {
36 return note_edit(note, editor);
37 }
38
39
40 /**
41 * Opens a note using the specified editor
42 *
43 * @param string editor Path to the editor binary to use for editing.
44 *
45 * @return int Success or failure of the command
46 */
47 int note_edit(note_t* note, char* editor) {
48 char cmd[strlen(editor) + 3 + strlen(note->path)];
49 strcpy(cmd, editor);
50 strcat(cmd, " ");
51 strcat(cmd, note->path);
52 return system(cmd);
53 }
54
55
56 /**
57 * Prints the contents of the specified note.
58 *
59 * @return int Status of the cat operation.
60 * 0 Success
61 * -1 File could not be opened
62 */
63 int note_cat(note_t* note) {
64 FILE* f = fopen(note->path, "r");
65 // Return early if the file could not be opened
66 if(!f) { return -1; }
67
68 char c;
69 // Print the contents of the note
70 while((c = fgetc(f)) != EOF) {
71 printf("%c", c);
72 }
73
74 // Close up shop
75 fclose(f);
76
77 printf("\n");
78
79 return 0;
80 }
81
82
83 /**
84 * Deletes the current note instance from the filesystem.
85 *
86 * @return int Success or failure
87 */
88 int note_rm(note_t* note) {
89 // Verify file exists.
90 FILE* f = fopen(note->path, "r");
91 if(f) {
92 return remove(note->path);
93 fclose(f);
94 } else {
95 return 1;
96 }
97 }
98
99 /**
100 * Gets the filename of the current note instance without its extension.
101 *
102 * @return string Note filename without extension
103 */
104 // string note::friendly_name() {
105 // return name.substr( 0, name.length() - extension.length() - 1 );
106 // }
107
108 /**
109 * TODO
110 */
111 int note_search(note_t* note, char* term, int case_insensitive) {
112 FILE* f = fopen(note->path, "r");
113
114 char buf[file_width];
115 int line_num = 0;
116 while((fgets(buf, file_width, f)) != NULL) {
117 if(str_contains(buf, term, case_insensitive) == 1) {
118 printf("%s: %02d: %s\n", note->name, line_num, buf);
119 }
120 line_num++;
121 }
122 fclose(f);
123 return 0;
124 }
|