summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/main.c3
-rw-r--r--src/note_list.c48
2 files changed, 43 insertions, 8 deletions
diff --git a/src/main.c b/src/main.c
index 049de6d..aa51cdb 100644
--- a/src/main.c
+++ b/src/main.c
@@ -19,6 +19,7 @@
#include <dirent.h>
#include <string.h>
#include <time.h>
+#include <errno.h>
#include "note_list.h"
#include "note.h"
@@ -132,6 +133,8 @@ int main(int argc, char* argv []) {
config_get(&c, "extension")
);
+ if(errno != 0) { return 1; }
+
if(strcmp(argv[1], "ls") == 0 || strcmp(argv[1], "list") == 0) {
list_notes(&list);
} else if(strcmp(argv[1], "new") == 0) {
diff --git a/src/note_list.c b/src/note_list.c
index d354257..f043866 100644
--- a/src/note_list.c
+++ b/src/note_list.c
@@ -27,6 +27,10 @@ int note_list_new(note_list* list, char* path, char* ext) {
list->cursor = 0;
// Open the dir fd
list->dirp = opendir(path);
+ if(list->dirp == NULL) {
+ printf("Error: Could not open path '%s'.\n", path);
+ return 1;
+ }
list->noteent = malloc(sizeof(note));
strcpy(list->path, path);
strcpy(list->ext, ext);
@@ -46,6 +50,7 @@ void note_list_free(note_list* list) {
note* note_list_read(note_list* list) {
struct dirent* de;
+ char fullpath[256];
char fext[64];
// Iterrate over dir entities
@@ -64,14 +69,10 @@ note* note_list_read(note_list* list) {
break;
}
- // 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';
+ // Construct the note full path
+ sprintf(fullpath, "%s/%s", list->path, de->d_name);
+ // Create the note entity
+ note_new(list->noteent, fullpath);
// Increment the file counter
++list->cursor;
@@ -80,6 +81,37 @@ note* note_list_read(note_list* list) {
/**
+ * Rewinds the note_list directory descriptior to zero. Basically executes
+ * rewinddir and reset the note cursor.
+ *
+ * @param list Note list to rewind
+ */
+void note_list_rewind(note_list* list) {
+ // Reset to dir fd to index 0
+ rewinddir(list->dirp);
+ // Set list cursor to 0
+ list->cursor = 0;
+}
+
+
+/**
+ * Seeks the note with the specified index (starting at 1).
+ *
+ * @param list Note list to seek through
+ * @param index Integer index to seek to
+ */
+note* note_list_seek(note_list* list, int index) {
+ // Reset dir to 0
+ note_list_rewind(list);
+
+ while(list->cursor < index) {
+ list->noteent = note_list_read(list);
+ }
+ return list->noteent;
+}
+
+
+/**
* Opens the specified note for editing
*
* @param list Note list the note-to-be-edited belongs to

Generated by cgit