Compare commits

...

7 Commits

Author SHA1 Message Date
TingPing eafc1e2b70 Use Gio for url logger 2016-01-31 16:20:10 -05:00
TingPing 06d323025d Use Gio for load_perform_file() 2016-01-31 16:20:10 -05:00
TingPing c0c2668c10 Use Gio for random_line() 2016-01-31 16:20:10 -05:00
TingPing 7cf631f93c Create convenience function to get datastreams 2016-01-31 16:16:18 -05:00
TingPing f5142c6724 Use Gio for cfgfiles 2016-01-31 16:15:14 -05:00
TingPing 3ba5581c6b Move writeline func to cfgfiles 2016-01-31 15:40:16 -05:00
TingPing d825b3514b Use Gio for servlist.conf 2016-01-31 15:39:08 -05:00
11 changed files with 282 additions and 224 deletions

View File

@ -77,7 +77,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];
@ -110,36 +110,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 = g_malloc (st.st_size);
read (fd, ibuf, st.st_size);
close (fd);
list_load_from_data (list, ibuf, st.st_size);
g_free (ibuf);
g_object_unref (file);
}
void
@ -214,40 +206,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;
g_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, guint16 r, guint16 g, guint16 b, char *var)
cfg_put_color (GOutputStream *ostream, guint16 r, guint16 g, guint16 b, char *var)
{
char buf[400];
int len;
g_snprintf (buf, sizeof buf, "%s = %04hx %04hx %04hx\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;
g_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
@ -338,18 +315,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[] =
@ -953,13 +918,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();
@ -1001,26 +974,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;
}
@ -1030,19 +1003,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;
}
}
@ -1050,23 +1021,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
@ -1357,3 +1320,69 @@ hexchat_fopen_file (const char *file, const char *mode, int xof_flags)
return fh;
}
/*
* Returns a #GFile* to a file in HexChat's config dir.
* Must be g_object_unref()'d when done.
* @filename must be in utf8 encoding.
*/
GFile *
hexchat_open_gfile (const char *filename)
{
GFile *file;
gchar *full_path, *full_path_fs;
if (g_path_is_absolute (filename))
full_path = g_strdup (filename);
else
full_path = g_build_filename (get_xdir(), filename, NULL);
full_path_fs = g_filename_from_utf8 (full_path, -1, NULL, NULL, NULL);
file = g_file_new_for_path (full_path_fs);
g_free (full_path);
g_free (full_path_fs);
return file;
}
G_GNUC_PRINTF (2, 3)
gsize
stream_writef (GOutputStream *ostream, const char *fmt, ...)
{
char *tmp;
va_list args;
gint len;
gsize ret;
va_start (args, fmt);
len = g_vasprintf (&tmp, fmt, args);
va_end (args);
ret = g_output_stream_write (ostream, tmp, len, NULL, NULL);
g_free (tmp);
return ret;
}
GDataInputStream *
file_get_datainputstream (GFile *file)
{
GInputStream *stream;
GDataInputStream *datastream;
stream = G_INPUT_STREAM(g_file_read (file, NULL, NULL));
if (!stream)
return NULL;
datastream = g_data_input_stream_new (stream);
/*
* This is to avoid any issues moving between windows/unix
* but the docs mention an invalid \r without a following \n
* can lock up the program
*/
g_data_input_stream_set_newline_type (datastream, G_DATA_STREAM_NEWLINE_TYPE_ANY);
g_object_unref (stream);
return datastream;
}

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, guint16 *r, guint16 *g, guint16 *b);
int cfg_put_color (int fh, guint16 r, guint16 g, guint16 b, char *var);
int cfg_put_color (GOutputStream *ostream, guint16 r, guint16 g, guint16 b, char *var);
char *get_xdir (void);
int check_config_dir (void);
void load_default_config (void);
@ -44,12 +45,15 @@ 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[]);
int hexchat_open_file (const char *file, int flags, int mode, int xof_flags);
FILE *hexchat_fopen_file (const char *file, const char *mode, int xof_flags);
GFile *hexchat_open_gfile (const char *filename);
gsize stream_writef (GOutputStream *ostream, const char *fmt, ...) G_GNUC_PRINTF (2, 3);
GDataInputStream *file_get_datainputstream (GFile *file);
#define XOF_DOMODE 1
#define XOF_FULLPATH 2

