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 strcpy(n->name, basename(path));
25
26 // Get file stats
27 struct stat s;
28 stat(path, &s);
29 // Last modified time
30 n->mtime = s.st_mtime;
31 }
32
33
34 /**
35 * Opens a new note using the specified editor.
36 *
37 * @param string editor Path to the editor binary to use for editing.
38 *
39 * @return int Success or failure of the command
40 */
41 int note_create(note* n, char* editor) {
42 return file_edit(n->path, editor);
43 }
44
45
46 /**
47 * Performs a date comparison on the two note structs. Returns an integer
48 * representing in seconds if struct b was modified more recently (positive
49 * int) or less recently (negative int) than struct a. If the two were modified
50 * on the same date and time, 0 is returned.
51 *
52 * @param a Note struct to compare last modified date
53 * @param b Note struct to compare last modified date
54 */
55 int note_date_cmp(const void* a, const void* b) {
56 note* note_a = (note*) a;
57 note* note_b = (note*) b;
58
59 return (int)(note_a->mtime - note_b->mtime);
60 }
61
62
63 /**
64 * Copies the contents of one note struct into the pointer to the second note
65 * struct.
66 *
67 * @param dest Destination note struct
68 * @param src Source note struct
69 */
70 void note_cpy(note* dest, note* src) {
71 strcpy(dest->extension, src->extension);
72 strcpy(dest->path, src->path);
73 strcpy(dest->name, src->name);
74 dest->mtime = src->mtime;
75 }
76
77
78 /**
79 * Prints the contents of the specified note.
80 *
81 * @return int Status of the cat operation.
82 * 0 Success
83 * -1 File could not be opened
84 */
85 int note_cat(note* n) {
86 FILE* f = fopen(n->path, "r");
87 // Return early if the file could not be opened
88 if(!f) { return -1; }
89
90 char c;
91 // Print the contents of the note
92 while((c = fgetc(f)) != EOF) {
93 printf("%c", c);
94 }
95
96 // Close up shop
97 fclose(f);
98
99 printf("\n");
100
101 return 0;
102 }
103
104
105 /**
106 * Deletes the current note instance from the filesystem.
107 *
108 * @return int Success or failure
109 */
110 int note_rm(note* n) {
111 // Verify file exists.
112 FILE* f = fopen(n->path, "r");
113 if(f) {
114 return remove(n->path);
115 fclose(f);
116 } else {
117 return 1;
118 }
119 }
120
121
122 /**
123 * TODO
124 */
125 int note_search(note* n, char* term, int case_insensitive) {
126 FILE* f = fopen(n->path, "r");
127
128 char buf[file_width];
129 int line_num = 1;
130 while((fgets(buf, file_width, f)) != NULL) {
131 if(str_contains(buf, term, case_insensitive) == 1) {
132 printf("%s: % 4d: %s", n->name, line_num, buf);
133 }
134 line_num++;
135 }
136 fclose(f);
137 return 0;
138 }
|