comparison src/rcfile.c @ 217:5bdab7ed4bcd

use quoted_value everywhere simplify parsing of multiple quoted values on one line
author nadvornik
date Mon, 31 Mar 2008 21:09:36 +0000
parents 3fa93ab9b119
children f4a0555794a9
comparison
equal deleted inserted replaced
216:a57b153e7fa4 217:5bdab7ed4bcd
21 /* 21 /*
22 *----------------------------------------------------------------------------- 22 *-----------------------------------------------------------------------------
23 * line write/parse routines (private) 23 * line write/parse routines (private)
24 *----------------------------------------------------------------------------- 24 *-----------------------------------------------------------------------------
25 */ 25 */
26 26
27 gchar *quoted_value(const gchar *text) 27 /*
28 returns text without quotes or NULL for empty or broken string
29 any text up to first '"' is skipped
30 tail is set to point at the char after the second '"'
31 or at the ending \0
32
33 */
34
35 gchar *quoted_value(const gchar *text, const gchar **tail)
28 { 36 {
29 const gchar *ptr; 37 const gchar *ptr;
30 gint c = 0; 38 gint c = 0;
31 gint l = strlen(text); 39 gint l = strlen(text);
32 gchar *retval = NULL; 40 gchar *retval = NULL;
41
42 if (tail) *tail = text;
33 43
34 if (l == 0) return retval; 44 if (l == 0) return retval;
35 45
36 while (c < l && text[c] !='"') c++; 46 while (c < l && text[c] !='"') c++;
37 if (text[c] == '"') 47 if (text[c] == '"')
56 retval = g_strcompress(substring); 66 retval = g_strcompress(substring);
57 g_free(substring); 67 g_free(substring);
58 } 68 }
59 } 69 }
60 } 70 }
71 if (tail) *tail = text + e + 1;
61 } 72 }
62 else 73 else
63 /* for compatibility with older formats (<0.3.7) 74 /* for compatibility with older formats (<0.3.7)
64 * read a line without quotes too */ 75 * read a line without quotes too */
65 { 76 {
67 while (c < l && text[c] !=' ' && text[c] !=8 && text[c] != '\n') c++; 78 while (c < l && text[c] !=' ' && text[c] !=8 && text[c] != '\n') c++;
68 if (c != 0) 79 if (c != 0)
69 { 80 {
70 retval = g_strndup(text, c); 81 retval = g_strndup(text, c);
71 } 82 }
83 if (tail) *tail = text + c;
72 } 84 }
73 85
74 return retval; 86 return retval;
75 } 87 }
76 88
77 gchar *escquote_value(const gchar *text) 89 gchar *escquote_value(const gchar *text)
78 { 90 {
79 gchar *e; 91 gchar *e;
80 gchar *retval; 92 gchar *retval;
81 93
82 if (!text) return g_strdup(""); 94 if (!text) return g_strdup("\"\"");
83 95
84 e = g_strescape(text, ""); 96 e = g_strescape(text, "");
85 if (e) 97 if (e)
86 { 98 {
87 gchar *retval = g_strdup_printf("\"%s\"", e); 99 gchar *retval = g_strdup_printf("\"%s\"", e);
88 g_free(e); 100 g_free(e);
89 return retval; 101 return retval;
90 } 102 }
91 return g_strdup(""); 103 return g_strdup("\"\"");
92 } 104 }
93 105
94 static void write_char_option(FILE *f, gchar *label, gchar *text) 106 static void write_char_option(FILE *f, gchar *label, gchar *text)
95 { 107 {
96 gchar *escval = escquote_value(text); 108 gchar *escval = escquote_value(text);
97 109
98 if (escval) 110 fprintf(f,"%s: %s\n", label, escval);
99 { 111 g_free(escval);
100 fprintf(f,"%s: %s\n", label, escval); 112 }
101 g_free(escval); 113
114 static gchar *read_char_option(FILE *f, gchar *option, gchar *label, gchar *value, gchar *text)
115 {
116 if (strcasecmp(option, label) == 0)
117 {
118 g_free(text);
119 text = quoted_value(value, NULL);
120 }
121 return text;
122 }
123
124 static void write_color_option(FILE *f, gchar *label, GdkColor *color)
125 {
126 if (color)
127 {
128 gchar *colorstring = gdk_color_to_string (color);
129
130 write_char_option(f, label, colorstring);
131 g_free(colorstring);
102 } 132 }
103 else 133 else
104 fprintf(f,"%s: \n", label); 134 fprintf(f,"%s: \n", label);
105 } 135 }
106 136
107 static gchar *read_char_option(FILE *f, gchar *option, gchar *label, gchar *value, gchar *text) 137 static GdkColor *read_color_option(FILE *f, gchar *option, gchar *label, gchar *value, GdkColor *color)
108 { 138 {
109 if (strcasecmp(option, label) == 0) 139 if (strcasecmp(option, label) == 0)
110 { 140 {
111 g_free(text); 141 gchar *colorstr = quoted_value(value, NULL);
112 text = quoted_value(value); 142 if (colorstr) gdk_color_parse(colorstr, color);
113 } 143 g_free(colorstr);
114 return text;
115 }
116
117 static void write_color_option(FILE *f, gchar *label, GdkColor *color)
118 {
119 if (color)
120 {
121 gchar *colorstring = gdk_color_to_string (color);
122
123 fprintf(f,"%s: \"%s\"\n", label, colorstring);
124 g_free(colorstring);
125 }
126 else
127 fprintf(f,"%s: \n", label);
128 }
129
130 static GdkColor *read_color_option(FILE *f, gchar *option, gchar *label, gchar *value, GdkColor *color)
131 {
132 if (strcasecmp(option, label) == 0)
133 {
134 gdk_color_parse(quoted_value(value), color);
135 } 144 }
136 return color; 145 return color;
137 } 146 }
138 147
139 148
396 fprintf(f,"# Maximum of 10 programs (external_1 through external_10)\n"); 405 fprintf(f,"# Maximum of 10 programs (external_1 through external_10)\n");
397 fprintf(f,"# format: external_n: \"menu name\" \"command line\"\n\n"); 406 fprintf(f,"# format: external_n: \"menu name\" \"command line\"\n\n");
398 407
399 for (i = 0; i < GQVIEW_EDITOR_SLOTS; i++) 408 for (i = 0; i < GQVIEW_EDITOR_SLOTS; i++)
400 { 409 {
401 fprintf(f,"external_%d: \"", i+1); 410 char *qname = escquote_value(editor_name[i]);
402 if (editor_name[i]) fprintf(f, "%s", editor_name[i]); 411 char *qcommand = escquote_value(editor_command[i]);
403 fprintf(f, "\" \""); 412 fprintf(f,"external_%d: %s %s\n", i+1, qname, qcommand);
404 if (editor_command[i]) fprintf(f, "%s", editor_command[i]); 413 g_free(qname);
405 fprintf(f, "\"\n"); 414 g_free(qcommand);
406 } 415 }
407 416
408 fprintf(f,"\n##### Collection Options #####\n\n"); 417 fprintf(f,"\n##### Collection Options #####\n\n");
409 418
410 write_bool_option(f, "rectangular_selections", collection_rectangular_selection); 419 write_bool_option(f, "rectangular_selections", collection_rectangular_selection);
691 if (strncasecmp(option, "external_", 9) == 0) 700 if (strncasecmp(option, "external_", 9) == 0)
692 { 701 {
693 i = strtol(option + 9, NULL, 0); 702 i = strtol(option + 9, NULL, 0);
694 if (i > 0 && i <= GQVIEW_EDITOR_SLOTS) 703 if (i > 0 && i <= GQVIEW_EDITOR_SLOTS)
695 { 704 {
696 gchar *ptr1, *ptr2; 705 const gchar *ptr;
697 i--; 706 i--;
698 c = 0;
699 l = strlen(value_all);
700 ptr1 = value_all;
701
702 g_free(editor_name[i]); 707 g_free(editor_name[i]);
703 editor_name[i] = NULL;
704 g_free(editor_command[i]); 708 g_free(editor_command[i]);
705 editor_command[i] = NULL; 709
706 710 editor_name[i] = quoted_value(value_all, &ptr);
707 while (c<l && value_all[c] !='"') c++; 711 editor_command[i] = quoted_value(ptr, NULL);
708 if (ptr1[c] == '"')
709 {
710 c++;
711 ptr2 = ptr1 + c;
712 while (c<l && value_all[c] !='"') c++;
713 if (ptr1[c] == '"')
714 {
715 ptr1[c] = '\0';
716 if (ptr1 + c - 1 != ptr2)
717 editor_name[i] = g_strdup(ptr2);
718 c++;
719 while (c<l && value_all[c] !='"') c++;
720 if (ptr1[c] == '"')
721 {
722 c++;
723 ptr2 = ptr1 + c;
724 while (value_all[c] != '\0') c++;
725 while (c > 0 && value_all[c] != '"') c--;
726 if (ptr1[c] == '"' && ptr1 + c > ptr2)
727 {
728 ptr1[c] = '\0';
729 editor_command[i] = g_strdup(ptr2);
730 }
731 }
732 }
733 }
734 } 712 }
735 } 713 }
736 714
737 /* colection options */ 715 /* colection options */
738 716