comparison src/misc.c @ 1023:650c02c0c8ff

Move quoted_value() and escquote_value() to misc.[ch].
author zas_
date Sun, 31 Aug 2008 11:27:24 +0000
parents 9962b24b6b43
children 1646720364cf
comparison
equal deleted inserted replaced
1022:9962b24b6b43 1023:650c02c0c8ff
106 return g_build_filename(home, G_DIR_SEPARATOR_S, slash + 1, NULL); 106 return g_build_filename(home, G_DIR_SEPARATOR_S, slash + 1, NULL);
107 else 107 else
108 return g_build_filename(home, G_DIR_SEPARATOR_S, NULL); 108 return g_build_filename(home, G_DIR_SEPARATOR_S, NULL);
109 #endif 109 #endif
110 } 110 }
111
112 /*
113 returns text without quotes or NULL for empty or broken string
114 any text up to first '"' is skipped
115 tail is set to point at the char after the second '"'
116 or at the ending \0
117
118 */
119
120 gchar *quoted_value(const gchar *text, const gchar **tail)
121 {
122 const gchar *ptr;
123 gint c = 0;
124 gint l = strlen(text);
125 gchar *retval = NULL;
126
127 if (tail) *tail = text;
128
129 if (l == 0) return retval;
130
131 while (c < l && text[c] != '"') c++;
132 if (text[c] == '"')
133 {
134 gint e;
135 c++;
136 ptr = text + c;
137 e = c;
138 while (e < l)
139 {
140 if (text[e-1] != '\\' && text[e] == '"') break;
141 e++;
142 }
143 if (text[e] == '"')
144 {
145 if (e - c > 0)
146 {
147 gchar *substring = g_strndup(ptr, e - c);
148
149 if (substring)
150 {
151 retval = g_strcompress(substring);
152 g_free(substring);
153 }
154 }
155 }
156 if (tail) *tail = text + e + 1;
157 }
158 else
159 /* for compatibility with older formats (<0.3.7)
160 * read a line without quotes too */
161 {
162 c = 0;
163 while (c < l && text[c] != '\n' && !g_ascii_isspace(text[c])) c++;
164 if (c != 0)
165 {
166 retval = g_strndup(text, c);
167 }
168 if (tail) *tail = text + c;
169 }
170
171 return retval;
172 }
173
174 gchar *escquote_value(const gchar *text)
175 {
176 gchar *e;
177
178 if (!text) return g_strdup("\"\"");
179
180 e = g_strescape(text, "");
181 if (e)
182 {
183 gchar *retval = g_strdup_printf("\"%s\"", e);
184 g_free(e);
185 return retval;
186 }
187 return g_strdup("\"\"");
188 }