cline.c

Sat, 02 Jul 2011 17:43:20 +0200

author
Mike Becker <universe@uap-core.de>
date
Sat, 02 Jul 2011 17:43:20 +0200
changeset 9
1dd63a32ffc4
parent 8
28319b20968c
child 11
06cbd0ec003d
permissions
-rw-r--r--

fixed pointer fail

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

mercurial