cline.c

Fri, 16 Sep 2011 10:36:45 +0200

author
Mike Becker <universe@uap-core.de>
date
Fri, 16 Sep 2011 10:36:45 +0200
changeset 14
ee9333c91dda
parent 12
902cb8d2053c
child 16
bc9a0fefd892
permissions
-rw-r--r--

some minor fixes + makefile now injects revisionnumber into cline.h

     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   const char* helpText = 
    15     "\nUsage:"
    16     "\n      cline [-hrm][-s suffix][<directory>]"
    17     "\n      cline [-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  -v, --version       - print out version information"
    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_version(settings_t* settings) {
    40   printf("cline - Version: %s", VERSION);
    41   destroy_settings_t(settings);
    42   return 0;
    43 }
    45 int exit_with_help(settings_t* settings, int code) {
    46   printHelpText();
    47   destroy_settings_t(settings);
    48   return code;
    49 }
    51 int main(int argc, char** argv) {
    53   // Settings
    54   settings_t *settings = new_settings_t();
    55   if (settings == NULL) {
    56     fprintf(stderr, "Memory allocation failed.\n");
    57     return 1;
    58   }
    60   // Get arguments
    61   char* directory = "./";
    62   char* suffix = " ";
    63   int checked = 0;
    65   for (int t = 1 ; t < argc ; t++) {
    67     int argflags = checkArgument(argv[t], "hsSrRmv");
    69     // s, S
    70     if ((argflags & 6) > 0) {
    71       if (registerArgument(&checked, 6)) {
    72         return exit_with_help(settings, 1);
    73       }
    74       settings->includeSuffixes = (argflags & 2) > 0;
    75       t++;
    76       if (t >= argc) {
    77         return exit_with_help(settings, 1);
    78       }
    79       suffix = argv[t]; 
    80     }
    81     // h
    82     if ((argflags & 1) > 0 || strcmp(argv[t], "--help") == 0) {
    83       return exit_with_help(settings, 0);
    84     }
    85     // r, R
    86     if ((argflags & 24) > 0) {
    87       if (registerArgument(&checked, 24)) {
    88         return exit_with_help(settings, 1);
    89       }
    90       settings->recursive = true;
    91     }
    92     // m
    93     if ((argflags & 32) > 0) {
    94       if (registerArgument(&checked, 32)) {
    95         return exit_with_help(settings, 1);
    96       }
    97       settings->matchesOnly = true;
    98     }
    99     // v
   100     if ((argflags & 64) > 0 || strcmp(argv[t], "--version") == 0) {
   101       return exit_with_version(settings);
   102     }
   103     // Path
   104     if (argflags == 0) {
   105       if (registerArgument(&checked, 1024)) {
   106         return exit_with_help(settings, 1);
   107       }
   108       directory = argv[t];
   109     }
   110   }
   112   // Find tokens
   113   char* finder = strtok(suffix, ",");
   114   while (finder != NULL) {
   115     add_suffix(settings->suffixList, finder);
   116     finder = strtok(NULL, ",");
   117   }
   119   // Open directory
   120   DIR *dir = opendir(directory);
   121   if (dir == NULL) {
   122     perror("Operation failed");
   123     destroy_settings_t(settings);
   124     return 1;
   125   }
   127   // Scan directory
   128   int lines = scanDirectory(dir, 0, directory, settings);
   129   closedir(dir);
   130   destroy_settings_t(settings);
   132   // Print double line and line count
   133   #ifdef _WIN32
   134     const int columns = 79;
   135   #else
   136     const int columns = 80;
   137   #endif /* _WIN32 */
   139   for (int t = 0 ; t < columns ; t++) {
   140     printf("=");
   141   }
   142   #ifdef _WIN32
   143     printf("\n%73d lines\n", lines);
   144   #else
   145     printf("\n%74d lines\n", lines);
   146   #endif /* _WIN32 */
   148   return 0;
   149 }

mercurial