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 }
25
26
27 /**
28 * Opens a new note using the specified editor.
29 *
30 * @param string editor Path to the editor binary to use for editing.
31 *
32 * @return int Success or failure of the command
33 */
34 int note_create(note_t* note, char* editor) {
35 return note_edit(note, editor);
36 }
37
38
39 /**
40 * Opens a note using the specified editor
41 *
42 * @param string editor Path to the editor binary to use for editing.
43 *
44 * @return int Success or failure of the command
45 */
46 int note_edit(note_t* note, char* editor) {
47 char cmd[strlen(editor) + 3 + strlen(note->path)];
48 strcpy(cmd, editor);
49 strcat(cmd, " ");
50 strcat(cmd, note->path);
51 return system(cmd);
52 }
53
54
55 /**
56 * Prints the contents of the specified note.
57 *
58 * @return int Status of the cat operation.
59 * 0 Success
60 * -1 File could not be opened
61 */
62 int note_cat(note_t* note) {
63 FILE* f = fopen(note->path, "r");
64 // Return early if the file could not be opened
65 if(!f) { return -1; }
66
67 char c;
68 // Print the contents of the note
69 while((c = fgetc(f)) != EOF) {
70 printf("%c", c);
71 }
72
73 // Close up shop
74 fclose(f);
75
76 printf("\n");
77
78 return 0;
79 }
80
81
82 /**
83 * Deletes the current note instance from the filesystem.
84 *
85 * @return int Success or failure
86 */
87 int note_rm(note_t* note) {
88 // Verify file exists.
89 FILE* f = fopen(note->path, "r");
90 if(f) {
91 return remove(note->path);
92 fclose(f);
93 } else {
94 return 1;
95 }
96 }
97
98 /**
99 * Gets the filename of the current note instance without its extension.
100 *
101 * @return string Note filename without extension
102 */
103 // string note::friendly_name() {
104 // return name.substr( 0, name.length() - extension.length() - 1 );
105 // }
106
107 /**
108 * TODO
109 */
110 // vector<string> note::find( bool case_sensitive, string term ) {
111 // vector<string> lines;
112 //
113 // // Memory for storing matched lines
114 // vector<string> matches;
115 //
116 // // Read the notes contents into memory
117 // read();
118 //
119 // for( int i = 0; i < body.size(); i++ ) {
120 // if( line_matches( body[i], term ) == true ) {
121 // // Format the output
122 // string out = friendly_name() + ": " + itos( i );
123 // out += ": " + body[i];
124 // // Add to the match list
125 // matches.push_back( out );
126 // }
127 // }
128 // return matches;
129 // }
130
131 /**
132 * Performs a char by char comparison of a line and a search term. If the
133 * search term is found anywhere within the line even once, returns true;
134 * Otherwise, returns false.
135 *
136 * @param string line The line of text to search
137 * @param string term The search term
138 *
139 * @return bool Whether or not the search term was found in the line
140 */
141 // bool note::line_matches( string line, string term ) {
142 // // Define the two long arrays
143 // long line_l[line.size()];
144 // long term_l[term.size()];
145 //
146 // // Convert the line to a long array
147 // for( int i = 0; i < line.size(); i++ ) { line_l[i] = line[i]; }
148 //
149 // // Convert the search term to a long array
150 // for( int i = 0; i < term.size(); i++ ) { term_l[i] = term[i]; }
151 //
152 //
153 // // Iterrate through each letter in the line
154 // for( int l = 0; l < line.size(); l++ ) {
155 // // Iterrate through the search term
156 // for( int t = 0; t < term.size(); t++ ) {
157 // if( term_l[t] >= 97 && term_l[t] <= 122 ) {
158 // // Term char is lower. Compare down then up
159 // if( line_l[l] != term_l[t]
160 // && line_l[l] != term_l[t] - 32 ) {
161 // break;
162 // }
163 // } else if( term_l[t] >= 65 && term_l[t] <= 90 ) {
164 // // Term char is upper. Compare up then down
165 // if( line_l[l] != term_l[t]
166 // && line_l[l] != term_l[t] + 32 ) {
167 // break;
168 // }
169 // } else {
170 // // Term char isn't a letter. Must be a symbol or something ELSE...
171 // // Teehee
172 // if( line_l[l] != term_l[t] ) { break; }
173 // }
174 //
175 // // Increment the line letter to check the next one on the next search
176 // // term letter loop
177 // l++;
178 // // If we reach the end of the search term, match!
179 // if( t == term.size() - 1 ) { return true; }
180 // }
181 // }
182 // return false;
183 // e
|