# HG changeset patch # User Mike Becker # Date 1316086146 -7200 # Node ID ecf787666f44f25465378bd33095074733cc425b # Parent 28319b20968cb9e7beec94558955bdef14564405 refactored sources diff -r 28319b20968c -r ecf787666f44 .cproject --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/.cproject Thu Sep 15 13:29:06 2011 +0200 @@ -0,0 +1,63 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff -r 28319b20968c -r ecf787666f44 .hgignore --- a/.hgignore Mon May 30 08:45:08 2011 +0200 +++ b/.hgignore Thu Sep 15 13:29:06 2011 +0200 @@ -1,5 +1,3 @@ syntax: regexp -^[^.]*\.o$ -^[^.]*\.exe$ -^cline$ +^build/.*$ diff -r 28319b20968c -r ecf787666f44 .project --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/.project Thu Sep 15 13:29:06 2011 +0200 @@ -0,0 +1,78 @@ + + + cline + + + + + + org.eclipse.cdt.managedbuilder.core.genmakebuilder + clean,full,incremental, + + + ?name? + + + + org.eclipse.cdt.make.core.append_environment + true + + + org.eclipse.cdt.make.core.autoBuildTarget + all + + + org.eclipse.cdt.make.core.buildArguments + + + + org.eclipse.cdt.make.core.buildCommand + make + + + org.eclipse.cdt.make.core.cleanBuildTarget + clean + + + org.eclipse.cdt.make.core.contents + org.eclipse.cdt.make.core.activeConfigSettings + + + org.eclipse.cdt.make.core.enableAutoBuild + false + + + org.eclipse.cdt.make.core.enableCleanBuild + true + + + org.eclipse.cdt.make.core.enableFullBuild + true + + + org.eclipse.cdt.make.core.fullBuildTarget + all + + + org.eclipse.cdt.make.core.stopOnError + true + + + org.eclipse.cdt.make.core.useDefaultBuildCmd + true + + + + + org.eclipse.cdt.managedbuilder.core.ScannerConfigBuilder + + + + + + org.eclipse.cdt.core.cnature + org.eclipse.cdt.core.ccnature + org.eclipse.cdt.managedbuilder.core.managedBuildNature + org.eclipse.cdt.managedbuilder.core.ScannerConfigNature + + diff -r 28319b20968c -r ecf787666f44 Makefile --- a/Makefile Mon May 30 08:45:08 2011 +0200 +++ b/Makefile Thu Sep 15 13:29:06 2011 +0200 @@ -1,11 +1,14 @@ CC = gcc -OBJ = cline.o functions.o +BUILDDIR = build +OBJ = ${BUILDDIR}/cline.o ${BUILDDIR}/scanner.o ${BUILDDIR}/settings.o ${BUILDDIR}/suffix_fnc.o ${BUILDDIR}/suffix_list.o ${BUILDDIR}/arguments.o +BIN = ${BUILDDIR}/cline -cline: ${OBJ} - ${CC} -o cline ${OBJ} +all: ${OBJ} + ${CC} -o ${BIN} ${OBJ} -%.o: %.c - ${CC} -c -std=c99 $< +${BUILDDIR}/%.o: %.c + mkdir -p ${BUILDDIR} + ${CC} -c -std=c99 -o ${BUILDDIR}/$*.o $< clean: - rm *.o + rm build/* diff -r 28319b20968c -r ecf787666f44 arguments.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/arguments.c Thu Sep 15 13:29:06 2011 +0200 @@ -0,0 +1,29 @@ +/* + * arguments.c + * + * Created on: 15.09.2011 + * Author: beckermi + */ + +#include "arguments.h" + +int checkArgument(const char* arg, const char* expected) { + int len = strlen(expected); + int ret = 0; + + if (arg[0] == '-') { + if (arg[1] != '-') { + for (int t = 0 ; t < len ; t++) { + ret |= (strchr(arg, expected[t]) > 0) << t; + } + } + } + + return ret; +} + +bool registerArgument(int* reg, int mask) { + bool ret = (*reg & mask) > 0; + *reg |= mask; + return ret; +} diff -r 28319b20968c -r ecf787666f44 arguments.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/arguments.h Thu Sep 15 13:29:06 2011 +0200 @@ -0,0 +1,24 @@ +/* + * arguments.h + * + * Created on: 15.09.2011 + * Author: beckermi + */ + +#ifndef ARGUMENTS_H_ +#define ARGUMENTS_H_ + +#include "stdinc.h" + +#ifdef _cplusplus +extern "C" { +#endif + +int checkArgument(const char*, const char*); +bool registerArgument(int*, int); + +#ifdef _cplusplus +} +#endif + +#endif /* ARGUMENTS_H_ */ diff -r 28319b20968c -r ecf787666f44 cline.c --- a/cline.c Mon May 30 08:45:08 2011 +0200 +++ b/cline.c Thu Sep 15 13:29:06 2011 +0200 @@ -1,50 +1,13 @@ +/* + * cline.c + * + * Created on: 23.05.2011 + * Author: beckermi + */ + #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; -} - -void destroy_suffix_list_t(suffix_list_t* list) { - while (--list->count >= 0) { - free(list->items[list->count]); - } - free(list); -} - -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) { - destroy_suffix_list_t(settings->suffixList); - free(settings); -} +#include "scanner.h" +#include "settings.h" void printHelpText(const char* prgName) { // Help text @@ -132,10 +95,7 @@ } // h if ((argflags & 1) > 0 || strcmp(argv[t], "--help") == 0) { - if (registerArgument(&checked, 1)) { - return exit_with_help(prgName, settings, 1); - } - showHelp = true; + return exit_with_help(prgName, settings, 1); } // r, R if ((argflags & 24) > 0) { @@ -160,11 +120,6 @@ } } - // Show help and quit - if (showHelp) { - return exit_with_help(prgName, settings, 0); - } - // Find tokens char* finder = strtok(suffix, ","); while (finder != NULL) { diff -r 28319b20968c -r ecf787666f44 cline.h --- a/cline.h Mon May 30 08:45:08 2011 +0200 +++ b/cline.h Thu Sep 15 13:29:06 2011 +0200 @@ -1,39 +1,25 @@ -#ifndef _CLINE_H -#define _CLINE_H +/* + * cline.h + * + * Created on: 23.05.2011 + * Author: beckermi + */ -#include -#include -#include -#include -#include +#ifndef CLINE_H_ +#define CLINE_H_ - -typedef struct _suffix_list { - int count; - char** items; -} suffix_list_t; - -typedef struct _settings { - char fileSeparator; - suffix_list_t* suffixList; - bool recursive; - bool includeSuffixes; - bool matchesOnly; -} settings_t; +#include "stdinc.h" +#include "settings.h" #ifdef _cplusplus extern "C" { #endif -settings_t* new_settings_t(); -void destroy_settings_t(settings_t*); -suffix_list_t* new_suffix_list_t(); -void destroy_suffix_list_t(suffix_list_t*); -void add_suffix(suffix_list_t*, char*); void printHelpText(const char*); int exit_with_help(char*, settings_t*, int); + #ifdef _cplusplus } #endif -#endif /* _CLINE_H */ +#endif /* CLINE_H_ */ diff -r 28319b20968c -r ecf787666f44 functions.c --- a/functions.c Mon May 30 08:45:08 2011 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,117 +0,0 @@ -#include "cline.h" -#include "functions.h" - -int checkArgument(const char* arg, const char* expected) { - int len = strlen(expected); - int ret = 0; - - if (arg[0] == '-') { - if (arg[1] != '-') { - for (int t = 0 ; t < len ; t++) { - ret |= (strchr(arg, expected[t]) > 0) << t; - } - } - } - - return ret; -} - -bool registerArgument(int* reg, int mask) { - bool ret = (*reg & mask) > 0; - *reg |= mask; - return ret; -} - -bool testSuffix(char* filename, settings_t* settings) { - bool ret = false; - int tokenlen, fnamelen = strlen(filename); - for (int t = 0 ; t < settings->suffixList->count ; t++) { - tokenlen = strlen(settings->suffixList->items[t]); - if (fnamelen >= tokenlen && tokenlen > 0) { - if (strncmp(filename+fnamelen-tokenlen, - settings->suffixList->items[t], tokenlen) == 0) { - ret = true; - break; - } - } - } - return ret ^ !settings->includeSuffixes; -} - -int scanDirectory(DIR *dir, const int spaces, - char* currdir, settings_t* settings) { - DIR *subdir; - char* subdirname; - struct dirent *entry; - int lines, digits, a; - int lineSum = 0; - - while ((entry = readdir(dir)) != NULL) { - if (strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0) { - // Print occurence - char entryname[strlen(entry->d_name)+spaces]; - for (int t = 0 ; t < spaces ; t++) { - entryname[t]=' '; - } - entryname[spaces] = 0; - strcat(entryname, entry->d_name); - - // Check for subdirectory - char subdirname[(1+strlen(currdir)+strlen(entry->d_name))]; - strcpy(subdirname, currdir); - strncat(subdirname, &settings->fileSeparator, 1); - strcat(subdirname, entry->d_name); - if ((subdir = opendir(subdirname)) != NULL) { - printf("%-60s\n", entryname); - if (settings->recursive) { - lineSum += scanDirectory(subdir, spaces+1, subdirname, settings); - } - closedir(subdir); - continue; - } - - // Count lines - lines = 0; - char filename[(1+strlen(currdir)+strlen(entry->d_name))]; - strcpy(filename, currdir); - strncat(filename, &settings->fileSeparator, 1); - strcat(filename, entry->d_name); - if (testSuffix(filename, settings)) { - FILE *file = fopen(filename, "r"); - if (file == NULL) { - perror(" File acces failed"); - continue; - } - - do { - a = fgetc(file); - - if (a == 10) { - lines++; - } - } while (a != EOF); - fclose(file); - - // Print line count - #ifdef _WIN32 - printf("%-60s%13d lines\n", entryname, lines); - #else - printf("%-60s%14d lines\n", entryname, lines); - #endif /* _WIN32 */ - - lineSum += lines; - } - else { - if (!settings->matchesOnly) { - // Print hint - #ifdef _WIN32 - printf("%-60s%19s\n", entryname, "no match"); - #else - printf("%-60s%20s\n", entryname, "no match"); - #endif /* _WIN32 */ - } - } - } - } - return lineSum; -} diff -r 28319b20968c -r ecf787666f44 functions.h --- a/functions.h Mon May 30 08:45:08 2011 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,18 +0,0 @@ -#ifndef _CLINE_FUNCTIONS_H -#define _CLINE_FUNCTIONS_H - -#ifdef _cplusplus -extern "C" { -#endif - -int checkArgument(const char*, const char*); -bool registerArgument(int*, int); -bool testSuffix(char* filename, settings_t* settings); -int scanDirectory(DIR *dir, const int spaces, - char* currdir, settings_t* settings); - -#ifdef _cplusplus -} -#endif - -#endif /* _CLINE_FUNCTIONS_H */ diff -r 28319b20968c -r ecf787666f44 scanner.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/scanner.c Thu Sep 15 13:29:06 2011 +0200 @@ -0,0 +1,88 @@ +/* + * functions.c + * + * Created on: 23.05.2011 + * Author: beckermi + */ + + +#include "scanner.h" +#include "suffix_fnc.h" + +int scanDirectory(DIR *dir, const int spaces, + char* currdir, settings_t* settings) { + DIR *subdir; + char* subdirname; + struct dirent *entry; + int lines, digits, a; + int lineSum = 0; + + while ((entry = readdir(dir)) != NULL) { + if (strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0) { + // Print occurence + char entryname[strlen(entry->d_name)+spaces]; + for (int t = 0 ; t < spaces ; t++) { + entryname[t]=' '; + } + entryname[spaces] = 0; + strcat(entryname, entry->d_name); + + // Check for subdirectory + char subdirname[(1+strlen(currdir)+strlen(entry->d_name))]; + strcpy(subdirname, currdir); + strncat(subdirname, &settings->fileSeparator, 1); + strcat(subdirname, entry->d_name); + if ((subdir = opendir(subdirname)) != NULL) { + printf("%-60s\n", entryname); + if (settings->recursive) { + lineSum += scanDirectory(subdir, spaces+1, subdirname, settings); + } + closedir(subdir); + continue; + } + + // Count lines + lines = 0; + char filename[(1+strlen(currdir)+strlen(entry->d_name))]; + strcpy(filename, currdir); + strncat(filename, &settings->fileSeparator, 1); + strcat(filename, entry->d_name); + if (testSuffix(filename, settings)) { + FILE *file = fopen(filename, "r"); + if (file == NULL) { + perror(" File acces failed"); + continue; + } + + do { + a = fgetc(file); + + if (a == 10) { + lines++; + } + } while (a != EOF); + fclose(file); + + // Print line count + #ifdef _WIN32 + printf("%-60s%13d lines\n", entryname, lines); + #else + printf("%-60s%14d lines\n", entryname, lines); + #endif /* _WIN32 */ + + lineSum += lines; + } + else { + if (!settings->matchesOnly) { + // Print hint + #ifdef _WIN32 + printf("%-60s%19s\n", entryname, "no match"); + #else + printf("%-60s%20s\n", entryname, "no match"); + #endif /* _WIN32 */ + } + } + } + } + return lineSum; +} diff -r 28319b20968c -r ecf787666f44 scanner.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/scanner.h Thu Sep 15 13:29:06 2011 +0200 @@ -0,0 +1,25 @@ +/* + * functions.h + * + * Created on: 23.05.2011 + * Author: beckermi + */ + +#ifndef SCANNER_H_ +#define SCANNER_H_ + +#include "stdinc.h" +#include "settings.h" + +#ifdef _cplusplus +extern "C" { +#endif + +int scanDirectory(DIR *dir, const int spaces, + char* currdir, settings_t* settings); + +#ifdef _cplusplus +} +#endif + +#endif /* SCANNER_H_ */ diff -r 28319b20968c -r ecf787666f44 settings.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/settings.c Thu Sep 15 13:29:06 2011 +0200 @@ -0,0 +1,30 @@ +/* + * settings.c + * + * Created on: 15.09.2011 + * Author: beckermi + */ + +#include "settings.h" + +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) { + destroy_suffix_list_t(settings->suffixList); + free(settings); +} diff -r 28319b20968c -r ecf787666f44 settings.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/settings.h Thu Sep 15 13:29:06 2011 +0200 @@ -0,0 +1,33 @@ +/* + * settings.h + * + * Created on: 15.09.2011 + * Author: beckermi + */ + +#ifndef SETTINGS_H_ +#define SETTINGS_H_ + +#include "stdinc.h" +#include "suffix_list.h" + +typedef struct _settings { + char fileSeparator; + suffix_list_t* suffixList; + bool recursive; + bool includeSuffixes; + bool matchesOnly; +} settings_t; + +#ifdef _cplusplus +extern "C" { +#endif + +settings_t* new_settings_t(); +void destroy_settings_t(settings_t*); + +#ifdef _cplusplus +} +#endif + +#endif /* SETTINGS_H_ */ diff -r 28319b20968c -r ecf787666f44 stdinc.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/stdinc.h Thu Sep 15 13:29:06 2011 +0200 @@ -0,0 +1,17 @@ +/* + * stdinc.h + * + * Created on: 15.09.2011 + * Author: beckermi + */ + +#ifndef STDINC_H_ +#define STDINC_H_ + +#include +#include +#include +#include +#include + +#endif /* STDINC_H_ */ diff -r 28319b20968c -r ecf787666f44 suffix_fnc.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/suffix_fnc.c Thu Sep 15 13:29:06 2011 +0200 @@ -0,0 +1,25 @@ +/* + * suffix_fnc.c + * + * Created on: 15.09.2011 + * Author: beckermi + */ + +#include "suffix_fnc.h" + +bool testSuffix(char* filename, settings_t* settings) { + bool ret = false; + int tokenlen, fnamelen = strlen(filename); + for (int t = 0 ; t < settings->suffixList->count ; t++) { + tokenlen = strlen(settings->suffixList->items[t]); + if (fnamelen >= tokenlen && tokenlen > 0) { + if (strncmp(filename+fnamelen-tokenlen, + settings->suffixList->items[t], tokenlen) == 0) { + ret = true; + break; + } + } + } + return ret ^ !settings->includeSuffixes; +} + diff -r 28319b20968c -r ecf787666f44 suffix_fnc.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/suffix_fnc.h Thu Sep 15 13:29:06 2011 +0200 @@ -0,0 +1,16 @@ +/* + * suffix_fnc.h + * + * Created on: 15.09.2011 + * Author: beckermi + */ + +#ifndef SUFFIX_FNC_H_ +#define SUFFIX_FNC_H_ + +#include "stdinc.h" +#include "settings.h" + +bool testSuffix(char*, settings_t*); + +#endif /* SUFFIX_FNC_H_ */ diff -r 28319b20968c -r ecf787666f44 suffix_list.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/suffix_list.c Thu Sep 15 13:29:06 2011 +0200 @@ -0,0 +1,34 @@ +/* + * suffix_list.c + * + * Created on: 15.09.2011 + * Author: beckermi + */ + +#include "suffix_list.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 destroy_suffix_list_t(suffix_list_t* list) { + while (--list->count >= 0) { + free(list->items[list->count]); + } + free(list); +} + +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++; + } +} + diff -r 28319b20968c -r ecf787666f44 suffix_list.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/suffix_list.h Thu Sep 15 13:29:06 2011 +0200 @@ -0,0 +1,30 @@ +/* + * suffix_list.h + * + * Created on: 15.09.2011 + * Author: beckermi + */ + +#ifndef SUFFIX_LIST_H_ +#define SUFFIX_LIST_H_ + +#include "stdinc.h" + +typedef struct _suffix_list { + int count; + char** items; +} suffix_list_t; + +#ifdef _cplusplus +extern "C" { +#endif + +suffix_list_t* new_suffix_list_t(); +void destroy_suffix_list_t(suffix_list_t*); +void add_suffix(suffix_list_t*, char*); + +#ifdef _cplusplus +} +#endif + +#endif /* SUFFIX_LIST_H_ */