cline version 2

Mon, 23 May 2011 16:43:13 +0200

author
Mike Becker <universe@uap-core.de>
date
Mon, 23 May 2011 16:43:13 +0200
changeset 1
34a5e235d16e
parent 0
518bfd1cc1e8
child 2
29ac790c27ad

cline version 2

Makefile file | annotate | diff | comparison | revisions
cline.c file | annotate | diff | comparison | revisions
cline.exe file | annotate | diff | comparison | revisions
cline.o 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
v2.o file | annotate | diff | comparison | revisions
--- a/Makefile	Mon May 23 16:42:44 2011 +0200
+++ b/Makefile	Mon May 23 16:43:13 2011 +0200
@@ -1,4 +1,8 @@
 CC = gcc
+OBJ = cline.o v2.o
 
-cline: cline.c
-	${CC} -o cline -std=c99 cline.c
+cline: ${OBJ}
+	${CC} -o cline ${OBJ}
+
+%.o: %.c
+	${CC} -c -std=c99 $<
--- a/cline.c	Mon May 23 16:42:44 2011 +0200
+++ b/cline.c	Mon May 23 16:43:13 2011 +0200
@@ -1,25 +1,26 @@
-#include <stdio.h>
-#include <string.h>
-#include <stdbool.h>
-#include <stdlib.h>
-#include <dirent.h>
+#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 testSuffix(char* filename) {
   bool ret = false;
   int tokenlen, fnamelen = strlen(filename);
