cline.c

Thu, 20 Oct 2011 14:13:56 +0200

author
Mike Becker <universe@uap-core.de>
date
Thu, 20 Oct 2011 14:13:56 +0200
changeset 20
43725438ac50
parent 19
8bac9fd0629d
child 21
91e0890464b0
permissions
-rw-r--r--

Changed author comments + added signatures for upcomming bfile heuristics

     1 /*
     2  * cline.c
     3  *
     4  *  Created on: 23.05.2011
     5  *      Author: Mike
     6  */
     8 #include "cline.h"
     9 #include "scanner.h"
    10 #include "settings.h"
    11 #include "arguments.h"
    12 #include "stream.h"
    14 void printHelpText() {
    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  -v, --version       - print out version information"
    30     "\n  -V                  - turn verbose output off, print the result only"
    31     "\n\n"
    32     "The default call without any options is:"    
    33     "\n  cline ./\n"
    34     "So each file in the working directory is counted. If you want to count C"
    35     "\nsource code in your working directory and its subdirectories, type:"
    36     "\n  cline -rs .c\n";
    38   printf(helpText);
    39 }
    41 int exit_with_version(settings_t* settings) {
    42   printf("cline - Revision: %s", VERSION);
    43   destroy_settings_t(settings);
    44   return 0;
    45 }
    47 int exit_with_help(settings_t* settings, int code) {
    48   printHelpText();
    49   destroy_settings_t(settings);
    50   return code;
    51 }
    53 int main(int argc, char** argv) {
    55   // Settings
    56   settings_t *settings = new_settings_t();
    57   if (settings == NULL) {
    58     fprintf(stderr, "Memory allocation failed.\n");
    59     return 1;
    60   }
    62   // Get arguments
    63   char* directory = "./";
    64   char* suffix = " ";
    65   int checked = 0;
    67   for (int t = 1 ; t < argc ; t++) {
    69     int argflags = checkArgument(argv[t], "hsSrRmvV");
    71     // s, S
    72     if ((argflags & 6) > 0) {
    73       if (registerArgument(&checked, 6)) {
    74         return exit_with_help(settings, 1);
    75       }
    76       settings->includeSuffixes = (argflags & 2) > 0;
    77       t++;
    78       if (t >= argc) {
    79         return exit_with_help(settings, 1);
    80       }
    81       suffix = argv[t]; 
    82     }
    83     // h
    84     if ((argflags & 1) > 0 || strcmp(argv[t], "--help") == 0) {
    85       return exit_with_help(settings, 0);
    86     }
    87     // r, R
    88     if ((argflags & 24) > 0) {
    89       if (registerArgument(&checked, 24)) {
    90         return exit_with_help(settings, 1);
    91       }
    92       settings->recursive = true;
    93     }
    94     // m
    95     if ((argflags & 32) > 0) {
    96       if (registerArgument(&checked, 32)) {
    97         return exit_with_help(settings, 1);
    98       }
    99       settings->matchesOnly = true;
   100     }
   101     // v
   102     if ((argflags & 64) > 0 || strcmp(argv[t], "--version") == 0) {
   103       return exit_with_version(settings);
   104     }
   105     // V
   106     if ((argflags & 128) > 0) {
   107       if (registerArgument(&checked, 128)) {
   108         return exit_with_help(settings, 1);
   109       }
   110       settings->verbose = false;
   111     }
   112     // Path
   113     if (argflags == 0) {
   114       if (registerArgument(&checked, 1024)) {
   115         return exit_with_help(settings, 1);
   116       }
   117       directory = argv[t];
   118     }
   119   }
   121   // Configure output
   122   if (!settings->verbose) {
   123     close_stdout();
   124   }
   126   // Find tokens
   127   char* finder = strtok(suffix, ",");
   128   while (finder != NULL) {
   129     add_string(settings->suffixList, finder);
   130     finder = strtok(NULL, ",");
   131   }
   133   // Open directory
   134   DIR *dir = opendir(directory);
   135   if (dir == NULL) {
   136     perror("Operation failed");
   137     destroy_settings_t(settings);
   138     return 1;
   139   }
   141   // Scan directory
   142   int lines = scanDirectory(dir, 0, directory, settings);
   143   closedir(dir);
   144   destroy_settings_t(settings);
   146   // Print double line and line count
   147 #ifdef _WIN32
   148     const int columns = 79;
   149 #else
   150     const int columns = 80;
   151 #endif /* _WIN32 */
   153   for (int t = 0 ; t < columns ; t++) {
   154     printf("=");
   155   }
   156 #ifdef _WIN32
   157     printf("\n%73d lines\n", lines);
   158 #else
   159     printf("\n%74d lines\n", lines);
   160 #endif /* _WIN32 */
   162   if (!settings->verbose) {
   163     reopen_stdout();
   164     printf("%d", lines);
   165   }
   167   fflush(stdout);
   168   return 0;
   169 }

mercurial