Tony Cesaro avatar Tony Cesaro committed 97eb8c4

Starting refactor into proper functions

Comments (0)

Files changed (1)

 
 //BEGIN CPP
 
-#define CHUNK 1024
+#define BUFFERCHUNK 1024
 
 //END CPP
 
 // BEGIN Functions
 
 void
-readPrint (FILE *inputFile, char *readBuffer)
+readPrint (FILE *inputFile, char *readBuffer, char *regexPattern)
 {
+  const char *error;
+  int errorOffset;
+  pcre *reCompiled;
+  unsigned int lineLength;
+  int ovector[1];
+  int regexMatchResult;
+  char *line;
+
+  reCompiled = pcre_compile(regexPattern, 0, &error, &errorOffset, NULL);
+
+  if (reCompiled == NULL)
+  {
+    printf("pcre_compile failed (offset: %d), %s\n", errorOffset, error);
+  }
+
   if (inputFile)
   {
     unsigned long curOffset = ftell(inputFile);
     {
       while ((nread = fread(readBuffer, 1, CHUNK, inputFile)) > 0)
       {
-        fwrite(readBuffer, 1, nread, stdout);
-        curOffset = ftell(inputFile);
-        int regexMatches = regexMatch(readBuffer,"*");
-        printf("Matches = %d", regexMatches);
+        while (fgets(readBuffer, CHUNK, inputFile) != NULL)
+        {
+          lineLength = (int)strlen(readBuffer);
+          regexMatchResult = pcre_exec(reCompiled, NULL, readBuffer, lineLength, 0, 0, ovector, 1);
+          printf("Matches = %d", regexMatchResult);
+          fwrite(readBuffer, 1, nread, stdout);
+          curOffset = ftell(inputFile);
+          printf("%d\n", curOffset);
+        }
+        sleep(1);
       }
-      sleep(1);
     }
     fclose(inputFile);
   }
 }
 
-int
-regexMatch (char *line, const char *regexPattern)
+char
+*readLine(FILE *inputFile)
 {
-  const char *error;
-  int errorOffset;
-  pcre *reCompiled;
-
-  reCompiled = pcre_compile(
-          regexPattern,
-          0,
-          &error,
-          &errorOffset,
-          NULL);
+  char *readBuffer;
+  char currentChar;
+  int count = 0;
+  int maxLength = BUFFERCHUNK;
 
-  if (reCompiled == NULL)
+  if(inputFile == NULL)
   {
-    printf("pcre_compile failed (offset: %d), %s\n", errorOffset, error);
-    return -1;
+    printf("Error: Null pointer to file passed");
+    exit(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)
+  readBuffer = (*char)malloc(sizeof(char) * maxLength);
+
+  if(readBuffer == NULL)
   {
-    printf("pcre_exec failed");
-    return -1;
+    printf("Error: Could not allocate memory for readBuffer");
+    exit(1);
   }
-  else
+
+  currentChar = fgetc(inputFile);
+  
+  while((currentChar != '\n') && (currentChar != EOF))
   {
-    return regexMatchResult;
+    if(count == maxLength)
+    {
+      maxLength += BUFFERCHUNK;
+      readBuffer = realloc(readBuffer, maxLength);
+      if(readBuffer == NULL)
+      {
+        printf("Error: Could not allocate memory for readBuffer");
+        exit(1);
+      }
+    }
+    
+    readBuffer[count] = currentChar;
+    count++;
+
+    currentChar = fgetc(inputFile);
   }
 
-}
+
+
+
+
 
 // END functions
 
 
   char *filename = NULL;
   char *pattern = NULL;
-  char *readBuffer = malloc(CHUNK);
   FILE *ifp;
 
-  if (argc - 1 < 1) 
-  {
-    fprintf(stderr, "Usage: %s <filename> <pattern>\n", argv[0]);
-    return 1;
-  } 
-
 
   if (argc - 1 >= 1) 
   {
+    if (argc - 1 < 1) 
+    {
+      fprintf(stderr, "Usage: %s <filename> <pattern>\n", argv[0]);
+      return 1;
+    } 
+
     while (--argc > 0)
     {
       filename = argv[1];
       pattern = argv[2];
     }
+
     ifp = fopen(filename, "r");
   }
   else
 
   printf ("File being concatenated: %s\n", filename);
   printf ("Pattern used for filter: %s\n\n", pattern);
-  readPrint (ifp, readBuffer);
+  readPrint (ifp, readBuffer, pattern);
 
   free (readBuffer);
   
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.