prepared code to add a way to change the notes directory
[ptask.git] / src / note.c
1 /*
2  * Copyright (C) 2012-2013 jeanfi@gmail.com
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of the
7  * License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  * 02110-1301 USA
18  */
19
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <sys/stat.h>
24 #include <sys/types.h>
25
26 #include <log.h>
27 #include <note.h>
28
29 static char *get_default_path(const char *uuid)
30 {
31         char *home, *dir, *path;
32
33         home = getenv("HOME");
34
35         if (!home) {
36                 log_err("HOME environment variable not defined");
37                 return NULL;
38         }
39
40         dir = malloc(strlen(home) + 1 + strlen(".task") + 1);
41         sprintf(dir, "%s/%s", home, ".task");
42         mkdir(dir, 0777);
43
44         path = malloc(strlen(dir) + 1 + strlen(uuid) + strlen(".note") + 1);
45         sprintf(path, "%s/%s.note", dir, uuid);
46
47         free(dir);
48
49         return path;
50 }
51
52 static char *get_path(const char *uuid)
53 {
54         return get_default_path(uuid);
55 }
56
57 void note_put(const char *uuid, const char *note)
58 {
59         char *path;
60         FILE *f;
61
62         path = get_path(uuid);
63
64         if (!path)
65                 return ;
66
67         log_debug("note_put %s %s %s", path, uuid, note);
68
69         f = fopen(path, "w");
70
71         if (f) {
72                 fwrite(note, 1, strlen(note), f);
73                 fclose(f);
74         } else {
75                 log_err("Failed to open %s", path);
76         }
77
78         free(path);
79 }
80
81 char *note_get(const char *uuid)
82 {
83         char *str, *tmp, *path;
84         FILE *f;
85         char buf[1024];
86         size_t s;
87
88         path = get_path(uuid);
89
90         if (!path)
91                 return NULL;
92
93         str = strdup("");
94
95         f = fopen(path, "r");
96
97         if (f) {
98                 while ((s = fread(buf, 1, 1024, f))) {
99                         tmp = malloc(strlen(str) + s + (size_t)1);
100                         memcpy(tmp, str, strlen(str));
101                         memcpy(tmp + strlen(str), buf, s);
102                         tmp[strlen(str) + s] = '\0';
103                         free(str);
104                         str = tmp;
105                 }
106                 fclose(f);
107         } else {
108                 log_debug("%s does not exist or cannot be opened", path);
109         }
110
111         return str;
112 }