From 228f197d2bf2a26ac2fac861007ef7a851b5c9f1 Mon Sep 17 00:00:00 2001 From: Carsten Haitzler Date: Thu, 12 Jun 2003 13:02:28 +0000 Subject: [PATCH] cleaner. centralise type parsing... and error checking SVN revision: 7017 --- legacy/edje/src/bin/edje_cc.h | 8 ++ legacy/edje/src/bin/edje_cc_handlers.c | 78 ++++--------- legacy/edje/src/bin/edje_cc_parse.c | 151 +++++++++++++++++++++++++ 3 files changed, 180 insertions(+), 57 deletions(-) diff --git a/legacy/edje/src/bin/edje_cc.h b/legacy/edje/src/bin/edje_cc.h index ba113d8723..35309f0f17 100644 --- a/legacy/edje/src/bin/edje_cc.h +++ b/legacy/edje/src/bin/edje_cc.h @@ -15,6 +15,7 @@ #include #include #include +#include typedef struct _New_Object_Handler New_Object_Handler; typedef struct _New_Statement_Handler New_Statement_Handler; @@ -37,6 +38,13 @@ void compile(void); int object_handler_num(void); int statement_handler_num(void); +char *parse_str(int n); +int parse_enum(int n, ...); +int parse_int(int n); +int parse_int_range(int n, int f, int t); +double parse_float(int n); +double parse_float_range(int n, int f, int t); + extern Evas_List *img_dirs; extern char *file_in; extern char *file_out; diff --git a/legacy/edje/src/bin/edje_cc_handlers.c b/legacy/edje/src/bin/edje_cc_handlers.c index 2eafbf9f36..b123226ac3 100644 --- a/legacy/edje/src/bin/edje_cc_handlers.c +++ b/legacy/edje/src/bin/edje_cc_handlers.c @@ -89,74 +89,38 @@ static void st_images_image(void) { Edje_Image_Directory_Entry *img; - char *str; + int v; img = evas_list_data(evas_list_last(edje_file->image_dir->entries)); - str = evas_list_nth(params, 0); - if (str) + img->entry = parse_str(0); + v = parse_enum(1, + "RAW", 0, + "COMP", 1, + "LOSSY", 2, + "USER", 3, + NULL); + if (v == 0) { - img->entry = strdup(str); - if (!str) - { - fprintf(stderr, "%s: Error. memory allocation of %i bytes failed. %s\n", - progname, strlen(str) + 1, strerror(errno)); - exit(-1); - } + img->source_type = EDJE_IMAGE_SOURCE_TYPE_INLINE_PERFECT; + img->source_param = 0; } - else + else if (v == 1) { - fprintf(stderr, "%s: Error. %s:%i: no filename for image as arg 1\n", - progname, file_in, line); - exit(-1); + img->source_type = EDJE_IMAGE_SOURCE_TYPE_INLINE_PERFECT; + img->source_param = 1; } - str = evas_list_nth(params, 1); - if (str) + else if (v == 2) { - if (!strcasecmp(str, "RAW")) - { - img->source_type = EDJE_IMAGE_SOURCE_TYPE_INLINE_PERFECT; - img->source_param = 0; - } - else if (!strcasecmp(str, "COMP")) - { - img->source_type = EDJE_IMAGE_SOURCE_TYPE_INLINE_PERFECT; - img->source_param = 1; - } - else if (!strcasecmp(str, "LOSSY")) - { - img->source_type = EDJE_IMAGE_SOURCE_TYPE_INLINE_LOSSY; - img->source_param = 0; - } - else if (!strcasecmp(str, "USER")) - { - img->source_type = EDJE_IMAGE_SOURCE_TYPE_EXTERNAL; - img->source_param = 0; - } - else - { - fprintf(stderr, "%s: Error. %s:%i: invalid encoding \"%s\" for image as arg 2\n", - progname, file_in, line, str); - exit(-1); - } + img->source_type = EDJE_IMAGE_SOURCE_TYPE_INLINE_LOSSY; + img->source_param = 0; } - else + else if (v == 3) { - fprintf(stderr, "%s: Error. %s:%i: no encoding type for image as arg 2\n", - progname, file_in, line); - exit(-1); + img->source_type = EDJE_IMAGE_SOURCE_TYPE_EXTERNAL; + img->source_param = 0; } if (img->source_type != EDJE_IMAGE_SOURCE_TYPE_INLINE_LOSSY) return; - str = evas_list_nth(params, 2); - if (str) - { - img->source_param = atoi(str); - } - else - { - fprintf(stderr, "%s: Error. %s:%i: no encoding quality for lossy as arg 3\n", - progname, file_in, line); - exit(-1); - } + img->source_param = parse_int_range(2, 0, 100); } static void diff --git a/legacy/edje/src/bin/edje_cc_parse.c b/legacy/edje/src/bin/edje_cc_parse.c index a7b28b1037..27ab7d1dd1 100644 --- a/legacy/edje/src/bin/edje_cc_parse.c +++ b/legacy/edje/src/bin/edje_cc_parse.c @@ -348,3 +348,154 @@ compile(void) } close(fd); } + +char * +parse_str(int n) +{ + char *str; + char *s; + + str = evas_list_nth(params, n); + if (!str) + { + fprintf(stderr, "%s: Error. %s:%i no parameter supplied as argument %i\n", + progname, file_in, line, n + 1); + exit(-1); + } + s = strdup(str); + if (!s) + { + fprintf(stderr, "%s: Error. memory allocation of %i bytes failed. %s\n", + progname, strlen(str) + 1, strerror(errno)); + exit(-1); + } + return s; +} + +int +parse_enum(int n, ...) +{ + char *str; + va_list va; + + str = evas_list_nth(params, n); + if (!str) + { + fprintf(stderr, "%s: Error. %s:%i no parameter supplied as argument %i\n", + progname, file_in, line, n + 1); + exit(-1); + } + va_start(va, n); + for (;;) + { + char *s; + int v; + + s = va_arg(va, char *); + if (!s) + { + fprintf(stderr, "%s: Error. %s:%i token %s not one of:", + progname, file_in, line, str); + va_start(va, n); + s = va_arg(va, char *); + while (s) + { + v = va_arg(va, int); + fprintf(stderr, " %s", s); + s = va_arg(va, char *); + if (!s) break; + } + fprintf(stderr, "\n"); + va_end(va); + exit(-1); + } + v = va_arg(va, int); + if (!strcmp(s, str)) + { + va_end(va); + return v; + } + } + va_end(va); + return 0; +} + +int +parse_int(int n) +{ + char *str; + int i; + + str = evas_list_nth(params, n); + if (!str) + { + fprintf(stderr, "%s: Error. %s:%i no parameter supplied as argument %i\n", + progname, file_in, line, n + 1); + exit(-1); + } + i = atoi(str); + return i; +} + +int +parse_int_range(int n, int f, int t) +{ + char *str; + int i; + + str = evas_list_nth(params, n); + if (!str) + { + fprintf(stderr, "%s: Error. %s:%i no parameter supplied as argument %i\n", + progname, file_in, line, n + 1); + exit(-1); + } + i = atoi(str); + if ((i < f) || (i > t)) + { + fprintf(stderr, "%s: Error. %s:%i integer %i out of range of %i to %i inclusive\n", + progname, file_in, line, i, f, t); + exit(-1); + } + return i; +} + +double +parse_float(int n) +{ + char *str; + double i; + + str = evas_list_nth(params, n); + if (!str) + { + fprintf(stderr, "%s: Error. %s:%i no parameter supplied as argument %i\n", + progname, file_in, line, n + 1); + exit(-1); + } + i = atof(str); + return i; +} + +double +parse_float_range(int n, int f, int t) +{ + char *str; + double i; + + str = evas_list_nth(params, n); + if (!str) + { + fprintf(stderr, "%s: Error. %s:%i no parameter supplied as argument %i\n", + progname, file_in, line, n + 1); + exit(-1); + } + i = atoi(str); + if ((i < f) || (i > t)) + { + fprintf(stderr, "%s: Error. %s:%i integer %i out of range of %i to %i inclusive\n", + progname, file_in, line, i, f, t); + exit(-1); + } + return i; +}