cline.c

Fri, 16 Sep 2011 09:14:59 +0200

author
Mike Becker <universe@uap-core.de>
date
Fri, 16 Sep 2011 09:14:59 +0200
changeset 12
902cb8d2053c
parent 11
06cbd0ec003d
child 14
ee9333c91dda
permissions
-rw-r--r--

removed dynamic programm name

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

mercurial