View File

@ -78,40 +78,72 @@ notc_msg (struct session *sess)
static char *
random_line (char *file_name)
{
FILE *fh;
char buf[512];
int lines, ran;
GFile *file;
char *data, *p, *ret = NULL;
int lines = 0, ran;
gsize len;
if (!file_name[0])
goto nofile;
return g_strdup ("");
fh = hexchat_fopen_file (file_name, "r", 0);
if (!fh)
file = hexchat_open_gfile (file_name);
if (!g_file_query_exists (file, NULL))
{
nofile:
/* reason is not a file, an actual reason! */
return g_strdup (file_name);
}
/* count number of lines in file */
lines = 0;
while (fgets (buf, sizeof (buf), fh))
lines++;
if (lines < 1)
goto nofile;
/* go down a random number */
rewind (fh);
ran = RAND_INT (lines);
do
if (!g_file_load_contents (file, NULL, &data, &len, NULL, NULL))
{
fgets (buf, sizeof (buf), fh);
lines--;
g_object_unref (file);
return g_strdup (file_name);
}
while (lines > ran);
fclose (fh);
return g_strdup (buf);
g_object_unref (file);
/* count number of lines in file */
p = data;
while (p != data + len)
{
if (*p == '\n')
lines++;
p++;
}
if (!lines)
{
g_free (data);
return g_strdup (file_name);
}
/* create random number */
ran = RAND_INT (lines);
/* get that random line */
p = data;
while (p != data + len)
{
if (*p == '\n')
{
if (!--ran)
{
char *end;
end = strchr (++p, '\n');
*end = '\0';
ret = g_strdup (p);
break;
}
}
p++;
}
g_free (data);
if (ret)
return ret;
else
return g_strdup (file_name);
}
void
@ -2487,30 +2519,32 @@ cmd_list (struct session *sess, char *tbuf, char *word[], char *word_eol[])
}
gboolean
load_perform_file (session *sess, char *file)
load_perform_file (session *sess, char *filename)
{
char tbuf[1024 + 4];
char *nl;
FILE *fp;
GFile *file;
GDataInputStream *istream;
gchar *buf;
fp = hexchat_fopen_file (file, "r", 0); /* load files from config dir */
if (!fp)
return FALSE;
file = hexchat_open_gfile (filename);
tbuf[1024] = 0;
while (fgets (tbuf, 1024, fp))
istream = file_get_datainputstream (file);
if (!istream)
{
nl = strchr (tbuf, '\n');
if (nl == tbuf) /* skip empty commands */
continue;
if (nl)
*nl = 0;
if (tbuf[0] == prefs.hex_input_command_char[0])
handle_command (sess, tbuf + 1, TRUE);
else
handle_command (sess, tbuf, TRUE);
g_object_unref (file);
return FALSE;
}
fclose (fp);
while ((buf = g_data_input_stream_read_line_utf8 (istream, NULL, NULL, NULL)))
{
if (g_str_has_prefix (buf, prefs.hex_input_command_char))
handle_command (sess, buf + 1, TRUE);
else
handle_command (sess, buf, TRUE);
g_free (buf);
}
g_object_unref (file);
g_object_unref (istream);
return TRUE;
}

View File

