parser: introduce line handling utility: OrcLine
authorFrancesco Romani <fromani@gmail.com>
Mon, 7 Jul 2014 06:03:26 +0000 (08:03 +0200)
committerSebastian Dröge <sebastian@centricular.com>
Wed, 12 Apr 2023 17:04:26 +0000 (20:04 +0300)
to extract and simplify the line handling logic.

Part-of: <https://gitlab.freedesktop.org/gstreamer/orc/-/merge_requests/30>

orc/orcparse.c

index 32f8c11..c1a0096 100644 (file)
@@ -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));