summaryrefslogtreecommitdiff
path: root/src/note_list.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/note_list.c')
-rw-r--r--src/note_list.c323
1 files changed, 103 insertions, 220 deletions
diff --git a/src/note_list.c b/src/note_list.c
index ed277ad..19727bb 100644
--- a/src/note_list.c
+++ b/src/note_list.c
@@ -19,241 +19,155 @@
/**
* Constructor
*
- * @param list List to be instantiated
+ * @param list List stream to be instantiated
* @param path Path to the treasure... er... notes
* @param ext Extension of the notes to pay attention to
*/
-void note_list_new(note_list_t* list, char* path, char* ext) {
- list->count = 0;
- strcpy(list->extension, ext);
+int note_list_new(note_list* list, char* path, char* ext) {
+ list->cursor = 0;
+ // Open the dir fd
+ list->dirp = opendir(path);
+ list->noteent = malloc(sizeof(note));
strcpy(list->path, path);
+ strcpy(list->ext, ext);
- DIR* d = opendir(path);
- struct dirent* ent;
+ // Skip the first two, as they are always . and ..
+ readdir(list->dirp);
+ readdir(list->dirp);
- int ext_len = strlen(ext);
-
- // 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++;
- }
- }
+ return 0;
+}
- // 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;
+void note_list_free(note_list* list) {
+ closedir(list->dirp);
+}
- // If the current file's extension matches the list extension...
- if(strncmp(&ent->d_name[ext_start], ext, ext_len) == 0) {
- // Allocate space for the entire filename (we'll strip the extension off
- // as needed later). This will make calculating path lengths much
- // simpler.
- list->names[i] = malloc(sizeof(char) * (strlen(ent->d_name) + 1));
- // Copy the filename into the struct
- strcpy(list->names[i], ent->d_name);
+note* note_list_read(note_list* list) {
+ struct dirent* de;
+ char fext[64];
- i++;
- }
- }
+ // Iterrate over dir entities
+ while(1) {
+ // Return null if readdir also returns null
+ if((de = readdir(list->dirp)) == NULL) { return NULL; }
+ // Skip any non-regular files (DT_REG == 8)
+ if(de->d_type != 8) { continue; }
+ // Skip hidden files
+ if(de->d_name[0] == '.') { continue; }
- closedir(d);
-}
+ // Skip files that don't match the specified extension
+ get_extension(de->d_name, fext);
+ if(strcmp(fext, list->ext) != 0) { continue; }
-
-void note_list_free(note_list_t* list) {
- for(int i = 0; i < list->count; i++) {
- free(list->names[i]);
+ break;
}
- free(list->names);
-}
-
-// 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 );
-// }
-// }
+ // Copy in the path
+ sprintf(list->noteent->path, "%s/%s", list->path, de->d_name);
+ // Copy in the extension
+ strcpy(list->noteent->extension, fext);
+ // Copy in the filename
+ strcpy(list->noteent->name, de->d_name);
+ // Overwrite . with null byte
+ list->noteent->name[strlen(de->d_name) - strlen(fext) - 1] = '\0';
+
+ // Increment the file counter
+ ++list->cursor;
+ return list->noteent;
+}
/**
- * Adds a new note item to the list by path.
- *
- * @param string path Path to the note to be added to the list
+ * Opens the specified note for editing
*
- * @return 0
+ * @param list Note list the note-to-be-edited belongs to
+ * @param editor Path to the editor to use for editing
+ * @param term Search term
*/
-// int note_list::add( string base, string name ) {
-// note n( base, name );
-// notes.push_back( n );
-// return 0;
-// }
-
+int note_list_edit(note_list* list, char* editor, char* term) {
+ note* n;
-/**
- * Opens a new note for editing
- *
- * @param string editor Path to the editor to use for editing
- * @param string note_name Name of the note to be created and edited
- *
- * @return int Success or failure (always success for now)
- */
-int note_list_create_note(note_list_t* list, char* editor, char* note_name) {
- char tmp_note_name[64];
- strcpy(tmp_note_name, note_name);
- strcat(tmp_note_name, ".");
- strcat(tmp_note_name, list->extension);
+ while(1) {
+ n = note_list_read(list);
+ if(n == NULL) { break; }
- // Check if note already exists
- for(int i = 0; i < list->count; i++) {
- if(strcmp(list->names[i], tmp_note_name) == 0) {
- return 1;
+ if(strncmp(n->name, term, strlen(term)) == 0) {
+ file_edit(n->path, editor);
+ return 0;
}
}
- // Construct full note path
- char note_path[256];
- strcpy(note_path, list->path);
- strcat(note_path, "/");
- strcat(note_path, tmp_note_name);
-
- note_t note;
- note_new(&note, note_path);
- note_edit(&note, editor);
-
- return 0;
+ printf("No notes exist that match the search term '%s'.\n", term);
+ return 1;
}
/**
- * Opens the specified note for editing
+ * Opens a new note for editing
*
- * @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
+ * @param string editor Path to the editor to use for editing
+ * @param string note_name Name of the note to be created and edited
+ *
+ * @return int Success or failure (always success for now)
*/
-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, list->names[id]);
+int note_list_create_note(note_list* list, char* editor, char* name) {
+ char fullname[128];
+ char fullpath[256];
+ note* n;
- note_t note;
- note_new(&note, path);
- note_edit(&note, editor);
- } else {
- printf("Note '%s' does not exist.\n", note_name);
- return 1;
+ // Create full file name
+ sprintf(fullname, "%s.%s", name, list->ext);
+
+ // Check if note already exists
+ while((n = note_list_read(list)) != NULL) {
+ if(strcmp(n->name, fullname) == 0) { return 1; }
}
+
+ // Construct full note path for the new note
+ sprintf(fullpath, "%s/%s", list->path, fullname);
+ file_edit(fullpath, editor);
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.
- *
- * 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.
+ * Returns the contents of the requested note
*
- * @param list Note list to search for the given note [partial] name
- * @param note_name Name of the note to search for
+ * @param string note_name Name of the note to get the contents for
*
- * @return int The integer of the note. -1 if note does not exist
+ * @return vector<string> Contents of the note, broken per line into a vector.
*/
-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;
+int note_list_cat_note(note_list* list, char* term) {
+ note* n;
+ // Check for the requested note
+ while((n = note_list_read(list)) != NULL) {
+ if(strncmp(n->name, term, strlen(term)) == 0) {
+ note_cat(n);
+ return 0;
}
}
- return -1;
+ return 1;
}
/**
- * Returns the contents of the requested note
- *
- * @param string note_name Name of the note to get the contents for
- *
- * @return vector<string> Contents of the note, broken per line into a vector.
+ * Searches each note in the list for the given search term.
+ *
+ * @return int Number of instances found in all notes
+ * TODO: This needs to be written still
*/
-int note_list_cat_note(note_list_t* list, char* note_name) {
- // Check for the requested note
- int id = note_list_get_note_id(list, note_name);
- if(id != -1) {
- // Construct the full note path
- char path[strlen(list->path) + strlen(list->names[id]) + 3];
- strcpy(path, list->path);
- strcat(path, "/");
- strcat(path, list->names[id]);
-
- note_t note;
- note_new(&note, path);
+int note_list_search(note_list* list, char* term, int case_insensitive) {
+ note* n;
- // Read in the note's contents
- note_cat(&note);
- } else {
- return 1;
+ while((n = note_list_read(list)) != NULL) {
+ note_search(n, term, case_insensitive);
}
return 0;
}
+
/**
* Deletes the specified note
*
@@ -261,45 +175,14 @@ int note_list_cat_note(note_list_t* list, char* note_name) {
*
* @return int Exit code (1 = error, 0 = success)
*/
-int note_list_rm_note(note_list_t* list, char* note_name) {
- int id = note_list_get_note_id(list, note_name);
- if(id == -1) {
- printf("Error: There is no note matching \"%s\".\n", note_name);
- return 1;
- } else {
- char note_path[256] = "";
- strcpy(note_path, list->path);
- strcat(note_path, "/");
- strcat(note_path, list->names[id]);
+int note_list_rm_note(note_list* list, char* name) {
+ note* n;
- note_t note;
- note_new(&note, note_path);
- return note_rm(&note);
+ while((n = note_list_read(list)) != NULL) {
+ if(strncmp(n->name, name, strlen(name)) == 0) {
+ return note_rm(n);
+ }
}
- return 0;
+ return 1;
}
-
-/**
- * Searches each note in the list for the given search term.
- *
- * @return int Number of instances found in all notes
- * TODO: This needs to be written still
- */
-int note_list_search(note_list_t* list, char* term, int case_insensitive) {
- for(int i = 0; i < list->count; i++) {
- // TODO: My gosh this section of code needs to be turned into a function.
- // I've written this far too many times.
- // Construct note path
- char path[256] = "";
- strcpy(path, list->path);
- strcat(path, "/");
- strcat(path, list->names[i]);
-
- note_t note;
- note_new(&note, path);
-
- note_search(&note, term, case_insensitive);
- }
- return 0;
-}

Generated by cgit