Skip to content

Commit 0c06510

Browse files
duplicated tool parser code moved to separate functions
1 parent 75db865 commit 0c06510

File tree

4 files changed

+183
-182
lines changed

4 files changed

+183
-182
lines changed

src/callbacks.c

Lines changed: 12 additions & 176 deletions
Original file line numberDiff line numberDiff line change
@@ -693,9 +693,7 @@ void on_mainapp_selection_received (GtkWidget *widget,
693693
gchar *name, *copy;
694694
GromitPaintContext *context_template=NULL;
695695

696-
GromitPaintType type;
697-
GdkRGBA *fg_color=NULL;
698-
guint width, arrowsize, minwidth, maxwidth;
696+
GromitStyleDef style;
699697

700698
if (token != G_TOKEN_EOF)
701699
{
@@ -706,182 +704,18 @@ void on_mainapp_selection_received (GtkWidget *widget,
706704
if(!name)
707705
goto cleanup;
708706

709-
token = g_scanner_cur_token(scanner);
710-
711-
if (token != G_TOKEN_EQUAL_SIGN)
712-
{
713-
g_scanner_unexp_token (scanner, G_TOKEN_EQUAL_SIGN, NULL,
714-
NULL, NULL, "aborting", TRUE);
715-
goto cleanup;
716-
}
707+
if (!parse_tool(data, scanner, &style))
708+
goto cleanup;
717709

718-
token = g_scanner_get_next_token (scanner);
719-
720-
/* defaults */
721-
type = GROMIT_PEN;
722-
width = 7;
723-
arrowsize = 0;
724-
minwidth = 1;
725-
maxwidth = G_MAXUINT;
726-
fg_color = data->red;
710+
token = g_scanner_cur_token(scanner);
727711

728-
if (token == G_TOKEN_SYMBOL)
729-
{
730-
type = (GromitPaintType) scanner->value.v_symbol;
731-
token = g_scanner_get_next_token (scanner);
732-
}
733-
else if (token == G_TOKEN_STRING)
712+
if (token == G_TOKEN_LEFT_PAREN)
734713
{
735-
copy = parse_name (scanner);
736-
if(!copy)
737-
goto cleanup;
714+
if (! parse_style(scanner, &style))
715+
goto cleanup;
738716
token = g_scanner_cur_token(scanner);
739-
context_template = g_hash_table_lookup (data->tool_config, copy);
740-
if (context_template)
741-
{
742-
type = context_template->type;
743-
width = context_template->width;
744-
arrowsize = context_template->arrowsize;
745-
minwidth = context_template->minwidth;
746-
maxwidth = context_template->maxwidth;
747-
fg_color = context_template->paint_color;
748-
}
749-
else
750-
{
751-
g_printerr ("WARNING: Unable to copy \"%s\": "
752-
"not yet defined!\n", copy);
753-
}
754-
}
755-
else
756-
{
757-
g_printerr ("Expected Tool-definition "
758-
"or name of template tool\n");
759-
goto cleanup;
760717
}
761718

762-
/* Are there any tool-options?
763-
*/
764-
765-
if (token == G_TOKEN_LEFT_PAREN)
766-
{
767-
GdkRGBA *color = NULL;
768-
g_scanner_set_scope (scanner, 2);
769-
scanner->config->int_2_float = 1;
770-
token = g_scanner_get_next_token (scanner);
771-
while (token != G_TOKEN_RIGHT_PAREN)
772-
{
773-
if (token == G_TOKEN_SYMBOL)
774-
{
775-
if ((intptr_t) scanner->value.v_symbol == 1)
776-
{
777-
token = g_scanner_get_next_token (scanner);
778-
if (token != G_TOKEN_EQUAL_SIGN)
779-
{
780-
g_printerr ("Missing \"=\"... aborting\n");
781-
goto cleanup;
782-
}
783-
token = g_scanner_get_next_token (scanner);
784-
if (token != G_TOKEN_FLOAT)
785-
{
786-
g_printerr ("Missing Size (float)... aborting\n");
787-
goto cleanup;
788-
}
789-
width = (guint) (scanner->value.v_float + 0.5);
790-
}
791-
else if ((intptr_t) scanner->value.v_symbol == 2)
792-
{
793-
token = g_scanner_get_next_token (scanner);
794-
if (token != G_TOKEN_EQUAL_SIGN)
795-
{
796-
g_printerr ("Missing \"=\"... aborting\n");
797-
goto cleanup;
798-
}
799-
token = g_scanner_get_next_token (scanner);
800-
if (token != G_TOKEN_STRING)
801-
{
802-
g_printerr ("Missing Color (string)... "
803-
"aborting\n");
804-
goto cleanup;
805-
}
806-
color = g_malloc (sizeof (GdkRGBA));
807-
if (gdk_rgba_parse (color, scanner->value.v_string))
808-
{
809-
fg_color = color;
810-
}
811-
else
812-
{
813-
g_printerr ("Unable to parse color. "
814-
"Keeping default.\n");
815-
g_free (color);
816-
}
817-
color = NULL;
818-
}
819-
else if ((intptr_t) scanner->value.v_symbol == 3)
820-
{
821-
token = g_scanner_get_next_token (scanner);
822-
if (token != G_TOKEN_EQUAL_SIGN)
823-
{
824-
g_printerr ("Missing \"=\"... aborting\n");
825-
goto cleanup;
826-
}
827-
token = g_scanner_get_next_token (scanner);
828-
if (token != G_TOKEN_FLOAT)
829-
{
830-
g_printerr ("Missing Arrowsize (float)... "
831-
"aborting\n");
832-
goto cleanup;
833-
}
834-
arrowsize = scanner->value.v_float;
835-
}
836-
else if ((intptr_t) scanner->value.v_symbol == 4)
837-
{
838-
token = g_scanner_get_next_token (scanner);
839-
if (token != G_TOKEN_EQUAL_SIGN)
840-
{
841-
g_printerr ("Missing \"=\"... aborting\n");
842-
goto cleanup;
843-
}
844-
token = g_scanner_get_next_token (scanner);
845-
if (token != G_TOKEN_FLOAT)
846-
{
847-
g_printerr ("Missing Minsize (float)... "
848-
"aborting\n");
849-
goto cleanup;
850-
}
851-
minwidth = scanner->value.v_float;
852-
}
853-
else if ((intptr_t) scanner->value.v_symbol == 5)
854-
{
855-
token = g_scanner_get_next_token (scanner);
856-
if (token != G_TOKEN_EQUAL_SIGN)
857-
{
858-
g_printerr ("Missing \"=\"... aborting\n");
859-
goto cleanup;
860-
}
861-
token = g_scanner_get_next_token (scanner);
862-
if (token != G_TOKEN_FLOAT)
863-
{
864-
g_printerr ("Missing Maxsize (float)... "
865-
"aborting\n");
866-
goto cleanup;
867-
}
868-
maxwidth = scanner->value.v_float;
869-
}
870-
else
871-
{
872-
g_printerr ("Unknown tool type ???\n");
873-
}
874-
}
875-
else
876-
{
877-
g_printerr("skipped unknown token: %d !!!\n", token);
878-
}
879-
token = g_scanner_get_next_token (scanner);
880-
} // while (token != G_TOKEN_RIGHT_PAREN)
881-
g_scanner_set_scope (scanner, 0);
882-
token = g_scanner_get_next_token (scanner);
883-
} // if (token == G_TOKEN_LEFT_PAREN)
884-
885719
// by now nothing should follow
886720
if (token != G_TOKEN_EOF)
887721
{
@@ -892,14 +726,16 @@ void on_mainapp_selection_received (GtkWidget *widget,
892726
GromitPaintContext *context =
893727
g_hash_table_lookup(data->tool_config, name);
894728
GromitPaintContext *new_context =
895-
paint_context_new (data, type, fg_color, width, arrowsize, minwidth, maxwidth);
729+
paint_context_new (data, style.type, style.paint_color,
730+
style.width, style.arrowsize,
731+
style.minwidth, style.maxwidth);
896732
*context = *new_context;
897733

898734
g_free(new_context);
899-
} // if (token == G_TOKEN_STRING)
735+
}
900736

901737
token = g_scanner_get_next_token (scanner);
902-
} // if (token != G_TOKEN_EOF)
738+
}
903739

904740
cleanup:
905741

src/config.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -495,13 +495,13 @@ gboolean parse_config (GromitData *data)
495495
goto cleanup;
496496

497497
if (!parse_tool(data, scanner, &style))
498-
{
499-
g_printerr("parse tool failed\n");
500-
goto cleanup;
501-
}
498+
goto cleanup;
502499

503-
// are there any tool-options?
504500
token = g_scanner_cur_token(scanner);
501+
//
502+
/* Are there any tool-options?
503+
*/
504+
505505
if (token == G_TOKEN_LEFT_PAREN)
506506
{
507507
if (! parse_style(scanner, &style))

src/parser.c

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,3 +137,156 @@ gchar* parse_name (GScanner *scanner)
137137

138138
return name;
139139
}
140+
141+
142+
gboolean parse_tool(GromitData *data, GScanner *scanner, GromitStyleDef *style) {
143+
GTokenType token = g_scanner_cur_token(scanner);
144+
145+
if (token != G_TOKEN_EQUAL_SIGN) {
146+
g_scanner_unexp_token(
147+
scanner, G_TOKEN_EQUAL_SIGN, NULL, NULL, NULL, "aborting", TRUE);
148+
return FALSE;
149+
}
150+
151+
token = g_scanner_get_next_token(scanner);
152+
153+
/* defaults */
154+
style->type = GROMIT_PEN;
155+
style->width = 7;
156+
style->arrowsize = 0;
157+
style->minwidth = 1;
158+
style->maxwidth = G_MAXUINT;
159+
style->paint_color = data->red;
160+
161+
if (token == G_TOKEN_SYMBOL) {
162+
style->type = (GromitPaintType)scanner->value.v_symbol;
163+
token = g_scanner_get_next_token(scanner);
164+
} else if (token == G_TOKEN_STRING) {
165+
gchar *copy = parse_name(scanner);
166+
if (!copy)
167+
return FALSE;
168+
token = g_scanner_cur_token(scanner);
169+
GromitPaintContext *context = g_hash_table_lookup(data->tool_config, copy);
170+
if (context) {
171+
style->type = context->type;
172+
style->width = context->width;
173+
style->arrowsize = context->arrowsize;
174+
style->minwidth = context->minwidth;
175+
style->maxwidth = context->maxwidth;
176+
style->paint_color = context->paint_color;
177+
} else {
178+
g_printerr(
179+
"WARNING: Unable to copy \"%s\": "
180+
"not yet defined!\n",
181+
copy);
182+
}
183+
} else {
184+
g_printerr(
185+
"Expected Tool-definition "
186+
"or name of template tool\n");
187+
return FALSE;
188+
}
189+
return TRUE;
190+
}
191+
192+
/*
193+
* parses a pen style definition and stores fields found in GromitStyleDef
194+
* returns FALSE upon any error
195+
*/
196+
197+
gboolean parse_style(GScanner *scanner, GromitStyleDef *style) {
198+
GdkRGBA *color = NULL;
199+
g_scanner_set_scope(scanner, 2);
200+
scanner->config->int_2_float = 1;
201+
GTokenType token = g_scanner_get_next_token(scanner);
202+
while (token != G_TOKEN_RIGHT_PAREN) {
203+
if (token == G_TOKEN_SYMBOL) {
204+
if ((intptr_t)scanner->value.v_symbol == 1) {
205+
token = g_scanner_get_next_token(scanner);
206+
if (token != G_TOKEN_EQUAL_SIGN) {
207+
g_printerr("Missing \"=\"... aborting\n");
208+
return FALSE;
209+
}
210+
token = g_scanner_get_next_token(scanner);
211+
if (token != G_TOKEN_FLOAT) {
212+
g_printerr("Missing Size (float)... aborting\n");
213+
return FALSE;
214+
}
215+
style->width = (guint)(scanner->value.v_float + 0.5);
216+
} else if ((intptr_t)scanner->value.v_symbol == 2) {
217+
token = g_scanner_get_next_token(scanner);
218+
if (token != G_TOKEN_EQUAL_SIGN) {
219+
g_printerr("Missing \"=\"... aborting\n");
220+
return FALSE;
221+
}
222+
token = g_scanner_get_next_token(scanner);
223+
if (token != G_TOKEN_STRING) {
224+
g_printerr(
225+
"Missing Color (string)... "
226+
"aborting\n");
227+
return FALSE;
228+
}
229+
color = g_malloc(sizeof(GdkRGBA));
230+
if (gdk_rgba_parse(color, scanner->value.v_string)) {
231+
style->paint_color = color;
232+
} else {
233+
g_printerr(
234+
"Unable to parse color. "
235+
"Keeping default.\n");
236+
g_free(color);
237+
}
238+
color = NULL;
239+
} else if ((intptr_t)scanner->value.v_symbol == 3) {
240+
token = g_scanner_get_next_token(scanner);
241+
if (token != G_TOKEN_EQUAL_SIGN) {
242+
g_printerr("Missing \"=\"... aborting\n");
243+
return FALSE;
244+
}
245+
token = g_scanner_get_next_token(scanner);
246+
if (token != G_TOKEN_FLOAT) {
247+
g_printerr(
248+
"Missing Arrowsize (float)... "
249+
"aborting\n");
250+
return FALSE;
251+
}
252+
style->arrowsize = scanner->value.v_float;
253+
} else if ((intptr_t)scanner->value.v_symbol == 4) {
254+
token = g_scanner_get_next_token(scanner);
255+
if (token != G_TOKEN_EQUAL_SIGN) {
256+
g_printerr("Missing \"=\"... aborting\n");
257+
return FALSE;
258+
}
259+
token = g_scanner_get_next_token(scanner);
260+
if (token != G_TOKEN_FLOAT) {
261+
g_printerr(
262+
"Missing Minsize (float)... "
263+
"aborting\n");
264+
return FALSE;
265+
}
266+
style->minwidth = scanner->value.v_float;
267+
} else if ((intptr_t)scanner->value.v_symbol == 5) {
268+
token = g_scanner_get_next_token(scanner);
269+
if (token != G_TOKEN_EQUAL_SIGN) {
270+
g_printerr("Missing \"=\"... aborting\n");
271+
return FALSE;
272+
}
273+
token = g_scanner_get_next_token(scanner);
274+
if (token != G_TOKEN_FLOAT) {
275+
g_printerr(
276+
"Missing Maxsize (float)... "
277+
"aborting\n");
278+
return FALSE;
279+
}
280+
style->maxwidth = scanner->value.v_float;
281+
} else {
282+
g_printerr("Unknown tool type ???\n");
283+
}
284+
} else {
285+
g_printerr("skipped unknown token: %d !!!\n", token);
286+
}
287+
token = g_scanner_get_next_token(scanner);
288+
} // while (token != G_TOKEN_RIGHT_PAREN)
289+
g_scanner_set_scope(scanner, 0);
290+
g_scanner_get_next_token(scanner);
291+
return TRUE;
292+
}

0 commit comments

Comments
 (0)