cline.c

Thu, 15 Sep 2011 13:29:06 +0200

author
Mike Becker <universe@uap-core.de>
date
Thu, 15 Sep 2011 13:29:06 +0200
changeset 10
ecf787666f44
parent 8
28319b20968c
child 11
06cbd0ec003d
permissions
-rw-r--r--

refactored sources

     1 /*
     2  * cline.c
     3  *
     4  *  Created on: 23.05.2011
     5  *      Author: beckermi
     6  */
     8 #include "cline.h"
     9 #include "scanner.h"
    10 #include "settings.h"
    12 void printHelpText(const char* prgName) {
    13   // Help text
    14   const char* helpText = 
    15     "\nUsage:"
    16     "\n      %s [-hrm][-s suffix][<directory>]"
    17     "\n      %s [-hrm][-S suffix][<directory>]"
    18     "\n\nCounts the line terminator characters (\\n) within all"
    19     " files in the specified\ndirectory."
    20     "\n\nOptions:"
    21     "\n  -h, --help          - this help text"
    22     "\n  -m                  - print information about matching files only"
    23     "\n  -s <suffixes>       - only count files with these suffixes (separated"
    24     "\n                        by commas)"
    25     "\n  -S <suffixes>       - count any file except those with these suffixes"
    26     "\n                        (separated by commas)"
    27     "\n  -r, -R              - includes subdirectories"
    28     "\n\n"
    29     "The default call without any options is:"    
    30     "\n  %s ./\n"
    31     "So each file in the working directory is counted. If you want to count C"
    32     "\nsource code in your working directory and its subdirectories, type:"
    33     "\n  %s -rs .c\n";
    35   printf(helpText, prgName, prgName, prgName, prgName);
    36 }
    38 int exit_with_help(char* prgName, settings_t* settings, int code) {
    39   printHelpText(prgName);
    40   destroy_settings_t(settings);
    41   return code;
    42 }
    44 int main(int argc, char** argv) {
    46   // Settings
    47   settings_t *settings = new_settings_t();
    48   if (settings == NULL) {
    49     fprintf(stderr, "Memory allocation failed.\n");
    50     return 1;
    51   }
    53   // Program name
    54   char* prgName = strrchr(argv[0], settings->fileSeparator);
    56   if (prgName == NULL) {
    57     prgName = argv[0];
    58   } else {
    59     prgName++;
    60   }
    62   // Get arguments
    63   char* directory = "./";
    64   char* suffix = " ";
    65   bool showHelp = false;
    66   int checked = 0;
    68   for (int t = 1 ; t < argc ; t++) {
    70     int argflags = checkArgument(argv[t], "hsSrRm");
    72     // s
    73     if ((argflags & 2) > 0) {
    74       if (registerArgument(&checked, 6)) {
    75         return exit_with_help(prgName, settings, 1);
    76       }
    77       settings->includeSuffixes = true;
    78       t++;
    79       if (t >= argc) {
    80         return exit_with_help(prgName, settings, 1);
    81       }
    82       suffix = argv[t]; 
    83     }
    84     // S
    85     if ((argflags & 4) > 0) {
    86       if (registerArgument(&checked, 6)) {
    87         return exit_with_help(prgName, settings, 1);
    88       }
    89       settings->includeSuffixes = false;
    90       t++;
    91       if (t >= argc) {
    92         return exit_with_help(prgName, settings, 1);
    93       }
    94       suffix = argv[t];
    95     }
    96     // h
    97     if ((argflags & 1) > 0 || strcmp(argv[t], "--help") == 0) {
    98       return exit_with_help(prgName, settings, 1);
    99     }
   100     // r, R
   101     if ((argflags & 24) > 0) {
   102       if (registerArgument(&checked, 24)) {
   103         return exit_with_help(prgName, settings, 1);
   104       }
   105       settings->recursive = true;
   106     }
   107     // m
   108     if ((argflags & 32) > 0) {
   109       if (registerArgument(&checked, 32)) {
   110         return exit_with_help(prgName, settings, 1);
   111       }
   112       settings->matchesOnly = true;
   113     }
   114     // Path
   115     if (argflags == 0) {
   116       if (registerArgument(&checked, 1024)) {
   117         return exit_with_help(prgName, settings, 1);
   118       }
   119       directory = argv[t];
   120     }
   121   }
   123   // Find tokens
   124   char* finder = strtok(suffix, ",");
   125   while (finder != NULL) {
   126     add_suffix(settings->suffixList, finder);
   127     finder = strtok(NULL, ",");
   128   }
   130   // Open directory
   131   DIR *dir = opendir(directory);
   132   if (dir == NULL) {
   133     perror("Operation failed");
   134     destroy_settings_t(settings);
   135     return 1;
   136   }
   138   // Scan directory
   139   int lines = scanDirectory(dir, 0, directory, settings);
   140   closedir(dir);
   141   destroy_settings_t(settings);
   143   // Print double line and line count
   144   #ifdef _WIN32
   145     const int columns = 79;
   146   #else
   147     const int columns = 80;
   148   #endif /* _WIN32 */
   150   for (int t = 0 ; t < columns ; t++) {
   151     printf("=");
   152   }
   153   #ifdef _WIN32
   154     printf("\n%73d lines\n", lines);
   155   #else
   156     printf("\n%74d lines\n", lines);
   157   #endif /* _WIN32 */
   159   return 0;
   160 }

mercurial