comparison src/rcfile.c @ 1023:650c02c0c8ff

Move quoted_value() and escquote_value() to misc.[ch].
author zas_
date Sun, 31 Aug 2008 11:27:24 +0000
parents 4aa1a6235458
children 1646720364cf
comparison
equal deleted inserted replaced
1022:9962b24b6b43 1023:650c02c0c8ff
17 #include "rcfile.h" 17 #include "rcfile.h"
18 18
19 #include "bar_exif.h" 19 #include "bar_exif.h"
20 #include "editors.h" 20 #include "editors.h"
21 #include "filefilter.h" 21 #include "filefilter.h"
22 #include "misc.h"
22 #include "pixbuf-renderer.h" 23 #include "pixbuf-renderer.h"
23 #include "secure_save.h" 24 #include "secure_save.h"
24 #include "slideshow.h" 25 #include "slideshow.h"
25 #include "ui_fileops.h" 26 #include "ui_fileops.h"
26
27 27
28 /* 28 /*
29 *----------------------------------------------------------------------------- 29 *-----------------------------------------------------------------------------
30 * line write/parse routines (private) 30 * line write/parse routines (private)
31 *----------------------------------------------------------------------------- 31 *-----------------------------------------------------------------------------
32 */ 32 */
33 33
34 /*
35 returns text without quotes or NULL for empty or broken string
36 any text up to first '"' is skipped
37 tail is set to point at the char after the second '"'
38 or at the ending \0
39
40 */
41
42 gchar *quoted_value(const gchar *text, const gchar **tail)
43 {
44 const gchar *ptr;
45 gint c = 0;
46 gint l = strlen(text);
47 gchar *retval = NULL;
48
49 if (tail) *tail = text;
50
51 if (l == 0) return retval;
52
53 while (c < l && text[c] != '"') c++;
54 if (text[c] == '"')
55 {
56 gint e;
57 c++;
58 ptr = text + c;
59 e = c;
60 while (e < l)
61 {
62 if (text[e-1] != '\\' && text[e] == '"') break;
63 e++;
64 }
65 if (text[e] == '"')
66 {
67 if (e - c > 0)
68 {
69 gchar *substring = g_strndup(ptr, e - c);
70
71 if (substring)
72 {
73 retval = g_strcompress(substring);
74 g_free(substring);
75 }
76 }
77 }
78 if (tail) *tail = text + e + 1;
79 }
80 else
81 /* for compatibility with older formats (<0.3.7)
82 * read a line without quotes too */
83 {
84 c = 0;
85 while (c < l && text[c] != '\n' && !g_ascii_isspace(text[c])) c++;
86 if (c != 0)
87 {
88 retval = g_strndup(text, c);
89 }
90 if (tail) *tail = text + c;
91 }
92
93 return retval;
94 }
95
96 gchar *escquote_value(const gchar *text)
97 {
98 gchar *e;
99
100 if (!text) return g_strdup("\"\"");
101
102 e = g_strescape(text, "");
103 if (e)
104 {
105 gchar *retval = g_strdup_printf("\"%s\"", e);
106 g_free(e);
107 return retval;
108 }
109 return g_strdup("\"\"");
110 }
111 34
112 static void write_char_option(SecureSaveInfo *ssi, gchar *label, gchar *text) 35 static void write_char_option(SecureSaveInfo *ssi, gchar *label, gchar *text)
113 { 36 {
114 gchar *escval = escquote_value(text); 37 gchar *escval = escquote_value(text);
115 38