2005-01-26 03:11:26 +08:00
|
|
|
/* LIBGIMP - The GIMP Library
|
2003-03-06 04:21:50 +08:00
|
|
|
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
|
|
|
|
*
|
|
|
|
* GimpConfigWriter
|
|
|
|
* Copyright (C) 2003 Sven Neumann <sven@gimp.org>
|
|
|
|
*
|
2009-01-18 06:28:01 +08:00
|
|
|
* This library is free software: you can redistribute it and/or
|
2005-01-26 03:11:26 +08:00
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
2009-01-18 06:28:01 +08:00
|
|
|
* version 3 of the License, or (at your option) any later version.
|
2003-03-06 04:21:50 +08:00
|
|
|
*
|
2005-01-26 03:11:26 +08:00
|
|
|
* This library is distributed in the hope that it will be useful,
|
2003-03-06 04:21:50 +08:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2005-01-26 03:11:26 +08:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Library General Public License for more details.
|
2003-03-06 04:21:50 +08:00
|
|
|
*
|
2005-01-26 03:11:26 +08:00
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
2009-01-18 06:28:01 +08:00
|
|
|
* License along with this library. If not, see
|
2018-07-12 05:27:07 +08:00
|
|
|
* <https://www.gnu.org/licenses/>.
|
2003-03-06 04:21:50 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
2003-03-11 03:01:07 +08:00
|
|
|
#include <string.h>
|
2005-02-07 09:10:50 +08:00
|
|
|
|
2013-10-15 07:58:39 +08:00
|
|
|
#include <gio/gio.h>
|
2003-03-06 04:21:50 +08:00
|
|
|
|
|
|
|
#ifdef G_OS_WIN32
|
2014-07-03 05:53:57 +08:00
|
|
|
#include <gio/gwin32outputstream.h>
|
|
|
|
#else
|
|
|
|
#include <gio/gunixoutputstream.h>
|
2003-03-06 04:21:50 +08:00
|
|
|
#endif
|
|
|
|
|
2004-01-24 01:24:44 +08:00
|
|
|
#include "libgimpbase/gimpbase.h"
|
|
|
|
|
2005-01-26 04:30:20 +08:00
|
|
|
#include "gimpconfigtypes.h"
|
|
|
|
|
2005-01-26 03:11:26 +08:00
|
|
|
#include "gimpconfigwriter.h"
|
|
|
|
#include "gimpconfig-iface.h"
|
2003-07-24 22:14:23 +08:00
|
|
|
#include "gimpconfig-error.h"
|
2003-03-06 04:21:50 +08:00
|
|
|
#include "gimpconfig-serialize.h"
|
2003-03-17 22:23:42 +08:00
|
|
|
#include "gimpconfig-utils.h"
|
2003-03-06 04:21:50 +08:00
|
|
|
|
2005-01-26 03:11:26 +08:00
|
|
|
#include "libgimp/libgimp-intl.h"
|
2003-03-06 04:21:50 +08:00
|
|
|
|
|
|
|
|
2010-06-30 02:57:52 +08:00
|
|
|
/**
|
|
|
|
* SECTION: gimpconfigwriter
|
|
|
|
* @title: GimpConfigWriter
|
|
|
|
* @short_description: Functions for writing config info to a file for
|
|
|
|
* libgimpconfig.
|
|
|
|
*
|
|
|
|
* Functions for writing config info to a file for libgimpconfig.
|
|
|
|
**/
|
|
|
|
|
|
|
|
|
2003-06-24 06:02:56 +08:00
|
|
|
struct _GimpConfigWriter
|
|
|
|
{
|
2019-08-08 03:50:51 +08:00
|
|
|
gint ref_count;
|
|
|
|
gboolean finished;
|
|
|
|
|
2014-07-03 05:53:57 +08:00
|
|
|
GOutputStream *output;
|
|
|
|
GFile *file;
|
|
|
|
GError *error;
|
|
|
|
GString *buffer;
|
|
|
|
gboolean comment;
|
|
|
|
gint depth;
|
|
|
|
gint marker;
|
2003-06-24 06:02:56 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2019-08-08 03:50:51 +08:00
|
|
|
G_DEFINE_BOXED_TYPE (GimpConfigWriter, gimp_config_writer,
|
|
|
|
gimp_config_writer_ref, gimp_config_writer_unref)
|
|
|
|
|
|
|
|
|
2014-07-04 05:25:21 +08:00
|
|
|
static inline void gimp_config_writer_flush (GimpConfigWriter *writer);
|
|
|
|
static inline void gimp_config_writer_newline (GimpConfigWriter *writer);
|
|
|
|
static gboolean gimp_config_writer_close_output (GimpConfigWriter *writer,
|
|
|
|
GError **error);
|
2003-03-06 04:21:50 +08:00
|
|
|
|
2004-11-14 10:50:33 +08:00
|
|
|
static inline void
|
|
|
|
gimp_config_writer_flush (GimpConfigWriter *writer)
|
|
|
|
{
|
2014-07-03 05:53:57 +08:00
|
|
|
GError *error = NULL;
|
|
|
|
|
2014-07-04 05:25:21 +08:00
|
|
|
if (! writer->output)
|
|
|
|
return;
|
|
|
|
|
2014-07-03 05:53:57 +08:00
|
|
|
if (! g_output_stream_write_all (writer->output,
|
|
|
|
writer->buffer->str,
|
|
|
|
writer->buffer->len,
|
2014-09-08 02:30:14 +08:00
|
|
|
NULL, NULL, &error))
|
2014-07-03 05:53:57 +08:00
|
|
|
{
|
|
|
|
g_set_error (&writer->error, GIMP_CONFIG_ERROR, GIMP_CONFIG_ERROR_WRITE,
|
|
|
|
_("Error writing to '%s': %s"),
|
2014-07-04 05:25:21 +08:00
|
|
|
writer->file ?
|
|
|
|
gimp_file_get_utf8_name (writer->file) : "output stream",
|
|
|
|
error->message);
|
2014-07-03 05:53:57 +08:00
|
|
|
g_clear_error (&error);
|
|
|
|
}
|
2004-11-14 10:50:33 +08:00
|
|
|
|
|
|
|
g_string_truncate (writer->buffer, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void
|
|
|
|
gimp_config_writer_newline (GimpConfigWriter *writer)
|
|
|
|
{
|
|
|
|
gint i;
|
|
|
|
|
|
|
|
g_string_append_c (writer->buffer, '\n');
|
|
|
|
|
|
|
|
if (writer->comment)
|
|
|
|
g_string_append_len (writer->buffer, "# ", 2);
|
|
|
|
|
|
|
|
for (i = 0; i < writer->depth; i++)
|
|
|
|
g_string_append_len (writer->buffer, " ", 4);
|
|
|
|
}
|
2003-03-06 04:21:50 +08:00
|
|
|
|
2003-10-12 09:49:51 +08:00
|
|
|
/**
|
2019-08-12 13:50:33 +08:00
|
|
|
* gimp_config_writer_new_from_file:
|
2003-10-12 09:49:51 +08:00
|
|
|
* @filename: a filename
|
|
|
|
* @atomic: if %TRUE the file is written atomically
|
|
|
|
* @header: text to include as comment at the top of the file
|
|
|
|
* @error: return location for errors
|
|
|
|
*
|
|
|
|
* Creates a new #GimpConfigWriter and sets it up to write to
|
|
|
|
* @filename. If @atomic is %TRUE, a temporary file is used to avoid
|
|
|
|
* possible race conditions. The temporary file is then moved to
|
|
|
|
* @filename when the writer is closed.
|
|
|
|
*
|
2019-08-03 06:36:59 +08:00
|
|
|
* Returns: (nullable): a new #GimpConfigWriter or %NULL in case of an error
|
2005-01-29 20:54:48 +08:00
|
|
|
*
|
2015-06-01 03:18:09 +08:00
|
|
|
* Since: 2.4
|
2003-10-12 09:49:51 +08:00
|
|
|
**/
|
2003-03-06 04:21:50 +08:00
|
|
|
GimpConfigWriter *
|
2019-08-12 13:50:33 +08:00
|
|
|
gimp_config_writer_new_from_file (const gchar *filename,
|
|
|
|
gboolean atomic,
|
|
|
|
const gchar *header,
|
|
|
|
GError **error)
|
2003-03-06 04:21:50 +08:00
|
|
|
{
|
|
|
|
GimpConfigWriter *writer;
|
2014-07-03 05:53:57 +08:00
|
|
|
GFile *file;
|
2003-03-06 04:21:50 +08:00
|
|
|
|
|
|
|
g_return_val_if_fail (filename != NULL, NULL);
|
|
|
|
g_return_val_if_fail (error == NULL || *error == NULL, NULL);
|
|
|
|
|
2014-07-03 05:53:57 +08:00
|
|
|
file = g_file_new_for_path (filename);
|
2003-03-06 04:21:50 +08:00
|
|
|
|
2019-08-12 13:50:33 +08:00
|
|
|
writer = gimp_config_writer_new_from_gfile (file, atomic, header, error);
|
2003-03-06 04:21:50 +08:00
|
|
|
|
2014-07-03 05:53:57 +08:00
|
|
|
g_object_unref (file);
|
2003-03-06 04:21:50 +08:00
|
|
|
|
|
|
|
return writer;
|
|
|
|
}
|
|
|
|
|
2014-07-01 07:54:31 +08:00
|
|
|
/**
|
2019-08-12 13:50:33 +08:00
|
|
|
* gimp_config_writer_new_from_gfile:
|
2014-07-01 07:54:31 +08:00
|
|
|
* @file: a #GFile
|
|
|
|
* @atomic: if %TRUE the file is written atomically
|
|
|
|
* @header: text to include as comment at the top of the file
|
|
|
|
* @error: return location for errors
|
|
|
|
*
|
|
|
|
* Creates a new #GimpConfigWriter and sets it up to write to
|
|
|
|
* @file. If @atomic is %TRUE, a temporary file is used to avoid
|
|
|
|
* possible race conditions. The temporary file is then moved to @file
|
|
|
|
* when the writer is closed.
|
|
|
|
*
|
2019-08-03 06:36:59 +08:00
|
|
|
* Returns: (nullable): a new #GimpConfigWriter or %NULL in case of an error
|
2014-07-01 07:54:31 +08:00
|
|
|
*
|
2015-06-01 03:18:09 +08:00
|
|
|
* Since: 2.10
|
2014-07-01 07:54:31 +08:00
|
|
|
**/
|
|
|
|
GimpConfigWriter *
|
2019-08-12 13:50:33 +08:00
|
|
|
gimp_config_writer_new_from_gfile (GFile *file,
|
|
|
|
gboolean atomic,
|
|
|
|
const gchar *header,
|
|
|
|
GError **error)
|
2014-07-01 07:54:31 +08:00
|
|
|
{
|
|
|
|
GimpConfigWriter *writer;
|
2014-07-03 05:53:57 +08:00
|
|
|
GOutputStream *output;
|
2018-04-23 04:11:13 +08:00
|
|
|
GFile *dir;
|
2014-07-01 07:54:31 +08:00
|
|
|
|
|
|
|
g_return_val_if_fail (G_IS_FILE (file), NULL);
|
2014-07-03 05:53:57 +08:00
|
|
|
g_return_val_if_fail (error == NULL || *error == NULL, NULL);
|
2014-07-01 07:54:31 +08:00
|
|
|
|
2018-04-23 04:11:13 +08:00
|
|
|
dir = g_file_get_parent (file);
|
|
|
|
if (dir && ! g_file_query_exists (dir, NULL))
|
|
|
|
{
|
|
|
|
if (! g_file_make_directory_with_parents (dir, NULL, error))
|
|
|
|
g_prefix_error (error,
|
|
|
|
_("Could not create directory '%s' for '%s': "),
|
|
|
|
gimp_file_get_utf8_name (dir),
|
|
|
|
gimp_file_get_utf8_name (file));
|
|
|
|
}
|
|
|
|
g_object_unref (dir);
|
|
|
|
|
|
|
|
if (error && *error)
|
|
|
|
return NULL;
|
|
|
|
|
2014-07-03 05:53:57 +08:00
|
|
|
if (atomic)
|
|
|
|
{
|
2014-10-04 08:44:54 +08:00
|
|
|
output = G_OUTPUT_STREAM (g_file_replace (file,
|
|
|
|
NULL, FALSE, G_FILE_CREATE_NONE,
|
|
|
|
NULL, error));
|
2014-07-03 05:53:57 +08:00
|
|
|
if (! output)
|
2014-10-04 08:44:54 +08:00
|
|
|
g_prefix_error (error,
|
|
|
|
_("Could not create temporary file for '%s': "),
|
|
|
|
gimp_file_get_utf8_name (file));
|
2014-07-03 05:53:57 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-10-04 08:44:54 +08:00
|
|
|
output = G_OUTPUT_STREAM (g_file_replace (file,
|
|
|
|
NULL, FALSE,
|
2014-07-03 05:53:57 +08:00
|
|
|
G_FILE_CREATE_REPLACE_DESTINATION,
|
2014-10-04 08:44:54 +08:00
|
|
|
NULL, error));
|
2014-07-03 05:53:57 +08:00
|
|
|
}
|
2014-07-01 07:54:31 +08:00
|
|
|
|
2014-07-03 05:53:57 +08:00
|
|
|
if (! output)
|
2014-10-04 08:44:54 +08:00
|
|
|
return NULL;
|
2014-07-01 07:54:31 +08:00
|
|
|
|
2014-07-03 05:53:57 +08:00
|
|
|
writer = g_slice_new0 (GimpConfigWriter);
|
|
|
|
|
2019-08-08 03:50:51 +08:00
|
|
|
writer->ref_count = 1;
|
|
|
|
writer->output = output;
|
|
|
|
writer->file = g_object_ref (file);
|
|
|
|
writer->buffer = g_string_new (NULL);
|
2014-07-03 05:53:57 +08:00
|
|
|
|
|
|
|
if (header)
|
|
|
|
{
|
|
|
|
gimp_config_writer_comment (writer, header);
|
|
|
|
gimp_config_writer_linefeed (writer);
|
|
|
|
}
|
2014-07-01 07:54:31 +08:00
|
|
|
|
|
|
|
return writer;
|
|
|
|
}
|
|
|
|
|
2014-07-04 05:25:21 +08:00
|
|
|
/**
|
2019-08-12 13:50:33 +08:00
|
|
|
* gimp_config_writer_new_from_stream:
|
2014-07-04 05:25:21 +08:00
|
|
|
* @output: a #GOutputStream
|
|
|
|
* @header: text to include as comment at the top of the file
|
|
|
|
* @error: return location for errors
|
|
|
|
*
|
|
|
|
* Creates a new #GimpConfigWriter and sets it up to write to
|
|
|
|
* @output.
|
|
|
|
*
|
2019-08-03 06:36:59 +08:00
|
|
|
* Returns: (nullable): a new #GimpConfigWriter or %NULL in case of an error
|
2014-07-04 05:25:21 +08:00
|
|
|
*
|
2015-06-01 03:18:09 +08:00
|
|
|
* Since: 2.10
|
2014-07-04 05:25:21 +08:00
|
|
|
**/
|
|
|
|
GimpConfigWriter *
|
2019-08-12 13:50:33 +08:00
|
|
|
gimp_config_writer_new_from_stream (GOutputStream *output,
|
|
|
|
const gchar *header,
|
|
|
|
GError **error)
|
2014-07-04 05:25:21 +08:00
|
|
|
{
|
|
|
|
GimpConfigWriter *writer;
|
|
|
|
|
|
|
|
g_return_val_if_fail (G_IS_OUTPUT_STREAM (output), NULL);
|
|
|
|
g_return_val_if_fail (error == NULL || *error == NULL, NULL);
|
|
|
|
|
|
|
|
writer = g_slice_new0 (GimpConfigWriter);
|
|
|
|
|
2019-08-08 03:50:51 +08:00
|
|
|
writer->ref_count = 1;
|
|
|
|
writer->output = g_object_ref (output);
|
|
|
|
writer->buffer = g_string_new (NULL);
|
2014-07-04 05:25:21 +08:00
|
|
|
|
|
|
|
if (header)
|
|
|
|
{
|
|
|
|
gimp_config_writer_comment (writer, header);
|
|
|
|
gimp_config_writer_linefeed (writer);
|
|
|
|
}
|
|
|
|
|
|
|
|
return writer;
|
|
|
|
}
|
|
|
|
|
2005-01-29 20:54:48 +08:00
|
|
|
/**
|
2019-08-12 13:50:33 +08:00
|
|
|
* gimp_config_writer_new_from_fd:
|
2005-01-29 20:54:48 +08:00
|
|
|
* @fd:
|
|
|
|
*
|
2019-08-03 06:36:59 +08:00
|
|
|
* Returns: (nullable): a new #GimpConfigWriter or %NULL in case of an error
|
2005-01-29 20:54:48 +08:00
|
|
|
*
|
2015-06-01 03:18:09 +08:00
|
|
|
* Since: 2.4
|
2005-01-29 20:54:48 +08:00
|
|
|
**/
|
2003-03-06 04:21:50 +08:00
|
|
|
GimpConfigWriter *
|
2019-08-12 13:50:33 +08:00
|
|
|
gimp_config_writer_new_from_fd (gint fd)
|
2003-03-06 04:21:50 +08:00
|
|
|
{
|
|
|
|
GimpConfigWriter *writer;
|
|
|
|
|
|
|
|
g_return_val_if_fail (fd > 0, NULL);
|
|
|
|
|
2007-05-22 19:34:58 +08:00
|
|
|
writer = g_slice_new0 (GimpConfigWriter);
|
2003-03-06 04:21:50 +08:00
|
|
|
|
2019-08-08 03:50:51 +08:00
|
|
|
writer->ref_count = 1;
|
|
|
|
|
2014-07-03 05:53:57 +08:00
|
|
|
#ifdef G_OS_WIN32
|
2014-07-03 07:16:51 +08:00
|
|
|
writer->output = g_win32_output_stream_new ((gpointer) fd, FALSE);
|
2014-07-03 05:53:57 +08:00
|
|
|
#else
|
|
|
|
writer->output = g_unix_output_stream_new (fd, FALSE);
|
|
|
|
#endif
|
|
|
|
|
2003-03-06 04:21:50 +08:00
|
|
|
writer->buffer = g_string_new (NULL);
|
|
|
|
|
|
|
|
return writer;
|
|
|
|
}
|
|
|
|
|
2005-01-29 20:54:48 +08:00
|
|
|
/**
|
2019-08-12 13:50:33 +08:00
|
|
|
* gimp_config_writer_new_from_string:
|
2005-01-29 20:54:48 +08:00
|
|
|
* @string:
|
|
|
|
*
|
2019-08-03 06:36:59 +08:00
|
|
|
* Returns: (nullable): a new #GimpConfigWriter or %NULL in case of an error
|
2005-01-29 20:54:48 +08:00
|
|
|
*
|
2015-06-01 03:18:09 +08:00
|
|
|
* Since: 2.4
|
2005-01-29 20:54:48 +08:00
|
|
|
**/
|
2003-06-24 06:02:56 +08:00
|
|
|
GimpConfigWriter *
|
2019-08-12 13:50:33 +08:00
|
|
|
gimp_config_writer_new_from_string (GString *string)
|
2003-06-24 06:02:56 +08:00
|
|
|
{
|
|
|
|
GimpConfigWriter *writer;
|
|
|
|
|
|
|
|
g_return_val_if_fail (string != NULL, NULL);
|
|
|
|
|
2007-05-22 19:34:58 +08:00
|
|
|
writer = g_slice_new0 (GimpConfigWriter);
|
2003-06-24 06:02:56 +08:00
|
|
|
|
2019-08-08 03:50:51 +08:00
|
|
|
writer->ref_count = 1;
|
|
|
|
writer->buffer = string;
|
2003-06-24 06:02:56 +08:00
|
|
|
|
|
|
|
return writer;
|
|
|
|
}
|
|
|
|
|
2019-08-08 03:50:51 +08:00
|
|
|
/**
|
|
|
|
* gimp_config_writer_ref:
|
|
|
|
* @writer: #GimpConfigWriter to ref
|
|
|
|
*
|
|
|
|
* Adds a reference to a #GimpConfigWriter.
|
|
|
|
*
|
|
|
|
* Returns: the same @writer.
|
|
|
|
*
|
|
|
|
* Since: 3.0
|
|
|
|
*/
|
|
|
|
GimpConfigWriter *
|
|
|
|
gimp_config_writer_ref (GimpConfigWriter *writer)
|
|
|
|
{
|
|
|
|
g_return_val_if_fail (writer != NULL, NULL);
|
|
|
|
|
|
|
|
writer->ref_count++;
|
|
|
|
|
|
|
|
return writer;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gimp_config_writer_unref:
|
|
|
|
* @writer: #GimpConfigWriter to unref
|
|
|
|
*
|
|
|
|
* Unref a #GimpConfigWriter. If the reference count drops to zero, the
|
2019-08-09 18:42:52 +08:00
|
|
|
* writer is freed.
|
2019-08-08 03:50:51 +08:00
|
|
|
*
|
|
|
|
* Note that at least one of the references has to be dropped using
|
|
|
|
* gimp_config_writer_finish().
|
|
|
|
*
|
|
|
|
* Since: 3.0
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
gimp_config_writer_unref (GimpConfigWriter *writer)
|
|
|
|
{
|
|
|
|
g_return_if_fail (writer != NULL);
|
|
|
|
|
|
|
|
writer->ref_count--;
|
|
|
|
|
|
|
|
if (writer->ref_count < 1)
|
|
|
|
{
|
|
|
|
if (! writer->finished)
|
|
|
|
{
|
|
|
|
GError *error = NULL;
|
|
|
|
|
|
|
|
g_printerr ("%s: dropping last reference via unref(), you should "
|
|
|
|
"call gimp_config_writer_finish()\n", G_STRFUNC);
|
|
|
|
|
|
|
|
if (! gimp_config_writer_finish (writer, NULL, &error))
|
|
|
|
{
|
|
|
|
g_printerr ("%s: error on finishing writer: %s\n",
|
|
|
|
G_STRFUNC, error->message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
g_slice_free (GimpConfigWriter, writer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-10-13 00:46:50 +08:00
|
|
|
/**
|
|
|
|
* gimp_config_writer_comment_mode:
|
|
|
|
* @writer: a #GimpConfigWriter
|
|
|
|
* @enable: %TRUE to enable comment mode, %FALSE to disable it
|
|
|
|
*
|
|
|
|
* This function toggles whether the @writer should create commented
|
|
|
|
* or uncommented output. This feature is used to generate the
|
|
|
|
* system-wide installed gimprc that documents the default settings.
|
|
|
|
*
|
|
|
|
* Since comments have to start at the beginning of a line, this
|
2013-01-27 23:52:38 +08:00
|
|
|
* function will insert a newline if necessary.
|
2005-01-29 20:54:48 +08:00
|
|
|
*
|
2015-06-01 03:18:09 +08:00
|
|
|
* Since: 2.4
|
2003-10-13 00:46:50 +08:00
|
|
|
**/
|
2003-10-12 09:49:51 +08:00
|
|
|
void
|
|
|
|
gimp_config_writer_comment_mode (GimpConfigWriter *writer,
|
|
|
|
gboolean enable)
|
|
|
|
{
|
|
|
|
g_return_if_fail (writer != NULL);
|
2019-08-08 03:50:51 +08:00
|
|
|
g_return_if_fail (writer->finished == FALSE);
|
2003-10-12 09:49:51 +08:00
|
|
|
|
|
|
|
if (writer->error)
|
|
|
|
return;
|
|
|
|
|
|
|
|
enable = (enable ? TRUE : FALSE);
|
|
|
|
|
2003-10-13 00:46:50 +08:00
|
|
|
if (writer->comment == enable)
|
|
|
|
return;
|
2003-10-12 09:49:51 +08:00
|
|
|
|
|
|
|
writer->comment = enable;
|
2003-10-13 00:46:50 +08:00
|
|
|
|
|
|
|
if (enable)
|
|
|
|
{
|
|
|
|
if (writer->buffer->len == 0)
|
|
|
|
g_string_append_len (writer->buffer, "# ", 2);
|
|
|
|
else
|
|
|
|
gimp_config_writer_newline (writer);
|
|
|
|
}
|
2003-10-12 09:49:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-10-13 00:46:50 +08:00
|
|
|
/**
|
|
|
|
* gimp_config_writer_open:
|
|
|
|
* @writer: a #GimpConfigWriter
|
|
|
|
* @name: name of the element to open
|
|
|
|
*
|
2018-04-26 07:10:55 +08:00
|
|
|
* This function writes the opening parenthesis followed by @name.
|
2003-10-13 00:46:50 +08:00
|
|
|
* It also increases the indentation level and sets a mark that
|
|
|
|
* can be used by gimp_config_writer_revert().
|
2005-01-29 20:54:48 +08:00
|
|
|
*
|
2015-06-01 03:18:09 +08:00
|
|
|
* Since: 2.4
|
2003-10-13 00:46:50 +08:00
|
|
|
**/
|
2003-03-06 04:21:50 +08:00
|
|
|
void
|
|
|
|
gimp_config_writer_open (GimpConfigWriter *writer,
|
2006-04-12 18:53:28 +08:00
|
|
|
const gchar *name)
|
2003-03-06 04:21:50 +08:00
|
|
|
{
|
|
|
|
g_return_if_fail (writer != NULL);
|
2019-08-08 03:50:51 +08:00
|
|
|
g_return_if_fail (writer->finished == FALSE);
|
2003-03-11 03:01:07 +08:00
|
|
|
g_return_if_fail (name != NULL);
|
2003-03-06 04:21:50 +08:00
|
|
|
|
|
|
|
if (writer->error)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* store the current buffer length so we can revert to this state */
|
|
|
|
writer->marker = writer->buffer->len;
|
|
|
|
|
|
|
|
if (writer->depth > 0)
|
2003-10-12 09:49:51 +08:00
|
|
|
gimp_config_writer_newline (writer);
|
2003-03-06 04:21:50 +08:00
|
|
|
|
|
|
|
writer->depth++;
|
|
|
|
|
|
|
|
g_string_append_printf (writer->buffer, "(%s", name);
|
|
|
|
}
|
|
|
|
|
2003-10-13 00:46:50 +08:00
|
|
|
/**
|
|
|
|
* gimp_config_writer_print:
|
|
|
|
* @writer: a #GimpConfigWriter
|
|
|
|
* @string: a string to write
|
|
|
|
* @len: number of bytes from @string or -1 if @string is NUL-terminated.
|
|
|
|
*
|
|
|
|
* Appends a space followed by @string to the @writer. Note that string
|
|
|
|
* must not contain any special characters that might need to be escaped.
|
2005-01-29 20:54:48 +08:00
|
|
|
*
|
2015-06-01 03:18:09 +08:00
|
|
|
* Since: 2.4
|
2003-10-13 00:46:50 +08:00
|
|
|
**/
|
2003-03-06 04:21:50 +08:00
|
|
|
void
|
|
|
|
gimp_config_writer_print (GimpConfigWriter *writer,
|
2006-04-12 18:53:28 +08:00
|
|
|
const gchar *string,
|
|
|
|
gint len)
|
2003-03-06 04:21:50 +08:00
|
|
|
{
|
|
|
|
g_return_if_fail (writer != NULL);
|
2019-08-08 03:50:51 +08:00
|
|
|
g_return_if_fail (writer->finished == FALSE);
|
2003-03-06 04:21:50 +08:00
|
|
|
g_return_if_fail (len == 0 || string != NULL);
|
|
|
|
|
|
|
|
if (writer->error)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (len < 0)
|
|
|
|
len = strlen (string);
|
|
|
|
|
2003-03-11 03:01:07 +08:00
|
|
|
if (len)
|
|
|
|
{
|
|
|
|
g_string_append_c (writer->buffer, ' ');
|
|
|
|
g_string_append_len (writer->buffer, string, len);
|
|
|
|
}
|
2003-03-06 04:21:50 +08:00
|
|
|
}
|
|
|
|
|
2003-10-13 00:46:50 +08:00
|
|
|
/**
|
|
|
|
* gimp_config_writer_printf:
|
|
|
|
* @writer: a #GimpConfigWriter
|
|
|
|
* @format: a format string as described for g_strdup_printf().
|
2011-11-26 04:39:55 +08:00
|
|
|
* @...: list of arguments according to @format
|
2003-10-13 00:46:50 +08:00
|
|
|
*
|
|
|
|
* A printf-like function for #GimpConfigWriter.
|
2005-01-29 20:54:48 +08:00
|
|
|
*
|
2015-06-01 03:18:09 +08:00
|
|
|
* Since: 2.4
|
2003-10-13 00:46:50 +08:00
|
|
|
**/
|
2003-03-06 04:21:50 +08:00
|
|
|
void
|
|
|
|
gimp_config_writer_printf (GimpConfigWriter *writer,
|
2006-04-12 18:53:28 +08:00
|
|
|
const gchar *format,
|
|
|
|
...)
|
2003-03-06 04:21:50 +08:00
|
|
|
{
|
|
|
|
gchar *buffer;
|
|
|
|
va_list args;
|
|
|
|
|
|
|
|
g_return_if_fail (writer != NULL);
|
2019-08-08 03:50:51 +08:00
|
|
|
g_return_if_fail (writer->finished == FALSE);
|
2003-03-06 04:21:50 +08:00
|
|
|
g_return_if_fail (format != NULL);
|
|
|
|
|
|
|
|
if (writer->error)
|
|
|
|
return;
|
|
|
|
|
|
|
|
va_start (args, format);
|
|
|
|
buffer = g_strdup_vprintf (format, args);
|
|
|
|
va_end (args);
|
|
|
|
|
2003-03-11 03:01:07 +08:00
|
|
|
g_string_append_c (writer->buffer, ' ');
|
2003-03-06 04:21:50 +08:00
|
|
|
g_string_append (writer->buffer, buffer);
|
|
|
|
|
|
|
|
g_free (buffer);
|
|
|
|
}
|
|
|
|
|
2003-10-13 00:46:50 +08:00
|
|
|
/**
|
|
|
|
* gimp_config_writer_string:
|
|
|
|
* @writer: a #GimpConfigWriter
|
|
|
|
* @string: a NUL-terminated string
|
|
|
|
*
|
|
|
|
* Writes a string value to @writer. The @string is quoted and special
|
|
|
|
* characters are escaped.
|
2005-01-29 20:54:48 +08:00
|
|
|
*
|
2015-06-01 03:18:09 +08:00
|
|
|
* Since: 2.4
|
2003-10-13 00:46:50 +08:00
|
|
|
**/
|
2003-03-10 23:48:16 +08:00
|
|
|
void
|
2003-03-17 22:23:42 +08:00
|
|
|
gimp_config_writer_string (GimpConfigWriter *writer,
|
|
|
|
const gchar *string)
|
2003-03-10 23:48:16 +08:00
|
|
|
{
|
|
|
|
g_return_if_fail (writer != NULL);
|
2019-08-08 03:50:51 +08:00
|
|
|
g_return_if_fail (writer->finished == FALSE);
|
2003-03-10 23:48:16 +08:00
|
|
|
|
|
|
|
if (writer->error)
|
|
|
|
return;
|
|
|
|
|
2003-03-17 22:23:42 +08:00
|
|
|
g_string_append_c (writer->buffer, ' ');
|
|
|
|
gimp_config_string_append_escaped (writer->buffer, string);
|
2003-03-10 23:48:16 +08:00
|
|
|
}
|
|
|
|
|
2004-05-19 05:19:43 +08:00
|
|
|
/**
|
|
|
|
* gimp_config_writer_identifier:
|
2004-05-24 22:51:15 +08:00
|
|
|
* @writer: a #GimpConfigWriter
|
|
|
|
* @identifier: a NUL-terminated string
|
2004-05-19 05:19:43 +08:00
|
|
|
*
|
|
|
|
* Writes an identifier to @writer. The @string is *not* quoted and special
|
|
|
|
* characters are *not* escaped.
|
2005-01-29 20:54:48 +08:00
|
|
|
*
|
2015-06-01 03:18:09 +08:00
|
|
|
* Since: 2.4
|
2004-05-19 05:19:43 +08:00
|
|
|
**/
|
|
|
|
void
|
|
|
|
gimp_config_writer_identifier (GimpConfigWriter *writer,
|
|
|
|
const gchar *identifier)
|
|
|
|
{
|
|
|
|
g_return_if_fail (writer != NULL);
|
2019-08-08 03:50:51 +08:00
|
|
|
g_return_if_fail (writer->finished == FALSE);
|
2004-05-19 05:19:43 +08:00
|
|
|
g_return_if_fail (identifier != NULL);
|
|
|
|
|
|
|
|
if (writer->error)
|
|
|
|
return;
|
|
|
|
|
|
|
|
g_string_append_printf (writer->buffer, " %s", identifier);
|
|
|
|
}
|
|
|
|
|
2005-01-29 20:54:48 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* gimp_config_writer_data:
|
|
|
|
* @writer: a #GimpConfigWriter
|
|
|
|
* @length:
|
|
|
|
* @data:
|
|
|
|
*
|
2015-06-01 03:18:09 +08:00
|
|
|
* Since: 2.4
|
2005-01-29 20:54:48 +08:00
|
|
|
**/
|
2004-05-19 05:19:43 +08:00
|
|
|
void
|
|
|
|
gimp_config_writer_data (GimpConfigWriter *writer,
|
|
|
|
gint length,
|
|
|
|
const guint8 *data)
|
|
|
|
{
|
|
|
|
gint i;
|
|
|
|
|
|
|
|
g_return_if_fail (writer != NULL);
|
2019-08-08 03:50:51 +08:00
|
|
|
g_return_if_fail (writer->finished == FALSE);
|
2018-07-06 10:04:07 +08:00
|
|
|
g_return_if_fail (length >= 0);
|
|
|
|
g_return_if_fail (data != NULL || length == 0);
|
2004-05-19 05:19:43 +08:00
|
|
|
|
|
|
|
if (writer->error)
|
|
|
|
return;
|
|
|
|
|
|
|
|
g_string_append (writer->buffer, " \"");
|
|
|
|
|
|
|
|
for (i = 0; i < length; i++)
|
|
|
|
{
|
|
|
|
if (g_ascii_isalpha (data[i]))
|
|
|
|
g_string_append_c (writer->buffer, data[i]);
|
|
|
|
else
|
|
|
|
g_string_append_printf (writer->buffer, "\\%o", data[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
g_string_append (writer->buffer, "\"");
|
|
|
|
}
|
|
|
|
|
2003-10-13 00:46:50 +08:00
|
|
|
/**
|
|
|
|
* gimp_config_writer_revert:
|
|
|
|
* @writer: a #GimpConfigWriter
|
|
|
|
*
|
|
|
|
* Reverts all changes to @writer that were done since the last call
|
|
|
|
* to gimp_config_writer_open(). This can only work if you didn't call
|
|
|
|
* gimp_config_writer_close() yet.
|
2005-01-29 20:54:48 +08:00
|
|
|
*
|
2015-06-01 03:18:09 +08:00
|
|
|
* Since: 2.4
|
2003-10-13 00:46:50 +08:00
|
|
|
**/
|
2003-03-06 04:21:50 +08:00
|
|
|
void
|
|
|
|
gimp_config_writer_revert (GimpConfigWriter *writer)
|
|
|
|
{
|
|
|
|
g_return_if_fail (writer != NULL);
|
2019-08-08 03:50:51 +08:00
|
|
|
g_return_if_fail (writer->finished == FALSE);
|
2003-03-06 04:21:50 +08:00
|
|
|
|
|
|
|
if (writer->error)
|
|
|
|
return;
|
|
|
|
|
2004-08-29 20:13:34 +08:00
|
|
|
g_return_if_fail (writer->depth > 0);
|
|
|
|
g_return_if_fail (writer->marker != -1);
|
|
|
|
|
2003-03-06 04:21:50 +08:00
|
|
|
g_string_truncate (writer->buffer, writer->marker);
|
|
|
|
|
|
|
|
writer->depth--;
|
2003-03-10 23:48:16 +08:00
|
|
|
writer->marker = -1;
|
2003-03-06 04:21:50 +08:00
|
|
|
}
|
|
|
|
|
2003-10-13 00:46:50 +08:00
|
|
|
/**
|
|
|
|
* gimp_config_writer_close:
|
|
|
|
* @writer: a #GimpConfigWriter
|
|
|
|
*
|
|
|
|
* Closes an element opened with gimp_config_writer_open().
|
2005-01-29 20:54:48 +08:00
|
|
|
*
|
2015-06-01 03:18:09 +08:00
|
|
|
* Since: 2.4
|
2003-10-13 00:46:50 +08:00
|
|
|
**/
|
2003-03-06 04:21:50 +08:00
|
|
|
void
|
|
|
|
gimp_config_writer_close (GimpConfigWriter *writer)
|
|
|
|
{
|
|
|
|
g_return_if_fail (writer != NULL);
|
2019-08-08 03:50:51 +08:00
|
|
|
g_return_if_fail (writer->finished == FALSE);
|
2003-03-06 04:21:50 +08:00
|
|
|
|
|
|
|
if (writer->error)
|
|
|
|
return;
|
|
|
|
|
2004-08-29 20:13:34 +08:00
|
|
|
g_return_if_fail (writer->depth > 0);
|
|
|
|
|
2003-03-06 04:21:50 +08:00
|
|
|
g_string_append_c (writer->buffer, ')');
|
|
|
|
|
|
|
|
if (--writer->depth == 0)
|
|
|
|
{
|
|
|
|
g_string_append_c (writer->buffer, '\n');
|
|
|
|
|
2014-07-04 05:25:21 +08:00
|
|
|
gimp_config_writer_flush (writer);
|
2003-03-06 04:21:50 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-10-13 00:46:50 +08:00
|
|
|
/**
|
|
|
|
* gimp_config_writer_finish:
|
|
|
|
* @writer: a #GimpConfigWriter
|
|
|
|
* @footer: text to include as comment at the bottom of the file
|
|
|
|
* @error: return location for possible errors
|
|
|
|
*
|
2019-08-08 03:50:51 +08:00
|
|
|
* This function finishes the work of @writer and unrefs it
|
|
|
|
* afterwards. It closes all open elements, appends an optional
|
|
|
|
* comment and releases all resources allocated by @writer.
|
|
|
|
*
|
|
|
|
* Using any function except gimp_config_writer_ref() or
|
|
|
|
* gimp_config_writer_unref() after this function is forbidden
|
|
|
|
* and will trigger warnings.
|
2003-10-13 00:46:50 +08:00
|
|
|
*
|
2019-08-03 06:10:14 +08:00
|
|
|
* Returns: %TRUE if everything could be successfully written,
|
2019-08-08 03:50:51 +08:00
|
|
|
* %FALSE otherwise
|
2005-01-29 20:54:48 +08:00
|
|
|
*
|
2015-06-01 03:18:09 +08:00
|
|
|
* Since: 2.4
|
2003-10-13 00:46:50 +08:00
|
|
|
**/
|
2003-03-06 04:21:50 +08:00
|
|
|
gboolean
|
|
|
|
gimp_config_writer_finish (GimpConfigWriter *writer,
|
2006-04-12 18:53:28 +08:00
|
|
|
const gchar *footer,
|
|
|
|
GError **error)
|
2003-03-06 04:21:50 +08:00
|
|
|
{
|
|
|
|
gboolean success = TRUE;
|
|
|
|
|
|
|
|
g_return_val_if_fail (writer != NULL, FALSE);
|
2019-08-08 03:50:51 +08:00
|
|
|
g_return_val_if_fail (writer->finished == FALSE, FALSE);
|
2003-03-06 04:21:50 +08:00
|
|
|
g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
|
|
|
|
|
|
|
|
if (writer->depth < 0)
|
|
|
|
{
|
|
|
|
g_warning ("gimp_config_writer_finish: depth < 0 !!");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
while (writer->depth)
|
2006-04-12 18:53:28 +08:00
|
|
|
gimp_config_writer_close (writer);
|
2003-03-06 04:21:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (footer)
|
|
|
|
{
|
|
|
|
gimp_config_writer_linefeed (writer);
|
|
|
|
gimp_config_writer_comment (writer, footer);
|
|
|
|
}
|
|
|
|
|
2014-07-03 05:53:57 +08:00
|
|
|
if (writer->output)
|
2003-06-24 06:02:56 +08:00
|
|
|
{
|
2014-07-04 05:25:21 +08:00
|
|
|
success = gimp_config_writer_close_output (writer, error);
|
2003-03-06 04:21:50 +08:00
|
|
|
|
2019-08-08 03:50:51 +08:00
|
|
|
g_clear_object (&writer->file);
|
2003-03-06 04:21:50 +08:00
|
|
|
|
2003-06-24 06:02:56 +08:00
|
|
|
g_string_free (writer->buffer, TRUE);
|
2019-08-08 03:50:51 +08:00
|
|
|
writer->buffer = NULL;
|
2003-06-24 06:02:56 +08:00
|
|
|
}
|
2003-03-06 04:21:50 +08:00
|
|
|
|
2007-06-22 03:55:40 +08:00
|
|
|
if (writer->error)
|
|
|
|
{
|
2014-07-04 05:25:21 +08:00
|
|
|
if (error && *error == NULL)
|
|
|
|
g_propagate_error (error, writer->error);
|
|
|
|
else
|
|
|
|
g_clear_error (&writer->error);
|
|
|
|
|
2007-07-17 14:21:54 +08:00
|
|
|
success = FALSE;
|
2007-06-22 03:55:40 +08:00
|
|
|
}
|
|
|
|
|
2019-08-08 03:50:51 +08:00
|
|
|
writer->finished = TRUE;
|
|
|
|
|
|
|
|
gimp_config_writer_unref (writer);
|
2007-07-17 14:21:54 +08:00
|
|
|
|
2003-03-06 04:21:50 +08:00
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gimp_config_writer_linefeed (GimpConfigWriter *writer)
|
|
|
|
{
|
|
|
|
g_return_if_fail (writer != NULL);
|
2019-08-08 03:50:51 +08:00
|
|
|
g_return_if_fail (writer->finished == FALSE);
|
2003-10-12 09:49:51 +08:00
|
|
|
|
2003-03-06 04:21:50 +08:00
|
|
|
if (writer->error)
|
|
|
|
return;
|
|
|
|
|
2014-07-04 05:25:21 +08:00
|
|
|
if (writer->output && writer->buffer->len == 0 && !writer->comment)
|
2003-03-10 23:48:16 +08:00
|
|
|
{
|
2014-07-03 05:53:57 +08:00
|
|
|
GError *error = NULL;
|
|
|
|
|
|
|
|
if (! g_output_stream_write_all (writer->output, "\n", 1,
|
2014-09-08 02:30:14 +08:00
|
|
|
NULL, NULL, &error))
|
2014-07-03 05:53:57 +08:00
|
|
|
{
|
|
|
|
g_set_error (&writer->error, GIMP_CONFIG_ERROR, GIMP_CONFIG_ERROR_WRITE,
|
|
|
|
_("Error writing to '%s': %s"),
|
2014-07-04 05:25:21 +08:00
|
|
|
writer->file ?
|
|
|
|
gimp_file_get_utf8_name (writer->file) : "output stream",
|
|
|
|
error->message);
|
2014-07-03 05:53:57 +08:00
|
|
|
g_clear_error (&error);
|
|
|
|
}
|
2003-03-10 23:48:16 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2003-10-12 09:49:51 +08:00
|
|
|
gimp_config_writer_newline (writer);
|
2003-03-10 23:48:16 +08:00
|
|
|
}
|
2003-03-06 04:21:50 +08:00
|
|
|
}
|
|
|
|
|
2003-10-12 09:49:51 +08:00
|
|
|
/**
|
|
|
|
* gimp_config_writer_comment:
|
|
|
|
* @writer: a #GimpConfigWriter
|
|
|
|
* @comment: the comment to write (ASCII only)
|
|
|
|
*
|
|
|
|
* Appends the @comment to @str and inserts linebreaks and hash-marks to
|
|
|
|
* format it as a comment. Note that this function does not handle non-ASCII
|
|
|
|
* characters.
|
2005-01-29 20:54:48 +08:00
|
|
|
*
|
2015-06-01 03:18:09 +08:00
|
|
|
* Since: 2.4
|
2003-10-12 09:49:51 +08:00
|
|
|
**/
|
2003-03-06 04:21:50 +08:00
|
|
|
void
|
|
|
|
gimp_config_writer_comment (GimpConfigWriter *writer,
|
2006-04-12 18:53:28 +08:00
|
|
|
const gchar *comment)
|
2003-03-06 04:21:50 +08:00
|
|
|
{
|
2003-10-12 09:49:51 +08:00
|
|
|
const gchar *s;
|
2003-10-13 00:46:50 +08:00
|
|
|
gboolean comment_mode;
|
2003-10-12 09:49:51 +08:00
|
|
|
gint i, len, space;
|
|
|
|
|
|
|
|
#define LINE_LENGTH 75
|
|
|
|
|
2003-03-06 04:21:50 +08:00
|
|
|
g_return_if_fail (writer != NULL);
|
2019-08-08 03:50:51 +08:00
|
|
|
g_return_if_fail (writer->finished == FALSE);
|
2003-03-06 04:21:50 +08:00
|
|
|
|
|
|
|
if (writer->error)
|
|
|
|
return;
|
|
|
|
|
2004-08-29 20:13:34 +08:00
|
|
|
g_return_if_fail (writer->depth == 0);
|
|
|
|
|
2003-03-06 04:21:50 +08:00
|
|
|
if (!comment)
|
|
|
|
return;
|
|
|
|
|
2003-10-13 00:46:50 +08:00
|
|
|
comment_mode = writer->comment;
|
|
|
|
gimp_config_writer_comment_mode (writer, TRUE);
|
2003-10-12 09:49:51 +08:00
|
|
|
|
2003-10-13 00:46:50 +08:00
|
|
|
len = strlen (comment);
|
2003-10-12 09:49:51 +08:00
|
|
|
|
|
|
|
while (len > 0)
|
|
|
|
{
|
|
|
|
for (s = comment, i = 0, space = 0;
|
|
|
|
*s != '\n' && (i <= LINE_LENGTH || space == 0) && i < len;
|
|
|
|
s++, i++)
|
|
|
|
{
|
|
|
|
if (g_ascii_isspace (*s))
|
|
|
|
space = i;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i > LINE_LENGTH && space && *s != '\n')
|
|
|
|
i = space;
|
|
|
|
|
|
|
|
g_string_append_len (writer->buffer, comment, i);
|
|
|
|
|
|
|
|
i++;
|
|
|
|
|
|
|
|
comment += i;
|
|
|
|
len -= i;
|
2003-10-13 00:46:50 +08:00
|
|
|
|
|
|
|
if (len > 0)
|
|
|
|
gimp_config_writer_newline (writer);
|
2003-10-12 09:49:51 +08:00
|
|
|
}
|
|
|
|
|
2003-10-13 00:46:50 +08:00
|
|
|
gimp_config_writer_comment_mode (writer, comment_mode);
|
|
|
|
gimp_config_writer_newline (writer);
|
2003-03-06 04:21:50 +08:00
|
|
|
|
2003-10-12 09:49:51 +08:00
|
|
|
if (writer->depth == 0)
|
|
|
|
gimp_config_writer_flush (writer);
|
|
|
|
|
|
|
|
#undef LINE_LENGTH
|
|
|
|
}
|
|
|
|
|
2003-03-06 04:21:50 +08:00
|
|
|
static gboolean
|
2014-07-04 05:25:21 +08:00
|
|
|
gimp_config_writer_close_output (GimpConfigWriter *writer,
|
|
|
|
GError **error)
|
2003-03-06 04:21:50 +08:00
|
|
|
{
|
2014-07-03 05:53:57 +08:00
|
|
|
g_return_val_if_fail (writer->output != NULL, FALSE);
|
|
|
|
|
2003-03-06 04:21:50 +08:00
|
|
|
if (writer->error)
|
|
|
|
{
|
2018-11-26 22:40:38 +08:00
|
|
|
GCancellable *cancellable = g_cancellable_new ();
|
|
|
|
|
|
|
|
/* Cancel the overwrite initiated by g_file_replace(). */
|
|
|
|
g_cancellable_cancel (cancellable);
|
|
|
|
g_output_stream_close (writer->output, cancellable, NULL);
|
|
|
|
g_object_unref (cancellable);
|
|
|
|
|
2019-08-08 03:50:51 +08:00
|
|
|
g_clear_object (&writer->output);
|
2014-07-04 05:25:21 +08:00
|
|
|
|
2007-06-22 03:55:40 +08:00
|
|
|
return FALSE;
|
2003-03-06 04:21:50 +08:00
|
|
|
}
|
|
|
|
|
2014-07-04 05:25:21 +08:00
|
|
|
if (writer->file)
|
2009-03-17 19:30:27 +08:00
|
|
|
{
|
2014-07-04 05:25:21 +08:00
|
|
|
GError *my_error = NULL;
|
2009-03-17 19:30:27 +08:00
|
|
|
|
2014-07-04 05:25:21 +08:00
|
|
|
if (! g_output_stream_close (writer->output, NULL, &my_error))
|
|
|
|
{
|
|
|
|
g_set_error (error, GIMP_CONFIG_ERROR, GIMP_CONFIG_ERROR_WRITE,
|
|
|
|
_("Error writing '%s': %s"),
|
|
|
|
gimp_file_get_utf8_name (writer->file),
|
|
|
|
my_error->message);
|
|
|
|
g_clear_error (&my_error);
|
2003-03-06 04:21:50 +08:00
|
|
|
|
2019-08-08 03:50:51 +08:00
|
|
|
g_clear_object (&writer->output);
|
2014-07-04 05:25:21 +08:00
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
2003-03-06 04:21:50 +08:00
|
|
|
}
|
|
|
|
|
2019-08-08 03:50:51 +08:00
|
|
|
g_clear_object (&writer->output);
|
2003-03-06 04:21:50 +08:00
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|