diff options
author | Francesco Romani <fromani@gmail.com> | 2014-07-07 08:03:26 +0200 |
---|---|---|
committer | Sebastian Dröge <sebastian@centricular.com> | 2023-04-12 20:04:26 +0300 |
commit | 7ef2d3bc4bd21c03c7bb357de462d2d08baeefbd (patch) | |
tree | 2e2da39b68895d8e3e9f9d1201f8d924c8b32463 | |
parent | 16e5a19f9fb5181627e88773e0417abd41fbad81 (diff) | |
download | orc-7ef2d3bc4bd21c03c7bb357de462d2d08baeefbd.tar.gz |
parser: introduce line handling utility: OrcLine
to extract and simplify the line handling logic.
Part-of: <https://gitlab.freedesktop.org/gstreamer/orc/-/merge_requests/30>
-rw-r--r-- | orc/orcparse.c | 76 |
1 files changed, 61 insertions, 15 deletions
diff --git a/orc/orcparse.c b/orc/orcparse.c index 32f8c11..c1a0096 100644 --- a/orc/orcparse.c +++ b/orc/orcparse.c @@ -18,6 +18,7 @@ #define ORC_ERROR_LENGTH 256 +#define ORC_LINE_MAX_TOKENS 16 typedef struct _OrcParser OrcParser; struct _OrcParser { @@ -42,6 +43,20 @@ struct _OrcParser { char *init_function; }; +typedef struct _OrcLine OrcLine; +struct _OrcLine { + char *p; + char *end; + const char *tokens[ORC_LINE_MAX_TOKENS]; + int n_tokens; +}; + +static void orc_line_init (OrcLine *line, OrcParser *parser); +static int orc_line_has_data (const OrcLine *line); +static int orc_line_is_comment (const OrcLine *line); +static int orc_line_is_blank (const OrcLine *line); +static void orc_line_skip_blanks (OrcLine *line); + static void orc_parse_add_error_valist (OrcParser *parser, const char *format, va_list args); static void orc_parse_add_error (OrcParser *parser, const char *format, ...); static OrcParseError * orc_parse_error_new (const char *source, int line_number, int where, const char *text); @@ -108,28 +123,25 @@ orc_parse_code (const char *code, OrcProgram ***programs, int *n_programs, while (orc_parse_has_data (parser)) { char *p; char *end; - char *token[10]; - int n_tokens; + char *token[10] = { NULL }; + int n_tokens = 0; + OrcLine _line; + OrcLine *line = &_line; orc_parse_get_line (parser); - if (parser->program) orc_program_set_line (parser->program, parser->line_number); - - p = parser->line; - end = p + strlen (p); - /* printf("%d: %s\n", parser->line_number, parser->line); */ - - while (p[0] == ' ' || p[0] == '\t') p++; - - if (p[0] == 0) { - continue; + if (parser->program) { + orc_program_set_line (parser->program, parser->line_number); } - if (p[0] == '#') { - /* printf("comment: %s\n", p+1); */ + orc_line_init (line, parser); + orc_line_skip_blanks (line); + + if (!orc_line_has_data (line) || orc_line_is_comment (line)) { continue; } - n_tokens = 0; + p = line->p; + end = line->end; while (p < end) { while (p[0] != 0 && (p[0] == ' ' || p[0] == '\t')) p++; @@ -452,6 +464,40 @@ orc_parse_code (const char *code, OrcProgram ***programs, int *n_programs, } static void +orc_line_init (OrcLine *line, OrcParser *parser) +{ + memset (line, 0, sizeof(*line)); + line->p = parser->line; + line->end = line->p + parser->line_length; +} + +static int +orc_line_has_data (const OrcLine *line) +{ + return line->p[0] != 0; +} + +static int +orc_line_is_comment (const OrcLine *line) +{ + return line->p[0] == '#'; +} + +static int +orc_line_is_blank (const OrcLine *line) +{ + return (line->p[0] == ' ' || line->p[0] == '\t'); +} + +static void +orc_line_skip_blanks (OrcLine *line) +{ + while (orc_line_has_data (line) && orc_line_is_blank (line)) { + line->p++; + } +} + +static void orc_parse_init (OrcParser *parser, const char *code, int enable_errors) { memset (parser, 0, sizeof(*parser)); |