Use Gio for cfgfiles

This commit is contained in:
TingPing 2014-08-26 18:12:37 -04:00
parent 316c5f4f19
commit 76d2ff07aa
5 changed files with 70 additions and 99 deletions

View File

@ -81,7 +81,7 @@ list_addentry (GSList ** list, char *cmd, char *name)
/* read it in from a buffer to our linked list */
static void
list_load_from_data (GSList ** list, char *ibuf, int size)
list_load_from_data (GSList ** list, char *ibuf, gsize size)
{
char cmd[384];
char name[128];
@ -114,36 +114,28 @@ list_load_from_data (GSList ** list, char *ibuf, int size)
}
void
list_loadconf (char *file, GSList ** list, char *defaultconf)
list_loadconf (char *filename, GSList ** list, char *defaultconf)
{
char *filebuf;
char *ibuf;
int fd;
struct stat st;
GFile *file;
char *data;
gsize len;
filebuf = g_build_filename (get_xdir (), file, NULL);
fd = g_open (filebuf, O_RDONLY | OFLAGS, 0);
g_free (filebuf);
file = hexchat_open_gfile (filename);
if (fd == -1)
if (!g_file_query_exists (file, NULL))
{
if (defaultconf)
list_load_from_data (list, defaultconf, strlen (defaultconf));
return;
}
if (fstat (fd, &st) != 0)
if (g_file_load_contents (file, NULL, &data, &len, NULL, NULL))
{
perror ("fstat");
abort ();
list_load_from_data (list, data, len);
g_free (data);
}
ibuf = malloc (st.st_size);
read (fd, ibuf, st.st_size);
close (fd);
list_load_from_data (list, ibuf, st.st_size);
free (ibuf);
g_object_unref (file);
}
void
@ -218,40 +210,25 @@ cfg_get_str (char *cfg, const char *var, char *dest, int dest_len)
}
}
static int
cfg_put_str (int fh, char *var, char *value)
int
cfg_put_str (GOutputStream *ostream, const char *var, const char *value)
{
char buf[512];
int len;
snprintf (buf, sizeof buf, "%s = %s\n", var, value);
len = strlen (buf);
return (write (fh, buf, len) == len);
return (stream_writef (ostream, "%s = %s\n", var, value) != 0);
}
int
cfg_put_color (int fh, int r, int g, int b, char *var)
cfg_put_color (GOutputStream *ostream, int r, int g, int b, char *var)
{
char buf[400];
int len;
snprintf (buf, sizeof buf, "%s = %04x %04x %04x\n", var, r, g, b);
len = strlen (buf);
return (write (fh, buf, len) == len);
return (stream_writef (ostream, "%s = %04x %04x %04x\n", var, r, g, b) != 0);
}
int
cfg_put_int (int fh, int value, char *var)
cfg_put_int (GOutputStream *ostream, int value, char *var)
{
char buf[400];
int len;
if (value == -1)
value = 1;
snprintf (buf, sizeof buf, "%s = %d\n", var, value);
len = strlen (buf);
return (write (fh, buf, len) == len);
return (stream_writef (ostream, "%s = %d\n", var, value) != 0);
}
int
@ -344,18 +321,6 @@ check_config_dir (void)
return g_access (get_xdir (), F_OK);
}
static char *
default_file (void)
{
static char *dfile = NULL;
if (!dfile)
{
dfile = g_build_filename (get_xdir (), "hexchat.conf", NULL);
}
return dfile;
}
/* Keep these sorted!! */
const struct prefs vars[] =
@ -955,13 +920,21 @@ make_dcc_dirs (void)
int
load_config (void)
{
GFile *file;
char *cfg, *sp;
int res, val, i;
g_assert(check_config_dir () == 0);
file = hexchat_open_gfile ("hexchat.conf");
if (!g_file_get_contents (default_file (), &cfg, NULL, NULL))
if (!g_file_load_contents (file, NULL, &cfg, NULL, NULL, NULL))
{
g_object_unref (file);
return -1;
}
g_object_unref (file);
/* If the config is incomplete we have the default values loaded */
load_default_config();
@ -1003,26 +976,26 @@ load_config (void)
int
save_config (void)
{
int fh, i;
char *config, *new_config;
GFile *file, *tmpfile;
GOutputStream *ostream;
GFileIOStream *tmpstream;
gboolean ret;
int i;
if (check_config_dir () != 0)
make_config_dirs ();
config = default_file ();
new_config = g_strconcat (config, ".new", NULL);
fh = g_open (new_config, OFLAGS | O_TRUNC | O_WRONLY | O_CREAT, 0600);
if (fh == -1)
tmpfile = g_file_new_tmp (NULL, &tmpstream, NULL);
if (!tmpfile)
{
g_free (new_config);
return 0;
}
ostream = g_io_stream_get_output_stream (G_IO_STREAM(tmpstream));
if (!cfg_put_str (fh, "version", PACKAGE_VERSION))
if (!cfg_put_str (ostream, "version", PACKAGE_VERSION))
{
close (fh);
g_free (new_config);
g_object_unref (tmpfile);
return 0;
}
@ -1032,19 +1005,17 @@ save_config (void)
switch (vars[i].type)
{
case TYPE_STR:
if (!cfg_put_str (fh, vars[i].name, (char *) &prefs + vars[i].offset))
if (!cfg_put_str (ostream, vars[i].name, (char *) &prefs + vars[i].offset))
{
close (fh);
g_free (new_config);
g_object_unref (tmpfile);
return 0;
}
break;
case TYPE_INT:
case TYPE_BOOL:
if (!cfg_put_int (fh, *((int *) &prefs + vars[i].offset), vars[i].name))
if (!cfg_put_int (ostream, *((int *) &prefs + vars[i].offset), vars[i].name))
{
close (fh);
g_free (new_config);
g_object_unref (tmpfile);
return 0;
}
}
@ -1052,23 +1023,15 @@ save_config (void)
}
while (vars[i].name);
if (close (fh) == -1)
{
g_free (new_config);
return 0;
}
g_object_unref (ostream);
#ifdef WIN32
g_unlink (config); /* win32 can't rename to an existing file */
#endif
if (g_rename (new_config, config) == -1)
{
g_free (new_config);
return 0;
}
g_free (new_config);
file = hexchat_open_gfile ("hexchat.conf");
ret = g_file_move (tmpfile, file, G_FILE_COPY_OVERWRITE, NULL, NULL, NULL, NULL);
return 1;
g_object_unref (tmpfile);
g_object_unref (file);
return ret;
}
static void