@ -998,9 +998,9 @@ servlist_load_defaults (void)
static int
servlist_load (void)
{
FILE *fp;
char buf[2048];
int len;
GFile *file;
GDataInputStream *istream;
gchar *buf;
ircnet *net = NULL;
/* simple migration we will keep for a short while */
@ -1015,15 +1015,16 @@ servlist_load (void)
g_free (oldfile);
g_free (newfile);
fp = hexchat_fopen_file ("servlist.conf", "r", 0);
if (!fp)
file = hexchat_open_gfile ("servlist.conf");
istream = file_get_datainputstream (file);
if (!istream)
return FALSE;
while (fgets (buf, sizeof (buf) - 2, fp))
while ((buf = g_data_input_stream_read_line_utf8 (istream, NULL, NULL, NULL)))
{
len = strlen (buf);
buf[len] = 0;
buf[len-1] = 0; /* remove the trailing \n */
if (net)
{
switch (buf[0])
@ -1096,8 +1097,12 @@ servlist_load (void)
}
if (buf[0] == 'N')
net = servlist_net_add (buf + 2, /* comment */ NULL, FALSE);
g_free (buf);
}
fclose (fp);
g_object_unref (file);
g_object_unref (istream);
return TRUE;
}
@ -1138,8 +1143,9 @@ servlist_check_encoding (char *charset)
int
servlist_save (void)
{
FILE *fp;
char *buf;
GFile *file;
GOutputStream *ostream;
gchar *buf;
ircnet *net;
ircserver *serv;
commandentry *cmd;
@ -1148,52 +1154,37 @@ servlist_save (void)
GSList *netlist;
GSList *cmdlist;
GSList *favlist;
#ifndef WIN32
int first = FALSE;
buf = g_build_filename (get_xdir (), "servlist.conf", NULL);
if (g_access (buf, F_OK) != 0)
first = TRUE;
#endif
file = hexchat_open_gfile ("servlist.conf");
fp = hexchat_fopen_file ("servlist.conf", "w", 0);
if (!fp)
{
#ifndef WIN32
g_free (buf);
#endif
ostream = G_OUTPUT_STREAM(g_file_replace (file, NULL, TRUE,
G_FILE_CREATE_PRIVATE, NULL, NULL));
if (!ostream)
return FALSE;
}
#ifndef WIN32
if (first)
g_chmod (buf, 0600);
g_free (buf);
#endif
fprintf (fp, "v=" PACKAGE_VERSION "\n\n");
stream_writef (ostream, "v=%s\n\n", PACKAGE_VERSION);
list = network_list;
while (list)
{
net = list->data;
fprintf (fp, "N=%s\n", net->name);
stream_writef (ostream, "N=%s\n", net->name);
if (net->nick)
fprintf (fp, "I=%s\n", net->nick);
stream_writef (ostream, "I=%s\n", net->nick);
if (net->nick2)
fprintf (fp, "i=%s\n", net->nick2);
stream_writef (ostream, "i=%s\n", net->nick2);
if (net->user)
fprintf (fp, "U=%s\n", net->user);
stream_writef (ostream, "U=%s\n", net->user);
if (net->real)
fprintf (fp, "R=%s\n", net->real);
stream_writef (ostream, "R=%s\n", net->real);
if (net->pass)
fprintf (fp, "P=%s\n", net->pass);
stream_writef (ostream, "P=%s\n", net->pass);
if (net->logintype)
fprintf (fp, "L=%d\n", net->logintype);
stream_writef (ostream, "L=%d\n", net->logintype);
if (net->encoding)
{
fprintf (fp, "E=%s\n", net->encoding);
stream_writef (ostream, "E=%s\n", net->encoding);
if (!servlist_check_encoding (net->encoding))
{
buf = g_strdup_printf (_("Warning: \"%s\" character set is unknown. No conversion will be applied for network %s."),
@ -1203,13 +1194,13 @@ servlist_save (void)
}
}
fprintf (fp, "F=%d\nD=%d\n", net->flags, net->selected);
stream_writef (ostream, "F=%d\nD=%d\n", net->flags, net->selected);
netlist = net->servlist;
while (netlist)
{
serv = netlist->data;
fprintf (fp, "S=%s\n", serv->hostname);
stream_writef (ostream, "S=%s\n", serv->hostname);
netlist = netlist->next;
}
@ -1217,7 +1208,7 @@ servlist_save (void)
while (cmdlist)
{
cmd = cmdlist->data;
fprintf (fp, "C=%s\n", cmd->command);
stream_writef (ostream, "C=%s\n", cmd->command);
cmdlist = cmdlist->next;
}
@ -1228,26 +1219,24 @@ servlist_save (void)
if (favchan->key)
{
fprintf (fp, "J=%s,%s\n", favchan->name, favchan->key);
stream_writef (ostream, "J=%s,%s\n", favchan->name, favchan->key);
}
else
{
fprintf (fp, "J=%s\n", favchan->name);
stream_writef (ostream, "J=%s\n", favchan->name);
}
favlist = favlist->next;
}
if (fprintf (fp, "\n") < 1)
{
fclose (fp);
if (!stream_writef (ostream, "\n"))
return FALSE;
}
list = list->next;
}
fclose (fp);
g_object_unref (file);
g_object_unref (ostream);
return TRUE;
}

