diff options
-rw-r--r-- | src/main.c | 21 | ||||
-rw-r--r-- | src/note.c | 38 | ||||
-rw-r--r-- | src/note.h | 7 |
3 files changed, 64 insertions, 2 deletions
@@ -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); } } @@ -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. @@ -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*); |