cline.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 1
34a5e235d16e
child 4
c3acfb3b4957
permissions
-rw-r--r--

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

     1 #include "cline.h"
     2 #include "functions.h"
     4 settings_t* new_settings_t() {
     5   settings_t *settings = malloc(sizeof(settings_t*));
     6 #ifdef _WIN32
     7   settings->fileSeparator      = '\\';
     8 #else
     9   settings->fileSeparator      = '/';
    10 #endif /* _WIN32 */
    11   settings->suffixc            = 1;
    12   settings->recursive          = false;
    13   settings->includeSuffixes    = false;
    14   settings->matchesOnly        = false;
    15 }
    17 void printHelpText(const char* prgName) {
    18   // Help text
    19   const char* helpText = 
    20     "\nUsage:"
    21     "\n      %s [-hrm][-s suffix][<directory>]"
    22     "\n      %s [-hrm][-S suffix][<directory>]"
    23     "\n\nCounts the line terminator characters (\\n) within all"
    24     " files in the specified\ndirectory."
    25     "\n\nOptions:"
    26     "\n  -h, --help          - this help text"
    27     "\n  -m                  - print information about matching files only"
    28     "\n  -s <suffixes>       - only count files with these suffixes (separated"
    29     "\n                        by commas)"
    30     "\n  -S <suffixes>       - count any file except those with these suffixes"
    31     "\n                        (separated by commas)"
    32     "\n  -r, -R              - includes subdirectories"
    33     "\n\n"
    34     "The default call without any options is:"    
    35     "\n  %s ./\n"
    36     "That means each file in each subdirectory is counted. If you want to count"
    37     "\nC source code in your working directory and its subdirectories, type:"
    38     "\n  %s -rs .c\n";
    40   printf(helpText, prgName, prgName, prgName, prgName);
    41 }
    43 int main(int argc, char** argv) {
    45   // Settings
    46   settings_t *settings = new_settings_t();
    48   // Program name
    49   char* prgName = strrchr(argv[0], settings->fileSeparator);
    51   if (prgName == NULL) {
    52     prgName = argv[0];
    53   } else {
    54     prgName++;
    55   }
    57   // Defaults
    58   char* _suffix = " ";
    59   char _directory[3];
    60   _directory[0] = '.';
    61   _directory[1] = settings->fileSeparator;
    62   _directory[2] = 0;
    64   // Get arguments
    65   char* directory;
    66   char* suffix;
    67   bool showHelp = false;
    68   char checked = 0;
    70   for (int t = 1 ; t < argc ; t++) {
    72     int argflags = checkArgument(argv[t], "hsSrRm");
    74     // s
    75     if ((argflags & 2) > 0) {
    76       if ((checked & 1) > 0) {
    77         printHelpText(prgName);
    78         return -1;
    79       }
    80       settings->includeSuffixes = true;
    81       t++;
    82       if (t >= argc) {
    83         printHelpText(prgName);
    84         return -1;
    85       }
    86       suffix = argv[t]; 
    87       checked |= 1;
    88     }
    89     // S
    90     if ((argflags & 4) > 0) {
    91       if ((checked & 1) > 0) {
    92         printHelpText(prgName);
    93         return -1;
    94       }
    95       settings->includeSuffixes = false;
    96       t++;
    97       if (t >= argc) {
    98         printHelpText(prgName);
    99         return -1;
   100       }
   101       suffix = argv[t];
   102       checked |= 1;
   103     }
   104     // h
   105     if ((argflags & 1) > 0 || strcmp(argv[t], "--help") == 0) {
   106       if ((checked & 2) > 0) {
   107         printHelpText(prgName);
   108         return -1;
   109       }
   110       checked |= 2;
   111       showHelp = true;
   112     }
   113     // r, R
   114     if ((argflags & 24) > 0) {
   115       if ((checked & 4) > 0) {
   116         printHelpText(prgName);
   117         return -1;
   118       }
   119       checked |= 4;
   120       settings->recursive = true;
   121     }
   122     if ((argflags & 32) > 0) {
   123       if ((checked & 32) > 0) {
   124         printHelpText(prgName);
   125         return -1;
   126       }
   127       checked |= 32;
   128       settings->matchesOnly = true;
   129     }
   130     // other
   131     if (argflags == 0) {
   132       if ((checked & 8) > 0) {
   133         printHelpText(prgName);
   134         return -1;
   135       }
   136       checked |= 8;
   137       directory = argv[t];
   138     }
   139   }
   141   // Show help and quit
   142   if (showHelp) {
   143     printHelpText(prgName);
   144     return 0;
   145   }
   147   // Default values
   148   if ((checked & 1) == 0) {
   149     suffix = _suffix;
   150   }
   152   if ((checked & 8) == 0) {
   153     directory = _directory;
   154   }
   156   // Find tokens
   157   char* finder;
   158   finder = strchr(suffix, ',');
   159   while (finder != NULL) {
   160     settings->suffixc++;
   161     finder = strchr(finder+1, ',');
   162   }
   163   settings->suffixv = (char**) malloc(sizeof(char**)*settings->suffixc);
   164   if (settings->suffixv == NULL) {
   165     fprintf(stderr, "Memory allocation failed.\n");
   166     return 1;
   167   }
   168   finder = strtok(suffix, ",");
   169   int c = 0;
   170   while (finder != NULL) {
   171     settings->suffixv[c] = finder;
   172     c++;
   173     finder = strtok(NULL, ",");
   174   }
   176   // Open directory
   177   DIR *dir = opendir(directory);
   178   if (dir == NULL) {
   179     perror("Operation failed");
   180     free(settings->suffixv);
   181     free(settings);
   182     return 1;
   183   }
   185   // Scan directory
   186   int lines = scanDirectory(dir, 0, directory, settings);
   188   // Print double line and line count
   189   #ifdef _WIN32
   190     const int columns = 79;
   191   #else
   192     const int columns = 80;
   193   #endif /* _WIN32 */
   195   for (int t = 0 ; t < columns ; t++) {
   196     printf("=");
   197   }
   198   #ifdef _WIN32
   199     printf("\n%73d lines\n", lines);
   200   #else
   201     printf("\n%74d lines\n", lines);
   202   #endif /* _WIN32 */
   204   closedir(dir);
   205   free(settings->suffixv);
   206   free(settings);
   207   return 0;
   208 }

mercurial