summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAaron Ball <nullspoon@iohq.net>2015-02-24 11:24:50 -0700
committerAaron Ball <nullspoon@iohq.net>2015-02-24 11:24:50 -0700
commit2f2c938825bb50a636fca57cdb9ce26f7e4e3045 (patch)
treeced238eb03287821961c38298f02a2ecbc43babe
parent9d197122015e46c93452fd534161ebaf3b355aed (diff)
downloadnoteless-2f2c938825bb50a636fca57cdb9ce26f7e4e3045.tar.gz
noteless-2f2c938825bb50a636fca57cdb9ce26f7e4e3045.tar.xz
Refactor of "edit" functionality
This one begins the refactoring of the note struct and related functions Updated note_list functions to work with the new note struct Refactored note edit, note_list get note id function More c++ cleanup work done Updated function comments for afforementioned functions
-rw-r--r--Makefile2
-rw-r--r--src/main.c11
-rw-r--r--src/note.c332
-rw-r--r--src/note.h71
-rw-r--r--src/note_list.c104
-rw-r--r--src/note_list.h8
6 files changed, 201 insertions, 327 deletions
diff --git a/Makefile b/Makefile
index cebd4eb..68e9503 100644
--- a/Makefile
+++ b/Makefile
@@ -9,7 +9,7 @@ all:
$(cc) $(dbg) $(warnings) -std=$(std) -c src/common.c -o $(obj)common.o
# $(cc) $(dbg) $(warnings) -std=$(std) -c src/path.c -o $(obj)path.o
$(cc) $(dbg) $(warnings) -std=$(std) -c src/config.c -o $(obj)config.o
- # $(cc) $(dbg) $(warnings) -std=$(std) -c src/note.c -o $(obj)note.o
+ $(cc) $(dbg) $(warnings) -std=$(std) -c src/note.c -o $(obj)note.o
$(cc) $(dbg) $(warnings) -std=$(std) -c src/note_list.c -o $(obj)note_list.o
# $(cc) $(dbg) $(warnings) -std=$(std) src/main.c $(obj)*.o -o $(out)
$(cc) $(dbg) $(warnings) -std=$(std) src/main.c $(obj)/* -o $(out)
diff --git a/src/main.c b/src/main.c
index a16d323..e6d3728 100644
--- a/src/main.c
+++ b/src/main.c
@@ -136,9 +136,6 @@ int main(int argc, char* argv[]) {
editor = tmp_editor;
}
- // TODO
- printf("Editor: %s\n", editor);
-
// Print helptext if no commands specified
if(argc == 1) {
printf("\nNo command specified. Printing help text.\n");
@@ -171,15 +168,14 @@ int main(int argc, char* argv[]) {
note_list_new(&list, note_path, note_ext);
- //for( int i = 1; i < argc; i++ ) {
if(strcmp(argv[1], "ls") == 0 || strcmp(argv[1], "list") == 0) {
list_notes(&list);
// } else if( arg == "new" ) {
// string name = argv[i + 1];
// return list.create( editor, name );
- // } else if( arg == "edit" ) {
- // string name = argv[i + 1];
- // return list.edit( editor, name );
+ } else if(strcmp(argv[1], "edit") == 0) {
+ char* name = argv[2];
+ return note_list_edit(&list, editor, name );
// } else if( arg == "rm" ) {
// string name = argv[i + 1];
// return list.rm( name );
@@ -198,7 +194,6 @@ int main(int argc, char* argv[]) {
printf("Error: Unknown command or note name '%s'.\n", argv[1]);
return 1;
}
- //}
// Clean up
note_list_free(&list);
diff --git a/src/note.c b/src/note.c
index 1e6a102..93b76da 100644
--- a/src/note.c
+++ b/src/note.c
@@ -19,31 +19,10 @@
/**
* Creates a new empty note object since no filename was specified.
*/
-note::note( path p ) {
- _path = p;
- name = _path.filename;
- extension = get_extension();
+void note_new(note_t* note, char* path) {
+ strcpy(note->path, path);
}
-/**
- * Creates a new empty note object since no filename was specified.
- */
-note::note( string p ) {
- path tmp_path( p );
- _path = tmp_path;
- name = _path.filename;
- extension = get_extension();
-}
-
-/**
- * Creates a new empty note object since no filename was specified.
- */
-note::note( string base, string note_name ) {
- path tmp_path( base + "/" + note_name );
- _path = tmp_path;
- name = note_name;
- extension = get_extension();
-}
/**
* Opens a new note using the specified editor.
@@ -52,10 +31,11 @@ note::note( string base, string note_name ) {
*
* @return int Success or failure of the command
*/
-int note::create( string editor ) {
- return edit( editor );
+int note_create(note_t* note, char* editor) {
+ return note_edit(note, editor);
}
+
/**
* Opens a note using the specified editor
*
@@ -63,64 +43,14 @@ int note::create( string editor ) {
*
* @return int Success or failure of the command
*/
-int note::edit( string editor ) {
- string cmd = editor;
- cmd += " ";
- cmd += _path.out();
- return system( cmd.c_str() );
-}
-
-/**
- * Converts a 1 digit integer to a single char equivelant
- *
- * @param int i The integer to be converted
- *
- * @return char The char equivelent of the int argument
- */
-const char note::itoc( int i ) {
- if( i == 0 ) {
- return '0';
- } else if( i == 1 ) {
- return '1';
- } else if( i == 2 ) {
- return '2';
- } else if( i == 3 ) {
- return '3';
- } else if( i == 4 ) {
- return '4';
- } else if( i == 5 ) {
- return '5';
- } else if( i == 6 ) {
- return '6';
- } else if( i == 7 ) {
- return '7';
- } else if( i == 8 ) {
- return '8';
- } else if( i == 9 ) {
- return '9';
- }
+int note_edit(note_t* note, char* editor) {
+ char cmd[strlen(editor) + 3 + strlen(note->path)];
+ strcpy(cmd, editor);
+ strcat(cmd, " ");
+ strcat(cmd, note->path);
+ return system(cmd);
}
-/**
- * Converts an integer into a string
- *
- * @param int i - Integer to be converted
- *
- * @return string String equivelent of integer argument
- */
-string note::itos( int i ) {
- string str = "";
- // Since we don't want any black holes, return 0 without trying to divide by
- // 10.
- if( i == 0 ) {
- return "0";
- }
- while( i != 0 ) {
- str = itoc( i % 10 ) + str;
- i /= 10;
- }
- return str;
-}
/**
* Reads in the contents of the note. This exists so we don't have to keep
@@ -128,132 +58,68 @@ string note::itos( int i ) {
*
* @return void
*/
-void note::read() {
- // Don't do this unless body hasn't already been read
- if( body.size() == 0 ) {
- // Open the file
- ifstream fs( _path.out().c_str() );
-
- // Verify the file handle opened sucessfully
- if( ! fs.is_open() ) {
- cout << "Could not open file at " << _path.out() << "." << endl;
- exit( 1 );
- }
-
- string line;
- while( getline( fs, line ) ) {
- body.push_back( line );
- }
- fs.close();
- }
-}
-
-/**
- * Returns the current localtime. Useful for "random" non-conflicting
- * filenames.
- *
- * @return string The current date time in format YYYYMMDD.HHMMSS.
- */
-string note::get_current_date_time() {
- string date;
- time_t rawtime;
- time( &rawtime );
- struct tm* now = localtime( &rawtime );
-
- // Date
- // Year
- int year = now->tm_year + 1900;
- date += itos( year );
- // Month
- int mon = now->tm_mon + 1;
- if( mon < 10 ) { date += "0"; }
- date += itos( mon );
- // Day
- int day = now->tm_mday;
- if( day < 10 ) { date += "0"; }
- date += itos( day );
-
- date += ".";
+// void note::read() {
+// // Don't do this unless body hasn't already been read
+// if( body.size() == 0 ) {
+// // Open the file
+// ifstream fs( _path.out().c_str() );
+//
+// // Verify the file handle opened sucessfully
+// if( ! fs.is_open() ) {
+// cout << "Could not open file at " << _path.out() << "." << endl;
+// exit( 1 );
+// }
+//
+// string line;
+// while( getline( fs, line ) ) {
+// body.push_back( line );
+// }
+// fs.close();
+// }
+// }
- // Time
- // Hour
- int hour = now->tm_hour;
- if( hour < 10 ) { date += "0"; }
- date += itos( hour );
- // Minute
- int min = now->tm_min;
- if( min < 10 ) { date += "0"; }
- date += itos( min );
- // Second
- int sec = now->tm_sec;
- if( sec < 10 ) { date += "0"; }
- date += itos( sec );
-
- return date;
-}
-
-/**
- * Parses the current instance filename to determine its extension.
- *
- * @return string The filename. Null pointer if file has no extension.
- */
-string note::get_extension() {
- string ext;
- // Iterrate from the right to the left. This will keep us from catching
- // filenames with multiple periods in them.
- for( int i = name.length(); i > 0; i-- ) {
- // Determine the index
- if( name[i] == '.' ) {
- // Increment so we don't get the period
- i++;
- ext = name.substr( i, name.length() - i );
- break;
- }
- }
- return ext;
-}
/**
* Deletes the current note instance from the filesystem.
*
* @return int Success or failure
*/
-int note::rm() {
- return remove( _path.out().c_str() );
-}
+// int note::rm() {
+// return remove( _path.out().c_str() );
+// }
/**
* Gets the filename of the current note instance without its extension.
*
* @return string Note filename without extension
*/
-string note::friendly_name() {
- return name.substr( 0, name.length() - extension.length() - 1 );
-}
+// string note::friendly_name() {
+// return name.substr( 0, name.length() - extension.length() - 1 );
+// }
/**
* TODO
*/
-vector<string> note::find( bool case_sensitive, string term ) {
- vector<string> lines;
-
- // Memory for storing matched lines
- vector<string> matches;
-
- // Read the notes contents into memory
- read();
-
- for( int i = 0; i < body.size(); i++ ) {
- if( line_matches( body[i], term ) == true ) {
- // Format the output
- string out = friendly_name() + ": " + itos( i );
- out += ": " + body[i];
- // Add to the match list
- matches.push_back( out );
- }
- }
- return matches;
-}
+// vector<string> note::find( bool case_sensitive, string term ) {
+// vector<string> lines;
+//
+// // Memory for storing matched lines
+// vector<string> matches;
+//
+// // Read the notes contents into memory
+// read();
+//
+// for( int i = 0; i < body.size(); i++ ) {
+// if( line_matches( body[i], term ) == true ) {
+// // Format the output
+// string out = friendly_name() + ": " + itos( i );
+// out += ": " + body[i];
+// // Add to the match list
+// matches.push_back( out );
+// }
+// }
+// return matches;
+// }
/**
* Performs a char by char comparison of a line and a search term. If the
@@ -265,46 +131,46 @@ vector<string> note::find( bool case_sensitive, string term ) {
*
* @return bool Whether or not the search term was found in the line
*/
-bool note::line_matches( string line, string term ) {
- // Define the two long arrays
- long line_l[line.size()];
- long term_l[term.size()];
-
- // Convert the line to a long array
- for( int i = 0; i < line.size(); i++ ) { line_l[i] = line[i]; }
-
- // Convert the search term to a long array
- for( int i = 0; i < term.size(); i++ ) { term_l[i] = term[i]; }
-
-
- // Iterrate through each letter in the line
- for( int l = 0; l < line.size(); l++ ) {
- // Iterrate through the search term
- for( int t = 0; t < term.size(); t++ ) {
- if( term_l[t] >= 97 && term_l[t] <= 122 ) {
- // Term char is lower. Compare down then up
- if( line_l[l] != term_l[t]
- && line_l[l] != term_l[t] - 32 ) {
- break;
- }
- } else if( term_l[t] >= 65 && term_l[t] <= 90 ) {
- // Term char is upper. Compare up then down
- if( line_l[l] != term_l[t]
- && line_l[l] != term_l[t] + 32 ) {
- break;
- }
- } else {
- // Term char isn't a letter. Must be a symbol or something ELSE...
- // Teehee
- if( line_l[l] != term_l[t] ) { break; }
- }
-
- // Increment the line letter to check the next one on the next search
- // term letter loop
- l++;
- // If we reach the end of the search term, match!
- if( t == term.size() - 1 ) { return true; }
- }
- }
- return false;
-}
+// bool note::line_matches( string line, string term ) {
+// // Define the two long arrays
+// long line_l[line.size()];
+// long term_l[term.size()];
+//
+// // Convert the line to a long array
+// for( int i = 0; i < line.size(); i++ ) { line_l[i] = line[i]; }
+//
+// // Convert the search term to a long array
+// for( int i = 0; i < term.size(); i++ ) { term_l[i] = term[i]; }
+//
+//
+// // Iterrate through each letter in the line
+// for( int l = 0; l < line.size(); l++ ) {
+// // Iterrate through the search term
+// for( int t = 0; t < term.size(); t++ ) {
+// if( term_l[t] >= 97 && term_l[t] <= 122 ) {
+// // Term char is lower. Compare down then up
+// if( line_l[l] != term_l[t]
+// && line_l[l] != term_l[t] - 32 ) {
+// break;
+// }
+// } else if( term_l[t] >= 65 && term_l[t] <= 90 ) {
+// // Term char is upper. Compare up then down
+// if( line_l[l] != term_l[t]
+// && line_l[l] != term_l[t] + 32 ) {
+// break;
+// }
+// } else {
+// // Term char isn't a letter. Must be a symbol or something ELSE...
+// // Teehee
+// if( line_l[l] != term_l[t] ) { break; }
+// }
+//
+// // Increment the line letter to check the next one on the next search
+// // term letter loop
+// l++;
+// // If we reach the end of the search term, match!
+// if( t == term.size() - 1 ) { return true; }
+// }
+// }
+// return false;
+// }
diff --git a/src/note.h b/src/note.h
index 8dc63f2..239ef0f 100644
--- a/src/note.h
+++ b/src/note.h
@@ -14,42 +14,39 @@
* You should have received a copy of the GNU General Public License
* along with noteless. If not, see <http://www.gnu.org/licenses/>.
*/
-using namespace std;
#include <stdlib.h>
-#include <iostream>
-#include <fstream>
-#include <time.h>
-#include <string>
-#include <vector>
-#include "path.h"
-#ifndef LIB_NOTE_H
-#define LIB_NOTE_H
-class note {
- private:
- // Methods
- const char itoc( int );
- string get_extension();
- bool line_matches( string, string );
- path _path;
-
- public:
- // Variables
- string extension;
- string name;
- vector<string> body;
-
- // Methods
- note( path );
- note( string );
- note( string, string );
- int create( string );
- int edit( string );
- int rm();
- void read();
- string itos( int );
- string get_current_date_time();
- string friendly_name();
- string get_fqp();
- vector<string> find( bool, string );
-};
+#include <stdio.h>
+#include <string.h>
+#include "common.h"
+//#include "path.h"
+
+#ifndef noteless_note_h
+#define noteless_note_h
+
+typedef struct note {
+ char extension[32];
+ char path[256];
+ char name[256];
+} note_t;
+
+void note_new(note_t*, char*);
+
+int note_create(note_t*, char*);
+
+int note_edit(note_t*, char*);
+
+int note_rm(note_t*);
+
+int line_matches(char*, char*);
+
+
+/**
+ * C++ code
+ */
+//vector<string> body;
+//void read();
+//string friendly_name();
+//string get_fqp();
+//vector<string> find( bool, string );
+
#endif
diff --git a/src/note_list.c b/src/note_list.c
index 327347d..b1527c2 100644
--- a/src/note_list.c
+++ b/src/note_list.c
@@ -53,11 +53,20 @@ void note_list_new(note_list_t* list, char* path, char* ext) {
// The start index of the extension in the current filename
int ext_start = strlen(ent->d_name) - ext_len;
+ // If the current file's extension matches the list extension
if(strncmp(&ent->d_name[ext_start], ext, ext_len) == 0) {
- //printf("%s\n", ent->d_name);
- list->names[i] = malloc(sizeof(char) * strlen(ent->d_name) - ext_len - 1);
+ // Allocate space in the array for the filename (all the way up to the
+ // period starting the extension)
+ list->names[i] = malloc(sizeof(char) * ext_start);
+
// Copy the temp filename into the struct
- strncpy(list->names[i], ent->d_name, strlen(ent->d_name) - ext_len - 1);
+ // As previously mentioned, we want to copy minus one char (the period)
+ // so we have room for the null byte char.
+ strncpy(list->names[i], ent->d_name, ext_start);
+
+ // Insert null byte char one before the extension (the period)
+ list->names[i][ext_start - 1] = '\0';
+
i++;
}
}
@@ -74,12 +83,6 @@ void note_list_free(note_list_t* list) {
}
-// note_list::note_list( string dirname, string ext ) {
-// _dirname = dirname;
-// _ext = ext;
-// get_notes( _dirname, _ext, "" );
-// }
-
// void note_list::get_notes( string base, string ext, string sub ) {
// // Dir pointer
// DIR* dp;
@@ -138,6 +141,7 @@ void note_list_free(note_list_t* list) {
// return 0;
// }
+
/**
* Opens a new note for editing
*
@@ -154,51 +158,62 @@ void note_list_free(note_list_t* list) {
// return 0;
// }
+
/**
* Opens the specified note for editing
*
- * @param string editor Path to the editor to use for editing
- * @param string note_name Name of the note to be edited
+ * @param list Note list the note-to-be-edited belongs to
+ * @param editor Path to the editor to use for editing
+ * @param note_name Name of the note to be edited
*/
-// int note_list::edit( string editor, string note_name ) {
-// int id = find_note_id( note_name );
-// if( id != -1 ) {
-// notes[id].edit( editor );
-// } else {
-// cout << "Note \"" << note_name << "\" does not exist." << endl;
-// return 1;
-// }
-// return 0;
-// }
+int note_list_edit(note_list_t* list, char* editor, char* note_name) {
+ int id = note_list_get_note_id(list, note_name);
+ if(id != -1) {
+ // Construct the note path
+ // It's safe to assume 256 here since various other path variables also
+ // limit to 256.
+ char path[256];
+ strcpy(path, list->path);
+ strcat(path, "/");
+ strcat(path, note_name);
+ strcat(path, ".");
+ strcat(path, list->extension);
+ printf("%s\n", path);
+
+ note_t note;
+ note_new(&note, path);
+ note_edit(&note, editor);
+ } else {
+ printf("Note '%s' does not exist.\n", note_name);
+ return 1;
+ }
+ return 0;
+}
+
/**
* Searches all note *names* (friendly and unfriendly) for the given name and
- * returns the note id (not necissarily persistent). If note does not exist,
- * returns -1. Can be used for a sort of "note_exists" method as such.
+ * returns the note id (not necissarily persistent). If note does not exist,
+ * returns -1. Can be used for a sort of "note_exists" method as such.
+ *
+ * Note that this does a partial comparison, so only the number of characters
+ * passed for note_name is the number compared, thus allowing for fuzzy-ish
+ * name matching. This will return the id of the first matched note name.
*
- * @param string note_name Name of the note to search for
+ * @param list Note list to search for the given note [partial] name
+ * @param note_name Name of the note to search for
*
* @return int The integer of the note. -1 if note does not exist
*/
-// int note_list::find_note_id( string note_name ) {
-// for( int i = 0; i < notes.size(); i++ ) {
-// if( notes[i].name == note_name || notes[i].friendly_name() == note_name ) {
-// return i;
-// }
-// }
-// return -1;
-// }
+int note_list_get_note_id(note_list_t* list, char* note_name) {
+ for(int i = 0; i < list->count; i++) {
+ if(strncmp(list->names[i], note_name, strlen(note_name)) == 0) {
+ return i;
+ }
+ }
+ return -1;
+}
-/**
- * Enumerates all note names.
- */
-// vector<string> note_list::enum_names() {
-// vector<string> names;
-// for( int i = 0; i < notes.size(); i++ ) {
-// names.push_back( notes[i].friendly_name() );
-// }
-// return names;
-// }
/**
* Returns the contents of the requested note
@@ -241,9 +256,8 @@ void note_list_free(note_list_t* list) {
/**
* TODO
*/
-// vector<string> note_list::find( bool case_sensitive, string term ) {
-// vector<string> matches;
-// for( int i = 0; i < notes.size(); i++ ) {
+// int note_list_find(note_list_t* list, int case_sensitive, char* term ) {
+// for(int i = 0; i < notes.size(); i++) {
// vector<string> note_matches = notes[i].find( case_sensitive, term );
// if( note_matches.size() > 0 ) {
// for( int m = 0; m < note_matches.size(); m++ ) {
diff --git a/src/note_list.h b/src/note_list.h
index 36bda27..07a2676 100644
--- a/src/note_list.h
+++ b/src/note_list.h
@@ -18,7 +18,7 @@
#include <dirent.h>
#include <stdio.h>
#include <string.h>
-//#include "note.h"
+#include "note.h"
//#include "path.h"
#ifndef noteless_note_list_h
@@ -35,16 +35,18 @@ void note_list_new(note_list_t*, char*, char*);
void note_list_free(note_list_t*);
+int note_list_edit(note_list_t*, char*, char*);
+
+int note_list_get_note_id(note_list_t*, char*);
+
// void get_notes(note_list_t*);
// note_list( string, string );
// int add( string, string );
// int create( string, string );
-// int edit( string, string );
// int rm( string );
// vector<string> enum_names();
// vector<string> find( bool, string );
// int cat_note( string, vector<string>* );
-// int find_note_id( string );
#endif

Generated by cgit