(no commit message)
authorJean-Philippe Orsini <jeanfi@gmail.com>
Wed, 17 Oct 2012 10:28:44 +0000 (10:28 +0000)
committerJean-Philippe Orsini <jeanfi@gmail.com>
Wed, 17 Oct 2012 10:28:44 +0000 (10:28 +0000)
src/note.c
src/note.h
src/tw.c

index f22f97d..c5b225a 100644 (file)
 
 #include <note.h>
 
-void note_put(const char *uuid, const char *note)
+static char *get_path(const char *uuid)
 {
        char *home, *dir, *path;
-       FILE *f;
 
        home = getenv("HOME");
 
-       if (!home)
-               return ;
+       if (!home) {
+               fprintf(stderr, "HOME environment variable not defined\n");
+               return NULL;
+       }
 
        dir = malloc(strlen(home) + 1 + strlen(".task") + 1);
        sprintf(dir, "%s/%s", home, ".task");
@@ -42,6 +43,21 @@ void note_put(const char *uuid, const char *note)
        path = malloc(strlen(dir) + 1 + strlen(uuid) + strlen(".note") + 1);
        sprintf(path, "%s/%s.note", dir, uuid);
 
+       free(dir);
+
+       return path;
+}
+
+void note_put(const char *uuid, const char *note)
+{
+       char *path;
+       FILE *f;
+
+       path = get_path(uuid);
+
+       if (!path)
+               return ;
+
        printf("note_put %s %s %s\n", path, uuid, note);
 
        f = fopen(path, "w");
@@ -53,6 +69,38 @@ void note_put(const char *uuid, const char *note)
                fprintf(stderr, "Failed to open %s\n", path);
        }
 
-       free(dir);
        free(path);
 }
+
+char *note_get(const char *uuid)
+{
+       char *str, *tmp, *path;
+       FILE *f;
+       char buf[1024];
+       size_t s;
+
+       path = get_path(uuid);
+
+       if (!path)
+               return NULL;
+
+       str = strdup("");
+
+       f = fopen(path, "r");
+
+       if (f) {
+               while ((s = fread(buf, 1, 1024, f))) {
+                       tmp = malloc(strlen(str) + s + (size_t)1);
+                       memcpy(tmp, str, strlen(str));
+                       memcpy(tmp + strlen(str), buf, s);
+                       tmp[strlen(str) + s] = '\0';
+                       free(str);
+                       str = tmp;
+               }
+               fclose(f);
+       } else {
+               fprintf(stderr, "Failed to open %s\n", path);
+       }
+
+       return str;
+}
index 1462480..17b98f5 100644 (file)
@@ -23,5 +23,6 @@
 #include "tw.h"
 
 void note_put(const char *uuid, const char *note);
+char *note_get(const char *uuid);
 
 #endif
index 1e9a23c..c92c1ee 100644 (file)
--- a/src/tw.c
+++ b/src/tw.c
@@ -21,6 +21,7 @@
 #include <stdlib.h>
 #include <string.h>
 
+#include "note.h"
 
 char *task_exec(char *opts)
 {
@@ -137,7 +138,7 @@ struct task **tw_get_all_tasks(const char *status)
                json = json_object_object_get(jtask, "uuid");
                tasks[i]->uuid = strdup(json_object_get_string(json));
 
-               tasks[i]->note = NULL;
+               tasks[i]->note = note_get(tasks[i]->uuid);
        }
 
        tasks[n] = NULL;