View File

@ -30,12 +30,13 @@ extern char *xdir;
extern const char * const languages[LANGUAGES_LENGTH];
char *cfg_get_str (char *cfg, const char *var, char *dest, int dest_len);
int cfg_put_str (GOutputStream *ostream, const char *var, const char *value);
int cfg_get_bool (char *var);
int cfg_get_int_with_result (char *cfg, char *var, int *result);
int cfg_get_int (char *cfg, char *var);
int cfg_put_int (int fh, int value, char *var);
int cfg_put_int (GOutputStream *ostream, int value, char *var);
int cfg_get_color (char *cfg, char *var, int *r, int *g, int *b);
int cfg_put_color (int fh, int r, int g, int b, char *var);
int cfg_put_color (GOutputStream *ostream, int r, int g, int b, char *var);
char *get_xdir (void);
int check_config_dir (void);
void load_default_config (void);
@ -44,7 +45,7 @@ int make_dcc_dirs (void);
int load_config (void);
int save_config (void);
void list_free (GSList ** list);
void list_loadconf (char *file, GSList ** list, char *defaultconf);
void list_loadconf (char *filename, GSList ** list, char *defaultconf);
int list_delentry (GSList ** list, char *name);
void list_addentry (GSList ** list, char *cmd, char *name);
int cmd_set (session *sess, char *tbuf, char *word[], char *word_eol[]);

View File

@ -645,7 +645,7 @@ get_sys_str (int with_cpu)
#endif
int
buf_get_line (char *ibuf, char **buf, int *position, int len)
buf_get_line (char *ibuf, char **buf, int *position, gsize len)
{
int pos = *position, spos = pos;

View File

@ -44,7 +44,7 @@ char *file_part (char *file);
void for_files (char *dirname, char *mask, void callback (char *file));
int rfc_casecmp (const char *, const char *);
int rfc_ncasecmp (char *, char *, int);
int buf_get_line (char *, char **, int *, int len);
int buf_get_line (char *, char **, int *, gsize len);
char *nocasestrstr (const char *text, const char *tofind);
char *country (char *);
void country_search (char *pattern, void *ud, void (*print)(void *, char *, ...));

View File

@ -152,26 +152,33 @@ palette_load (void)
void
palette_save (void)
{
int i, j, fh;
GFile *file;
GOutputStream *ostream;
int i, j;
char prefname[256];
fh = hexchat_open_file ("colors.conf", O_TRUNC | O_WRONLY | O_CREAT, 0600, XOF_DOMODE);
if (fh != -1)
file = hexchat_open_gfile ("colors.conf");
ostream = G_OUTPUT_STREAM(g_file_replace (file, NULL, FALSE, G_FILE_CREATE_NONE, NULL, NULL));
if (ostream)
{
/* mIRC colors 0-31 are here */
for (i = 0; i < 32; i++)
{
snprintf (prefname, sizeof prefname, "color_%d", i);
cfg_put_color (fh, colors[i].red, colors[i].green, colors[i].blue, prefname);
cfg_put_color (ostream, colors[i].red, colors[i].green, colors[i].blue, prefname);
}
/* our special colors are mapped at 256+ */
for (i = 256, j = 32; j < MAX_COL+1; i++, j++)
{
snprintf (prefname, sizeof prefname, "color_%d", i);
cfg_put_color (fh, colors[j].red, colors[j].green, colors[j].blue, prefname);
cfg_put_color (ostream, colors[j].red, colors[j].green, colors[j].blue, prefname);
}
close (fh);
g_object_unref (ostream);
}
g_object_unref (file);
}