comparison src/image-overlay.c @ 752:85c9412d77d2

Allow to escape star characters in the template string with a \. One can insert a \ by escaping it with another \. Fix deletion of empty parts when using | trick. Append the separator " - " only if data is neither NULL nor empty. Patch by Uwe Ohse and Laurent Monin.
author zas_
date Sat, 24 May 2008 20:01:59 +0000
parents a7289f9e8d29
children 649b44dd558b
comparison
equal deleted inserted replaced
751:f73df252aa05 752:85c9412d77d2
304 if (extra) 304 if (extra)
305 { 305 {
306 if (data && *data) 306 if (data && *data)
307 { 307 {
308 /* Display data between left and right parts of extra string 308 /* Display data between left and right parts of extra string
309 * the data is expressed by a '*' character. 309 * the data is expressed by a '*' character. A '*' may be escaped
310 * by a \. You should escape all '*' characters, do not rely on the
311 * current implementation which only replaces the first unescaped '*'.
310 * If no "*" is present, the extra string is just appended to data string. 312 * If no "*" is present, the extra string is just appended to data string.
311 * Pango mark up is accepted in left and right parts. 313 * Pango mark up is accepted in left and right parts.
312 * Any \n is replaced by a newline 314 * Any \n is replaced by a newline
313 * Examples: 315 * Examples:
314 * "<i>*</i>\n" -> data is displayed in italics ended with a newline 316 * "<i>*</i>\n" -> data is displayed in italics ended with a newline
315 * "\n" -> ended with newline 317 * "\n" -> ended with newline
316 * "ISO *" -> prefix data with "ISO " (ie. "ISO 100") 318 * "ISO *" -> prefix data with "ISO " (ie. "ISO 100")
319 * "\**\*" -> prefix data with a star, and append a star (ie. "*100*")
320 * "\\*" -> prefix data with an anti slash (ie "\100")
317 * "Collection <b>*</b>\n" -> display data in bold prefixed by "Collection " and a newline is appended 321 * "Collection <b>*</b>\n" -> display data in bold prefixed by "Collection " and a newline is appended
318 * 322 *
319 * FIXME: using background / foreground colors lead to weird results. 323 * FIXME: using background / foreground colors lead to weird results.
320 */ 324 */
321 gchar *new_data; 325 gchar *new_data;
322 gchar *left = NULL; 326 gchar *left = NULL;
323 gchar *right = extra; 327 gchar *right = extra;
324 gchar *p; 328 gchar *p;
325 guint len = strlen(extra); 329 guint len = strlen(extra);
326 330
327 /* Search and replace "\n" by a newline character */ 331 /* Search for left and right parts and unescape characters */
328 for (p = extra; *p; p++, len--) 332 for (p = extra; *p; p++, len--)
329 if (p[0] == '\\' && p[1] == 'n') 333 if (p[0] == '\\')
330 { 334 {
331 memmove(p+1, p+2, --len); 335 if (p[1] == 'n')
332 *p = '\n'; 336 {
337 memmove(p+1, p+2, --len);
338 p[0] = '\n';
339 }
340 else if (p[1] != '\0')
341 memmove(p, p+1, len--); // includes \0
333 } 342 }
334 343 else if (p[0] == '*' && !left)
335 /* Search for left and right parts */
336 for (p = extra; *p; p++)
337 if (*p == '*')
338 { 344 {
339 *p = '\0'; 345 right = p + 1;
340 p++;
341 right = p;
342 left = extra; 346 left = extra;
343 break;
344 } 347 }
345 348
349 if (left) right[-1] = '\0';
350
346 new_data = g_strdup_printf("%s%s%s", left ? left : "", data, right); 351 new_data = g_strdup_printf("%s%s%s", left ? left : "", data, right);
347 g_free(data); 352 g_free(data);
348 data = new_data; 353 data = new_data;
349 } 354 }
350 g_free(extra); 355 g_free(extra);
352 357
353 g_string_erase(new, pos, end-start+1); 358 g_string_erase(new, pos, end-start+1);
354 if (data) 359 if (data)
355 g_string_insert(new, pos, data); 360 g_string_insert(new, pos, data);
356 361
357 if (pos-prev == 2 && new->str[pos-1] == imp) 362 if (pos-prev >= 2 && new->str[pos-1] == imp)
358 { 363 {
359 g_string_erase(new, --pos, 1); 364 g_string_erase(new, --pos, 1);
360 if (last && data) 365 if (last && data && *data)
361 { 366 {
362 g_string_insert(new, pos, sep); 367 g_string_insert(new, pos, sep);
363 pos += strlen(sep); 368 pos += strlen(sep);
364 } 369 }
365 } 370 }