1
0
mirror of https://github.com/irssi/irssi.git synced 2024-09-29 04:45:57 -04:00

Use an io channel to write the config file.

git-svn-id: file:///var/www/svn.irssi.org/SVN/irssi/trunk@4990 dbcabf3a-b0e7-0310-adc4-f8d773084564
This commit is contained in:
Emanuele Giaquinta 2009-01-16 17:12:27 +00:00 committed by exg
parent d993ce7b06
commit 4534ef242d
3 changed files with 20 additions and 18 deletions

View File

@ -48,7 +48,6 @@ struct _CONFIG_NODE {
struct _CONFIG_REC {
char *fname;
int handle;
int create_mode;
int modifycounter; /* increase every time something is changed */
@ -60,6 +59,7 @@ struct _CONFIG_REC {
GScanner *scanner;
/* while writing to configuration file.. */
GIOChannel *handle;
int tmp_indent_level; /* indentation position */
int tmp_last_lf; /* last character was a line feed */
};

View File

@ -269,20 +269,21 @@ void config_parse_init(CONFIG_REC *rec, const char *name)
int config_parse(CONFIG_REC *rec)
{
int fd;
g_return_val_if_fail(rec != NULL, -1);
g_return_val_if_fail(rec->fname != NULL, -1);
rec->handle = open(rec->fname, O_RDONLY);
if (rec->handle == -1)
fd = open(rec->fname, O_RDONLY);
if (fd == -1)
return config_error(rec, g_strerror(errno));
config_parse_init(rec, rec->fname);
g_scanner_input_file(rec->scanner, rec->handle);
g_scanner_input_file(rec->scanner, fd);
config_parse_loop(rec, rec->mainnode, G_TOKEN_EOF);
g_scanner_destroy(rec->scanner);
close(rec->handle);
rec->handle = -1;
close(fd);
return rec->last_error == NULL ? 0 : -1;
}
@ -310,7 +311,6 @@ CONFIG_REC *config_open(const char *fname, int create_mode)
rec = g_new0(CONFIG_REC, 1);
rec->fname = fname == NULL ? NULL : g_strdup(fname);
rec->handle = -1;
rec->create_mode = create_mode;
rec->mainnode = g_new0(CONFIG_NODE, 1);
rec->mainnode->type = NODE_TYPE_BLOCK;
@ -327,7 +327,6 @@ void config_close(CONFIG_REC *rec)
config_nodes_remove_all(rec);
g_free(rec->mainnode);
if (rec->handle != -1) close(rec->handle);
g_hash_table_foreach(rec->cache, (GHFunc) g_free, NULL);
g_hash_table_destroy(rec->cache);
g_hash_table_destroy(rec->cache_nodes);

View File

@ -32,7 +32,8 @@ static int config_write_indent(CONFIG_REC *rec)
int n;
for (n = 0; n < rec->tmp_indent_level/CONFIG_INDENT_SIZE; n++) {
if (write(rec->handle, indent_block, CONFIG_INDENT_SIZE) == -1)
if (g_io_channel_write_chars(rec->handle, indent_block, CONFIG_INDENT_SIZE,
NULL, NULL) == G_IO_STATUS_ERROR)
return -1;
}
@ -57,12 +58,14 @@ static int config_write_str(CONFIG_REC *rec, const char *str)
p = strchr(strpos, '\n');
if (p == NULL) {
if (write(rec->handle, strpos, strlen(strpos)) == -1)
if (g_io_channel_write_chars(rec->handle, strpos, strlen(strpos),
NULL, NULL) == G_IO_STATUS_ERROR)
return -1;
strpos = "";
rec->tmp_last_lf = FALSE;
} else {
if (write(rec->handle, strpos, (int) (p-strpos)+1) == -1)
if (g_io_channel_write_chars(rec->handle, strpos, (int) (p-strpos)+1,
NULL, NULL) == G_IO_STATUS_ERROR)
return -1;
strpos = p+1;
rec->tmp_last_lf = TRUE;
@ -297,20 +300,20 @@ static int config_write_block(CONFIG_REC *rec, CONFIG_NODE *node, int list, int
int config_write(CONFIG_REC *rec, const char *fname, int create_mode)
{
int ret;
int fd;
g_return_val_if_fail(rec != NULL, -1);
g_return_val_if_fail(fname != NULL || rec->fname != NULL, -1);
g_return_val_if_fail(create_mode != -1 || rec->create_mode != -1, -1);
if (rec->handle != -1)
close(rec->handle);
rec->handle = open(fname != NULL ? fname : rec->fname,
fd = open(fname != NULL ? fname : rec->fname,
O_WRONLY | O_TRUNC | O_CREAT,
create_mode != -1 ? create_mode : rec->create_mode);
if (rec->handle == -1)
if (fd == -1)
return config_error(rec, g_strerror(errno));
rec->handle = g_io_channel_unix_new(fd);
g_io_channel_set_encoding(rec->handle, NULL, NULL);
rec->tmp_indent_level = 0;
rec->tmp_last_lf = TRUE;
ret = config_write_block(rec, rec->mainnode, FALSE, TRUE);
@ -319,8 +322,8 @@ int config_write(CONFIG_REC *rec, const char *fname, int create_mode)
config_error(rec, errno == 0 ? "bug" : g_strerror(errno));
}
close(rec->handle);
rec->handle = -1;
g_io_channel_unref(rec->handle);
rec->handle = NULL;
return ret;
}