Moved some functions to functions.c

Thu, 26 May 2011 14:39:52 +0200

author
Mike Becker <universe@uap-core.de>
date
Thu, 26 May 2011 14:39:52 +0200
changeset 3
510d6b198dde
parent 2
29ac790c27ad
child 4
c3acfb3b4957

Moved some functions to functions.c
Replaced static variables by settings_t type

Makefile file | annotate | diff | comparison | revisions
cline.c file | annotate | diff | comparison | revisions
cline.h file | annotate | diff | comparison | revisions
functions.c file | annotate | diff | comparison | revisions
functions.h file | annotate | diff | comparison | revisions
include.h file | annotate | diff | comparison | revisions
v2.c file | annotate | diff | comparison | revisions
v2.h file | annotate | diff | comparison | revisions
--- a/Makefile	Mon May 23 16:54:56 2011 +0200
+++ b/Makefile	Thu May 26 14:39:52 2011 +0200
@@ -1,8 +1,11 @@
 CC = gcc
-OBJ = cline.o v2.o
+OBJ = cline.o functions.o
 
 cline: ${OBJ}
 	${CC} -o cline ${OBJ}
 
 %.o: %.c
 	${CC} -c -std=c99 $<
+
+clean:
+	rm *.o
--- a/cline.c	Mon May 23 16:54:56 2011 +0200
+++ b/cline.c	Thu May 26 14:39:52 2011 +0200
@@ -1,109 +1,17 @@
-#include "include.h"
-#include "v2.h"
-
-
-#ifdef _WIN32
-static char fileSeparator = '\\';
-#else
-static char fileSeparator = '/';
-#endif /* _WIN32 */
-
-static int suffixc;
-static char** suffixv;
-static bool recursive;
-static bool includeSuffixes;
-static bool matchesOnly;
-
-bool testSuffix(char* filename) {
-  bool ret = false;
-  int tokenlen, fnamelen = strlen(filename);
-  for (int t = 0 ; t < suffixc ; t++) {
-    tokenlen = strlen(suffixv[t]);
-    if (fnamelen >= tokenlen && tokenlen > 0) {
-      if (strncmp(filename+fnamelen-tokenlen, suffixv[t], tokenlen) == 0) {
-        ret = true;
-        break;
-      }
-    }
-  }
-  return ret ^ !includeSuffixes;
-}
-
-int scanDirectory(DIR *dir, const int spaces, char* currdir) {
-  DIR *subdir;
-  char* subdirname;
-  struct dirent *entry;
-  int lines, digits, a;
-  int lineSum = 0;
+#include "cline.h"
+#include "functions.h"
 
-  while ((entry = readdir(dir)) != NULL) {
-    if (strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0) {
-      // Print occurence
-      char entryname[strlen(entry->d_name)+spaces];
-      for (int t = 0 ; t < spaces ; t++) {
-        entryname[t]=' ';
-      }
-      entryname[spaces] = 0;
-      strcat(entryname, entry->d_name);
-  
-      // Check for subdirectory
-      char subdirname[(1+strlen(currdir)+strlen(entry->d_name))];
-      strcpy(subdirname, currdir);
-      strncat(subdirname, &fileSeparator, 1);
-      strcat(subdirname, entry->d_name);
-      if ((subdir = opendir(subdirname)) != NULL) {
-        printf("%-60s\n", entryname);
-        if (recursive) {
-          lineSum += scanDirectory(subdir, spaces+1, subdirname);
-        }
-        closedir(subdir);
-        continue;
-      }
-
-      // Count lines
-      lines = 0;
-      char filename[(1+strlen(currdir)+strlen(entry->d_name))];
-      strcpy(filename, currdir);
-      strncat(filename, &fileSeparator, 1);
-      strcat(filename, entry->d_name);
-      if (testSuffix(filename)) {
-        FILE *file = fopen(filename, "r");
-        if (file == NULL) {
-          perror("  File acces failed");
-          continue;
-        }
-
-        do {
-          a = fgetc(file);
-
-          if (a == 10) {
-            lines++;
-          }
-        } while (a != EOF);
-        fclose(file);
-
-        // Print line count
-        #ifdef _WIN32
-          printf("%-60s%13d lines\n", entryname, lines);
-        #else
-          printf("%-60s%14d lines\n", entryname, lines);
-        #endif /* _WIN32 */
-
-        lineSum += lines;
-      }
-      else {
-        if (!matchesOnly) {
-          // Print hint
-          #ifdef _WIN32
-            printf("%-60s%19s\n", entryname, "no match");
-          #else
-            printf("%-60s%20s\n", entryname, "no match");
-          #endif /* _WIN32 */
-        }
-      }
-    }
-  }
-  return lineSum;
+settings_t* new_settings_t() {
+  settings_t *settings = malloc(sizeof(settings_t*));
+#ifdef _WIN32
+  settings->fileSeparator      = '\\';
+#else
+  settings->fileSeparator      = '/';
+#endif /* _WIN32 */
+  settings->suffixc            = 1;
+  settings->recursive          = false;
+  settings->includeSuffixes    = false;
+  settings->matchesOnly        = false;
 }
 
 void printHelpText(const char* prgName) {
@@ -129,18 +37,20 @@
     "\nC source code in your working directory and its subdirectories, type:"
     "\n  %s -rs .c\n";
     
-    printf(helpText, prgName, prgName, prgName, prgName);
+  printf(helpText, prgName, prgName, prgName, prgName);
 }
 
 int main(int argc, char** argv) {
 
+  // Settings
+  settings_t *settings = new_settings_t();
+
   // Program name
-  char* prgName = strrchr(argv[0], fileSeparator);
+  char* prgName = strrchr(argv[0], settings->fileSeparator);
   
   if (prgName == NULL) {
     prgName = argv[0];
-  }
-  else {
+  } else {
     prgName++;
   }
 
@@ -148,15 +58,13 @@
   char* _suffix = " ";
   char _directory[3];
   _directory[0] = '.';
-  _directory[1] = fileSeparator;
+  _directory[1] = settings->fileSeparator;
   _directory[2] = 0;
 
   // Get arguments
   char* directory;
   char* suffix;
   bool showHelp = false;
-  recursive = false;
-  includeSuffixes = false;
   char checked = 0;
 
   for (int t = 1 ; t < argc ; t++) {
@@ -169,7 +77,7 @@
         printHelpText(prgName);
         return -1;
       }
-      includeSuffixes = true;
+      settings->includeSuffixes = true;
       t++;
       if (t >= argc) {
         printHelpText(prgName);
@@ -184,7 +92,7 @@
         printHelpText(prgName);
         return -1;
       }
-      includeSuffixes = false;
+      settings->includeSuffixes = false;
       t++;
       if (t >= argc) {
         printHelpText(prgName);
@@ -209,7 +117,7 @@
         return -1;
       }
       checked |= 4;
-      recursive = true;
+      settings->recursive = true;
     }
     if ((argflags & 32) > 0) {
       if ((checked & 32) > 0) {
@@ -217,7 +125,7 @@
         return -1;
       }
       checked |= 32;
-      matchesOnly = true;
+      settings->matchesOnly = true;
     }
     // other
     if (argflags == 0) {
@@ -247,21 +155,20 @@
 
   // Find tokens
   char* finder;
-  suffixc = 1;
   finder = strchr(suffix, ',');
   while (finder != NULL) {
-    suffixc++;
+    settings->suffixc++;
     finder = strchr(finder+1, ',');
   }
-  suffixv = (char**) malloc(sizeof(suffixv)*suffixc);
-  if (suffixv == NULL) {
+  settings->suffixv = (char**) malloc(sizeof(char**)*settings->suffixc);
+  if (settings->suffixv == NULL) {
     fprintf(stderr, "Memory allocation failed.\n");
     return 1;
   }
   finder = strtok(suffix, ",");
   int c = 0;
   while (finder != NULL) {
-    suffixv[c] = finder;
+    settings->suffixv[c] = finder;
     c++;
     finder = strtok(NULL, ",");
   }
@@ -270,30 +177,32 @@
   DIR *dir = opendir(directory);
   if (dir == NULL) {
     perror("Operation failed");
-    free(suffixv);
+    free(settings->suffixv);
+    free(settings);
     return 1;
   }
   
   // Scan directory
-  int lines = scanDirectory(dir, 0, directory);
+  int lines = scanDirectory(dir, 0, directory, settings);
 
   // Print double line and line count
   #ifdef _WIN32
-     const int columns = 79;
+    const int columns = 79;
   #else
-     const int columns = 80;
+    const int columns = 80;
   #endif /* _WIN32 */
 
   for (int t = 0 ; t < columns ; t++) {
     printf("=");
   }
   #ifdef _WIN32
-     printf("\n%73d lines\n", lines);
+    printf("\n%73d lines\n", lines);
   #else
-     printf("\n%74d lines\n", lines);
+    printf("\n%74d lines\n", lines);
   #endif /* _WIN32 */
 
   closedir(dir);
-  free(suffixv);
+  free(settings->suffixv);
+  free(settings);
   return 0;
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/cline.h	Thu May 26 14:39:52 2011 +0200
@@ -0,0 +1,23 @@
+#ifndef _CLINE_H
+#define _CLINE_H
+
+#include <stdio.h>
+#include <string.h>
+#include <stdbool.h>
+#include <stdlib.h>
+#include <dirent.h>
+
+typedef struct _settings {
+  char fileSeparator;
+  int suffixc;
+  char** suffixv;
+  bool recursive;
+  bool includeSuffixes;
+  bool matchesOnly;
+} settings_t;
+
+settings_t* new_settings_t();
+
+void printHelpText(const char* prgName);
+
+#endif /* _CLINE_H */
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/functions.c	Thu May 26 14:39:52 2011 +0200
@@ -0,0 +1,111 @@
+#include "cline.h"
+#include "functions.h"
+
+int checkArgument(const char* arg, const char* expected) {
+  int len = strlen(expected);
+  int ret = 0;
+
+  if (arg[0] == '-') {
+    if (arg[1] != '-') {
+      for (int t = 0 ; t < len ; t++) {
+        ret |= (strchr(arg, expected[t]) > 0) << t;
+      }
+    }
+  }  
+
+  return ret;
+}
+
+bool testSuffix(char* filename, settings_t* settings) {
+  bool ret = false;
+  int tokenlen, fnamelen = strlen(filename);
+  for (int t = 0 ; t < settings->suffixc ; t++) {
+    tokenlen = strlen(settings->suffixv[t]);
+    if (fnamelen >= tokenlen && tokenlen > 0) {
+      if (strncmp(filename+fnamelen-tokenlen,
+                  settings->suffixv[t], tokenlen) == 0) {
+        ret = true;
+        break;
+      }
+    }
+  }
+  return ret ^ !settings->includeSuffixes;
+}
+
+int scanDirectory(DIR *dir, const int spaces,
+                  char* currdir, settings_t* settings) {
+  DIR *subdir;
+  char* subdirname;
+  struct dirent *entry;
+  int lines, digits, a;
+  int lineSum = 0;
+
+  while ((entry = readdir(dir)) != NULL) {
+    if (strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0) {
+      // Print occurence
+      char entryname[strlen(entry->d_name)+spaces];
+      for (int t = 0 ; t < spaces ; t++) {
+        entryname[t]=' ';
+      }
+      entryname[spaces] = 0;
+      strcat(entryname, entry->d_name);
+  
+      // Check for subdirectory
+      char subdirname[(1+strlen(currdir)+strlen(entry->d_name))];
+      strcpy(subdirname, currdir);
+      strncat(subdirname, &settings->fileSeparator, 1);
+      strcat(subdirname, entry->d_name);
+      if ((subdir = opendir(subdirname)) != NULL) {
+        printf("%-60s\n", entryname);
+        if (settings->recursive) {
+          lineSum += scanDirectory(subdir, spaces+1, subdirname, settings);
+        }
+        closedir(subdir);
+        continue;
+      }
+
+      // Count lines
+      lines = 0;
+      char filename[(1+strlen(currdir)+strlen(entry->d_name))];
+      strcpy(filename, currdir);
+      strncat(filename, &settings->fileSeparator, 1);
+      strcat(filename, entry->d_name);
+      if (testSuffix(filename, settings)) {
+        FILE *file = fopen(filename, "r");
+        if (file == NULL) {
+          perror("  File acces failed");
+          continue;
+        }
+
+        do {
+          a = fgetc(file);
+
+          if (a == 10) {
+            lines++;
+          }
+        } while (a != EOF);
+        fclose(file);
+
+        // Print line count
+        #ifdef _WIN32
+          printf("%-60s%13d lines\n", entryname, lines);
+        #else
+          printf("%-60s%14d lines\n", entryname, lines);
+        #endif /* _WIN32 */
+
+        lineSum += lines;
+      }
+      else {
+        if (!settings->matchesOnly) {
+          // Print hint
+          #ifdef _WIN32
+            printf("%-60s%19s\n", entryname, "no match");
+          #else
+            printf("%-60s%20s\n", entryname, "no match");
+          #endif /* _WIN32 */
+        }
+      }
+    }
+  }
+  return lineSum;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/functions.h	Thu May 26 14:39:52 2011 +0200
@@ -0,0 +1,9 @@
+#ifndef _CLINE_FUNCTIONS_H
+#define _CLINE_FUNCTIONS_H
+
+int checkArgument(const char*, const char*);
+bool testSuffix(char* filename, settings_t* settings);
+int scanDirectory(DIR *dir, const int spaces,
+                  char* currdir, settings_t* settings);
+
+#endif /* V2_H */
--- a/include.h	Mon May 23 16:54:56 2011 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,10 +0,0 @@
-#ifndef INCLUDE_H
-#define INCLUDE_H
-
-#include <stdio.h>
-#include <string.h>
-#include <stdbool.h>
-#include <stdlib.h>
-#include <dirent.h>
-
-#endif /* INCLUDE_H */
--- a/v2.c	Mon May 23 16:54:56 2011 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,17 +0,0 @@
-#include "v2.h"
-#include "include.h"
-
-int checkArgument(const char* arg, const char* expected) {
-  int len = strlen(expected);
-  int ret = 0;
-
-  if (arg[0] == '-') {
-    if (arg[1] != '-') {
-      for (int t = 0 ; t < len ; t++) {
-        ret |= (strchr(arg, expected[t]) > 0) << t;
-      }
-    }
-  }  
-
-  return ret;
-}
--- a/v2.h	Mon May 23 16:54:56 2011 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,6 +0,0 @@
-#ifndef V2_H
-#define V2_H
-
-int checkArgument(const char*, const char*);
-
-#endif /* V2_H */

mercurial