-  for (int t = 0 ; t < suffixc ; t++)
-  {
+  for (int t = 0 ; t < suffixc ; t++) {
     tokenlen = strlen(suffixv[t]);
-    if (fnamelen >= tokenlen)
-    {
-      if (strncmp(filename+fnamelen-tokenlen, suffixv[t], tokenlen) == 0)
-      {
+    if (fnamelen >= tokenlen && tokenlen > 0) {
+      if (strncmp(filename+fnamelen-tokenlen, suffixv[t], tokenlen) == 0) {
         ret = true;
         break;
       }
@@ -28,38 +29,31 @@
   return ret ^ !includeSuffixes;
 }
 
-int scanDirectory(DIR *dir, const int spaces, char* currdir)
-{
+int scanDirectory(DIR *dir, const int spaces, char* currdir) {
   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)
-    {
+  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++)
-      {
+      for (int t = 0 ; t < spaces ; t++) {
         entryname[t]=' ';
       }
       entryname[spaces] = 0;
       strcat(entryname, entry->d_name);
-      printf("%-60s", entryname);
   
       // Check for subdirectory
       char subdirname[(1+strlen(currdir)+strlen(entry->d_name))];
       strcpy(subdirname, currdir);
-      strcat(subdirname, "/");
+      strncat(subdirname, &fileSeparator, 1);
       strcat(subdirname, entry->d_name);
-      if ((subdir = opendir(subdirname)) != NULL)
-      {
-        printf("\n");
-        if (recursive)
-        {
+      if ((subdir = opendir(subdirname)) != NULL) {
+        printf("%-60s\n", entryname);
+        if (recursive) {
           lineSum += scanDirectory(subdir, spaces+1, subdirname);
         }
         closedir(subdir);
@@ -70,169 +64,184 @@
       lines = 0;
       char filename[(1+strlen(currdir)+strlen(entry->d_name))];
       strcpy(filename, currdir);
-      strcat(filename, "/");
+      strncat(filename, &fileSeparator, 1);
       strcat(filename, entry->d_name);
-      if (testSuffix(filename))
-      {
+      if (testSuffix(filename)) {
         FILE *file = fopen(filename, "r");
-        if (file == NULL)
-        {
+        if (file == NULL) {
           perror("  File acces failed");
           continue;
         }
 
-        do
-        {
+        do {
           a = fgetc(file);
 
-          if (a == 10)
-          {
+          if (a == 10) {
             lines++;
           }
         } while (a != EOF);
         fclose(file);
 
         // Print line count
-        printf("%14d lines\n", lines);
+        #ifdef _WIN32
+          printf("%-60s%13d lines\n", entryname, lines);
+        #else
+          printf("%-60s%14d lines\n", entryname, lines);
+        #endif /* _WIN32 */
 
         lineSum += lines;
       }
-      else
-      {
-        // Print hint
-        printf("%20s\n", "no match");
+      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;
 }
 
-int main(int argc, char** argv)
-{
+void printHelpText(const char* prgName) {
   // Help text
   const char* helpText = 
-    "\nUsage:\n%s [-h|--help|(-s=<suffix>|-S=<suffix>)|(-r|-R)|<directory>]"
+    "\nUsage:"
+    "\n      %s [-hrm][-s suffix][<directory>]"
+    "\n      %s [-hrm][-S suffix][<directory>]"
     "\n\nCounts the line terminator characters (\\n) within all"
     " files in the specified\ndirectory."
     "\n\nOptions:"
     "\n  -h, --help          - this help text"
-    "\n  -s=<suffixes>       - only count files with these suffixes (separated"
+    "\n  -m                  - print information about matching files only"
+    "\n  -s <suffixes>       - only count files with these suffixes (separated"
     "\n                        by commas)"
-    "\n  -S=<suffixes>       - count any file except those with these suffixes"
+    "\n  -S <suffixes>       - count any file except those with these suffixes"
     "\n                        (separated by commas)"
-    "\n  -R                  - excludes subdirectories"
-    "\n  -r                  - includes subdirecotires"
+    "\n  -r, -R              - includes subdirectories"
     "\n\n"
-    "The default call without any options is:"
-    "\n  %s -r ./ -S=\n"
+    "The default call without any options is:"    
+    "\n  %s ./\n"
     "That means each file in each subdirectory is counted. If you want to count"
     "\nC source code in your working directory and its subdirectories, type:"
-    "\n  %s -s=.c\n";
+    "\n  %s -rs .c\n";
+    
+    printf(helpText, prgName, prgName, prgName, prgName);
+}
+
+int main(int argc, char** argv) {
 
   // Program name
-  char* prgName = strrchr(argv[0], '/');
-  if (prgName == NULL)
-  {
+  char* prgName = strrchr(argv[0], fileSeparator);
+  
+  if (prgName == NULL) {
     prgName = argv[0];
   }
-  else
-  {
+  else {
     prgName++;
   }
 
   // Defaults
-  char* _suffix = "";
-  char* _directory = "./";
+  char* _suffix = " ";
+  char _directory[3];
+  _directory[0] = '.';
+  _directory[1] = fileSeparator;
+  _directory[2] = 0;
 
   // Get arguments
   char* directory;
   char* suffix;
   bool showHelp = false;
-  recursive = true;
+  recursive = false;
   includeSuffixes = false;
   char checked = 0;
 
-  for (int t = 1 ; t < argc ; t++)
-  {
-    if (strncmp(argv[t], "-s=", 3) == 0)
-    {
-      if ((checked & 1) > 0)
-      {
-        printf(helpText, prgName, prgName, prgName);
+  for (int t = 1 ; t < argc ; t++) {
+
+    int argflags = checkArgument(argv[t], "hsSrRm");
+
+    // s
+    if ((argflags & 2) > 0) {
+      if ((checked & 1) > 0) {
+        printHelpText(prgName);
         return -1;
       }
       includeSuffixes = true;
-      suffix = argv[t]+3; 
+      t++;
+      if (t >= argc) {
+        printHelpText(prgName);
+        return -1;
+      }
+      suffix = argv[t]; 
       checked |= 1;
     }
-    else if (strncmp(argv[t], "-S=", 3) == 0)
-    {
-      if ((checked & 1) > 0)
-      {
-        printf(helpText, prgName, prgName, prgName);
+    // S
+    if ((argflags & 4) > 0) {
+      if ((checked & 1) > 0) {
+        printHelpText(prgName);
         return -1;
       }
       includeSuffixes = false;
-      suffix = argv[t]+3;
+      t++;
+      if (t >= argc) {
+        printHelpText(prgName);
+        return -1;
+      }
+      suffix = argv[t];
       checked |= 1;
     }
-    else if (strcmp(argv[t], "-h") == 0 || strcmp(argv[t], "--help") == 0)
-    {
-      if ((checked & 2) > 0)
-      {
-        printf(helpText, prgName, prgName, prgName);
+    // h
+    if ((argflags & 1) > 0 || strcmp(argv[t], "--help") == 0) {
+      if ((checked & 2) > 0) {
+        printHelpText(prgName);
         return -1;
       }
       checked |= 2;
       showHelp = true;
     }
-    else if (strcmp(argv[t], "-r") == 0)
-    {
-      if ((checked & 4) > 0)
-      {
-        printf(helpText, prgName, prgName, prgName);
+    // r, R
+    if ((argflags & 24) > 0) {
+      if ((checked & 4) > 0) {
+        printHelpText(prgName);
         return -1;
       }
       checked |= 4;
       recursive = true;
     }
-    else if (strcmp(argv[t], "-R") == 0)
-    {
-      if ((checked & 4) > 0)
-      {
-        printf(helpText, prgName, prgName, prgName);
+    if ((argflags & 32) > 0) {
+      if ((checked & 32) > 0) {
+        printHelpText(prgName);
         return -1;
       }
-      checked |= 4;
-      recursive = false;
+      checked |= 32;
+      matchesOnly = true;
     }
-    else
-    {
-      if ((checked & 8) > 0)
-      {
-        printf(helpText, prgName, prgName, prgName);
+    // other
+    if (argflags == 0) {
+      if ((checked & 8) > 0) {
+        printHelpText(prgName);
         return -1;
       }
-      checked |= 8;      
+      checked |= 8;
       directory = argv[t];
     }
   }
 
   // Show help and quit
-  if (showHelp)
-  {
-    printf(helpText, prgName, prgName, prgName);
+  if (showHelp) {
+    printHelpText(prgName);
     return 0;
   }
 
   // Default values
-  if ((checked & 1) == 0)
-  {
+  if ((checked & 1) == 0) {
     suffix = _suffix;
   }
 
-  if ((checked & 8) == 0)
-  {
+  if ((checked & 8) == 0) {
     directory = _directory;
   }
 
@@ -240,21 +249,18 @@
   char* finder;
   suffixc = 1;
   finder = strchr(suffix, ',');
-  while (finder != NULL)
-  {
+  while (finder != NULL) {
     suffixc++;
     finder = strchr(finder+1, ',');
   }
   suffixv = (char**) malloc(sizeof(suffixv)*suffixc);
-  if (suffixv == NULL)
-  {
+  if (suffixv == NULL) {
     fprintf(stderr, "Memory allocation failed.\n");
     return 1;
   }
   finder = strtok(suffix, ",");
   int c = 0;
-  while (finder != NULL)
-  {
+  while (finder != NULL) {
     suffixv[c] = finder;
     c++;
     finder = strtok(NULL, ",");
@@ -262,8 +268,7 @@
 
   // Open directory
   DIR *dir = opendir(directory);
-  if (dir == NULL)
-  {
+  if (dir == NULL) {
     perror("Operation failed");
     free(suffixv);
     return 1;
@@ -273,11 +278,20 @@
   int lines = scanDirectory(dir, 0, directory);
 
   // Print double line and line count
-  for (int t = 0 ; t < 80 ; t++)
-  {
+  #ifdef _WIN32
+     const int columns = 79;
+  #else
+     const int columns = 80;
+  #endif /* _WIN32 */
+
+  for (int t = 0 ; t < columns ; t++) {
     printf("=");
   }
-  printf("\n%74d lines\n", lines);
+  #ifdef _WIN32
+     printf("\n%73d lines\n", lines);
+  #else
+     printf("\n%74d lines\n", lines);
+  #endif /* _WIN32 */
 
   closedir(dir);
   free(suffixv);
Binary file cline.exe has changed
Binary file cline.o has changed
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/include.h	Mon May 23 16:43:13 2011 +0200
@@ -0,0 +1,10 @@
+#ifndef INCLUDE_H
+#define INCLUDE_H
+
+#include <stdio.h>
+#include <string.h>
+#include <stdbool.h>
+#include <stdlib.h>
+#include <dirent.h>
+
+#endif /* INCLUDE_H */
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/v2.c	Mon May 23 16:43:13 2011 +0200
@@ -0,0 +1,17 @@
+#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;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/v2.h	Mon May 23 16:43:13 2011 +0200
@@ -0,0 +1,6 @@
+#ifndef V2_H
+#define V2_H
+
+int checkArgument(const char*, const char*);
+
+#endif /* V2_H */
Binary file v2.o has changed

mercurial