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

#include "cline.h"
#include "functions.h"

suffix_list_t* new_suffix_list_t() {
  suffix_list_t* suffixList = malloc(sizeof(suffix_list_t));
  suffixList->count = 0;
  suffixList->items = NULL;

  return suffixList;
}

void add_suffix(suffix_list_t* list, char* item) {
  char** reallocated_list =
    realloc(list->items, sizeof(char*) * list->count + 1);
  if (reallocated_list != NULL) {
    list->items = reallocated_list;
    list->items[list->count] = item;
    list->count++;
  }
}

settings_t* new_settings_t() {
  settings_t *settings = malloc(sizeof(settings_t));
  if (settings != NULL) {
  #ifdef _WIN32
    settings->fileSeparator      = '\\';
  #else
    settings->fileSeparator      = '/';
  #endif /* _WIN32 */
    settings->recursive          = false;
    settings->includeSuffixes    = false;
    settings->matchesOnly        = false;
    settings->suffixList         = new_suffix_list_t();
  }
  
  return settings;
}

void destroy_settings_t(settings_t* settings) {
  free(settings->suffixList);
  free(settings);
}

void printHelpText(const char* prgName) {
  // Help text
  const char* helpText = 
    "\nUsage:"
    "\n      %s [-hrm][-s suffix][<directory>]"
    "\n      %s [-hrm][-S suffix][<directory>]"
    "\n\nCounts the line terminator characters (\\n) within all"
    " files in the specified\ndirectory."
    "\n\nOptions:"
    "\n  -h, --help          - this help text"
    "\n  -m                  - print information about matching files only"
    "\n  -s <suffixes>       - only count files with these suffixes (separated"
    "\n                        by commas)"
    "\n  -S <suffixes>       - count any file except those with these suffixes"
    "\n                        (separated by commas)"
    "\n  -r, -R              - includes subdirectories"
    "\n\n"
    "The default call without any options is:"    
    "\n  %s ./\n"
    "So each file in the working directory is counted. If you want to count C"
    "\nsource code in your working directory and its subdirectories, type:"
    "\n  %s -rs .c\n";
    
  printf(helpText, prgName, prgName, prgName, prgName);
}

int exit_with_help(char* prgName, settings_t* settings, int code) {
  printHelpText(prgName);
  destroy_settings_t(settings);
  return code;
}

int main(int argc, char** argv) {

  // Settings
  settings_t *settings = new_settings_t();
  if (settings == NULL) {
    fprintf(stderr, "Memory allocation failed.\n");
    return 1;
  }

  // Program name
  char* prgName = strrchr(argv[0], settings->fileSeparator);
  
  if (prgName == NULL) {
    prgName = argv[0];
  } else {
    prgName++;
  }

  // Get arguments
  char* directory = "./";
  char* suffix = " ";
  bool showHelp = false;
  int checked = 0;

  for (int t = 1 ; t < argc ; t++) {

    int argflags = checkArgument(argv[t], "hsSrRm");

    // s
    if ((argflags & 2) > 0) {
      if (registerArgument(&checked, 6)) {
        return exit_with_help(prgName, settings, 1);
      }
      settings->includeSuffixes = true;
      t++;
      if (t >= argc) {
        return exit_with_help(prgName, settings, 1);
      }
      suffix = argv[t]; 
    }
    // S
    if ((argflags & 4) > 0) {
      if (registerArgument(&checked, 6)) {
        return exit_with_help(prgName, settings, 1);
      }
      settings->includeSuffixes = false;
      t++;
      if (t >= argc) {
        return exit_with_help(prgName, settings, 1);
      }
      suffix = argv[t];
    }
    // h
    if ((argflags & 1) > 0 || strcmp(argv[t], "--help") == 0) {
      if (registerArgument(&checked, 1)) {
        return exit_with_help(prgName, settings, 1);
      }
      showHelp = true;
    }
    // r, R
    if ((argflags & 24) > 0) {
      if (registerArgument(&checked, 24)) {
        return exit_with_help(prgName, settings, 1);
      }
      settings->recursive = true;
    }
    // m
    if ((argflags & 32) > 0) {
      if (registerArgument(&checked, 32)) {
        return exit_with_help(prgName, settings, 1);
      }
      settings->matchesOnly = true;
    }
    // Path
    if (argflags == 0) {
      if (registerArgument(&checked, 1024)) {
        return exit_with_help(prgName, settings, 1);
      }
      directory = argv[t];
    }
  }

  // Show help and quit
  if (showHelp) {
    return exit_with_help(prgName, settings, 0);
  }

  // Find tokens
  char* finder = strtok(suffix, ",");
  while (finder != NULL) {
    add_suffix(settings->suffixList, finder);
    finder = strtok(NULL, ",");
  }

  // Open directory
  DIR *dir = opendir(directory);
  if (dir == NULL) {
    perror("Operation failed");
    destroy_settings_t(settings);
    return 1;
  }
  
  // Scan directory
  int lines = scanDirectory(dir, 0, directory, settings);
  closedir(dir);
  destroy_settings_t(settings);

  // Print double line and line count
  #ifdef _WIN32
    const int columns = 79;
  #else
    const int columns = 80;
  #endif /* _WIN32 */

  for (int t = 0 ; t < columns ; t++) {
    printf("=");
  }
  #ifdef _WIN32
    printf("\n%73d lines\n", lines);
  #else
    printf("\n%74d lines\n", lines);
  #endif /* _WIN32 */

  return 0;
}

mercurial