1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
|
/*
Copyright (c) 2001-2007 Michael Terry
Copyright (c) 2011 David Hull
Copyright (c) 2013-2014 Arthur Borsboom
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "../config.h"
#include <stdlib.h>
#include <string.h>
#include <gio/gio.h>
#include <glib/gstdio.h>
#include <glib/gi18n.h>
#include "fio.h"
#include "xpad-app.h"
/*
* Sets filename to full path of filename.
* If the path is relative, prepends a path depending on the parameter: config dir or current work dir.
* Returns a GFile representing the file.
*/
static GFile *
fio_fill_filename (const gchar *filename, DirectoryType dirType)
{
if (g_path_is_absolute (filename)) {
return g_file_new_for_path (filename);
} else {
gchar *full_path;
const gchar *path;
GFile *file;
switch (dirType) {
case CONFIG_DIR:
path = xpad_app_get_config_dir ();
break;
case CURRENT_WORK_DIR:
path = g_get_current_dir();
break;
default:
g_critical ("Unexpected directory type encountered. Falling back to Xpad config folder.");
path = xpad_app_get_config_dir ();
break;
}
full_path = g_build_filename (path, filename, NULL);
file = g_file_new_for_path (full_path);
g_free (full_path);
return file;
}
}
/* This function returns 'string' with all instances
of 'obj' replaced with instances of 'replacement'
It modifies string and re-allocs it.
*/
gchar *str_replace_tokens (gchar **string, gchar obj, gchar *replacement)
{
gchar *p;
gsize rsize = strlen (replacement);
gsize osize = 1;
gsize diff = rsize - osize;
p = *string;
while ((p = strchr (p, obj)))
{
long offset = p - *string;
*string = g_realloc (*string, strlen (*string) + diff + 1);
p = *string + offset;
memmove (p + rsize, p + osize, strlen (p + osize) + 1);
memcpy (p, replacement, rsize);
p = p + rsize;
}
return *string;
}
gchar *
fio_unique_name (const gchar *prefix)
{
int fd;
gchar *name, *base, *pattern;
pattern = g_strconcat (prefix, "XXXXXX", NULL);
name = g_build_filename (xpad_app_get_config_dir (), pattern, NULL);
g_free (pattern);
fd = g_mkstemp (name);
if (fd == -1)
{
g_free (name);
return NULL;
}
close (fd);
base = g_path_get_basename (name);
g_free (name);
return base;
}
/* This callously overwrites name~ -- but this is fine since this function is for
our private .xpad directory anyway */
gboolean fio_set_file (const gchar *name, const gchar *value)
{
GFile *file;
GFileOutputStream *stream;
GError *error = NULL;
file = fio_fill_filename (name, CONFIG_DIR);
stream = g_file_replace (file, NULL, FALSE, G_FILE_CREATE_PRIVATE, NULL, &error);
if (stream)
{
g_output_stream_write_all (G_OUTPUT_STREAM (stream), value, strlen (value),
NULL, NULL, &error);
g_clear_object (&stream);
}
if (error)
{
gchar *usertext;
gchar *parse_name;
parse_name = g_file_get_parse_name (file);
usertext = g_strdup_printf (_("Could not write to file %s: %s"), parse_name, error->message);
xpad_app_error (NULL, usertext, NULL);
g_error_free (error);
g_free (usertext);
g_free (parse_name);
}
g_clear_object (&file);
return !error;
}
/*
* Returned gchar * must be g_free'd.
*/
gchar *fio_get_file (const gchar *filename, DirectoryType dirType)
{
GFile *file;
gchar *contents = NULL;
file = fio_fill_filename (filename, dirType);
g_file_load_contents (file, NULL, &contents, NULL, NULL, NULL);
g_clear_object (&file);
return contents;
}
/*
* list is a variable number of (gchar *) / (gchar ** or gint *) groups,
* terminated by a NULL variable. Each gchar ** pointer will hereafter
* point to memory that must be g_free'd.
*/
gint fio_get_values_from_file (const gchar *filename, ...)
{
gchar *buf;
const gchar *item;
va_list ap;
size_t len;
buf = fio_get_file (filename, CONFIG_DIR);
if (!buf)
return 1;
/*
* because of the way we look for a matching variable name, which is
* to look for an endline, the variable name, and a space, we insert a
* newline at the beginning, so that the first variable name is caught.
*/
len = strlen (buf);
buf = g_realloc (buf, len + 2);
memmove (buf + 1, buf, len + 1);
buf[0] = '\n';
va_start (ap, filename);
while ((item = va_arg (ap, gchar *)))
{
gchar *fullitem;
gint *value;
gchar *where;
gsize size;
gchar type;
type = item[0];
item = &item[2]; /* skip type and '|' */
fullitem = g_strdup_printf ("\n%s ", item);
value = va_arg (ap, void *);
where = strstr (buf, fullitem);
if (where)
{
gchar *temp;
where = strstr (where, " ") + 1;
size = strcspn (where, "\n");
temp = g_malloc (size + 1);
strncpy (temp, where, size);
temp[size] = '\0';
switch (type)
{
case 'i':
*((gint *) value) = atoi (temp);
break;
case 'u':
*((guint *) value) = (guint) strtoul (temp, NULL, 0);
break;
case 'h':
*((guint16 *) value) = (guint16) strtoul (temp, NULL, 0);
break;
case 's':
g_free (*((gchar **) value));
*((gchar **) value) = g_strdup (temp);
break;
case 'b':
*((gboolean *) value) = atoi (temp) ? TRUE : FALSE;
break;
default:
g_warning ("Bad type to fio_get_values_from_file: %c\n", type);
break;
}
g_free (temp);
}
g_free (fullitem);
}
va_end (ap);
g_free (buf);
return 0;
}
/* list is a variable number of (gchar *) / (gchar ** or gint *) groups,
terminated by a NULL variable */
gint fio_set_values_to_file (const gchar *filename, ...)
{
gchar *buf, *tmpbuf;
const gchar *item;
va_list ap;
va_start (ap, filename);
buf = g_strdup ("");
while ((item = va_arg (ap, gchar *)))
{
gchar *final_string;
gchar *value_string;
gchar type;
type = item[0];
item = &item[2]; /* skip type and '|' */
/* translate our types to printf types */
switch (type)
{
case 'b':
value_string = g_strdup_printf ("%i", va_arg (ap, gboolean));
break;
case 'h':
case 'i':
value_string = g_strdup_printf ("%i", va_arg (ap, gint));
break;
case 'u':
value_string = g_strdup_printf ("%u", va_arg (ap, guint));
break;
case 's':
value_string = g_strdup_printf ("%s", va_arg (ap, gchar *));
break;
default:
g_warning ("Bad type to fio_set_values_to_file: %c\n", type);
value_string = g_strdup ("");
break;
}
final_string = g_strdup_printf ("%s%s %s", (buf[0] == 0) ? "" : "\n", item, value_string);
g_free (value_string);
tmpbuf = buf;
buf = g_strconcat (buf, final_string, NULL);
g_free (tmpbuf);
g_free (final_string);
}
va_end (ap);
tmpbuf = buf;
buf = g_strconcat (buf, "\n", NULL);
g_free (tmpbuf);
if (!fio_set_file (filename, buf))
{
g_free (buf);
return 1;
}
g_free (buf);
return 0;
}
/* Remove a file from the configuration directory */
void fio_remove_file (const gchar *filename)
{
GFile *file;
file = fio_fill_filename (filename, CONFIG_DIR);
g_file_delete (file, NULL, NULL);
g_clear_object (&file);
}
|