From: Tomas Mlcoch Date: Thu, 19 Sep 2013 13:28:52 +0000 (+0200) Subject: misc: Add cr_write_to_file, cr_key_file_get_boolean_default and cr_safe_string_chunk_... X-Git-Tag: upstream/0.2.1~16 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=26064c23f02d5cb021cde89f999b1ee77695b92f;p=services%2Fcreaterepo_c.git misc: Add cr_write_to_file, cr_key_file_get_boolean_default and cr_safe_string_chunk_insert_and_free functions. --- diff --git a/src/misc.c b/src/misc.c index be1d611..14b3412 100644 --- a/src/misc.c +++ b/src/misc.c @@ -981,3 +981,38 @@ cr_warning_cb(cr_XmlParserWarningType type, g_warning("%s: %s", cbdata, msg); } + +gboolean +cr_write_to_file(GError **err, gchar *filename, const char *format, ...) +{ + assert(filename); + assert(!err || *err == NULL); + + if (!format) + return TRUE; + + FILE *f = fopen(filename, "w"); + if (!f) { + g_set_error(err, CR_MISC_ERROR, CRE_IO, + "Cannot open %s: %s", filename, strerror(errno)); + return FALSE; + } + + va_list args; + va_start(args, format); + vfprintf (f, format, args); + va_end(args); + + gboolean ret = TRUE; + + if (ferror(f)) { + g_set_error(err, CR_MISC_ERROR, CRE_IO, + "Cannot write content to %s: %s", + filename, strerror(errno)); + ret = FALSE; + } + + fclose(f); + + return ret; +} diff --git a/src/misc.h b/src/misc.h index 2de9550..d105be2 100644 --- a/src/misc.h +++ b/src/misc.h @@ -288,6 +288,20 @@ cr_safe_string_chunk_insert(GStringChunk *chunk, const char *str) return g_string_chunk_insert(chunk, str); } +/** Safe insert into GStringChunk with free the str afterwards. + * @param chunk a GStringChunk + * @param str string to add or NULL + * @return pointer to the copy of str on NULL if str was NULL + */ +static inline gchar * +cr_safe_string_chunk_insert_and_free(GStringChunk *chunk, char *str) +{ + if (!str) return NULL; + gchar *copy = g_string_chunk_insert(chunk, str); + g_free(str); + return copy; +} + /** Safe insert into GStringChunk. If str is NULL or "\0" inserts nothing and * returns NULL. * @param chunk a GStringChunk @@ -314,6 +328,22 @@ cr_safe_string_chunk_insert_const(GStringChunk *chunk, const char *str) return g_string_chunk_insert_const(chunk, str); } +static inline gboolean +cr_key_file_get_boolean_default(GKeyFile *key_file, + const gchar *group_name, + const gchar *key, + gboolean default_value, + GError **error) +{ + GError *tmp_err = NULL; + gboolean ret = g_key_file_get_boolean(key_file, group_name, key, &tmp_err); + if (tmp_err) { + g_propagate_error(error, tmp_err); + return default_value; + } + return ret; +} + /** Warning callback for xml parser warnings. * For use in xml parsers like primary, filelists, other or repomd parser. * Name of the parser should be passed as a string via @@ -325,6 +355,15 @@ cr_warning_cb(cr_XmlParserWarningType type, void *cbdata, GError **err); +/** Open file and write content. + * @param err GError ** + * @param filename Filename + * @param format Format string + * @param ... Arguments + */ +gboolean +cr_write_to_file(GError **err, gchar *filename, const char *format, ...); + /** @} */ #ifdef __cplusplus