summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAaron Ball <nullspoon@oper.io>2017-01-02 23:30:23 -0700
committerAaron Ball <nullspoon@oper.io>2017-01-02 23:30:23 -0700
commit40fb2056cf51d140378b627637271e7b57229d52 (patch)
tree25553b0c7a247154e138781f11b4b1e9468f8228
parentedbc358ae78c3ef1706fa2dde6bf0c2d5a1348fe (diff)
parentbf9762a9e0e129718bda20a28c9ef8e8b66b4a02 (diff)
downloadnoteless-40fb2056cf51d140378b627637271e7b57229d52.tar.gz
noteless-40fb2056cf51d140378b627637271e7b57229d52.tar.xz
Merge branch 'ls-date'
-rw-r--r--src/main.c21
-rw-r--r--src/note.c38
-rw-r--r--src/note.h7
3 files changed, 64 insertions, 2 deletions
diff --git a/src/main.c b/src/main.c
index 8e02d4f..049de6d 100644
--- a/src/main.c
+++ b/src/main.c
@@ -18,6 +18,7 @@
#include <stdlib.h>
#include <dirent.h>
#include <string.h>
+#include <time.h>
#include "note_list.h"
#include "note.h"
@@ -47,15 +48,31 @@ void get_help() {
/**
- * Prints names of all notes
+ * Prints names of all notes, sorted by last modified date
*
* @param list Note list whos names to enumerate
*/
void list_notes(note_list* list) {
note* n;
+ int i = 0;
+ int count = 0;
+ note list_preload[256];
+ char modtime[256];
+ // Load note structs into an array
while((n = note_list_read(list)) != NULL) {
- printf("* %s\n", n->name);
+ note_cpy(&list_preload[count], n);
+ count++;
+ }
+
+ // Sort the preloaded list
+ qsort(list_preload, count, sizeof(note), note_date_cmp);
+
+ // Output from oldest to newest
+ for(i = 0; i < count; i++) {
+ strftime(modtime, 256, "%F %T", localtime(&list_preload[i].mtime));
+ strip_extension(list_preload[i].name);
+ printf("%s %s\n", modtime, list_preload[i].name);
}
}
diff --git a/src/note.c b/src/note.c
index 8d5146d..6b42ca4 100644
--- a/src/note.c
+++ b/src/note.c
@@ -22,6 +22,12 @@
void note_new(note* n, char* path) {
strcpy(n->path, path);
basename(path, n->name);
+
+ // Get file stats
+ struct stat s;
+ stat(path, &s);
+ // Last modified time
+ n->mtime = s.st_mtime;
}
@@ -38,6 +44,38 @@ int note_create(note* n, char* editor) {
/**
+ * Performs a date comparison on the two note structs. Returns an integer
+ * representing in seconds if struct b was modified more recently (positive
+ * int) or less recently (negative int) than struct a. If the two were modified
+ * on the same date and time, 0 is returned.
+ *
+ * @param a Note struct to compare last modified date
+ * @param b Note struct to compare last modified date
+ */
+int note_date_cmp(const void* a, const void* b) {
+ note* note_a = (note*) a;
+ note* note_b = (note*) b;
+
+ return (int)(note_a->mtime - note_b->mtime);
+}
+
+
+/**
+ * Copies the contents of one note struct into the pointer to the second note
+ * struct.
+ *
+ * @param dest Destination note struct
+ * @param src Source note struct
+ */
+void note_cpy(note* dest, note* src) {
+ strcpy(dest->extension, src->extension);
+ strcpy(dest->path, src->path);
+ strcpy(dest->name, src->name);
+ dest->mtime = src->mtime;
+}
+
+
+/**
* Prints the contents of the specified note.
*
* @return int Status of the cat operation.
diff --git a/src/note.h b/src/note.h
index 04a3404..8f6ad33 100644
--- a/src/note.h
+++ b/src/note.h
@@ -17,6 +17,8 @@
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
+#include <sys/stat.h>
+#include <time.h>
#include "common.h"
@@ -29,12 +31,17 @@ typedef struct {
char extension[32];
char path[256];
char name[256];
+ time_t mtime;
} note;
void note_new(note*, char*);
int note_create(note*, char*);
+int note_date_cmp(const void*, const void*);
+
+void note_cpy(note*, note*);
+
int note_edit(note*, char*);
int note_cat(note*);

Generated by cgit