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() {
88 // return remove( _path.out().c_str() );
89 // }
90
91 /**
92 * Gets the filename of the current note instance without its extension.
93 *
94 * @return string Note filename without extension
95 */
96 // string note::friendly_name() {
97 // return name.substr( 0, name.length() - extension.length() - 1 );
98 // }
99
100 /**
101 * TODO
102 */
103 // vector<string> note::find( bool case_sensitive, string term ) {
104 // vector<string> lines;
105 //
106 // // Memory for storing matched lines
107 // vector<string> matches;
108 //
109 // // Read the notes contents into memory
110 // read();
111 //
112 // for( int i = 0; i < body.size(); i++ ) {
113 // if( line_matches( body[i], term ) == true ) {
114 // // Format the output
115 // string out = friendly_name() + ": " + itos( i );
116 // out += ": " + body[i];
117 // // Add to the match list
118 // matches.push_back( out );
119 // }
120 // }
121 // return matches;
122 // }
123
124 /**
125 * Performs a char by char comparison of a line and a search term. If the
126 * search term is found anywhere within the line even once, returns true;
127 * Otherwise, returns false.
128 *
129 * @param string line The line of text to search
130 * @param string term The search term
131 *
132 * @return bool Whether or not the search term was found in the line
133 */
134 // bool note::line_matches( string line, string term ) {
135 // // Define the two long arrays
136 // long line_l[line.size()];
137 // long term_l[term.size()];
138 //
139 // // Convert the line to a long array
140 // for( int i = 0; i < line.size(); i++ ) { line_l[i] = line[i]; }
141 //
142 // // Convert the search term to a long array
143 // for( int i = 0; i < term.size(); i++ ) { term_l[i] = term[i]; }
144 //
145 //
146 // // Iterrate through each letter in the line
147 // for( int l = 0; l < line.size(); l++ ) {
148 // // Iterrate through the search term
149 // for( int t = 0; t < term.size(); t++ ) {
150 // if( term_l[t] >= 97 && term_l[t] <= 122 ) {
151 // // Term char is lower. Compare down then up
152 // if( line_l[l] != term_l[t]
153 // && line_l[l] != term_l[t] - 32 ) {
154 // break;
155 // }
156 // } else if( term_l[t] >= 65 && term_l[t] <= 90 ) {
157 // // Term char is upper. Compare up then down
158 // if( line_l[l] != term_l[t]
159 // && line_l[l] != term_l[t] + 32 ) {
160 // break;
161 // }
162 // } else {
163 // // Term char isn't a letter. Must be a symbol or something ELSE...
164 // // Teehee
165 // if( line_l[l] != term_l[t] ) { break; }
166 // }
167 //
168 // // Increment the line letter to check the next one on the next search
169 // // term letter loop
170 // l++;
171 // // If we reach the end of the search term, match!
172 // if( t == term.size() - 1 ) { return true; }
173 // }
174 // }
175 // return false;
176 // e
|