2003-03-06 04:21:50 +08:00
|
|
|
/* The GIMP -- an image manipulation program
|
|
|
|
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
|
|
|
|
*
|
|
|
|
* GimpConfigWriter
|
|
|
|
* Copyright (C) 2003 Sven Neumann <sven@gimp.org>
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdio.h>
|
2003-03-11 03:01:07 +08:00
|
|
|
#include <string.h>
|
2003-03-06 04:21:50 +08:00
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
|
|
|
#include <glib-object.h>
|
|
|
|
|
|
|
|
#ifdef G_OS_WIN32
|
|
|
|
#include <io.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "config-types.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
|
|
|
#include "gimpconfigwriter.h"
|
|
|
|
|
2003-03-26 00:38:19 +08:00
|
|
|
#include "gimp-intl.h"
|
2003-03-06 04:21:50 +08:00
|
|
|
|
|
|
|
|
2003-06-24 06:02:56 +08:00
|
|
|
struct _GimpConfigWriter
|
|
|
|
{
|
|
|
|
gint fd;
|
|
|
|
gchar *filename;
|
|
|
|
gchar *tmpname;
|
|
|
|
GError *error;
|
|
|
|
GString *buffer;
|
2003-10-12 09:49:51 +08:00
|
|
|
gboolean comment;
|
2003-06-24 06:02:56 +08:00
|
|
|
gint depth;
|
|
|
|
gint marker;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2003-10-12 09:49:51 +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_file (GimpConfigWriter *writer,
|
|
|
|
GError **error);
|
2003-03-06 04:21:50 +08:00
|
|
|
|
|
|
|
|
2003-10-12 09:49:51 +08:00
|
|
|
/**
|
|
|
|
* gimp_config_writer_new_file:
|
|
|
|
* @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.
|
|
|
|
*
|
|
|
|
* Return value: a new #GimpConfigWriter or %NULL in case of an error
|
|
|
|
**/
|
2003-03-06 04:21:50 +08:00
|
|
|
GimpConfigWriter *
|
2003-06-24 06:02:56 +08:00
|
|
|
gimp_config_writer_new_file (const gchar *filename,
|
2003-09-02 20:55:38 +08:00
|
|
|
gboolean atomic,
|
2003-06-24 06:02:56 +08:00
|
|
|
const gchar *header,
|
|
|
|
GError **error)
|
2003-03-06 04:21:50 +08:00
|
|
|
{
|
|
|
|
GimpConfigWriter *writer;
|
|
|
|
gchar *tmpname = NULL;
|
|
|
|
gint fd;
|
|
|
|
|
|
|
|
g_return_val_if_fail (filename != NULL, NULL);
|
|
|
|
g_return_val_if_fail (error == NULL || *error == NULL, NULL);
|
|
|
|
|
2003-09-02 20:55:38 +08:00
|
|
|
if (atomic)
|
2003-03-06 04:21:50 +08:00
|
|
|
{
|
|
|
|
tmpname = g_strconcat (filename, "XXXXXX", NULL);
|
2003-10-12 09:49:51 +08:00
|
|
|
|
2003-03-06 04:21:50 +08:00
|
|
|
fd = g_mkstemp (tmpname);
|
|
|
|
|
|
|
|
if (fd == -1)
|
|
|
|
{
|
2003-11-14 23:33:40 +08:00
|
|
|
g_set_error (error, GIMP_CONFIG_ERROR, GIMP_CONFIG_ERROR_WRITE,
|
|
|
|
_("Could not create temporary file for '%s': %s"),
|
2003-03-06 04:21:50 +08:00
|
|
|
filename, g_strerror (errno));
|
|
|
|
g_free (tmpname);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2003-03-10 23:48:16 +08:00
|
|
|
fd = creat (filename, 0644);
|
2003-03-06 04:21:50 +08:00
|
|
|
|
|
|
|
if (fd == -1)
|
|
|
|
{
|
2003-11-14 23:33:40 +08:00
|
|
|
g_set_error (error, GIMP_CONFIG_ERROR, GIMP_CONFIG_ERROR_WRITE,
|
|
|
|
_("Could not open '%s' for writing: %s"),
|
2003-03-06 04:21:50 +08:00
|
|
|
filename, g_strerror (errno));
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
writer = g_new0 (GimpConfigWriter, 1);
|
|
|
|
|
|
|
|
writer->fd = fd;
|
|
|
|
writer->filename = g_strdup (filename);
|
|
|
|
writer->tmpname = tmpname;
|
|
|
|
writer->buffer = g_string_new (NULL);
|
|
|
|
|
|
|
|
if (header)
|
|
|
|
{
|
|
|
|
gimp_config_writer_comment (writer, header);
|
|
|
|
gimp_config_writer_linefeed (writer);
|
|
|
|
}
|
|
|
|
|
|
|
|
return writer;
|
|
|
|
}
|
|
|
|
|
|
|
|
GimpConfigWriter *
|
2003-06-24 06:02:56 +08:00
|
|
|
gimp_config_writer_new_fd (gint fd)
|
2003-03-06 04:21:50 +08:00
|
|
|
{
|
|
|
|
GimpConfigWriter *writer;
|
|
|
|
|
|
|
|
g_return_val_if_fail (fd > 0, NULL);
|
|
|
|
|
|
|
|
writer = g_new0 (GimpConfigWriter, 1);
|
|
|
|
|
|
|
|
writer->fd = fd;
|
|
|
|
writer->buffer = g_string_new (NULL);
|
|
|
|
|
|
|
|
return writer;
|
|
|
|
}
|
|
|
|
|
2003-06-24 06:02:56 +08:00
|
|
|
GimpConfigWriter *
|
|
|
|
gimp_config_writer_new_string (GString *string)
|
|
|
|
{
|
|
|
|
GimpConfigWriter *writer;
|
|
|
|
|
|
|
|
g_return_val_if_fail (string != NULL, NULL);
|
|
|
|
|
|
|
|
writer = g_new0 (GimpConfigWriter, 1);
|
|
|
|
|
|
|
|
writer->buffer = string;
|
|
|
|
|
|
|
|
return 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
|
|
|
|
* funtion will insert a newline if necessary.
|
|
|
|
**/
|
2003-10-12 09:49:51 +08:00
|
|
|
void
|
|
|
|
gimp_config_writer_comment_mode (GimpConfigWriter *writer,
|
|
|
|
gboolean enable)
|
|
|
|
{
|
|
|
|
g_return_if_fail (writer != NULL);
|
|
|
|
|
|
|
|
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
|
|
|
|
*
|
|
|
|
* This function writes the opening parenthese followed by @name.
|
|
|
|
* It also increases the indentation level and sets a mark that
|
|
|
|
* can be used by gimp_config_writer_revert().
|
|
|
|
**/
|
2003-03-06 04:21:50 +08:00
|
|
|
void
|
|
|
|
gimp_config_writer_open (GimpConfigWriter *writer,
|
|
|
|
const gchar *name)
|
|
|
|
{
|
|
|
|
g_return_if_fail (writer != NULL);
|
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.
|
|
|
|
**/
|
2003-03-06 04:21:50 +08:00
|
|
|
void
|
|
|
|
gimp_config_writer_print (GimpConfigWriter *writer,
|
|
|
|
const gchar *string,
|
|
|
|
gint len)
|
|
|
|
{
|
|
|
|
g_return_if_fail (writer != NULL);
|
|
|
|
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().
|
|
|
|
* @Varargs: list of arguments according to @format
|
|
|
|
*
|
|
|
|
* A printf-like function for #GimpConfigWriter.
|
|
|
|
**/
|
2003-03-06 04:21:50 +08:00
|
|
|
void
|
|
|
|
gimp_config_writer_printf (GimpConfigWriter *writer,
|
|
|
|
const gchar *format,
|
|
|
|
...)
|
|
|
|
{
|
|
|
|
gchar *buffer;
|
|
|
|
va_list args;
|
|
|
|
|
|
|
|
g_return_if_fail (writer != NULL);
|
|
|
|
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.
|
|
|
|
**/
|
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);
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
**/
|
2003-03-06 04:21:50 +08:00
|
|
|
void
|
|
|
|
gimp_config_writer_revert (GimpConfigWriter *writer)
|
|
|
|
{
|
|
|
|
g_return_if_fail (writer != NULL);
|
|
|
|
g_return_if_fail (writer->depth > 0);
|
2003-03-10 23:48:16 +08:00
|
|
|
g_return_if_fail (writer->marker != -1);
|
2003-03-06 04:21:50 +08:00
|
|
|
|
|
|
|
if (writer->error)
|
|
|
|
return;
|
|
|
|
|
|
|
|
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().
|
|
|
|
**/
|
2003-03-06 04:21:50 +08:00
|
|
|
void
|
|
|
|
gimp_config_writer_close (GimpConfigWriter *writer)
|
|
|
|
{
|
|
|
|
g_return_if_fail (writer != NULL);
|
|
|
|
g_return_if_fail (writer->depth > 0);
|
|
|
|
|
|
|
|
if (writer->error)
|
|
|
|
return;
|
|
|
|
|
|
|
|
g_string_append_c (writer->buffer, ')');
|
|
|
|
|
|
|
|
if (--writer->depth == 0)
|
|
|
|
{
|
|
|
|
g_string_append_c (writer->buffer, '\n');
|
|
|
|
|
2003-06-24 06:02:56 +08:00
|
|
|
if (writer->fd)
|
2003-10-12 09:49:51 +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
|
|
|
|
*
|
|
|
|
* This function finishes the work of @writer and frees it afterwards.
|
|
|
|
* It closes all open elements, appends an optional comment and
|
|
|
|
* releases all resources allocated by @writer. You must not access
|
|
|
|
* the @writer afterwards.
|
|
|
|
*
|
|
|
|
* Return value: %TRUE if everything could be successfully written,
|
|
|
|
* %FALSE otherwise
|
|
|
|
**/
|
2003-03-06 04:21:50 +08:00
|
|
|
gboolean
|
|
|
|
gimp_config_writer_finish (GimpConfigWriter *writer,
|
|
|
|
const gchar *footer,
|
|
|
|
GError **error)
|
|
|
|
{
|
|
|
|
gboolean success = TRUE;
|
|
|
|
|
|
|
|
g_return_val_if_fail (writer != NULL, FALSE);
|
|
|
|
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)
|
|
|
|
gimp_config_writer_close (writer);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (footer)
|
|
|
|
{
|
|
|
|
gimp_config_writer_linefeed (writer);
|
|
|
|
gimp_config_writer_comment (writer, footer);
|
|
|
|
}
|
|
|
|
|
2003-06-24 06:02:56 +08:00
|
|
|
if (writer->fd)
|
|
|
|
{
|
|
|
|
success = gimp_config_writer_close_file (writer, error);
|
2003-03-06 04:21:50 +08:00
|
|
|
|
2003-06-24 06:02:56 +08:00
|
|
|
g_free (writer->filename);
|
|
|
|
g_free (writer->tmpname);
|
2003-03-06 04:21:50 +08:00
|
|
|
|
2003-06-24 06:02:56 +08:00
|
|
|
g_string_free (writer->buffer, TRUE);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
success = TRUE;
|
|
|
|
}
|
2003-03-06 04:21:50 +08:00
|
|
|
|
|
|
|
g_free (writer);
|
|
|
|
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gimp_config_writer_linefeed (GimpConfigWriter *writer)
|
|
|
|
{
|
|
|
|
g_return_if_fail (writer != NULL);
|
2003-10-12 09:49:51 +08:00
|
|
|
|
2003-03-06 04:21:50 +08:00
|
|
|
if (writer->error)
|
|
|
|
return;
|
|
|
|
|
2003-10-12 09:49:51 +08:00
|
|
|
if (writer->buffer->len == 0 && !writer->comment)
|
2003-03-10 23:48:16 +08:00
|
|
|
{
|
|
|
|
if (write (writer->fd, "\n", 1) < 0)
|
2003-11-14 23:33:40 +08:00
|
|
|
g_set_error (&writer->error, GIMP_CONFIG_ERROR, GIMP_CONFIG_ERROR_WRITE,
|
2003-03-10 23:48:16 +08:00
|
|
|
g_strerror (errno));
|
|
|
|
}
|
|
|
|
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.
|
|
|
|
**/
|
2003-03-06 04:21:50 +08:00
|
|
|
void
|
|
|
|
gimp_config_writer_comment (GimpConfigWriter *writer,
|
|
|
|
const gchar *comment)
|
|
|
|
{
|
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);
|
|
|
|
g_return_if_fail (writer->depth == 0);
|
|
|
|
|
|
|
|
if (writer->error)
|
|
|
|
return;
|
|
|
|
|
|
|
|
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-10-13 00:46:50 +08:00
|
|
|
|
2003-10-12 09:49:51 +08:00
|
|
|
static inline void
|
|
|
|
gimp_config_writer_flush (GimpConfigWriter *writer)
|
|
|
|
{
|
2003-03-06 04:21:50 +08:00
|
|
|
if (write (writer->fd, writer->buffer->str, writer->buffer->len) < 0)
|
2003-11-14 23:33:40 +08:00
|
|
|
g_set_error (&writer->error, GIMP_CONFIG_ERROR, GIMP_CONFIG_ERROR_WRITE,
|
2003-03-06 04:21:50 +08:00
|
|
|
g_strerror (errno));
|
2003-10-12 09:49:51 +08:00
|
|
|
|
2003-03-06 04:21:50 +08:00
|
|
|
g_string_truncate (writer->buffer, 0);
|
|
|
|
}
|
|
|
|
|
2003-10-12 09:49:51 +08:00
|
|
|
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
|
|
|
static gboolean
|
|
|
|
gimp_config_writer_close_file (GimpConfigWriter *writer,
|
|
|
|
GError **error)
|
|
|
|
{
|
2003-06-24 06:02:56 +08:00
|
|
|
g_return_val_if_fail (writer->fd != 0, FALSE);
|
|
|
|
|
2003-03-06 04:21:50 +08:00
|
|
|
if (! writer->filename)
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
if (writer->error)
|
|
|
|
{
|
|
|
|
close (writer->fd);
|
|
|
|
|
|
|
|
if (writer->tmpname)
|
|
|
|
unlink (writer->tmpname);
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (close (writer->fd) != 0)
|
|
|
|
{
|
|
|
|
if (writer->tmpname)
|
|
|
|
{
|
|
|
|
if (g_file_test (writer->filename, G_FILE_TEST_EXISTS))
|
|
|
|
{
|
|
|
|
g_set_error (error, GIMP_CONFIG_ERROR, GIMP_CONFIG_ERROR_WRITE,
|
|
|
|
_("Error writing to temporary file for '%s': %s\n"
|
|
|
|
"The original file has not been touched."),
|
|
|
|
writer->filename, g_strerror (errno));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
g_set_error (error, GIMP_CONFIG_ERROR, GIMP_CONFIG_ERROR_WRITE,
|
|
|
|
_("Error writing to temporary file for '%s': %s\n"
|
|
|
|
"No file has been created."),
|
|
|
|
writer->filename, g_strerror (errno));
|
|
|
|
}
|
|
|
|
|
|
|
|
unlink (writer->tmpname);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
g_set_error (error, GIMP_CONFIG_ERROR, GIMP_CONFIG_ERROR_WRITE,
|
|
|
|
_("Error writing to '%s': %s"),
|
|
|
|
writer->filename, g_strerror (errno));
|
|
|
|
}
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (writer->tmpname)
|
|
|
|
{
|
|
|
|
#ifdef G_OS_WIN32
|
|
|
|
/* win32 rename can't overwrite */
|
|
|
|
unlink (writer->filename);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (rename (writer->tmpname, writer->filename) == -1)
|
|
|
|
{
|
2003-11-14 23:33:40 +08:00
|
|
|
g_set_error (error, GIMP_CONFIG_ERROR, GIMP_CONFIG_ERROR_WRITE,
|
|
|
|
_("Could not create '%s': %s"),
|
2003-03-06 04:21:50 +08:00
|
|
|
writer->filename, g_strerror (errno));
|
2003-10-12 09:49:51 +08:00
|
|
|
|
2003-03-06 04:21:50 +08:00
|
|
|
unlink (writer->tmpname);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|