cline project init

Mon, 23 May 2011 16:42:44 +0200

author
Mike Becker <universe@uap-core.de>
date
Mon, 23 May 2011 16:42:44 +0200
changeset 0
518bfd1cc1e8
child 1
34a5e235d16e

cline project init

Makefile file | annotate | diff | comparison | revisions
cline.c file | annotate | diff | comparison | revisions
cline.exe file | annotate | diff | comparison | revisions
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/Makefile	Mon May 23 16:42:44 2011 +0200
     1.3 @@ -0,0 +1,4 @@
     1.4 +CC = gcc
     1.5 +
     1.6 +cline: cline.c
     1.7 +	${CC} -o cline -std=c99 cline.c
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/cline.c	Mon May 23 16:42:44 2011 +0200
     2.3 @@ -0,0 +1,285 @@
     2.4 +#include <stdio.h>
     2.5 +#include <string.h>
     2.6 +#include <stdbool.h>
     2.7 +#include <stdlib.h>
     2.8 +#include <dirent.h>
     2.9 +
    2.10 +static int suffixc;
    2.11 +static char** suffixv;
    2.12 +static bool recursive;
    2.13 +static bool includeSuffixes;
    2.14 +
    2.15 +bool testSuffix(char* filename)
    2.16 +{
    2.17 +  bool ret = false;
    2.18 +  int tokenlen, fnamelen = strlen(filename);
    2.19 +  for (int t = 0 ; t < suffixc ; t++)
    2.20 +  {
    2.21 +    tokenlen = strlen(suffixv[t]);
    2.22 +    if (fnamelen >= tokenlen)
    2.23 +    {
    2.24 +      if (strncmp(filename+fnamelen-tokenlen, suffixv[t], tokenlen) == 0)
    2.25 +      {
    2.26 +        ret = true;
    2.27 +        break;
    2.28 +      }
    2.29 +    }
    2.30 +  }
    2.31 +  return ret ^ !includeSuffixes;
    2.32 +}
    2.33 +
    2.34 +int scanDirectory(DIR *dir, const int spaces, char* currdir)
    2.35 +{
    2.36 +  DIR *subdir;
    2.37 +  char* subdirname;
    2.38 +  struct dirent *entry;
    2.39 +  int lines, digits, a;
    2.40 +  int lineSum = 0;
    2.41 +
    2.42 +  while ((entry = readdir(dir)) != NULL)
    2.43 +  {
    2.44 +    if (strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0)
    2.45 +    {
    2.46 +      // Print occurence
    2.47 +      char entryname[strlen(entry->d_name)+spaces];
    2.48 +      for (int t = 0 ; t < spaces ; t++)
    2.49 +      {
    2.50 +        entryname[t]=' ';
    2.51 +      }
    2.52 +      entryname[spaces] = 0;
    2.53 +      strcat(entryname, entry->d_name);
    2.54 +      printf("%-60s", entryname);
    2.55 +  
    2.56 +      // Check for subdirectory
    2.57 +      char subdirname[(1+strlen(currdir)+strlen(entry->d_name))];
    2.58 +      strcpy(subdirname, currdir);
    2.59 +      strcat(subdirname, "/");
    2.60 +      strcat(subdirname, entry->d_name);
    2.61 +      if ((subdir = opendir(subdirname)) != NULL)
    2.62 +      {
    2.63 +        printf("\n");
    2.64 +        if (recursive)
    2.65 +        {
    2.66 +          lineSum += scanDirectory(subdir, spaces+1, subdirname);
    2.67 +        }
    2.68 +        closedir(subdir);
    2.69 +        continue;
    2.70 +      }
    2.71 +
    2.72 +      // Count lines
    2.73 +      lines = 0;
    2.74 +      char filename[(1+strlen(currdir)+strlen(entry->d_name))];
    2.75 +      strcpy(filename, currdir);
    2.76 +      strcat(filename, "/");
    2.77 +      strcat(filename, entry->d_name);
    2.78 +      if (testSuffix(filename))
    2.79 +      {
    2.80 +        FILE *file = fopen(filename, "r");
    2.81 +        if (file == NULL)
    2.82 +        {
    2.83 +          perror("  File acces failed");
    2.84 +          continue;
    2.85 +        }
    2.86 +
    2.87 +        do
    2.88 +        {
    2.89 +          a = fgetc(file);
    2.90 +
    2.91 +          if (a == 10)
    2.92 +          {
    2.93 +            lines++;
    2.94 +          }
    2.95 +        } while (a != EOF);
    2.96 +        fclose(file);
    2.97 +
    2.98 +        // Print line count
    2.99 +        printf("%14d lines\n", lines);
   2.100 +
   2.101 +        lineSum += lines;
   2.102 +      }
   2.103 +      else
   2.104 +      {
   2.105 +        // Print hint
   2.106 +        printf("%20s\n", "no match");
   2.107 +      }
   2.108 +    }
   2.109 +  }
   2.110 +  return lineSum;
   2.111 +}
   2.112 +
   2.113 +int main(int argc, char** argv)
   2.114 +{
   2.115 +  // Help text
   2.116 +  const char* helpText = 
   2.117 +    "\nUsage:\n%s [-h|--help|(-s=<suffix>|-S=<suffix>)|(-r|-R)|<directory>]"
   2.118 +    "\n\nCounts the line terminator characters (\\n) within all"
   2.119 +    " files in the specified\ndirectory."
   2.120 +    "\n\nOptions:"
   2.121 +    "\n  -h, --help          - this help text"
   2.122 +    "\n  -s=<suffixes>       - only count files with these suffixes (separated"
   2.123 +    "\n                        by commas)"
   2.124 +    "\n  -S=<suffixes>       - count any file except those with these suffixes"
   2.125 +    "\n                        (separated by commas)"
   2.126 +    "\n  -R                  - excludes subdirectories"
   2.127 +    "\n  -r                  - includes subdirecotires"
   2.128 +    "\n\n"
   2.129 +    "The default call without any options is:"
   2.130 +    "\n  %s -r ./ -S=\n"
   2.131 +    "That means each file in each subdirectory is counted. If you want to count"
   2.132 +    "\nC source code in your working directory and its subdirectories, type:"
   2.133 +    "\n  %s -s=.c\n";
   2.134 +
   2.135 +  // Program name
   2.136 +  char* prgName = strrchr(argv[0], '/');
   2.137 +  if (prgName == NULL)
   2.138 +  {
   2.139 +    prgName = argv[0];
   2.140 +  }
   2.141 +  else
   2.142 +  {
   2.143 +    prgName++;
   2.144 +  }
   2.145 +
   2.146 +  // Defaults
   2.147 +  char* _suffix = "";
   2.148 +  char* _directory = "./";
   2.149 +
   2.150 +  // Get arguments
   2.151 +  char* directory;
   2.152 +  char* suffix;
   2.153 +  bool showHelp = false;
   2.154 +  recursive = true;
   2.155 +  includeSuffixes = false;
   2.156 +  char checked = 0;
   2.157 +
   2.158 +  for (int t = 1 ; t < argc ; t++)
   2.159 +  {
   2.160 +    if (strncmp(argv[t], "-s=", 3) == 0)
   2.161 +    {
   2.162 +      if ((checked & 1) > 0)
   2.163 +      {
   2.164 +        printf(helpText, prgName, prgName, prgName);
   2.165 +        return -1;
   2.166 +      }
   2.167 +      includeSuffixes = true;
   2.168 +      suffix = argv[t]+3; 
   2.169 +      checked |= 1;
   2.170 +    }
   2.171 +    else if (strncmp(argv[t], "-S=", 3) == 0)
   2.172 +    {
   2.173 +      if ((checked & 1) > 0)
   2.174 +      {
   2.175 +        printf(helpText, prgName, prgName, prgName);
   2.176 +        return -1;
   2.177 +      }
   2.178 +      includeSuffixes = false;
   2.179 +      suffix = argv[t]+3;
   2.180 +      checked |= 1;
   2.181 +    }
   2.182 +    else if (strcmp(argv[t], "-h") == 0 || strcmp(argv[t], "--help") == 0)
   2.183 +    {
   2.184 +      if ((checked & 2) > 0)
   2.185 +      {
   2.186 +        printf(helpText, prgName, prgName, prgName);
   2.187 +        return -1;
   2.188 +      }
   2.189 +      checked |= 2;
   2.190 +      showHelp = true;
   2.191 +    }
   2.192 +    else if (strcmp(argv[t], "-r") == 0)
   2.193 +    {
   2.194 +      if ((checked & 4) > 0)
   2.195 +      {
   2.196 +        printf(helpText, prgName, prgName, prgName);
   2.197 +        return -1;
   2.198 +      }
   2.199 +      checked |= 4;
   2.200 +      recursive = true;
   2.201 +    }
   2.202 +    else if (strcmp(argv[t], "-R") == 0)
   2.203 +    {
   2.204 +      if ((checked & 4) > 0)
   2.205 +      {
   2.206 +        printf(helpText, prgName, prgName, prgName);
   2.207 +        return -1;
   2.208 +      }
   2.209 +      checked |= 4;
   2.210 +      recursive = false;
   2.211 +    }
   2.212 +    else
   2.213 +    {
   2.214 +      if ((checked & 8) > 0)
   2.215 +      {
   2.216 +        printf(helpText, prgName, prgName, prgName);
   2.217 +        return -1;
   2.218 +      }
   2.219 +      checked |= 8;      
   2.220 +      directory = argv[t];
   2.221 +    }
   2.222 +  }
   2.223 +
   2.224 +  // Show help and quit
   2.225 +  if (showHelp)
   2.226 +  {
   2.227 +    printf(helpText, prgName, prgName, prgName);
   2.228 +    return 0;
   2.229 +  }
   2.230 +
   2.231 +  // Default values
   2.232 +  if ((checked & 1) == 0)
   2.233 +  {
   2.234 +    suffix = _suffix;
   2.235 +  }
   2.236 +
   2.237 +  if ((checked & 8) == 0)
   2.238 +  {
   2.239 +    directory = _directory;
   2.240 +  }
   2.241 +
   2.242 +  // Find tokens
   2.243 +  char* finder;
   2.244 +  suffixc = 1;
   2.245 +  finder = strchr(suffix, ',');
   2.246 +  while (finder != NULL)
   2.247 +  {
   2.248 +    suffixc++;
   2.249 +    finder = strchr(finder+1, ',');
   2.250 +  }
   2.251 +  suffixv = (char**) malloc(sizeof(suffixv)*suffixc);
   2.252 +  if (suffixv == NULL)
   2.253 +  {
   2.254 +    fprintf(stderr, "Memory allocation failed.\n");
   2.255 +    return 1;
   2.256 +  }
   2.257 +  finder = strtok(suffix, ",");
   2.258 +  int c = 0;
   2.259 +  while (finder != NULL)
   2.260 +  {
   2.261 +    suffixv[c] = finder;
   2.262 +    c++;
   2.263 +    finder = strtok(NULL, ",");
   2.264 +  }
   2.265 +
   2.266 +  // Open directory
   2.267 +  DIR *dir = opendir(directory);
   2.268 +  if (dir == NULL)
   2.269 +  {
   2.270 +    perror("Operation failed");
   2.271 +    free(suffixv);
   2.272 +    return 1;
   2.273 +  }
   2.274 +  
   2.275 +  // Scan directory
   2.276 +  int lines = scanDirectory(dir, 0, directory);
   2.277 +
   2.278 +  // Print double line and line count
   2.279 +  for (int t = 0 ; t < 80 ; t++)
   2.280 +  {
   2.281 +    printf("=");
   2.282 +  }
   2.283 +  printf("\n%74d lines\n", lines);
   2.284 +
   2.285 +  closedir(dir);
   2.286 +  free(suffixv);
   2.287 +  return 0;
   2.288 +}
     3.1 Binary file cline.exe has changed

mercurial