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;
+}
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
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
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