2 * Copyright (C) 2011-2012 jeanfi@gmail.com
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.
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.
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
21 #define _(String) gettext(String)
32 static const char *cache_dir;
34 static const char *get_cache_dir()
39 home = getenv("HOME");
42 cache_dir = malloc(strlen(home)
43 + 1 + strlen(".ppastats/cache")
45 sprintf((char *)cache_dir,
46 "%s/%s", home, ".ppastats/cache");
48 mkdirs(cache_dir, 0777);
50 log_warn(_("$HOME not defined"));
57 static char *key_to_path(const char *key)
62 dir = get_cache_dir();
66 path = malloc(strlen(dir) + 1 + strlen(key) + strlen("/data.json") + 1);
67 sprintf(path, "%s%s/data.json", dir, key);
72 char *fcache_get(const char *key)
76 path = key_to_path(key);
80 content = file_get_content(path);
83 log_debug(_("file cache hit %s"), key);
85 log_debug(_("file cache miss %s %s"), key, path);
92 void fcache_put(const char *key, char *value)
94 char *path, *dir, *tmp;
97 path = key_to_path(key);
106 f = fopen(path, "w");
113 log_err(_("Failed to open %s"), path);
119 void fcache_cleanup()
121 free((char *)cache_dir);