misc: Add cr_write_to_file, cr_key_file_get_boolean_default and cr_safe_string_chunk_...
authorTomas Mlcoch <tmlcoch@redhat.com>
Thu, 19 Sep 2013 13:28:52 +0000 (15:28 +0200)
committerTomas Mlcoch <tmlcoch@redhat.com>
Thu, 19 Sep 2013 13:28:52 +0000 (15:28 +0200)
src/misc.c
src/misc.h

index be1d611..14b3412 100644 (file)
@@ -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;
+}
index 2de9550..d105be2 100644 (file)
@@ -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