1
/* The GIMP -- an image manipulation program
2
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
5
* Copyright (C) 2003 Sven Neumann <sven@gimp.org>
7
* This program is free software; you can redistribute it and/or modify
8
* it under the terms of the GNU General Public License as published by
9
* the Free Software Foundation; either version 2 of the License, or
10
* (at your option) any later version.
12
* This program is distributed in the hope that it will be useful,
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
* GNU General Public License for more details.
17
* You should have received a copy of the GNU General Public License
18
* along with this program; if not, write to the Free Software
19
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
31
#include <sys/types.h>
34
#include <glib-object.h>
40
#include "libgimpbase/gimpbase.h"
42
#include "config-types.h"
44
#include "gimpconfig-error.h"
45
#include "gimpconfig-serialize.h"
46
#include "gimpconfig-utils.h"
47
#include "gimpconfigwriter.h"
49
#include "gimp-intl.h"
52
struct _GimpConfigWriter
65
static inline void gimp_config_writer_flush (GimpConfigWriter *writer);
66
static inline void gimp_config_writer_newline (GimpConfigWriter *writer);
67
static gboolean gimp_config_writer_close_file (GimpConfigWriter *writer,
71
gimp_config_writer_flush (GimpConfigWriter *writer)
73
if (write (writer->fd, writer->buffer->str, writer->buffer->len) < 0)
74
g_set_error (&writer->error, GIMP_CONFIG_ERROR, GIMP_CONFIG_ERROR_WRITE,
77
g_string_truncate (writer->buffer, 0);
81
gimp_config_writer_newline (GimpConfigWriter *writer)
85
g_string_append_c (writer->buffer, '\n');
88
g_string_append_len (writer->buffer, "# ", 2);
90
for (i = 0; i < writer->depth; i++)
91
g_string_append_len (writer->buffer, " ", 4);
95
* gimp_config_writer_new_file:
96
* @filename: a filename
97
* @atomic: if %TRUE the file is written atomically
98
* @header: text to include as comment at the top of the file
99
* @error: return location for errors
101
* Creates a new #GimpConfigWriter and sets it up to write to
102
* @filename. If @atomic is %TRUE, a temporary file is used to avoid
103
* possible race conditions. The temporary file is then moved to
104
* @filename when the writer is closed.
106
* Return value: a new #GimpConfigWriter or %NULL in case of an error
109
gimp_config_writer_new_file (const gchar *filename,
114
GimpConfigWriter *writer;
115
gchar *tmpname = NULL;
118
g_return_val_if_fail (filename != NULL, NULL);
119
g_return_val_if_fail (error == NULL || *error == NULL, NULL);
123
tmpname = g_strconcat (filename, "XXXXXX", NULL);
125
fd = g_mkstemp (tmpname);
129
g_set_error (error, GIMP_CONFIG_ERROR, GIMP_CONFIG_ERROR_WRITE,
130
_("Could not create temporary file for '%s': %s"),
131
gimp_filename_to_utf8 (filename), g_strerror (errno));
138
fd = creat (filename, 0644);
142
g_set_error (error, GIMP_CONFIG_ERROR, GIMP_CONFIG_ERROR_WRITE,
143
_("Could not open '%s' for writing: %s"),
144
gimp_filename_to_utf8 (filename), g_strerror (errno));
149
writer = g_new0 (GimpConfigWriter, 1);
152
writer->filename = g_strdup (filename);
153
writer->tmpname = tmpname;
154
writer->buffer = g_string_new (NULL);
158
gimp_config_writer_comment (writer, header);
159
gimp_config_writer_linefeed (writer);
166
gimp_config_writer_new_fd (gint fd)
168
GimpConfigWriter *writer;
170
g_return_val_if_fail (fd > 0, NULL);
172
writer = g_new0 (GimpConfigWriter, 1);
175
writer->buffer = g_string_new (NULL);
181
gimp_config_writer_new_string (GString *string)
183
GimpConfigWriter *writer;
185
g_return_val_if_fail (string != NULL, NULL);
187
writer = g_new0 (GimpConfigWriter, 1);
189
writer->buffer = string;
195
* gimp_config_writer_comment_mode:
196
* @writer: a #GimpConfigWriter
197
* @enable: %TRUE to enable comment mode, %FALSE to disable it
199
* This function toggles whether the @writer should create commented
200
* or uncommented output. This feature is used to generate the
201
* system-wide installed gimprc that documents the default settings.
203
* Since comments have to start at the beginning of a line, this
204
* funtion will insert a newline if necessary.
207
gimp_config_writer_comment_mode (GimpConfigWriter *writer,
210
g_return_if_fail (writer != NULL);
215
enable = (enable ? TRUE : FALSE);
217
if (writer->comment == enable)
220
writer->comment = enable;
224
if (writer->buffer->len == 0)
225
g_string_append_len (writer->buffer, "# ", 2);
227
gimp_config_writer_newline (writer);
233
* gimp_config_writer_open:
234
* @writer: a #GimpConfigWriter
235
* @name: name of the element to open
237
* This function writes the opening parenthese followed by @name.
238
* It also increases the indentation level and sets a mark that
239
* can be used by gimp_config_writer_revert().
242
gimp_config_writer_open (GimpConfigWriter *writer,
245
g_return_if_fail (writer != NULL);
246
g_return_if_fail (name != NULL);
251
/* store the current buffer length so we can revert to this state */
252
writer->marker = writer->buffer->len;
254
if (writer->depth > 0)
255
gimp_config_writer_newline (writer);
259
g_string_append_printf (writer->buffer, "(%s", name);
263
* gimp_config_writer_print:
264
* @writer: a #GimpConfigWriter
265
* @string: a string to write
266
* @len: number of bytes from @string or -1 if @string is NUL-terminated.
268
* Appends a space followed by @string to the @writer. Note that string
269
* must not contain any special characters that might need to be escaped.
272
gimp_config_writer_print (GimpConfigWriter *writer,
276
g_return_if_fail (writer != NULL);
277
g_return_if_fail (len == 0 || string != NULL);
283
len = strlen (string);
287
g_string_append_c (writer->buffer, ' ');
288
g_string_append_len (writer->buffer, string, len);
293
* gimp_config_writer_printf:
294
* @writer: a #GimpConfigWriter
295
* @format: a format string as described for g_strdup_printf().
296
* @Varargs: list of arguments according to @format
298
* A printf-like function for #GimpConfigWriter.
301
gimp_config_writer_printf (GimpConfigWriter *writer,
308
g_return_if_fail (writer != NULL);
309
g_return_if_fail (format != NULL);
314
va_start (args, format);
315
buffer = g_strdup_vprintf (format, args);
318
g_string_append_c (writer->buffer, ' ');
319
g_string_append (writer->buffer, buffer);
325
* gimp_config_writer_string:
326
* @writer: a #GimpConfigWriter
327
* @string: a NUL-terminated string
329
* Writes a string value to @writer. The @string is quoted and special
330
* characters are escaped.
333
gimp_config_writer_string (GimpConfigWriter *writer,
336
g_return_if_fail (writer != NULL);
341
g_string_append_c (writer->buffer, ' ');
342
gimp_config_string_append_escaped (writer->buffer, string);
346
* gimp_config_writer_identifier:
347
* @writer: a #GimpConfigWriter
348
* @identifier: a NUL-terminated string
350
* Writes an identifier to @writer. The @string is *not* quoted and special
351
* characters are *not* escaped.
354
gimp_config_writer_identifier (GimpConfigWriter *writer,
355
const gchar *identifier)
357
g_return_if_fail (writer != NULL);
358
g_return_if_fail (identifier != NULL);
363
g_string_append_printf (writer->buffer, " %s", identifier);
367
gimp_config_writer_data (GimpConfigWriter *writer,
373
g_return_if_fail (writer != NULL);
374
g_return_if_fail (length > 0);
375
g_return_if_fail (data != NULL);
380
g_string_append (writer->buffer, " \"");
382
for (i = 0; i < length; i++)
384
if (g_ascii_isalpha (data[i]))
385
g_string_append_c (writer->buffer, data[i]);
387
g_string_append_printf (writer->buffer, "\\%o", data[i]);
390
g_string_append (writer->buffer, "\"");
394
* gimp_config_writer_revert:
395
* @writer: a #GimpConfigWriter
397
* Reverts all changes to @writer that were done since the last call
398
* to gimp_config_writer_open(). This can only work if you didn't call
399
* gimp_config_writer_close() yet.
402
gimp_config_writer_revert (GimpConfigWriter *writer)
404
g_return_if_fail (writer != NULL);
409
g_return_if_fail (writer->depth > 0);
410
g_return_if_fail (writer->marker != -1);
412
g_string_truncate (writer->buffer, writer->marker);
419
* gimp_config_writer_close:
420
* @writer: a #GimpConfigWriter
422
* Closes an element opened with gimp_config_writer_open().
425
gimp_config_writer_close (GimpConfigWriter *writer)
427
g_return_if_fail (writer != NULL);
432
g_return_if_fail (writer->depth > 0);
434
g_string_append_c (writer->buffer, ')');
436
if (--writer->depth == 0)
438
g_string_append_c (writer->buffer, '\n');
441
gimp_config_writer_flush (writer);
446
* gimp_config_writer_finish:
447
* @writer: a #GimpConfigWriter
448
* @footer: text to include as comment at the bottom of the file
449
* @error: return location for possible errors
451
* This function finishes the work of @writer and frees it afterwards.
452
* It closes all open elements, appends an optional comment and
453
* releases all resources allocated by @writer. You must not access
454
* the @writer afterwards.
456
* Return value: %TRUE if everything could be successfully written,
460
gimp_config_writer_finish (GimpConfigWriter *writer,
464
gboolean success = TRUE;
466
g_return_val_if_fail (writer != NULL, FALSE);
467
g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
469
if (writer->depth < 0)
471
g_warning ("gimp_config_writer_finish: depth < 0 !!");
475
while (writer->depth)
476
gimp_config_writer_close (writer);
481
gimp_config_writer_linefeed (writer);
482
gimp_config_writer_comment (writer, footer);
487
success = gimp_config_writer_close_file (writer, error);
489
g_free (writer->filename);
490
g_free (writer->tmpname);
492
g_string_free (writer->buffer, TRUE);
505
gimp_config_writer_linefeed (GimpConfigWriter *writer)
507
g_return_if_fail (writer != NULL);
512
if (writer->buffer->len == 0 && !writer->comment)
514
if (write (writer->fd, "\n", 1) < 0)
515
g_set_error (&writer->error, GIMP_CONFIG_ERROR, GIMP_CONFIG_ERROR_WRITE,
520
gimp_config_writer_newline (writer);
525
* gimp_config_writer_comment:
526
* @writer: a #GimpConfigWriter
527
* @comment: the comment to write (ASCII only)
529
* Appends the @comment to @str and inserts linebreaks and hash-marks to
530
* format it as a comment. Note that this function does not handle non-ASCII
534
gimp_config_writer_comment (GimpConfigWriter *writer,
535
const gchar *comment)
538
gboolean comment_mode;
541
#define LINE_LENGTH 75
543
g_return_if_fail (writer != NULL);
548
g_return_if_fail (writer->depth == 0);
553
comment_mode = writer->comment;
554
gimp_config_writer_comment_mode (writer, TRUE);
556
len = strlen (comment);
560
for (s = comment, i = 0, space = 0;
561
*s != '\n' && (i <= LINE_LENGTH || space == 0) && i < len;
564
if (g_ascii_isspace (*s))
568
if (i > LINE_LENGTH && space && *s != '\n')
571
g_string_append_len (writer->buffer, comment, i);
579
gimp_config_writer_newline (writer);
582
gimp_config_writer_comment_mode (writer, comment_mode);
583
gimp_config_writer_newline (writer);
585
if (writer->depth == 0)
586
gimp_config_writer_flush (writer);
592
gimp_config_writer_close_file (GimpConfigWriter *writer,
595
g_return_val_if_fail (writer->fd != 0, FALSE);
597
if (! writer->filename)
605
unlink (writer->tmpname);
610
if (close (writer->fd) != 0)
614
if (g_file_test (writer->filename, G_FILE_TEST_EXISTS))
616
g_set_error (error, GIMP_CONFIG_ERROR, GIMP_CONFIG_ERROR_WRITE,
617
_("Error writing to temporary file for '%s': %s\n"
618
"The original file has not been touched."),
619
gimp_filename_to_utf8 (writer->filename),
624
g_set_error (error, GIMP_CONFIG_ERROR, GIMP_CONFIG_ERROR_WRITE,
625
_("Error writing to temporary file for '%s': %s\n"
626
"No file has been created."),
627
gimp_filename_to_utf8 (writer->filename),
631
unlink (writer->tmpname);
635
g_set_error (error, GIMP_CONFIG_ERROR, GIMP_CONFIG_ERROR_WRITE,
636
_("Error writing to '%s': %s"),
637
gimp_filename_to_utf8 (writer->filename),
647
/* win32 rename can't overwrite */
648
unlink (writer->filename);
651
if (rename (writer->tmpname, writer->filename) == -1)
653
g_set_error (error, GIMP_CONFIG_ERROR, GIMP_CONFIG_ERROR_WRITE,
654
_("Could not create '%s': %s"),
655
gimp_filename_to_utf8 (writer->filename),
658
unlink (writer->tmpname);