View File

@ -218,7 +218,6 @@ scrollback_save (session *sess, char *text, time_t stamp)
void
scrollback_load (session *sess)
{
GInputStream *stream;
GDataInputStream *istream;
gchar *buf, *text;
gint lines = 0;
@ -244,19 +243,10 @@ scrollback_load (session *sess)
g_free (buf);
}
stream = G_INPUT_STREAM(g_file_read (sess->scrollfile, NULL, NULL));
if (!stream)
istream = file_get_datainputstream (sess->scrollfile);
if (!istream)
return;
istream = g_data_input_stream_new (stream);
/*
* This is to avoid any issues moving between windows/unix
* but the docs mention an invalid \r without a following \n
* can lock up the program... (Our write() always adds \n)
*/
g_data_input_stream_set_newline_type (istream, G_DATA_STREAM_NEWLINE_TYPE_ANY);
g_object_unref (stream);
while (1)
{
GError *err = NULL;

View File

@ -68,42 +68,47 @@ url_clear (void)
}
static int
url_save_cb (char *url, FILE *fd)
url_save_cb (char *url, GOutputStream *ostream)
{
fprintf (fd, "%s\n", url);
stream_writef (ostream, "%s\n", url);
return TRUE;
}
void
url_save_tree (const char *fname, const char *mode, gboolean fullpath)
url_save_tree (const char *fname)
{
FILE *fd;
GFile *file;
GOutputStream *ostream;
if (fullpath)
fd = hexchat_fopen_file (fname, mode, XOF_FULLPATH);
else
fd = hexchat_fopen_file (fname, mode, 0);
if (fd == NULL)
return;
file = hexchat_open_gfile (fname);
tree_foreach (url_tree, (tree_traverse_func *)url_save_cb, fd);
fclose (fd);
ostream = G_OUTPUT_STREAM(g_file_append_to (file, G_FILE_CREATE_NONE, NULL, NULL));
if (ostream)
{
tree_foreach (url_tree, (tree_traverse_func *)url_save_cb, ostream);
g_object_unref (ostream);
}
g_object_unref (file);
}
static void
url_save_node (char* url)
{
FILE *fd;
GFile *file;
GOutputStream *ostream;
/* open <config>/url.log in append mode */
fd = hexchat_fopen_file ("url.log", "a", 0);
if (fd == NULL)
file = hexchat_open_gfile ("url.log");
ostream = G_OUTPUT_STREAM(g_file_append_to (file, G_FILE_CREATE_NONE, NULL, NULL));
if (ostream)
{
return;
stream_writef (ostream, "%s\n", url);
g_object_unref (ostream);
}
fprintf (fd, "%s\n", url);
fclose (fd);
g_object_unref (file);
}
static int

View File

@ -33,7 +33,7 @@ extern void *url_tree;
#define WORD_PATH -2
void url_clear (void);
void url_save_tree (const char *fname, const char *mode, gboolean fullpath);
void url_save_tree (const char *fname);
int url_last (int *, int *);
int url_check_word (const char *word);
void url_check_line (char *buf);

View File

@ -530,7 +530,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

@ -146,26 +146,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++)
{
g_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++)
{
g_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);
}

View File

@ -138,7 +138,7 @@ url_save_callback (void *arg1, char *file)
{
if (file)
{
url_save_tree (file, "w", TRUE);
url_save_tree (file);
}
}