summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAaron Ball <nullspoon@iohq.net>2015-02-21 18:52:39 -0700
committerAaron Ball <nullspoon@iohq.net>2015-02-21 18:52:39 -0700
commit0f6330046ad32d9301c9818abbb789946a625330 (patch)
tree6a30e9f321876a0ce9f4bbaf528afab04808cd45
parentd6be9fc96c31c617060535651427dc22623459cb (diff)
downloadnoteless-0f6330046ad32d9301c9818abbb789946a625330.tar.gz
noteless-0f6330046ad32d9301c9818abbb789946a625330.tar.xz
Initial refactor of note_list to c
This version currently supports the ls,list command. Commented out the c++ code that hasn't been refactored yet in note_list.
-rw-r--r--Makefile2
-rw-r--r--src/main.c107
-rw-r--r--src/note_list.c284
-rw-r--r--src/note_list.h56
4 files changed, 252 insertions, 197 deletions
diff --git a/Makefile b/Makefile
index a5a4540..cebd4eb 100644
--- a/Makefile
+++ b/Makefile
@@ -10,7 +10,7 @@ all:
# $(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_list.c -o $(obj)note_list.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 d297aee..5f31893 100644
--- a/src/main.c
+++ b/src/main.c
@@ -19,8 +19,8 @@
#include <string.h>
#include "common.h"
#include "config.h"
+#include "note_list.h"
// #include "note.h"
-// #include "note_list.h"
// #include "path.h"
int get_help() {
@@ -45,16 +45,16 @@ int get_help() {
/**
* Prints names of all notes
+ *
+ * @param list Note list whos names to enumerate
*/
-// void list_notes( note_list list ) {
-// // List notes
-// vector<string> names = list.enum_names();
-// cout << endl;
-// for( int i = 0; i < names.size(); i++ ) {
-// cout << "* " << names[i] << endl;
-// }
-// cout << endl;
-// }
+void list_notes(note_list_t* list) {
+ // List notes
+ for(int i = 0; i < list->count; i++) {
+ printf("* %s\n", list->names[i]);
+ }
+}
+
//
// /**
// * Searches all note contents for the specified text
@@ -137,19 +137,15 @@ int main(int argc, char* argv[]) {
editor = tmp_editor;
}
-
- // Verbose variable outputs
- printf("Note Path: %s\n", note_path);
- printf("Note Ext: %s\n", note_ext);
- printf("Conf Path: %s\n", conf_path);
+ // TODO
printf("Editor: %s\n", editor);
- config_free(&c);
-
- // if( argc == 1 ) {
- // cout << "\nNo command specified. Printing help text.\n" << endl;
- // return get_help();
- // }
+ // Print helptext if no commands specified
+ if(argc == 1) {
+ printf("\nNo command specified. Printing help text.\n");
+ get_help();
+ return 1;
+ }
// // If the init command was passed, we want to init before checking if the
// // note path exists, otherwise the error will display and the store will
@@ -172,37 +168,42 @@ int main(int argc, char* argv[]) {
// return 1;
// }
- // // Create new list
- // note_list list( note_path, note_ext );
-
- // for( int i = 1; i < argc; i++ ) {
- // string arg = argv[i];
- // if( arg == "ls" || arg == "list" ) {
- // 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( arg == "rm" ) {
- // string name = argv[i + 1];
- // return list.rm( name );
- // } else if( arg == "find" ) {
- // string term = argv[i + 1];
- // return search_notes( list, term );
- // } else if( arg == "cat" ) {
- // string name = argv[i + 1];
- // return cat_note( list, name );
- // } else if( arg == "help" ) {
- // return get_help();
- // } else if( list.find_note_id( arg ) != -1 ) {
- // // Try to open the note if it exists
- // return list.edit( editor, arg );
- // } else {
- // cout << "Error: Unknown command or note name '" << arg << "'." << endl;
- // return 1;
- // }
- // }
+ note_list_t list;
+
+ 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( arg == "rm" ) {
+ // string name = argv[i + 1];
+ // return list.rm( name );
+ // } else if( arg == "find" ) {
+ // string term = argv[i + 1];
+ // return search_notes( list, term );
+ // } else if( arg == "cat" ) {
+ // string name = argv[i + 1];
+ // return cat_note( list, name );
+ // } else if( arg == "help" ) {
+ // return get_help();
+ // } else if( list.find_note_id( arg ) != -1 ) {
+ // // Try to open the note if it exists
+ // return list.edit( editor, arg );
+ } else {
+ printf("Error: Unknown command or note name '%s'.\n", argv[1]);
+ return 1;
+ }
+ //}
+
+ // Clean up
+ note_list_free(&list);
+ config_free(&c);
+
return 0;
}
diff --git a/src/note_list.c b/src/note_list.c
index dcd39f4..327347d 100644
--- a/src/note_list.c
+++ b/src/note_list.c
@@ -19,60 +19,112 @@
/**
* Constructor
*
- * @param string dirname Path to the treasure... er... notes
- * @param string ext Extension of the notes to pay attention to
+ * @param list List to be instantiated
+ * @param path Path to the treasure... er... notes
+ * @param ext Extension of the notes to pay attention to
*/
-note_list::note_list( string dirname, string ext ) {
- _dirname = dirname;
- _ext = ext;
- get_notes( _dirname, _ext, "" );
-}
+void note_list_new(note_list_t* list, char* path, char* ext) {
+ list->count = 0;
+ strcpy(list->extension, ext);
+ strcpy(list->path, path);
+
+ DIR* d = opendir(path);
+ struct dirent* ent;
-void note_list::get_notes( string base, string ext, string sub ) {
- // Dir pointer
- DIR* dp;
+ int ext_len = strlen(ext);
- string search_dir = base;
- // Append the subdir if necessary
- if( sub != "" ) {
- // Open the subdir if it's specified
- search_dir = base + "/" + sub;
+ // First iterration to get a matching file count
+ while((ent = readdir(d))) {
+ // The start index of the extension in the current filename
+ int ext_start = strlen(ent->d_name) - ext_len;
+
+ if(strncmp(&ent->d_name[ext_start], ext, ext_len) == 0) {
+ list->count++;
+ }
}
- dp = opendir( search_dir.c_str() );
-
- if( dp ) {
- // Prepare to iterrate through dir items
- struct dirent* item;
- while( item = readdir( dp ) ) {
- string name = item->d_name;
-
- path p( base + "/" + sub + "/" + name );
-
- if( name[0] != '.' ) {
- if( p.is_dir() == 1 ) {
- // TODO
- // I really hate the way I'm doing this repeatedly. Going to need a
- // better way.
- if( sub != "" ) {
- get_notes( base, ext, sub + "/" + name );
- } else {
- get_notes( base, ext, name );
- }
- } else {
- // This will be replaced later with path.join() when it's implemented
- if( sub != "" ) {
- add( base, sub + "/" + name );
- } else {
- add( base, name );
- }
- }
- }
+
+ // Create name list of previously discovered size
+ list->names = malloc(sizeof(char*) * list->count);
+
+ rewinddir(d);
+ int i = 0;
+ // Second iterration for populating the file list
+ while((ent = readdir(d))) {
+ // The start index of the extension in the current filename
+ int ext_start = strlen(ent->d_name) - ext_len;
+
+ 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);
+ // Copy the temp filename into the struct
+ strncpy(list->names[i], ent->d_name, strlen(ent->d_name) - ext_len - 1);
+ i++;
}
- closedir( dp );
}
+
+ closedir(d);
}
+void note_list_free(note_list_t* list) {
+ for(int i = 0; i < list->count; i++) {
+ free(list->names[i]);
+ }
+ free(list->names);
+}
+
+
+// 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;
+//
+// string search_dir = base;
+// // Append the subdir if necessary
+// if( sub != "" ) {
+// // Open the subdir if it's specified
+// search_dir = base + "/" + sub;
+// }
+// dp = opendir( search_dir.c_str() );
+//
+// if( dp ) {
+// // Prepare to iterrate through dir items
+// struct dirent* item;
+// while( item = readdir( dp ) ) {
+// string name = item->d_name;
+//
+// path p( base + "/" + sub + "/" + name );
+//
+// if( name[0] != '.' ) {
+// if( p.is_dir() == 1 ) {
+// // TODO
+// // I really hate the way I'm doing this repeatedly. Going to need a
+// // better way.
+// if( sub != "" ) {
+// get_notes( base, ext, sub + "/" + name );
+// } else {
+// get_notes( base, ext, name );
+// }
+// } else {
+// // This will be replaced later with path.join() when it's implemented
+// if( sub != "" ) {
+// add( base, sub + "/" + name );
+// } else {
+// add( base, name );
+// }
+// }
+// }
+// }
+// closedir( dp );
+// }
+// }
+
+
/**
* Adds a new note item to the list by path.
*
@@ -80,11 +132,11 @@ void note_list::get_notes( string base, string ext, string sub ) {
*
* @return 0
*/
-int note_list::add( string base, string name ) {
- note n( base, name );
- notes.push_back( n );
- return 0;
-}
+// int note_list::add( string base, string name ) {
+// note n( base, name );
+// notes.push_back( n );
+// return 0;
+// }
/**
* Opens a new note for editing
@@ -94,13 +146,13 @@ int note_list::add( string base, string name ) {
*
* @return int Success or failure (always success for now)
*/
-int note_list::create( string editor, string note_name ) {
- string spath = _dirname + '/' + note_name + '.' + _ext;
- path p( spath );
- note n( p );
- n.edit( editor );
- return 0;
-}
+// int note_list::create( string editor, string note_name ) {
+// string spath = _dirname + '/' + note_name + '.' + _ext;
+// path p( spath );
+// note n( p );
+// n.edit( editor );
+// return 0;
+// }
/**
* Opens the specified note for editing
@@ -108,16 +160,16 @@ int note_list::create( string editor, string note_name ) {
* @param string editor Path to the editor to use for editing
* @param string 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( 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;
+// }
/**
* Searches all note *names* (friendly and unfriendly) for the given name and
@@ -128,25 +180,25 @@ int note_list::edit( string editor, string note_name ) {
*
* @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::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;
+// }
/**
* 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;
-}
+// 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
@@ -155,18 +207,18 @@ vector<string> note_list::enum_names() {
*
* @return vector<string> Contents of the note, broken per line into a vector.
*/
-int note_list::cat_note( string note_name, vector<string>* pbody ) {
- // Check for the requested note
- int id = find_note_id( note_name );
- if( id != -1 ) {
- // Read in the note's contents
- notes[id].read();
- *pbody = notes[id].body;
- } else {
- return 1;
- }
- return 0;
-}
+// int note_list::cat_note( string note_name, vector<string>* pbody ) {
+// // Check for the requested note
+// int id = find_note_id( note_name );
+// if( id != -1 ) {
+// // Read in the note's contents
+// notes[id].read();
+// *pbody = notes[id].body;
+// } else {
+// return 1;
+// }
+// return 0;
+// }
/**
* Deletes the specified note
@@ -175,29 +227,29 @@ int note_list::cat_note( string note_name, vector<string>* pbody ) {
*
* @return int Exit code (1 = error, 0 = success)
*/
-int note_list::rm( string note_name ) {
- int id = find_note_id( note_name );
- if( id == -1 ) {
- cout << "Note \"" << note_name << "\" does not exist." << endl;
- return 1;
- } else {
- notes[id].rm();
- }
- return 0;
-}
+// int note_list::rm( string note_name ) {
+// int id = find_note_id( note_name );
+// if( id == -1 ) {
+// cout << "Note \"" << note_name << "\" does not exist." << endl;
+// return 1;
+// } else {
+// notes[id].rm();
+// }
+// return 0;
+// }
/**
* TODO
*/
-vector<string> note_list::find( bool case_sensitive, string term ) {
- vector<string> matches;
- 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++ ) {
- matches.push_back( note_matches[m] );
- }
- }
- }
- return matches;
-}
+// vector<string> note_list::find( bool case_sensitive, string term ) {
+// vector<string> matches;
+// 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++ ) {
+// matches.push_back( note_matches[m] );
+// }
+// }
+// }
+// return matches;
+// }
diff --git a/src/note_list.h b/src/note_list.h
index 3e3a2c6..36bda27 100644
--- a/src/note_list.h
+++ b/src/note_list.h
@@ -14,35 +14,37 @@
* 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 <dirent.h>
-#include <string>
-#include <vector>
-#include "note.h"
-#include "path.h"
+#include <stdio.h>
+#include <string.h>
+//#include "note.h"
+//#include "path.h"
-#ifndef LIB_NOTE_COLLECTION_H
-#define LIB_NOTE_COLLECTION_H
-class note_list {
- private:
- // Properties
- vector<note> notes;
- string _dirname;
- string _ext;
- // Methods
- void get_notes( string, string, string );
+#ifndef noteless_note_list_h
+#define noteless_note_list_h
+
+typedef struct note_list {
+ char path[256];
+ char extension[32];
+ char** names;
+ int count;
+} note_list_t;
+
+void note_list_new(note_list_t*, char*, char*);
+
+void note_list_free(note_list_t*);
+
+// 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 );
- public:
- // Methods
- 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