regex_parser.c

changeset 28
72a98cbcb9f1
parent 27
95a958e3de88
child 29
fa625066ae52
--- a/regex_parser.c	Thu Jan 26 15:55:52 2012 +0100
+++ b/regex_parser.c	Thu Feb 02 14:17:35 2012 +0100
@@ -14,11 +14,26 @@
     ret->matched_lines = 0;
     ret->pattern_match = 0;
     ret->compiled_patterns = NULL;
+    ret->compiled_pattern_count = 0;
   }
   return ret;
 }
 
+void regex_destcomppats(regex_parser_t* parser) {
+  if (parser->compiled_patterns != NULL) {
+    for (int i = 0 ; i < parser->compiled_pattern_count ; i++) {
+      if (parser->compiled_patterns[i] != NULL) {
+        free(parser->compiled_patterns[i]);
+      }
+    }
+    free(parser->compiled_patterns);
+    parser->compiled_patterns = NULL;
+    parser->compiled_pattern_count = 0;
+  }
+}
+
 void destroy_regex_parser_t(regex_parser_t* parser) {
+  regex_destcomppats(parser);
   destroy_string_list_t(parser->pattern_list);
   free(parser);
 }
@@ -27,24 +42,69 @@
   return parser->pattern_match > 0;
 }
 
-void regex_compile_all(regex_parser_t* parser) {
-  size_t pcount = parser->pattern_list->count;
-  if (pcount > 0) {
-    if (parser->compiled_patterns != NULL) {
-      free(parser->compiled_patterns);
-    }
-    parser->compiled_patterns = calloc(pcount, sizeof(regex_t));
+int regex_parser_do(regex_parser_t* parser, char* input) {
+  int err = REG_NOMATCH;
+  if (parser->compiled_pattern_count > 0) {
+    regmatch_t match;
+
+    if (regex_parser_matching(parser)) {
+      parser->matched_lines++;
 
-    regex_t* re = malloc(sizeof(regex_t));
-    for (int i = 0 ; i < pcount ; i++) {
-      if (regcomp(re, parser->pattern_list->items[i],
-          REG_EXTENDED|REG_NOSUB) == 0) {
-        parser->compiled_patterns[i] = re;
-      } else {
-        fprintf(stderr, "Cannot compile: %s\n",
-            (parser->pattern_list->items[i]));
-        parser->compiled_patterns[i] = NULL;
+      err = regexec(parser->compiled_patterns[parser->pattern_match],
+          input, 1, &match, 0);
+      if (err > 0 && err != REG_NOMATCH) {
+        fprintf(stderr, "Regex-Error: 0x%08x", err);
+      }
+      if (err == 0) {
+        parser->pattern_match = 0;
+        /* do not match line, if it does not end with the pattern */
+        if (match.rm_eo < strlen(input)) {
+          parser->matched_lines--;
+        }
+      }
+    } else {
+      for (int i = 0 ; i < parser->compiled_pattern_count - 1 ; i += 2) {
+        err = regexec(parser->compiled_patterns[i], input, 1, &match, 0);
+        if (err > 0 && err != REG_NOMATCH) {
+          fprintf(stderr, "Regex-Error: 0x%08x", err);
+        }
+        if (err == 0) {
+          parser->pattern_match = i+1;
+          parser->matched_lines = 0;
+          /* Check, if end pattern is also in this line */
+          regex_parser_do(parser, input);
+          /* do not match line, if it does not start with the pattern */
+          if (match.rm_so > 0 && parser->matched_lines > 0) {
+            parser->matched_lines--;
+          }
+          break;
+        }
       }
     }
   }
+  return err;
 }
+
+bool regex_compile_all(regex_parser_t* parser) {
+  bool success = true;
+  size_t pcount = parser->pattern_list->count;
+  if (pcount > 0) {
+    regex_destcomppats(parser);
+    parser->compiled_patterns = calloc(pcount, sizeof(regex_t));
+    parser->compiled_pattern_count = pcount;
+
+    regex_t* re;
+    for (int i = 0 ; i < pcount ; i++) {
+      re = malloc(sizeof(regex_t));
+      if (regcomp(re, parser->pattern_list->items[i], REG_EXTENDED) == 0) {
+        parser->compiled_patterns[i] = re;
+      } else {
+        fprintf(stderr, "Cannot compile pattern: %s\n",
+            (parser->pattern_list->items[i]));
+        parser->compiled_patterns[i] = NULL;
+        success = false;
+      }
+    }
+  }
+  return success;
+}

mercurial