/*
* Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 2002 Match Grun
+ * Copyright (C) 2002-2012 Match Grun and the Claws Mail team
*
* 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 2 of the License, or
+ * 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,
* 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.
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
*/
/*
#include "xml.h"
#include "mgutils.h"
#include "xmlprops.h"
+#include "utils.h"
/* Element tag names */
#define XMLS_ELTAG_PROP_LIST "property-list"
#define XMLS_ATTAG_NAME "name"
#define XMLS_ATTAG_VALUE "value"
+static void xmlprops_clear ( XmlProperty *props );
+
+typedef struct _HashLoopData {
+ FILE *fp;
+ int error;
+} HashLoopData;
+
/*
* Create new props.
*/
* Properties - file path.
*/
void xmlprops_set_path( XmlProperty *props, const gchar *value ) {
- g_return_if_fail( props != NULL );
+ cm_return_if_fail( props != NULL );
props->path = mgu_replace_string( props->path, value );
}
-void xmlprops_set_encoding( XmlProperty *props, const gchar *value ) {
- g_return_if_fail( props != NULL );
- props->encoding = mgu_replace_string( props->encoding, value );
-}
/*
* Free hash table visitor function.
* Clear all properties.
* Enter: props Property object.
*/
-void xmlprops_clear( XmlProperty *props ) {
- g_return_if_fail( props != NULL );
+static void xmlprops_clear( XmlProperty *props ) {
+ cm_return_if_fail( props != NULL );
g_hash_table_foreach_remove(
props->propertyTable, xmlprops_free_entry_vis, NULL );
}
* Enter: props Property object.
*/
void xmlprops_free( XmlProperty *props ) {
- g_return_if_fail( props != NULL );
+ cm_return_if_fail( props != NULL );
/* Clear property table */
xmlprops_clear( props );
g_free( props );
}
-static void xmlprops_write_elem_s( FILE *fp, gint lvl, gchar *name ) {
+static int xmlprops_write_elem_s( FILE *fp, gint lvl, gchar *name ) {
gint i;
- for( i = 0; i < lvl; i++ ) fputs( " ", fp );
- fputs( "<", fp );
- fputs( name, fp );
+ for( i = 0; i < lvl; i++ ) {
+ if(fputs( " ", fp ) == EOF)
+ return -1;
+ }
+ if(fputs( "<", fp ) == EOF)
+ return -1;
+ if(fputs( name, fp ) == EOF)
+ return -1;
+
+ return 0;
}
-static void xmlprops_write_elem_e( FILE *fp, gint lvl, gchar *name ) {
+static int xmlprops_write_elem_e( FILE *fp, gint lvl, gchar *name ) {
gint i;
- for( i = 0; i < lvl; i++ ) fputs( " ", fp );
- fputs( "</", fp );
- fputs( name, fp );
- fputs( ">\n", fp );
+ for( i = 0; i < lvl; i++ ) {
+ if(fputs( " ", fp ) == EOF)
+ return -1;
+ }
+ if(fputs( "</", fp ) == EOF)
+ return -1;
+ if(fputs( name, fp ) == EOF)
+ return -1;
+ if(fputs( ">\n", fp ) == EOF)
+ return -1;
+
+ return 0;
}
-static void xmlprops_write_attr( FILE *fp, gchar *name, gchar *value ) {
- fputs( " ", fp );
- fputs( name, fp );
- fputs( "=\"", fp );
- xml_file_put_escape_str( fp, value );
- fputs( "\"", fp );
+static int xmlprops_write_attr( FILE *fp, gchar *name, gchar *value ) {
+ if(fputs( " ", fp ) == EOF)
+ return -1;
+ if(fputs( name, fp ) == EOF)
+ return -1;
+ if(fputs( "=\"", fp ) == EOF)
+ return -1;
+ if(xml_file_put_escape_str( fp, value ) < 0)
+ return -1;
+ if(fputs( "\"", fp ) == EOF)
+ return -1;
+
+ return 0;
}
-static void xmlprops_write_vis( gpointer key, gpointer value, gpointer data ) {
- FILE *fp = ( FILE * ) data;
-
- xmlprops_write_elem_s( fp, 1, XMLS_ELTAG_PROPERTY );
- xmlprops_write_attr( fp, XMLS_ATTAG_NAME, key );
- xmlprops_write_attr( fp, XMLS_ATTAG_VALUE, value );
- fputs( " />\n", fp );
+static void xmlprops_write_vis( gpointer key, gpointer value, gpointer d ) {
+ HashLoopData *data = (HashLoopData *)d;
+
+ if(xmlprops_write_elem_s( data->fp, 1, XMLS_ELTAG_PROPERTY ) < 0)
+ data->error = 1;
+ if(xmlprops_write_attr( data->fp, XMLS_ATTAG_NAME, key ) < 0)
+ data->error = 1;
+ if(xmlprops_write_attr( data->fp, XMLS_ATTAG_VALUE, value ) < 0)
+ data->error = 1;
+ if(fputs( " />\n", data->fp ) == EOF)
+ data->error = 1;
}
static gint xmlprops_write_to( XmlProperty *props, const gchar *fileSpec ) {
PrefFile *pfile;
FILE *fp;
+ HashLoopData data;
props->retVal = MGU_OPEN_FILE;
pfile = prefs_write_open( fileSpec );
if( pfile ) {
fp = pfile->fp;
- fprintf( fp, "<?xml version=\"1.0\"" );
+ if(fprintf( fp, "<?xml version=\"1.0\"" ) < 0)
+ goto revert;
if( props->encoding && *props->encoding ) {
- fprintf( fp, " encoding=\"%s\"", props->encoding );
+ if(fprintf( fp, " encoding=\"%s\"", props->encoding ) < 0)
+ goto revert;
}
- fprintf( fp, " ?>\n" );
- xmlprops_write_elem_s( fp, 0, XMLS_ELTAG_PROP_LIST );
- fputs( ">\n", fp );
+ if(fprintf( fp, " ?>\n" ) < 0)
+ goto revert;
+ if(xmlprops_write_elem_s( fp, 0, XMLS_ELTAG_PROP_LIST ) < 0)
+ goto revert;
+ if(fputs( ">\n", fp ) == EOF)
+ goto revert;
/* Output all properties */
- g_hash_table_foreach( props->propertyTable, xmlprops_write_vis, fp );
+ data.fp = fp;
+ data.error = 0;
+ g_hash_table_foreach( props->propertyTable, xmlprops_write_vis, &data );
+
+ if (data.error)
+ goto revert;
- xmlprops_write_elem_e( fp, 0, XMLS_ELTAG_PROP_LIST );
+ if(xmlprops_write_elem_e( fp, 0, XMLS_ELTAG_PROP_LIST ) < 0)
+ goto revert;
+
props->retVal = MGU_SUCCESS;
if( prefs_file_close( pfile ) < 0 ) {
props->retVal = MGU_ERROR_WRITE;
+ goto out;
}
+ goto out;
+revert:
+ props->retVal = MGU_ERROR_WRITE;
+ if( prefs_file_close_revert( pfile ) < 0 ) {
+ props->retVal = MGU_ERROR_WRITE;
+ }
+
}
-
+out:
return props->retVal;
}
* return: Status code.
*/
gint xmlprops_save_file( XmlProperty *props ) {
- g_return_val_if_fail( props != NULL, -1 );
+ cm_return_val_if_fail( props != NULL, -1 );
props->retVal = MGU_NO_FILE;
if( props->path == NULL || *props->path == '\0' ) return props->retVal;
xmlprops_write_to( props, props->path );
- /*
- if( props->retVal == MGU_SUCCESS ) {
- }
- */
- return props->retVal;
-}
-static void xmlprops_print_vis( gpointer key, gpointer value, gpointer data ) {
- FILE *stream = ( FILE * ) data;
-
- fprintf( stream, "-\tname/value:\t%s / %s\n", (char *)key, (char *)value );
-}
-
-void xmlprops_print( XmlProperty *props, FILE *stream ) {
- fprintf( stream, "Property File: %s\n", props->path );
- g_hash_table_foreach( props->propertyTable, xmlprops_print_vis, stream );
- fprintf( stream, "---\n" );
+ return props->retVal;
}
static void xmlprops_save_property(
static void xmlprops_read_props( XmlProperty *props, XMLFile *file ) {
GList *attr;
gchar *name, *value;
- gchar pName[ ATTR_BUFSIZE ];
- gchar pValue[ ATTR_BUFSIZE ];
+ gchar *pName;
+ gchar *pValue;
while( TRUE ) {
- *pName = '\0';
- *pValue = '\0';
+ pName = g_strdup("");
+ pValue = g_strdup("");
if (! file->level ) break;
xml_parse_next_tag( file );
- xml_get_current_tag( file );
if( xml_compare_tag( file, XMLS_ELTAG_PROPERTY ) ) {
attr = xml_get_current_tag_attr( file );
while( attr ) {
name = ( ( XMLAttr * ) attr->data )->name;
value = ( ( XMLAttr * ) attr->data )->value;
if( strcmp( name, XMLS_ATTAG_NAME ) == 0 ) {
- strcpy( pName, value );
+ g_free(pName);
+ pName = g_strdup( value );
}
else if( strcmp( name, XMLS_ATTAG_VALUE ) == 0 ) {
- strcpy( pValue, value );
+ g_free(pValue);
+ pValue = g_strdup( value );
}
attr = g_list_next( attr );
}
xmlprops_save_property( props, pName, pValue );
}
+ g_free(pName);
+ g_free(pValue);
}
}
gint xmlprops_load_file( XmlProperty *props ) {
XMLFile *file = NULL;
- g_return_val_if_fail( props != NULL, -1 );
+ cm_return_val_if_fail( props != NULL, -1 );
props->retVal = MGU_NO_FILE;
file = xml_open_file( props->path );
if( file == NULL ) {
gchar *key = NULL;
gchar *val;
- g_return_if_fail( props != NULL );
+ cm_return_if_fail( props != NULL );
if( name == NULL || strlen( name ) == 0 ) return;
if( value == NULL || strlen( value ) == 0 ) return;
val = g_hash_table_lookup( props->propertyTable, name );
{
gchar buf[32];
- g_return_if_fail( props != NULL );
+ cm_return_if_fail( props != NULL );
sprintf( buf, "%d", value );
xmlprops_set_property( props, name, buf );
}
void xmlprops_set_property_b(
XmlProperty *props, const gchar *name, const gboolean value )
{
- g_return_if_fail( props != NULL );
+ cm_return_if_fail( props != NULL );
if( value ) {
xmlprops_set_property( props, name, "y" );
}
}
}
-/*
- * Get property.
- * Enter: props Property object.
- * name Property name.
- * Return: value found, or NULL if none. Should be g_free() when done.
- */
-gchar *xmlprops_get_property( XmlProperty *props, const gchar *name ) {
- gchar *val, *value;
-
- value = NULL;
- g_return_val_if_fail( props != NULL, value );
- val = g_hash_table_lookup( props->propertyTable, name );
- if( val ) {
- value = g_strdup( val );
- }
- return value;
-}
-
/*
* Get property into a buffer.
* Enter: props Property object.
XmlProperty *props, const gchar *name, gchar *buffer ) {
gchar *val;
- g_return_if_fail( props != NULL );
+ cm_return_if_fail( props != NULL );
if( buffer == NULL ) return;
val = g_hash_table_lookup( props->propertyTable, name );
if( val ) {
gint value;
value = 0;
- g_return_val_if_fail( props != NULL, value );
+ cm_return_val_if_fail( props != NULL, value );
val = g_hash_table_lookup( props->propertyTable, name );
if( val ) {
endptr = NULL;
gboolean value;
value = FALSE;
- g_return_val_if_fail( props != NULL, value );
+ cm_return_val_if_fail( props != NULL, value );
val = g_hash_table_lookup( props->propertyTable, name );
if( val ) {
- value = ( g_strcasecmp( val, "y" ) == 0 );
+ value = ( g_ascii_strcasecmp( val, "y" ) == 0 );
}
return value;
}