X-Git-Url: https://git.wpitchoune.net/gitweb/?a=blobdiff_plain;f=src%2Fnote.c;h=247d8d9a4e7eec80aaf2ba1888032f0ab848f0c2;hb=d90d80c49709335e9101657d1a82cdd964e14c40;hp=f22f97db21d2a9ab9abc56585479588789850317;hpb=8036222a5a9454b6af7667ffdaf1efdd6f077633;p=ptask.git diff --git a/src/note.c b/src/note.c index f22f97d..247d8d9 100644 --- a/src/note.c +++ b/src/note.c @@ -1,5 +1,5 @@ /* - * 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 @@ -23,26 +23,66 @@ #include #include +#include #include +#include +#include -void note_put(const char *uuid, const char *note) +static const char *NOTE_SUF = ".note"; + +static char *get_default_path() { - char *home, *dir, *path; - FILE *f; + char *home, *dir; 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"); mkdir(dir, 0777); - path = malloc(strlen(dir) + 1 + strlen(uuid) + strlen(".note") + 1); - sprintf(path, "%s/%s.note", dir, uuid); + return dir; +} + +static char *get_path(const char *uuid) +{ + const char *sdir; + char *path, *dir; + + sdir = settings_get_notes_dir(); + + if (sdir == NULL || *sdir == '\0') { + dir = get_default_path(); + settings_set_notes_dir(dir); + } else { + dir = strdup(sdir); + } + + mkdirs(dir, 0777); + + path = malloc(strlen(dir) + 1 + strlen(uuid) + strlen(NOTE_SUF) + 1); + sprintf(path, "%s/%s%s", dir, uuid, NOTE_SUF); + + 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); + log_debug("note_put %s %s %s", path, uuid, note); f = fopen(path, "w"); @@ -50,9 +90,41 @@ void note_put(const char *uuid, const char *note) 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_debug("%s does not exist or cannot be opened", path); + } + + return str; +}