Commits

Tony Cesaro  committed d7498ce

Adding pcre functionality and linking the library, working on filtering/including lines based on regex patterns

  • Participants
  • Parent commits cbbd757

Comments (0)

Files changed (2)

-CFLAGS=-Wall -g
+CFLAGS=-Wall -g -lpcre
 
 all: tg
 
 #include <stdio.h>
 #include <stdlib.h>
+#include <unistd.h>
+#include <string.h>
+#include <pcre.h>
 
 #define CHUNK 1024
 
             {
                 fwrite(readBuffer, 1, nread, stdout);
                 curOffset = ftell(inputFile);
+                int regexMatches = regexMatch(readBuffer,"*");
+                printf("Matches = %d", regexMatches);
             }
+            sleep(1);
         }
         fclose(inputFile);
     }
 }
 
-void
-grepInclude ()
-{
-
-}
-
-void
-grepExclude ()
+int
+regexMatch (char *line, const char *regexPattern)
 {
+    const char *error;
+    int errorOffset;
+    pcre *reCompiled;
+
+    reCompiled = pcre_compile(
+                    regexPattern,
+                    0,
+                    &error,
+                    &errorOffset,
+                    NULL);
+
+    if (reCompiled == NULL)
+    {
+        printf("pcre_compile failed (offset: %d), %s\n", errorOffset, error);
+        return -1;
+    }
 
+    unsigned int lineLength = (int)strlen(line);
+    int ovector[1];
+    int regexMatchResult = pcre_exec(
+                        reCompiled,
+                        NULL,
+                        line,
+                        lineLength,
+                        0,
+                        0,
+                        ovector,
+                        1);
+    
+    if (regexMatchResult == NULL)
+    {
+        printf("pcre_exec failed");
+        return -1;
+    }
+    else
+    {
+        return regexMatchResult;
+    }
+                        
 }
 
 int