# HG changeset patch # User Mike Becker # Date 1356705868 -3600 # Node ID fa9bda32de17fc5722386d2b7b897121e8593549 # Parent 1a2d7298bc8236f30f4ca6cfec9816b163e1287d moved src files to src subdirectory and added licence text diff -r 1a2d7298bc82 -r fa9bda32de17 .hgignore --- a/.hgignore Tue Oct 02 10:49:25 2012 +0200 +++ b/.hgignore Fri Dec 28 15:44:28 2012 +0100 @@ -1,3 +1,8 @@ syntax: regexp ^build/.*$ +\.orig\..*$ +\.orig$ +\.chg\..*$ +\.rej$ +\.conflict\~$ diff -r 1a2d7298bc82 -r fa9bda32de17 Makefile --- a/Makefile Tue Oct 02 10:49:25 2012 +0200 +++ b/Makefile Fri Dec 28 15:44:28 2012 +0100 @@ -38,6 +38,10 @@ #endif VERSION_PREFIX=1.0. +SRCDIR=src/ +BUILDDIR=build/ +OBJ = $(shell ls ${SRCDIR} | grep '\.c' | sed 's/^\([^.]*\)\.c$$/${BUILDDIR:/=\/}\1.o/g' | tr '\n' ' ') +BIN = ${BUILDDIR}cline include ${CONF}.mk @@ -52,17 +56,19 @@ compile: ${OBJ} ${LD} -o ${BIN} ${OBJ} ${LDFLAGS} -setup: - mkdir -p ${BUILDDIR} +setup: ${BUILDDIR} rm -f ${BUILDDIR}cline.o - mv cline.h cline.src - cat cline.src | sed "s/VERSION.*/VERSION=\"${VERSION_PREFIX}$(shell hg identify -n) ($(shell hg identify -i))\";/g" > cline.h + mv ${SRCDIR}cline.h ${SRCDIR}cline.src + cat ${SRCDIR}cline.src | sed "s/VERSION.*/VERSION=\"${VERSION_PREFIX}$(shell hg identify -n) ($(shell hg identify -i))\";/g" > ${SRCDIR}cline.h + +${BUILDDIR}: + mkdir ${BUILDDIR} teardown: - rm -f cline.h - mv cline.src cline.h + rm -f ${SRCDIR}cline.h + mv ${SRCDIR}cline.src ${SRCDIR}cline.h -${BUILDDIR}%.o: %.c +${BUILDDIR}%.o: ${SRCDIR}%.c ${CC} ${CFLAGS} -c -o ${BUILDDIR}$*.o $< clean: diff -r 1a2d7298bc82 -r fa9bda32de17 arguments.c --- a/arguments.c Tue Oct 02 10:49:25 2012 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,45 +0,0 @@ -/* - * arguments.c - * - * Created on: 15.09.2011 - * Author: Mike - */ - -#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; -} - -bool checkParamOpt(int* paropt) { - bool ret = *paropt == 0; - *paropt = 1; - return ret; -} - -void parseCSL(char* csl, string_list_t* list) { - if (csl != NULL) { - char* finder = strtok(csl, ","); - while (finder != NULL) { - add_string(list, finder); - finder = strtok(NULL, ","); - } - } -} diff -r 1a2d7298bc82 -r fa9bda32de17 arguments.h --- a/arguments.h Tue Oct 02 10:49:25 2012 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,27 +0,0 @@ -/* - * arguments.h - * - * Created on: 15.09.2011 - * Author: Mike - */ - -#ifndef ARGUMENTS_H_ -#define ARGUMENTS_H_ - -#include "stdinc.h" -#include "string_list.h" - -#ifdef _cplusplus -extern "C" { -#endif - -int checkArgument(const char*, const char*); -bool checkParamOpt(int*); -bool registerArgument(int*, int); -void parseCSL(char*, string_list_t*); - -#ifdef _cplusplus -} -#endif - -#endif /* ARGUMENTS_H_ */ diff -r 1a2d7298bc82 -r fa9bda32de17 bfile_heuristics.c --- a/bfile_heuristics.c Tue Oct 02 10:49:25 2012 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,57 +0,0 @@ -/* - * bfile_heuristics.c - * - * Created on: 20.10.2011 - * Author: Mike - */ - -#include "bfile_heuristics.h" -#include - -bfile_heuristics_t *new_bfile_heuristics_t() { - bfile_heuristics_t *ret = malloc(sizeof(bfile_heuristics_t)); - ret->level = BFILE_MEDIUM_ACCURACY; - bfile_reset(ret); - return ret; -} - -void destroy_bfile_heuristics_t(bfile_heuristics_t *def) { - free(def); -} - -void bfile_reset(bfile_heuristics_t *def) { - def->bcount = 0; - def->tcount = 0; -} - -bool bfile_check(bfile_heuristics_t *def, int next_char) { - bool ret = false; - if (def->level != BFILE_IGNORE) { - def->tcount++; - if (!isprint(next_char) && !isspace(next_char)) { - def->bcount++; - } - - if (def->tcount > 1) { /* empty files are text files */ - switch (def->level) { - case BFILE_LOW_ACCURACY: - if (def->tcount > 15 || next_char == EOF) { - ret = (1.0*def->bcount)/def->tcount > 0.32; - } - break; - case BFILE_HIGH_ACCURACY: - if (def->tcount > 500 || next_char == EOF) { - ret = (1.0*def->bcount)/def->tcount > 0.1; - } - break; - default: /* BFILE_MEDIUM_ACCURACY */ - if (def->tcount > 100 || next_char == EOF) { - ret = (1.0*def->bcount)/def->tcount > 0.1; - } - break; - } - } - } - - return ret; -} diff -r 1a2d7298bc82 -r fa9bda32de17 bfile_heuristics.h --- a/bfile_heuristics.h Tue Oct 02 10:49:25 2012 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,37 +0,0 @@ -/* - * bfile_heuristics.h - * - * Created on: 20.10.2011 - * Author: Mike - */ - -#ifndef BFILE_HEURISTICS_H_ -#define BFILE_HEURISTICS_H_ - -#include "stdinc.h" - -#define BFILE_IGNORE 0x00 -#define BFILE_LOW_ACCURACY 0x01 -#define BFILE_MEDIUM_ACCURACY 0x02 -#define BFILE_HIGH_ACCURACY 0x04 - -typedef struct { - unsigned int level; - unsigned int bcount; /* 'binary' character count */ - unsigned int tcount; /* total count */ -} bfile_heuristics_t; - -#ifdef _cplusplus -extern "C" { -#endif - -bfile_heuristics_t *new_bfile_heuristics_t(); -void destroy_bfile_heuristics_t(bfile_heuristics_t *def); -void bfile_reset(bfile_heuristics_t *def); -bool bfile_check(bfile_heuristics_t *def, int next_char); - -#ifdef _cplusplus -} -#endif - -#endif /* BFILE_HEURISTICS_H_ */ diff -r 1a2d7298bc82 -r fa9bda32de17 cline.c --- a/cline.c Tue Oct 02 10:49:25 2012 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,247 +0,0 @@ -/* - * cline.c - * - * Created on: 23.05.2011 - * Author: Mike - */ - -#include "cline.h" -#include "scanner.h" -#include "settings.h" -#include "arguments.h" -#include "stream.h" -#include "regex_parser.h" - -void printHelpText() { - const char* helpText = - "\nUsage:" - "\n cline [Options] [Directories...]" - "\n cline [Options] [Directories...]" - "\n\nCounts the line terminator characters (\\n) within all" - " files in the specified\ndirectories." - "\n\nOptions:" - "\n -b - binary file heuristics level (default medium)" - "\n One of: ignore low medium high" - "\n -E - Excludes any line matching the " - "\n -e - Excludes lines between and " - "\n You may use these options multiple times" - "\n -h, --help - this help text" - "\n -m - print information about matching files only" - "\n -s - only count files with these suffixes (separated" - "\n by commas)" - "\n -S - count any file except those with these suffixes" - "\n (separated by commas)" - "\n -r, -R - includes subdirectories" - "\n -v, --version - print out version information" - "\n -V - turn verbose output off, print the result only" - "\n\nShortcuts:" - "\n --exclude-cstyle-comments" - "\n = -E \"\\s*//\" -e \"\\s*/\\*\" \"\\*/\\s*\"" - "\n\n" - "The default call without any options is:" - "\n cline ./\n\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 cline -rs .c\n" - "\nIf you want to exclude comment lines, you may use the -e/-E option." - "\nAfter a line matches the regex pattern any following line is" - "\nnot counted unless a line matches the pattern. A line is still " - "\ncounted when it does not start or end with the respective patterns." - "\nPlease note, that cline does not remove whitespace characters as this" - "\nmight not be reasonable in some cases." - "\n\nExample (C without comments):" - "\n cline -s .c,.h --exclude-cstyle-comments"; - - printf(helpText); -} - -int exit_with_version(settings_t* settings) { - printf("cline - Revision: %s\n", VERSION); - destroy_settings_t(settings); - return 0; -} - -int exit_with_help(settings_t* settings, int code) { - printHelpText(); - 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; - } - - /* Get arguments */ - string_list_t *directories = new_string_list_t(); - if (directories == NULL) { - fprintf(stderr, "Memory allocation failed.\n"); - return 1; - } - char* includeSuffix = NULL; - char* excludeSuffix = NULL; - int checked = 0; - - for (int t = 1 ; t < argc ; t++) { - - int argflags = checkArgument(argv[t], "hsSrRmvVbeE"); - int paropt = 0; - - /* s */ - if ((argflags & 2) > 0) { - if (!checkParamOpt(&paropt) || registerArgument(&checked, 2)) { - return exit_with_help(settings, 1); - } - t++; - if (t >= argc) { - return exit_with_help(settings, 1); - } - includeSuffix = argv[t]; - } - /* S */ - if ((argflags & 4) > 0) { - if (!checkParamOpt(&paropt) || registerArgument(&checked, 4)) { - return exit_with_help(settings, 1); - } - t++; - if (t >= argc) { - return exit_with_help(settings, 1); - } - excludeSuffix = argv[t]; - } - /* h */ - if ((argflags & 1) > 0 || strcmp(argv[t], "--help") == 0) { - return exit_with_help(settings, 0); - } - /* r, R */ - if ((argflags & 24) > 0) { - if (registerArgument(&checked, 24)) { - return exit_with_help(settings, 1); - } - settings->recursive = true; - } - /* m */ - if ((argflags & 32) > 0) { - if (registerArgument(&checked, 32)) { - return exit_with_help(settings, 1); - } - settings->matchesOnly = true; - } - /* v */ - if ((argflags & 64) > 0 || strcmp(argv[t], "--version") == 0) { - return exit_with_version(settings); - } - /* V */ - if ((argflags & 128) > 0) { - if (registerArgument(&checked, 128)) { - return exit_with_help(settings, 1); - } - settings->verbose = false; - } - /* b */ - if ((argflags & 256) > 0) { - if (!checkParamOpt(&paropt) || registerArgument(&checked, 256)) { - return exit_with_help(settings, 1); - } - t++; - if (t >= argc) { - return exit_with_help(settings, 1); - } - if (strcasecmp(argv[t], "ignore") == 0) { - settings->bfileHeuristics->level = BFILE_IGNORE; - } else if (strcasecmp(argv[t], "low") == 0) { - settings->bfileHeuristics->level = BFILE_LOW_ACCURACY; - } else if (strcasecmp(argv[t], "medium") == 0) { - settings->bfileHeuristics->level = BFILE_MEDIUM_ACCURACY; - } else if (strcasecmp(argv[t], "high") == 0) { - settings->bfileHeuristics->level = BFILE_HIGH_ACCURACY; - } else { - return exit_with_help(settings, 1); - } - } - /* e */ - if ((argflags & 512) > 0) { - if (!checkParamOpt(&paropt) || t + 2 >= argc) { - return exit_with_help(settings, 1); - } - t++; add_string(settings->regex->pattern_list, argv[t]); - t++; add_string(settings->regex->pattern_list, argv[t]); - } - /* E */ - if ((argflags & 1024) > 0) { - t++; - if (!checkParamOpt(&paropt) || t >= argc) { - return exit_with_help(settings, 1); - } - add_string(settings->regex->pattern_list, argv[t]); - add_string(settings->regex->pattern_list, "$"); - } - if (argflags == 0) { - /* SHORTCUTS */ - /* exclude-cstyle-comments */ - if (strcmp(argv[t], "--exclude-cstyle-comments") == 0) { - add_string(settings->regex->pattern_list, "\\s*//"); - add_string(settings->regex->pattern_list, "$"); - add_string(settings->regex->pattern_list, "\\s*/\\*"); - add_string(settings->regex->pattern_list, "\\*/\\s*"); - } - /* Path */ - else { - add_string(directories, argv[t]); - } - } - } - - /* Configure output */ - if (!settings->verbose) { - close_stdout(); - } - - /* Find tokens */ - parseCSL(includeSuffix, settings->includeSuffixes); - parseCSL(excludeSuffix, settings->excludeSuffixes); - - /* Scan directories */ - if (regex_compile_all(settings->regex)) { - int lines = 0; - if (directories->count == 0) { - add_string(directories, "."); - } - for (int t = 0 ; t < directories->count ; t++) { - if (t > 0) { - for (int u = 0 ; u < 79 ; u++) { - printf("-"); - } - printf("\n"); - } - lines += scanDirectory((scanner_t){directories->items[t], 0}, settings); - } - destroy_string_list_t(directories); - - /* Print double line and line count */ - for (int t = 0 ; t < 79 ; t++) { - printf("="); - } - printf("\n%73d lines\n", lines); - - if (settings->confusing_lnlen && settings->regex->pattern_list->count > 0) { - printf("\nSome files contain too long lines.\n" - "The regex parser currently supports a maximum line length of %d." - "\nThe result might be wrong.\n", REGEX_MAX_LINELENGTH); - } - - if (!settings->verbose) { - reopen_stdout(); - printf("%d", lines); - } - destroy_settings_t(settings); - } - - fflush(stdout); - fflush(stderr); - return 0; -} diff -r 1a2d7298bc82 -r fa9bda32de17 cline.h --- a/cline.h Tue Oct 02 10:49:25 2012 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,28 +0,0 @@ -/* - * cline.h - * - * Created on: 23.05.2011 - * Author: Mike - */ - -#ifndef CLINE_H_ -#define CLINE_H_ - -const char* VERSION=""; /* will be replaced by makefile */ - -#include "stdinc.h" -#include "settings.h" - -#ifdef _cplusplus -extern "C" { -#endif - -void printHelpText(); -int exit_with_version(settings_t*); -int exit_with_help(settings_t*, int); - -#ifdef _cplusplus -} -#endif - -#endif /* CLINE_H_ */ diff -r 1a2d7298bc82 -r fa9bda32de17 gcc-debug.mk --- a/gcc-debug.mk Tue Oct 02 10:49:25 2012 +0200 +++ b/gcc-debug.mk Fri Dec 28 15:44:28 2012 +0100 @@ -1,34 +1,31 @@ -# -# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. -# -# Copyright 2011 Mike Becker. All rights reserved. -# -# Redistribution and use in source and binary forms, with or without -# modification, are permitted provided that the following conditions are met: -# -# 1. Redistributions of source code must retain the above copyright -# notice, this list of conditions and the following disclaimer. -# -# 2. Redistributions in binary form must reproduce the above copyright -# notice, this list of conditions and the following disclaimer in the -# documentation and/or other materials provided with the distribution. -# -# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" -# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE -# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE -# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE -# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL -# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR -# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER -# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, -# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -# - -CC = gcc -LD = gcc -CFLAGS = -Wall -std=gnu99 -O0 -ggdb -LDFLAGS = -BUILDDIR = build/ -OBJ = $(shell ls | grep '\.c' | sed 's/^\([^.]*\)\.c$$/${BUILDDIR:/=\/}\1.o/g' | tr '\n' ' ') -BIN = ${BUILDDIR}cline +# +# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. +# +# Copyright 2011 Mike Becker. All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions are met: +# +# 1. Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# +# 2. Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE +# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +# + +CC = gcc +LD = gcc +CFLAGS = -Wall -std=gnu99 -O0 -ggdb +LDFLAGS = diff -r 1a2d7298bc82 -r fa9bda32de17 gcc.mk --- a/gcc.mk Tue Oct 02 10:49:25 2012 +0200 +++ b/gcc.mk Fri Dec 28 15:44:28 2012 +0100 @@ -1,34 +1,31 @@ -# -# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. -# -# Copyright 2011 Mike Becker. All rights reserved. -# -# Redistribution and use in source and binary forms, with or without -# modification, are permitted provided that the following conditions are met: -# -# 1. Redistributions of source code must retain the above copyright -# notice, this list of conditions and the following disclaimer. -# -# 2. Redistributions in binary form must reproduce the above copyright -# notice, this list of conditions and the following disclaimer in the -# documentation and/or other materials provided with the distribution. -# -# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" -# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE -# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE -# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE -# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL -# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR -# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER -# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, -# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -# - -CC = gcc -LD = gcc -CFLAGS = -Wall -std=gnu99 -O -LDFLAGS = -BUILDDIR = build/ -OBJ = $(shell ls | grep '\.c' | sed 's/^\([^.]*\)\.c$$/${BUILDDIR:/=\/}\1.o/g' | tr '\n' ' ') -BIN = ${BUILDDIR}cline +# +# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. +# +# Copyright 2011 Mike Becker. All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions are met: +# +# 1. Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# +# 2. Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE +# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +# + +CC = gcc +LD = gcc +CFLAGS = -Wall -std=gnu99 -O +LDFLAGS = diff -r 1a2d7298bc82 -r fa9bda32de17 mingw-debug.mk --- a/mingw-debug.mk Tue Oct 02 10:49:25 2012 +0200 +++ b/mingw-debug.mk Fri Dec 28 15:44:28 2012 +0100 @@ -1,34 +1,31 @@ -# -# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. -# -# Copyright 2011 Mike Becker. All rights reserved. -# -# Redistribution and use in source and binary forms, with or without -# modification, are permitted provided that the following conditions are met: -# -# 1. Redistributions of source code must retain the above copyright -# notice, this list of conditions and the following disclaimer. -# -# 2. Redistributions in binary form must reproduce the above copyright -# notice, this list of conditions and the following disclaimer in the -# documentation and/or other materials provided with the distribution. -# -# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" -# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE -# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE -# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE -# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL -# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR -# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER -# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, -# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -# - -CC = gcc -LD = gcc -CFLAGS = -Wall -std=gnu99 -O0 -g -LDFLAGS = -static -lregex -BUILDDIR = build/ -OBJ = $(shell ls | grep '\.c' | sed 's/^\([^.]*\)\.c$$/${BUILDDIR:/=\/}\1.o/g' | tr '\n' ' ') -BIN = ${BUILDDIR}cline +# +# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. +# +# Copyright 2011 Mike Becker. All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions are met: +# +# 1. Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# +# 2. Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE +# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +# + +CC = gcc +LD = gcc +CFLAGS = -Wall -std=gnu99 -O0 -g +LDFLAGS = -static -lregex diff -r 1a2d7298bc82 -r fa9bda32de17 mingw.mk --- a/mingw.mk Tue Oct 02 10:49:25 2012 +0200 +++ b/mingw.mk Fri Dec 28 15:44:28 2012 +0100 @@ -1,34 +1,31 @@ -# -# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. -# -# Copyright 2011 Mike Becker. All rights reserved. -# -# Redistribution and use in source and binary forms, with or without -# modification, are permitted provided that the following conditions are met: -# -# 1. Redistributions of source code must retain the above copyright -# notice, this list of conditions and the following disclaimer. -# -# 2. Redistributions in binary form must reproduce the above copyright -# notice, this list of conditions and the following disclaimer in the -# documentation and/or other materials provided with the distribution. -# -# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" -# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE -# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE -# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE -# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL -# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR -# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER -# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, -# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -# - -CC = gcc -LD = gcc -CFLAGS = -Wall -std=gnu99 -O -LDFLAGS = -static -lregex -BUILDDIR = build/ -OBJ = $(shell ls | grep '\.c' | sed 's/^\([^.]*\)\.c$$/${BUILDDIR:/=\/}\1.o/g' | tr '\n' ' ') -BIN = ${BUILDDIR}cline +# +# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. +# +# Copyright 2011 Mike Becker. All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions are met: +# +# 1. Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# +# 2. Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE +# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +# + +CC = gcc +LD = gcc +CFLAGS = -Wall -std=gnu99 -O +LDFLAGS = -static -lregex diff -r 1a2d7298bc82 -r fa9bda32de17 nbproject/Package-Default.bash --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/nbproject/Package-Default.bash Fri Dec 28 15:44:28 2012 +0100 @@ -0,0 +1,75 @@ +#!/bin/bash -x + +# +# Generated - do not edit! +# + +# Macros +TOP=`pwd` +CND_PLATFORM=GNU-Linux-x86 +CND_CONF=Default +CND_DISTDIR=dist +CND_BUILDDIR=build +NBTMPDIR=${CND_BUILDDIR}/${CND_CONF}/${CND_PLATFORM}/tmp-packaging +TMPDIRNAME=tmp-packaging +OUTPUT_PATH=MissingOutputInProject +OUTPUT_BASENAME=MissingOutputInProject +PACKAGE_TOP_DIR=cline/ + +# Functions +function checkReturnCode +{ + rc=$? + if [ $rc != 0 ] + then + exit $rc + fi +} +function makeDirectory +# $1 directory path +# $2 permission (optional) +{ + mkdir -p "$1" + checkReturnCode + if [ "$2" != "" ] + then + chmod $2 "$1" + checkReturnCode + fi +} +function copyFileToTmpDir +# $1 from-file path +# $2 to-file path +# $3 permission +{ + cp "$1" "$2" + checkReturnCode + if [ "$3" != "" ] + then + chmod $3 "$2" + checkReturnCode + fi +} + +# Setup +cd "${TOP}" +mkdir -p ${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM}/package +rm -rf ${NBTMPDIR} +mkdir -p ${NBTMPDIR} + +# Copy files and create directories and links +cd "${TOP}" +makeDirectory "${NBTMPDIR}/cline" +copyFileToTmpDir "${OUTPUT_PATH}" "${NBTMPDIR}/${PACKAGE_TOP_DIR}bin/${OUTPUT_BASENAME}" 0755 + + +# Generate tar file +cd "${TOP}" +rm -f ${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM}/package/cline.tar +cd ${NBTMPDIR} +tar -vcf ../../../../${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM}/package/cline.tar * +checkReturnCode + +# Cleanup +cd "${TOP}" +rm -rf ${NBTMPDIR} diff -r 1a2d7298bc82 -r fa9bda32de17 nbproject/configurations.xml --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/nbproject/configurations.xml Fri Dec 28 15:44:28 2012 +0100 @@ -0,0 +1,57 @@ + + + + + + + arguments.c + arguments.h + bfile_heuristics.c + bfile_heuristics.h + cline.c + cline.h + regex_parser.c + regex_parser.h + scanner.c + scanner.h + settings.c + settings.h + stdinc.h + stream.c + stream.h + string_list.c + string_list.h + suffix_fnc.c + suffix_fnc.h + + + Makefile + + + ^(nbproject)$ + + . + + Makefile + + + + LOCAL_SOURCES + default + + + + + + . + ${MAKE} -f Makefile + ${MAKE} -f Makefile clean + + + + + + diff -r 1a2d7298bc82 -r fa9bda32de17 nbproject/private/Default.properties --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/nbproject/private/Default.properties Fri Dec 28 15:44:28 2012 +0100 @@ -0,0 +1,9 @@ +/home/mike/workspace/c/cline/bfile_heuristics.c=/home/mike/workspace/c/cline#-Wall -std=gnu99 -O -c -o build/bfile_heuristics.o bfile_heuristics.c +/home/mike/workspace/c/cline/suffix_fnc.c=/home/mike/workspace/c/cline#-Wall -std=gnu99 -O -c -o build/suffix_fnc.o suffix_fnc.c +/home/mike/workspace/c/cline/regex_parser.c=/home/mike/workspace/c/cline#-Wall -std=gnu99 -O -c -o build/regex_parser.o regex_parser.c +/home/mike/workspace/c/cline/stream.c=/home/mike/workspace/c/cline#-Wall -std=gnu99 -O -c -o build/stream.o stream.c +/home/mike/workspace/c/cline/settings.c=/home/mike/workspace/c/cline#-Wall -std=gnu99 -O -c -o build/settings.o settings.c +/home/mike/workspace/c/cline/scanner.c=/home/mike/workspace/c/cline#-Wall -std=gnu99 -O -c -o build/scanner.o scanner.c +/home/mike/workspace/c/cline/arguments.c=/home/mike/workspace/c/cline#-Wall -std=gnu99 -O -c -o build/arguments.o arguments.c +/home/mike/workspace/c/cline/cline.c=/home/mike/workspace/c/cline#-Wall -std=gnu99 -O -c -o build/cline.o cline.c +/home/mike/workspace/c/cline/string_list.c=/home/mike/workspace/c/cline#-Wall -std=gnu99 -O -c -o build/string_list.o string_list.c diff -r 1a2d7298bc82 -r fa9bda32de17 nbproject/private/configurations.xml --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/nbproject/private/configurations.xml Fri Dec 28 15:44:28 2012 +0100 @@ -0,0 +1,39 @@ + + + Makefile + + + + localhost + 2 + + + + + + + + + + + + + + + gdb + + + + "${OUTPUT_PATH}" + + "${OUTPUT_PATH}" + . + true + 0 + 0 + + + + + + diff -r 1a2d7298bc82 -r fa9bda32de17 nbproject/private/private.xml --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/nbproject/private/private.xml Fri Dec 28 15:44:28 2012 +0100 @@ -0,0 +1,10 @@ + + + + true + + + 0 + 0 + + diff -r 1a2d7298bc82 -r fa9bda32de17 nbproject/project.xml --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/nbproject/project.xml Fri Dec 28 15:44:28 2012 +0100 @@ -0,0 +1,23 @@ + + + org.netbeans.modules.cnd.makeproject + + + cline + c + + h + UTF-8 + + + . + + + + Default + 0 + + + + + diff -r 1a2d7298bc82 -r fa9bda32de17 regex_parser.c --- a/regex_parser.c Tue Oct 02 10:49:25 2012 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,110 +0,0 @@ -/* - * regex_parser.c - * - * Created on: 26.01.2012 - * Author: Mike - */ - -#include "regex_parser.h" - -regex_parser_t* new_regex_parser_t() { - regex_parser_t* ret = malloc(sizeof(regex_parser_t)); - if (ret != NULL) { - ret->pattern_list = new_string_list_t(); - ret->matched_lines = 0; - ret->pattern_match = 0; - ret->compiled_patterns = NULL; - ret->compiled_pattern_count = 0; - } - return ret; -} - -void regex_destcomppats(regex_parser_t* parser) { - if (parser->compiled_patterns != NULL) { - for (int i = 0 ; i < parser->compiled_pattern_count ; i++) { - if (parser->compiled_patterns[i] != NULL) { - free(parser->compiled_patterns[i]); - } - } - free(parser->compiled_patterns); - parser->compiled_patterns = NULL; - parser->compiled_pattern_count = 0; - } -} - -void destroy_regex_parser_t(regex_parser_t* parser) { - regex_destcomppats(parser); - destroy_string_list_t(parser->pattern_list); - free(parser); -} - -bool regex_parser_matching(regex_parser_t* parser) { - return parser->pattern_match > 0; -} - -int regex_parser_do(regex_parser_t* parser, char* input) { - int err = REG_NOMATCH; - if (parser->compiled_pattern_count > 0) { - regmatch_t match; - - if (regex_parser_matching(parser)) { - parser->matched_lines++; - - err = regexec(parser->compiled_patterns[parser->pattern_match], - input, 1, &match, 0); - if (err > 0 && err != REG_NOMATCH) { - fprintf(stderr, "Regex-Error: 0x%08x", err); - } - if (err == 0) { - parser->pattern_match = 0; - /* do not match line, if it does not end with the pattern */ - if (match.rm_eo < strlen(input)) { - parser->matched_lines--; - } - } - } else { - for (int i = 0 ; i < parser->compiled_pattern_count - 1 ; i += 2) { - err = regexec(parser->compiled_patterns[i], input, 1, &match, 0); - if (err > 0 && err != REG_NOMATCH) { - fprintf(stderr, "Regex-Error: 0x%08x", err); - } - if (err == 0) { - parser->pattern_match = i+1; - parser->matched_lines = 0; - /* Check, if end pattern is also in this line */ - regex_parser_do(parser, input); - /* do not match line, if it does not start with the pattern */ - if (match.rm_so > 0 && parser->matched_lines > 0) { - parser->matched_lines--; - } - break; - } - } - } - } - return err; -} - -bool regex_compile_all(regex_parser_t* parser) { - bool success = true; - size_t pcount = parser->pattern_list->count; - if (pcount > 0) { - regex_destcomppats(parser); - parser->compiled_patterns = calloc(pcount, sizeof(regex_t)); - parser->compiled_pattern_count = pcount; - - regex_t* re; - for (int i = 0 ; i < pcount ; i++) { - re = malloc(sizeof(regex_t)); - if (regcomp(re, parser->pattern_list->items[i], REG_EXTENDED) == 0) { - parser->compiled_patterns[i] = re; - } else { - fprintf(stderr, "Cannot compile pattern: %s\n", - (parser->pattern_list->items[i])); - parser->compiled_patterns[i] = NULL; - success = false; - } - } - } - return success; -} diff -r 1a2d7298bc82 -r fa9bda32de17 regex_parser.h --- a/regex_parser.h Tue Oct 02 10:49:25 2012 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,42 +0,0 @@ -/* - * regex_parser.h - * - * Created on: 26.01.2012 - * Author: Mike - */ - -#ifndef REGEX_PARSER_H_ -#define REGEX_PARSER_H_ - -#define REGEX_MAX_LINELENGTH 2048 - -#include -#include -#include -#include "string_list.h" - -typedef struct { - string_list_t* pattern_list; /* even entries: start ; odd entries: end */ - regex_t** compiled_patterns; - size_t compiled_pattern_count; - unsigned int pattern_match; /* save position of end pattern to match - - NULL when a start pattern shall match first */ - unsigned int matched_lines; -} regex_parser_t; - -#ifdef _cplusplus -extern "C" { -#endif - -regex_parser_t* new_regex_parser_t(); -void destroy_regex_parser_t(regex_parser_t*); - -bool regex_parser_matching(regex_parser_t*); -bool regex_compile_all(regex_parser_t*); -int regex_parser_do(regex_parser_t*, char*); - -#ifdef _cplusplus -} -#endif - -#endif /* REGEX_PARSER_H_ */ diff -r 1a2d7298bc82 -r fa9bda32de17 scanner.c --- a/scanner.c Tue Oct 02 10:49:25 2012 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,126 +0,0 @@ -/* - * scanner.c - * - * Created on: 23.05.2011 - * Author: Mike - */ - - -#include "scanner.h" -#include "suffix_fnc.h" -#include "bfile_heuristics.h" -#include "regex_parser.h" -#include - -int scanDirectory(scanner_t scanner, settings_t* settings) { - - DIR *dirf; - struct dirent *entry; - int lines, a; - int lineSum = 0; - bool bfile; - struct stat statbuf; - - if ((dirf = opendir(scanner.dir)) == NULL) { - printf(scanner.dir); - perror(" Directory access failed"); - return 0; - } - - while ((entry = readdir(dirf)) != NULL) { - if (strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0) { - /* Construct tree view and absolute pathname strings */ - char entryname[strlen(entry->d_name)+scanner.spaces]; - for (int t = 0 ; t < scanner.spaces ; t++) { - entryname[t]=' '; - } - entryname[scanner.spaces] = 0; - strcat(entryname, entry->d_name); - - char filename[(1+strlen(scanner.dir)+strlen(entry->d_name))]; - strcpy(filename, scanner.dir); - strncat(filename, &settings->fileSeparator, 1); - strcat(filename, entry->d_name); - - /* Check for subdirectory */ - if (stat(filename, &statbuf) == 0) { - if (!(statbuf.st_mode & S_IFREG)) { - printf("%-60s\n", entryname); - if (settings->recursive && (statbuf.st_mode & S_IFDIR)) { - lineSum += scanDirectory( - (scanner_t) {filename, scanner.spaces+1}, settings); - } - continue; - } - } else { - perror(" Error in stat call"); - continue; - } - - if ((settings->includeSuffixes->count == 0 - || testSuffix(filename, settings->includeSuffixes)) - && !testSuffix(filename, settings->excludeSuffixes)) { - /* Count lines */ - lines = 0; - bfile = false; - bfile_reset(settings->bfileHeuristics); - char line_buffer[REGEX_MAX_LINELENGTH]; - int line_buffer_offset = 0; - - FILE *file = fopen(filename, "r"); - if (file == NULL) { - printf(entryname); - perror(" File acces failed"); - continue; - } - - do { - a = fgetc(file); - - bfile = bfile_check(settings->bfileHeuristics, a); - - if (a == 10 || a == EOF) { - line_buffer[line_buffer_offset] = 0; - if (regex_parser_do(settings->regex, line_buffer) == 0) { - /* Only subtract lines when matching has finished */ - if (!regex_parser_matching(settings->regex)) { - lines -= settings->regex->matched_lines; - } - } - - line_buffer_offset = 0; - lines++; - } else { - if (line_buffer_offset < REGEX_MAX_LINELENGTH) { - line_buffer[line_buffer_offset] = a; - line_buffer_offset++; - } else { - line_buffer[line_buffer_offset-1] = 0; - settings->confusing_lnlen = true; - } - } - } while (!bfile && a != EOF); - fclose(file); - - /* Print and sum line count */ - if (bfile) { - if (!settings->matchesOnly) { - printf("%-60s%19s\n", entryname, "binary"); - } - } else { - lineSum += lines; - printf("%-60s%13d lines\n", entryname, lines); - } - } else { - if (!settings->matchesOnly) { - /* Print hint */ - printf("%-60s%19s\n", entryname, "no match"); - } - } - } - } - - closedir(dirf); - - return lineSum; -} diff -r 1a2d7298bc82 -r fa9bda32de17 scanner.h --- a/scanner.h Tue Oct 02 10:49:25 2012 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,29 +0,0 @@ -/* - * scanner.h - * - * Created on: 23.05.2011 - * Author: Mike - */ - -#ifndef SCANNER_H_ -#define SCANNER_H_ - -#include "stdinc.h" -#include "settings.h" - -typedef struct { - char *dir; - int spaces; -} scanner_t; - -#ifdef _cplusplus -extern "C" { -#endif - -int scanDirectory(scanner_t scanner, settings_t* settings); - -#ifdef _cplusplus -} -#endif - -#endif /* SCANNER_H_ */ diff -r 1a2d7298bc82 -r fa9bda32de17 settings.c --- a/settings.c Tue Oct 02 10:49:25 2012 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,37 +0,0 @@ -/* - * settings.c - * - * Created on: 15.09.2011 - * Author: Mike - */ - -#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->matchesOnly = false; - settings->includeSuffixes = new_string_list_t(); - settings->excludeSuffixes = new_string_list_t(); - settings->verbose = true; - settings->bfileHeuristics = new_bfile_heuristics_t(); - settings->confusing_lnlen = false; - settings->regex = new_regex_parser_t(); - } - - return settings; -} - -void destroy_settings_t(settings_t* settings) { - destroy_regex_parser_t(settings->regex); - destroy_string_list_t(settings->includeSuffixes); - destroy_string_list_t(settings->excludeSuffixes); - destroy_bfile_heuristics_t(settings->bfileHeuristics); - free(settings); -} diff -r 1a2d7298bc82 -r fa9bda32de17 settings.h --- a/settings.h Tue Oct 02 10:49:25 2012 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,39 +0,0 @@ -/* - * settings.h - * - * Created on: 15.09.2011 - * Author: Mike - */ - -#ifndef SETTINGS_H_ -#define SETTINGS_H_ - -#include "stdinc.h" -#include "string_list.h" -#include "bfile_heuristics.h" -#include "regex_parser.h" - -typedef struct _settings { - string_list_t* includeSuffixes; - string_list_t* excludeSuffixes; - regex_parser_t* regex; - bfile_heuristics_t* bfileHeuristics; - char fileSeparator; - bool recursive; - bool matchesOnly; - bool verbose; - bool confusing_lnlen; /* this flag is set by the scanner */ -} 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 1a2d7298bc82 -r fa9bda32de17 src/arguments.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/arguments.c Fri Dec 28 15:44:28 2012 +0100 @@ -0,0 +1,70 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * Copyright 2011 Mike Becker. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * + * arguments.c + * + * Created on: 15.09.2011 + * Author: Mike + */ + +#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; +} + +bool checkParamOpt(int* paropt) { + bool ret = *paropt == 0; + *paropt = 1; + return ret; +} + +void parseCSL(char* csl, string_list_t* list) { + if (csl != NULL) { + char* finder = strtok(csl, ","); + while (finder != NULL) { + add_string(list, finder); + finder = strtok(NULL, ","); + } + } +} diff -r 1a2d7298bc82 -r fa9bda32de17 src/arguments.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/arguments.h Fri Dec 28 15:44:28 2012 +0100 @@ -0,0 +1,51 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * Copyright 2011 Mike Becker. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * arguments.h + * + * Created on: 15.09.2011 + * Author: Mike + */ + +#ifndef ARGUMENTS_H_ +#define ARGUMENTS_H_ + +#include "stdinc.h" +#include "string_list.h" + +#ifdef _cplusplus +extern "C" { +#endif + +int checkArgument(const char*, const char*); +bool checkParamOpt(int*); +bool registerArgument(int*, int); +void parseCSL(char*, string_list_t*); + +#ifdef _cplusplus +} +#endif + +#endif /* ARGUMENTS_H_ */ diff -r 1a2d7298bc82 -r fa9bda32de17 src/bfile_heuristics.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/bfile_heuristics.c Fri Dec 28 15:44:28 2012 +0100 @@ -0,0 +1,81 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * Copyright 2011 Mike Becker. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * bfile_heuristics.c + * + * Created on: 20.10.2011 + * Author: Mike + */ + +#include "bfile_heuristics.h" +#include + +bfile_heuristics_t *new_bfile_heuristics_t() { + bfile_heuristics_t *ret = malloc(sizeof(bfile_heuristics_t)); + ret->level = BFILE_MEDIUM_ACCURACY; + bfile_reset(ret); + return ret; +} + +void destroy_bfile_heuristics_t(bfile_heuristics_t *def) { + free(def); +} + +void bfile_reset(bfile_heuristics_t *def) { + def->bcount = 0; + def->tcount = 0; +} + +bool bfile_check(bfile_heuristics_t *def, int next_char) { + bool ret = false; + if (def->level != BFILE_IGNORE) { + def->tcount++; + if (!isprint(next_char) && !isspace(next_char)) { + def->bcount++; + } + + if (def->tcount > 1) { /* empty files are text files */ + switch (def->level) { + case BFILE_LOW_ACCURACY: + if (def->tcount > 15 || next_char == EOF) { + ret = (1.0*def->bcount)/def->tcount > 0.32; + } + break; + case BFILE_HIGH_ACCURACY: + if (def->tcount > 500 || next_char == EOF) { + ret = (1.0*def->bcount)/def->tcount > 0.1; + } + break; + default: /* BFILE_MEDIUM_ACCURACY */ + if (def->tcount > 100 || next_char == EOF) { + ret = (1.0*def->bcount)/def->tcount > 0.1; + } + break; + } + } + } + + return ret; +} diff -r 1a2d7298bc82 -r fa9bda32de17 src/bfile_heuristics.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/bfile_heuristics.h Fri Dec 28 15:44:28 2012 +0100 @@ -0,0 +1,61 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * Copyright 2011 Mike Becker. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * bfile_heuristics.h + * + * Created on: 20.10.2011 + * Author: Mike + */ + +#ifndef BFILE_HEURISTICS_H_ +#define BFILE_HEURISTICS_H_ + +#include "stdinc.h" + +#define BFILE_IGNORE 0x00 +#define BFILE_LOW_ACCURACY 0x01 +#define BFILE_MEDIUM_ACCURACY 0x02 +#define BFILE_HIGH_ACCURACY 0x04 + +typedef struct { + unsigned int level; + unsigned int bcount; /* 'binary' character count */ + unsigned int tcount; /* total count */ +} bfile_heuristics_t; + +#ifdef _cplusplus +extern "C" { +#endif + +bfile_heuristics_t *new_bfile_heuristics_t(); +void destroy_bfile_heuristics_t(bfile_heuristics_t *def); +void bfile_reset(bfile_heuristics_t *def); +bool bfile_check(bfile_heuristics_t *def, int next_char); + +#ifdef _cplusplus +} +#endif + +#endif /* BFILE_HEURISTICS_H_ */ diff -r 1a2d7298bc82 -r fa9bda32de17 src/cline.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/cline.c Fri Dec 28 15:44:28 2012 +0100 @@ -0,0 +1,269 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * Copyright 2011 Mike Becker. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * cline.c + * + * Created on: 23.05.2011 + * Author: Mike + */ + +#include "cline.h" +#include "scanner.h" +#include "settings.h" +#include "arguments.h" +#include "stream.h" +#include "regex_parser.h" + +void printHelpText() { + printf( + "\nUsage:" + "\n cline [Options] [Directories...]" + "\n cline [Options] [Directories...]" + "\n\nCounts the line terminator characters (\\n) within all" + " files in the specified\ndirectories." + "\n\nOptions:" + "\n -b - binary file heuristics level (default medium)" + "\n One of: ignore low medium high" + "\n -E - Excludes any line matching the " + "\n -e - Excludes lines between and " + "\n You may use these options multiple times" + "\n -h, --help - this help text" + "\n -m - print information about matching files only" + "\n -s - only count files with these suffixes (separated" + "\n by commas)" + "\n -S - count any file except those with these suffixes" + "\n (separated by commas)" + "\n -r, -R - includes subdirectories" + "\n -v, --version - print out version information" + "\n -V - turn verbose output off, print the result only" + "\n\nShortcuts:" + "\n --exclude-cstyle-comments" + "\n = -E \"\\s*//\" -e \"\\s*/\\*\" \"\\*/\\s*\"" + "\n\n" + "The default call without any options is:" + "\n cline ./\n\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 cline -rs .c\n" + "\nIf you want to exclude comment lines, you may use the -e/-E option." + "\nAfter a line matches the regex pattern any following line is" + "\nnot counted unless a line matches the pattern. A line is still " + "\ncounted when it does not start or end with the respective patterns." + "\nPlease note, that cline does not remove whitespace characters as this" + "\nmight not be reasonable in some cases." + "\n\nExample (C without comments):" + "\n cline -s .c,.h --exclude-cstyle-comments"); +} + +int exit_with_version(settings_t* settings) { + printf("cline - Revision: %s\n", VERSION); + destroy_settings_t(settings); + return 0; +} + +int exit_with_help(settings_t* settings, int code) { + printHelpText(); + 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; + } + + /* Get arguments */ + string_list_t *directories = new_string_list_t(); + if (directories == NULL) { + fprintf(stderr, "Memory allocation failed.\n"); + return 1; + } + char* includeSuffix = NULL; + char* excludeSuffix = NULL; + int checked = 0; + + for (int t = 1 ; t < argc ; t++) { + + int argflags = checkArgument(argv[t], "hsSrRmvVbeE"); + int paropt = 0; + + /* s */ + if ((argflags & 2) > 0) { + if (!checkParamOpt(&paropt) || registerArgument(&checked, 2)) { + return exit_with_help(settings, 1); + } + t++; + if (t >= argc) { + return exit_with_help(settings, 1); + } + includeSuffix = argv[t]; + } + /* S */ + if ((argflags & 4) > 0) { + if (!checkParamOpt(&paropt) || registerArgument(&checked, 4)) { + return exit_with_help(settings, 1); + } + t++; + if (t >= argc) { + return exit_with_help(settings, 1); + } + excludeSuffix = argv[t]; + } + /* h */ + if ((argflags & 1) > 0 || strcmp(argv[t], "--help") == 0) { + return exit_with_help(settings, 0); + } + /* r, R */ + if ((argflags & 24) > 0) { + if (registerArgument(&checked, 24)) { + return exit_with_help(settings, 1); + } + settings->recursive = true; + } + /* m */ + if ((argflags & 32) > 0) { + if (registerArgument(&checked, 32)) { + return exit_with_help(settings, 1); + } + settings->matchesOnly = true; + } + /* v */ + if ((argflags & 64) > 0 || strcmp(argv[t], "--version") == 0) { + return exit_with_version(settings); + } + /* V */ + if ((argflags & 128) > 0) { + if (registerArgument(&checked, 128)) { + return exit_with_help(settings, 1); + } + settings->verbose = false; + } + /* b */ + if ((argflags & 256) > 0) { + if (!checkParamOpt(&paropt) || registerArgument(&checked, 256)) { + return exit_with_help(settings, 1); + } + t++; + if (t >= argc) { + return exit_with_help(settings, 1); + } + if (strcasecmp(argv[t], "ignore") == 0) { + settings->bfileHeuristics->level = BFILE_IGNORE; + } else if (strcasecmp(argv[t], "low") == 0) { + settings->bfileHeuristics->level = BFILE_LOW_ACCURACY; + } else if (strcasecmp(argv[t], "medium") == 0) { + settings->bfileHeuristics->level = BFILE_MEDIUM_ACCURACY; + } else if (strcasecmp(argv[t], "high") == 0) { + settings->bfileHeuristics->level = BFILE_HIGH_ACCURACY; + } else { + return exit_with_help(settings, 1); + } + } + /* e */ + if ((argflags & 512) > 0) { + if (!checkParamOpt(&paropt) || t + 2 >= argc) { + return exit_with_help(settings, 1); + } + t++; add_string(settings->regex->pattern_list, argv[t]); + t++; add_string(settings->regex->pattern_list, argv[t]); + } + /* E */ + if ((argflags & 1024) > 0) { + t++; + if (!checkParamOpt(&paropt) || t >= argc) { + return exit_with_help(settings, 1); + } + add_string(settings->regex->pattern_list, argv[t]); + add_string(settings->regex->pattern_list, "$"); + } + if (argflags == 0) { + /* SHORTCUTS */ + /* exclude-cstyle-comments */ + if (strcmp(argv[t], "--exclude-cstyle-comments") == 0) { + add_string(settings->regex->pattern_list, "\\s*//"); + add_string(settings->regex->pattern_list, "$"); + add_string(settings->regex->pattern_list, "\\s*/\\*"); + add_string(settings->regex->pattern_list, "\\*/\\s*"); + } + /* Path */ + else { + add_string(directories, argv[t]); + } + } + } + + /* Configure output */ + if (!settings->verbose) { + close_stdout(); + } + + /* Find tokens */ + parseCSL(includeSuffix, settings->includeSuffixes); + parseCSL(excludeSuffix, settings->excludeSuffixes); + + /* Scan directories */ + if (regex_compile_all(settings->regex)) { + int lines = 0; + if (directories->count == 0) { + add_string(directories, "."); + } + for (int t = 0 ; t < directories->count ; t++) { + if (t > 0) { + for (int u = 0 ; u < 79 ; u++) { + printf("-"); + } + printf("\n"); + } + lines += scanDirectory((scanner_t){directories->items[t], 0}, settings); + } + destroy_string_list_t(directories); + + /* Print double line and line count */ + for (int t = 0 ; t < 79 ; t++) { + printf("="); + } + printf("\n%73d lines\n", lines); + + if (settings->confusing_lnlen && settings->regex->pattern_list->count > 0) { + printf("\nSome files contain too long lines.\n" + "The regex parser currently supports a maximum line length of %d." + "\nThe result might be wrong.\n", REGEX_MAX_LINELENGTH); + } + + if (!settings->verbose) { + reopen_stdout(); + printf("%d", lines); + } + destroy_settings_t(settings); + } + + fflush(stdout); + fflush(stderr); + return 0; +} diff -r 1a2d7298bc82 -r fa9bda32de17 src/cline.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/cline.h Fri Dec 28 15:44:28 2012 +0100 @@ -0,0 +1,52 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * Copyright 2011 Mike Becker. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * cline.h + * + * Created on: 23.05.2011 + * Author: Mike + */ + +#ifndef CLINE_H_ +#define CLINE_H_ + +const char* VERSION=""; /* will be replaced by makefile */ + +#include "stdinc.h" +#include "settings.h" + +#ifdef _cplusplus +extern "C" { +#endif + +void printHelpText(); +int exit_with_version(settings_t*); +int exit_with_help(settings_t*, int); + +#ifdef _cplusplus +} +#endif + +#endif /* CLINE_H_ */ diff -r 1a2d7298bc82 -r fa9bda32de17 src/regex_parser.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/regex_parser.c Fri Dec 28 15:44:28 2012 +0100 @@ -0,0 +1,134 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * Copyright 2011 Mike Becker. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * regex_parser.c + * + * Created on: 26.01.2012 + * Author: Mike + */ + +#include "regex_parser.h" + +regex_parser_t* new_regex_parser_t() { + regex_parser_t* ret = malloc(sizeof(regex_parser_t)); + if (ret != NULL) { + ret->pattern_list = new_string_list_t(); + ret->matched_lines = 0; + ret->pattern_match = 0; + ret->compiled_patterns = NULL; + ret->compiled_pattern_count = 0; + } + return ret; +} + +void regex_destcomppats(regex_parser_t* parser) { + if (parser->compiled_patterns != NULL) { + for (int i = 0 ; i < parser->compiled_pattern_count ; i++) { + if (parser->compiled_patterns[i] != NULL) { + free(parser->compiled_patterns[i]); + } + } + free(parser->compiled_patterns); + parser->compiled_patterns = NULL; + parser->compiled_pattern_count = 0; + } +} + +void destroy_regex_parser_t(regex_parser_t* parser) { + regex_destcomppats(parser); + destroy_string_list_t(parser->pattern_list); + free(parser); +} + +bool regex_parser_matching(regex_parser_t* parser) { + return parser->pattern_match > 0; +} + +int regex_parser_do(regex_parser_t* parser, char* input) { + int err = REG_NOMATCH; + if (parser->compiled_pattern_count > 0) { + regmatch_t match; + + if (regex_parser_matching(parser)) { + parser->matched_lines++; + + err = regexec(parser->compiled_patterns[parser->pattern_match], + input, 1, &match, 0); + if (err > 0 && err != REG_NOMATCH) { + fprintf(stderr, "Regex-Error: 0x%08x", err); + } + if (err == 0) { + parser->pattern_match = 0; + /* do not match line, if it does not end with the pattern */ + if (match.rm_eo < strlen(input)) { + parser->matched_lines--; + } + } + } else { + for (int i = 0 ; i < parser->compiled_pattern_count - 1 ; i += 2) { + err = regexec(parser->compiled_patterns[i], input, 1, &match, 0); + if (err > 0 && err != REG_NOMATCH) { + fprintf(stderr, "Regex-Error: 0x%08x", err); + } + if (err == 0) { + parser->pattern_match = i+1; + parser->matched_lines = 0; + /* Check, if end pattern is also in this line */ + regex_parser_do(parser, input); + /* do not match line, if it does not start with the pattern */ + if (match.rm_so > 0 && parser->matched_lines > 0) { + parser->matched_lines--; + } + break; + } + } + } + } + return err; +} + +bool regex_compile_all(regex_parser_t* parser) { + bool success = true; + size_t pcount = parser->pattern_list->count; + if (pcount > 0) { + regex_destcomppats(parser); + parser->compiled_patterns = calloc(pcount, sizeof(regex_t)); + parser->compiled_pattern_count = pcount; + + regex_t* re; + for (int i = 0 ; i < pcount ; i++) { + re = malloc(sizeof(regex_t)); + if (regcomp(re, parser->pattern_list->items[i], REG_EXTENDED) == 0) { + parser->compiled_patterns[i] = re; + } else { + fprintf(stderr, "Cannot compile pattern: %s\n", + (parser->pattern_list->items[i])); + parser->compiled_patterns[i] = NULL; + success = false; + } + } + } + return success; +} diff -r 1a2d7298bc82 -r fa9bda32de17 src/regex_parser.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/regex_parser.h Fri Dec 28 15:44:28 2012 +0100 @@ -0,0 +1,66 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * Copyright 2011 Mike Becker. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * regex_parser.h + * + * Created on: 26.01.2012 + * Author: Mike + */ + +#ifndef REGEX_PARSER_H_ +#define REGEX_PARSER_H_ + +#define REGEX_MAX_LINELENGTH 2048 + +#include +#include +#include +#include "string_list.h" + +typedef struct { + string_list_t* pattern_list; /* even entries: start ; odd entries: end */ + regex_t** compiled_patterns; + size_t compiled_pattern_count; + unsigned int pattern_match; /* save position of end pattern to match - + NULL when a start pattern shall match first */ + unsigned int matched_lines; +} regex_parser_t; + +#ifdef _cplusplus +extern "C" { +#endif + +regex_parser_t* new_regex_parser_t(); +void destroy_regex_parser_t(regex_parser_t*); + +bool regex_parser_matching(regex_parser_t*); +bool regex_compile_all(regex_parser_t*); +int regex_parser_do(regex_parser_t*, char*); + +#ifdef _cplusplus +} +#endif + +#endif /* REGEX_PARSER_H_ */ diff -r 1a2d7298bc82 -r fa9bda32de17 src/scanner.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/scanner.c Fri Dec 28 15:44:28 2012 +0100 @@ -0,0 +1,150 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * Copyright 2011 Mike Becker. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * scanner.c + * + * Created on: 23.05.2011 + * Author: Mike + */ + + +#include "scanner.h" +#include "suffix_fnc.h" +#include "bfile_heuristics.h" +#include "regex_parser.h" +#include + +int scanDirectory(scanner_t scanner, settings_t* settings) { + + DIR *dirf; + struct dirent *entry; + int lines, a; + int lineSum = 0; + bool bfile; + struct stat statbuf; + + if ((dirf = opendir(scanner.dir)) == NULL) { + printf("%s", scanner.dir); + perror(" Directory access failed"); + return 0; + } + + while ((entry = readdir(dirf)) != NULL) { + if (strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0) { + /* Construct tree view and absolute pathname strings */ + char entryname[strlen(entry->d_name)+scanner.spaces]; + for (int t = 0 ; t < scanner.spaces ; t++) { + entryname[t]=' '; + } + entryname[scanner.spaces] = 0; + strcat(entryname, entry->d_name); + + char filename[(1+strlen(scanner.dir)+strlen(entry->d_name))]; + strcpy(filename, scanner.dir); + strncat(filename, &settings->fileSeparator, 1); + strcat(filename, entry->d_name); + + /* Check for subdirectory */ + if (stat(filename, &statbuf) == 0) { + if (!(statbuf.st_mode & S_IFREG)) { + printf("%-60s\n", entryname); + if (settings->recursive && (statbuf.st_mode & S_IFDIR)) { + lineSum += scanDirectory( + (scanner_t) {filename, scanner.spaces+1}, settings); + } + continue; + } + } else { + perror(" Error in stat call"); + continue; + } + + if ((settings->includeSuffixes->count == 0 + || testSuffix(filename, settings->includeSuffixes)) + && !testSuffix(filename, settings->excludeSuffixes)) { + /* Count lines */ + lines = 0; + bfile = false; + bfile_reset(settings->bfileHeuristics); + char line_buffer[REGEX_MAX_LINELENGTH]; + int line_buffer_offset = 0; + + FILE *file = fopen(filename, "r"); + if (file == NULL) { + printf("%s", entryname); + perror(" File acces failed"); + continue; + } + + do { + a = fgetc(file); + + bfile = bfile_check(settings->bfileHeuristics, a); + + if (a == 10 || a == EOF) { + line_buffer[line_buffer_offset] = 0; + if (regex_parser_do(settings->regex, line_buffer) == 0) { + /* Only subtract lines when matching has finished */ + if (!regex_parser_matching(settings->regex)) { + lines -= settings->regex->matched_lines; + } + } + + line_buffer_offset = 0; + lines++; + } else { + if (line_buffer_offset < REGEX_MAX_LINELENGTH) { + line_buffer[line_buffer_offset] = a; + line_buffer_offset++; + } else { + line_buffer[line_buffer_offset-1] = 0; + settings->confusing_lnlen = true; + } + } + } while (!bfile && a != EOF); + fclose(file); + + /* Print and sum line count */ + if (bfile) { + if (!settings->matchesOnly) { + printf("%-60s%19s\n", entryname, "binary"); + } + } else { + lineSum += lines; + printf("%-60s%13d lines\n", entryname, lines); + } + } else { + if (!settings->matchesOnly) { + /* Print hint */ + printf("%-60s%19s\n", entryname, "no match"); + } + } + } + } + + closedir(dirf); + + return lineSum; +} diff -r 1a2d7298bc82 -r fa9bda32de17 src/scanner.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/scanner.h Fri Dec 28 15:44:28 2012 +0100 @@ -0,0 +1,53 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * Copyright 2011 Mike Becker. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * scanner.h + * + * Created on: 23.05.2011 + * Author: Mike + */ + +#ifndef SCANNER_H_ +#define SCANNER_H_ + +#include "stdinc.h" +#include "settings.h" + +typedef struct { + char *dir; + int spaces; +} scanner_t; + +#ifdef _cplusplus +extern "C" { +#endif + +int scanDirectory(scanner_t scanner, settings_t* settings); + +#ifdef _cplusplus +} +#endif + +#endif /* SCANNER_H_ */ diff -r 1a2d7298bc82 -r fa9bda32de17 src/settings.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/settings.c Fri Dec 28 15:44:28 2012 +0100 @@ -0,0 +1,61 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * Copyright 2011 Mike Becker. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * settings.c + * + * Created on: 15.09.2011 + * Author: Mike + */ + +#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->matchesOnly = false; + settings->includeSuffixes = new_string_list_t(); + settings->excludeSuffixes = new_string_list_t(); + settings->verbose = true; + settings->bfileHeuristics = new_bfile_heuristics_t(); + settings->confusing_lnlen = false; + settings->regex = new_regex_parser_t(); + } + + return settings; +} + +void destroy_settings_t(settings_t* settings) { + destroy_regex_parser_t(settings->regex); + destroy_string_list_t(settings->includeSuffixes); + destroy_string_list_t(settings->excludeSuffixes); + destroy_bfile_heuristics_t(settings->bfileHeuristics); + free(settings); +} diff -r 1a2d7298bc82 -r fa9bda32de17 src/settings.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/settings.h Fri Dec 28 15:44:28 2012 +0100 @@ -0,0 +1,63 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * Copyright 2011 Mike Becker. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * settings.h + * + * Created on: 15.09.2011 + * Author: Mike + */ + +#ifndef SETTINGS_H_ +#define SETTINGS_H_ + +#include "stdinc.h" +#include "string_list.h" +#include "bfile_heuristics.h" +#include "regex_parser.h" + +typedef struct _settings { + string_list_t* includeSuffixes; + string_list_t* excludeSuffixes; + regex_parser_t* regex; + bfile_heuristics_t* bfileHeuristics; + char fileSeparator; + bool recursive; + bool matchesOnly; + bool verbose; + bool confusing_lnlen; /* this flag is set by the scanner */ +} 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 1a2d7298bc82 -r fa9bda32de17 src/stdinc.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/stdinc.h Fri Dec 28 15:44:28 2012 +0100 @@ -0,0 +1,41 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * Copyright 2011 Mike Becker. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * stdinc.h + * + * Created on: 15.09.2011 + * Author: Mike + */ + +#ifndef STDINC_H_ +#define STDINC_H_ + +#include +#include +#include +#include +#include + +#endif /* STDINC_H_ */ diff -r 1a2d7298bc82 -r fa9bda32de17 src/stream.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/stream.c Fri Dec 28 15:44:28 2012 +0100 @@ -0,0 +1,49 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * Copyright 2011 Mike Becker. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * stream.c + * + * Created on: 20.09.2011 + * Author: Mike + */ + +#include "stream.h" + +void close_stdout() { +#ifdef _WIN32 + _STREAM_STDOUT = dup(STDOUT_FILENO); +#endif + stdout = freopen("/dev/null", "w", stdout); +} + +void reopen_stdout() { +#ifdef _WIN32 + close(STDOUT_FILENO); + fdopen(dup(_STREAM_STDOUT), "wa"); + close(_STREAM_STDOUT); +#else + stdout = freopen("/dev/stdout", "w", stdout); +#endif +} diff -r 1a2d7298bc82 -r fa9bda32de17 src/stream.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/stream.h Fri Dec 28 15:44:28 2012 +0100 @@ -0,0 +1,52 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * Copyright 2011 Mike Becker. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * stream.h + * + * Created on: 20.09.2011 + * Author: Mike + */ + +#ifndef STREAM_H_ +#define STREAM_H_ + +#include "stdinc.h" + +#ifdef _WIN32 +int _STREAM_STDOUT; +#endif + +#ifdef _cplusplus +extern "C" { +#endif + +void close_stdout(); +void reopen_stdout(); + +#ifdef _cplusplus +extern "C" } +#endif + +#endif /* STREAM_H_ */ diff -r 1a2d7298bc82 -r fa9bda32de17 src/string_list.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/string_list.c Fri Dec 28 15:44:28 2012 +0100 @@ -0,0 +1,58 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * Copyright 2011 Mike Becker. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * string_list.c + * + * Created on: 15.09.2011 + * Author: Mike + */ + +#include "string_list.h" + +string_list_t* new_string_list_t() { + string_list_t* stringList = malloc(sizeof(string_list_t)); + stringList->count = 0; + stringList->items = NULL; + + return stringList; +} + +void destroy_string_list_t(string_list_t* list) { + if (list->items != NULL) { + free(list->items); + } + free(list); +} + +void add_string(string_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 1a2d7298bc82 -r fa9bda32de17 src/string_list.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/string_list.h Fri Dec 28 15:44:28 2012 +0100 @@ -0,0 +1,54 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * Copyright 2011 Mike Becker. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * string_list.h + * + * Created on: 15.09.2011 + * Author: Mike + */ + +#ifndef STRING_LIST_H_ +#define STRING_LIST_H_ + +#include "stdinc.h" + +typedef struct _string_list { + size_t count; + char** items; +} string_list_t; + +#ifdef _cplusplus +extern "C" { +#endif + +string_list_t* new_string_list_t(); +void destroy_string_list_t(string_list_t*); +void add_string(string_list_t*, char*); + +#ifdef _cplusplus +} +#endif + +#endif /* STRING_LIST_H_ */ diff -r 1a2d7298bc82 -r fa9bda32de17 src/suffix_fnc.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/suffix_fnc.c Fri Dec 28 15:44:28 2012 +0100 @@ -0,0 +1,49 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * Copyright 2011 Mike Becker. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * suffix_fnc.c + * + * Created on: 15.09.2011 + * Author: Mike + */ + +#include "suffix_fnc.h" + +bool testSuffix(char* filename, string_list_t* list) { + bool ret = false; + int tokenlen, fnamelen = strlen(filename); + for (int t = 0 ; t < list->count ; t++) { + tokenlen = strlen(list->items[t]); + if (fnamelen >= tokenlen && tokenlen > 0) { + if (strncmp(filename+fnamelen-tokenlen, + list->items[t], tokenlen) == 0) { + ret = true; + break; + } + } + } + return ret; +} + diff -r 1a2d7298bc82 -r fa9bda32de17 src/suffix_fnc.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/suffix_fnc.h Fri Dec 28 15:44:28 2012 +0100 @@ -0,0 +1,40 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * Copyright 2011 Mike Becker. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * suffix_fnc.h + * + * Created on: 15.09.2011 + * Author: Mike + */ + +#ifndef SUFFIX_FNC_H_ +#define SUFFIX_FNC_H_ + +#include "stdinc.h" +#include "string_list.h" + +bool testSuffix(char*, string_list_t*); + +#endif /* SUFFIX_FNC_H_ */ diff -r 1a2d7298bc82 -r fa9bda32de17 stdinc.h --- a/stdinc.h Tue Oct 02 10:49:25 2012 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,17 +0,0 @@ -/* - * stdinc.h - * - * Created on: 15.09.2011 - * Author: Mike - */ - -#ifndef STDINC_H_ -#define STDINC_H_ - -#include -#include -#include -#include -#include - -#endif /* STDINC_H_ */ diff -r 1a2d7298bc82 -r fa9bda32de17 stream.c --- a/stream.c Tue Oct 02 10:49:25 2012 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,25 +0,0 @@ -/* - * stream.c - * - * Created on: 20.09.2011 - * Author: Mike - */ - -#include "stream.h" - -void close_stdout() { -#ifdef _WIN32 - _STREAM_STDOUT = dup(STDOUT_FILENO); -#endif - freopen("/dev/null", "w", stdout); -} - -void reopen_stdout() { -#ifdef _WIN32 - close(STDOUT_FILENO); - fdopen(dup(_STREAM_STDOUT), "wa"); - close(_STREAM_STDOUT); -#else - freopen("/dev/stdout", "w", stdout); -#endif -} diff -r 1a2d7298bc82 -r fa9bda32de17 stream.h --- a/stream.h Tue Oct 02 10:49:25 2012 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,28 +0,0 @@ -/* - * stream.h - * - * Created on: 20.09.2011 - * Author: Mike - */ - -#ifndef STREAM_H_ -#define STREAM_H_ - -#include "stdinc.h" - -#ifdef _WIN32 -int _STREAM_STDOUT; -#endif - -#ifdef _cplusplus -extern "C" { -#endif - -void close_stdout(); -void reopen_stdout(); - -#ifdef _cplusplus -extern "C" } -#endif - -#endif /* STREAM_H_ */ diff -r 1a2d7298bc82 -r fa9bda32de17 string_list.c --- a/string_list.c Tue Oct 02 10:49:25 2012 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,34 +0,0 @@ -/* - * string_list.c - * - * Created on: 15.09.2011 - * Author: Mike - */ - -#include "string_list.h" - -string_list_t* new_string_list_t() { - string_list_t* stringList = malloc(sizeof(string_list_t)); - stringList->count = 0; - stringList->items = NULL; - - return stringList; -} - -void destroy_string_list_t(string_list_t* list) { - if (list->items != NULL) { - free(list->items); - } - free(list); -} - -void add_string(string_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 1a2d7298bc82 -r fa9bda32de17 string_list.h --- a/string_list.h Tue Oct 02 10:49:25 2012 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,30 +0,0 @@ -/* - * string_list.h - * - * Created on: 15.09.2011 - * Author: Mike - */ - -#ifndef STRING_LIST_H_ -#define STRING_LIST_H_ - -#include "stdinc.h" - -typedef struct _string_list { - size_t count; - char** items; -} string_list_t; - -#ifdef _cplusplus -extern "C" { -#endif - -string_list_t* new_string_list_t(); -void destroy_string_list_t(string_list_t*); -void add_string(string_list_t*, char*); - -#ifdef _cplusplus -} -#endif - -#endif /* STRING_LIST_H_ */ diff -r 1a2d7298bc82 -r fa9bda32de17 suffix_fnc.c --- a/suffix_fnc.c Tue Oct 02 10:49:25 2012 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,25 +0,0 @@ -/* - * suffix_fnc.c - * - * Created on: 15.09.2011 - * Author: Mike - */ - -#include "suffix_fnc.h" - -bool testSuffix(char* filename, string_list_t* list) { - bool ret = false; - int tokenlen, fnamelen = strlen(filename); - for (int t = 0 ; t < list->count ; t++) { - tokenlen = strlen(list->items[t]); - if (fnamelen >= tokenlen && tokenlen > 0) { - if (strncmp(filename+fnamelen-tokenlen, - list->items[t], tokenlen) == 0) { - ret = true; - break; - } - } - } - return ret; -} - diff -r 1a2d7298bc82 -r fa9bda32de17 suffix_fnc.h --- a/suffix_fnc.h Tue Oct 02 10:49:25 2012 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,16 +0,0 @@ -/* - * suffix_fnc.h - * - * Created on: 15.09.2011 - * Author: Mike - */ - -#ifndef SUFFIX_FNC_H_ -#define SUFFIX_FNC_H_ - -#include "stdinc.h" -#include "string_list.h" - -bool testSuffix(char*, string_list_t*); - -#endif /* SUFFIX_FNC_H_ */