/*
- * Copyright (C) 2010-2012 jeanfi@gmail.com
+ * Copyright (C) 2012-2013 jeanfi@gmail.com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
#include <sys/stat.h>
#include <sys/types.h>
+#include <log.h>
#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) {
+ log_err("HOME environment variable not defined");
+ return NULL;
+ }
dir = malloc(strlen(home) + 1 + strlen(".task") + 1);
sprintf(dir, "%s/%s", home, ".task");
path = malloc(strlen(dir) + 1 + strlen(uuid) + strlen(".note") + 1);
sprintf(path, "%s/%s.note", dir, uuid);
- printf("note_put %s %s %s\n", path, uuid, note);
+ free(dir);
+
+ return path;
+}
+
+void note_put(const char *uuid, const char *note)
+{
+ char *path;
+ FILE *f;
+
+ path = get_path(uuid);
+
+ if (!path)
+ return ;
+
+ log_debug("note_put %s %s %s", path, uuid, note);
f = fopen(path, "w");
fwrite(note, 1, strlen(note), f);
fclose(f);
} else {
- fprintf(stderr, "Failed to open %s\n", path);
+ log_err("Failed to open %s", 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 {
+ log_err("Failed to open %s", path);
+ }
+
+ return str;
+}