From 0490455b350619969222e072f231b7cb9f117f8a Mon Sep 17 00:00:00 2001 From: martinahansen Date: Tue, 5 Aug 2008 00:22:07 +0000 Subject: [PATCH] fixing up c code git-svn-id: http://biopieces.googlecode.com/svn/trunk@203 74ccb610-7750-0410-82ae-013aeee3265d --- code_c/Maasha/src/Makefile | 49 ++++------------ code_c/Maasha/src/inc/common.h | 6 +- code_c/Maasha/src/inc/strings.h | 3 + code_c/Maasha/src/lib/Makefile | 11 ++-- code_c/Maasha/src/lib/common.c | 4 +- code_c/Maasha/src/lib/strings.c | 37 ++++++++++++ code_c/Maasha/src/repeat-O-matic.c | 2 +- code_c/Maasha/src/test/Makefile | 19 +++++++ code_c/Maasha/src/test/test_strings.c | 78 ++++++++++++++++++++++++++ code_c/Maasha/src/test_pointer_addys.c | 49 ++++++++++++++++ 10 files changed, 211 insertions(+), 47 deletions(-) create mode 100644 code_c/Maasha/src/inc/strings.h create mode 100644 code_c/Maasha/src/lib/strings.c create mode 100644 code_c/Maasha/src/test/Makefile create mode 100644 code_c/Maasha/src/test/test_strings.c create mode 100644 code_c/Maasha/src/test_pointer_addys.c diff --git a/code_c/Maasha/src/Makefile b/code_c/Maasha/src/Makefile index c5bf7ba..81fbdf3 100644 --- a/code_c/Maasha/src/Makefile +++ b/code_c/Maasha/src/Makefile @@ -1,58 +1,29 @@ CC = gcc -# Cflags = -Wall -# Cflags = -Wall -g -O0 # for valgrind -Cflags = -Wall -g -pg # for gprof +# Cflags = -Wall -Werror +Cflags = -Wall -Werror -g -pg # for gprof -INC_DIR = inc/ -LIB_DIR = lib/ +INC_DIR = inc/ +LIB_DIR = lib/ +TEST_DIR = test/ INC = -I $(INC_DIR) LIB = -lm $(LIB_DIR)*.o -all: libs test test_revcomp fasta_count test_fasta test_oligo2bin repeat-O-matic test_list test_hash test_split test_binary_search test_biotools test_file_buffer test_bed +all: libs utest fasta_count repeat-O-matic libs: cd $(LIB_DIR) && ${MAKE} all -test: test.c - $(CC) $(Cflags) $(INC) $(LIB) test.c -o test - -test_revcomp: test_revcomp.c - $(CC) $(Cflags) $(INC) $(LIB) test_revcomp.c -o test_revcomp +utest: + cd $(TEST_DIR) && ${MAKE} all fasta_count: fasta_count.c $(CC) $(Cflags) $(INC) $(LIB) fasta_count.c -o fasta_count -test_fasta: test_fasta.c - $(CC) $(Cflags) $(INC) $(LIB) test_fasta.c -o test_fasta - -test_oligo2bin: test_oligo2bin.c - $(CC) $(Cflags) $(INC) $(LIB) test_oligo2bin.c -o test_oligo2bin - repeat-O-matic: repeat-O-matic.c $(CC) $(Cflags) $(INC) $(LIB) repeat-O-matic.c -o repeat-O-matic -test_list: test_list.c - $(CC) $(Cflags) $(INC) $(LIB) test_list.c -o test_list - -test_hash: test_hash.c - $(CC) $(Cflags) $(INC) $(LIB) test_hash.c -o test_hash - -test_split: test_split.c - $(CC) $(Cflags) $(INC) $(LIB) test_split.c -o test_split - -test_binary_search: test_binary_search.c - $(CC) $(Cflags) $(INC) $(LIB) test_binary_search.c -o test_binary_search - -test_biotools: test_biotools.c - $(CC) $(Cflags) $(INC) $(LIB) test_biotools.c -o test_biotools - -test_file_buffer: test_file_buffer.c $(LIB_DIR)filesys.c - $(CC) $(Cflags) $(INC) $(LIB) test_file_buffer.c -o test_file_buffer - -test_bed: test_bed.c $(LIB_DIR)ucsc.c - $(CC) $(Cflags) $(INC) $(LIB) test_bed.c -o test_bed - clean: cd $(LIB_DIR) && ${MAKE} clean - rm -f test test_revcomp fasta_count test_fasta test_oligo2bin repeat-O-matic test_list test_hash test_split test_binary_search test_biotools test_file_buffer test_bed + cd $(TEST_DIR) && ${MAKE} clean + rm -f fasta_count repeat-O-matic diff --git a/code_c/Maasha/src/inc/common.h b/code_c/Maasha/src/inc/common.h index 41096bc..687fd42 100644 --- a/code_c/Maasha/src/inc/common.h +++ b/code_c/Maasha/src/inc/common.h @@ -6,6 +6,7 @@ #include #include #include +#include /* Define a shorthand for unsigned int */ #define uint unsigned int @@ -16,7 +17,7 @@ #define FALSE 0 /* Macro for resetting a pointer to all \0's. */ -#define ZERO( pt ) ( memset( pt, '\0', sizeof( *pt ) ) ) +#define MEM_ZERO( pt ) ( memset( pt, '\0', sizeof( *pt ) ) ) /* Macro for dynamic allocation of memory. */ #define MEM_GET( pt ) ( pt = mem_get( sizeof( *pt ) ) ) @@ -36,6 +37,9 @@ /* >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> STRUCTURE DECLARATIONS <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/ +// At some point test if typdef struct list list will allow us to move all this stuff to list.h and list.c + + /* Singly linked list with a pointer to the next element and a pointer to a value. */ struct list { diff --git a/code_c/Maasha/src/inc/strings.h b/code_c/Maasha/src/inc/strings.h new file mode 100644 index 0000000..c2cae78 --- /dev/null +++ b/code_c/Maasha/src/inc/strings.h @@ -0,0 +1,3 @@ +/* Locate a substr in a str allowing for a given number of mismatches. */ +/* Returns position of match begin or -1 if not found. */ +int match_substr( char *str, int str_len, char *substr, int substr_len, int mismatch ); diff --git a/code_c/Maasha/src/lib/Makefile b/code_c/Maasha/src/lib/Makefile index 84dc3a3..dfdb4f9 100644 --- a/code_c/Maasha/src/lib/Makefile +++ b/code_c/Maasha/src/lib/Makefile @@ -1,13 +1,16 @@ CC = gcc -# Cflags = -Wall -Cflags = -Wall -g -pg # gprof +# Cflags = -Wall -Werror +Cflags = -Wall -Werror -g -pg # gprof INC_DIR = -I ../inc/ -all: common.o seq.o filesys.o fasta.o list.o hash.o ucsc.o +all: common.o strings.o seq.o filesys.o fasta.o list.o hash.o ucsc.o common.o: common.c $(CC) $(Cflags) $(INC_DIR) -c common.c +strings.o: strings.c + $(CC) $(Cflags) $(INC_DIR) -c strings.c + seq.o: seq.c $(CC) $(Cflags) $(INC_DIR) -c seq.c @@ -27,4 +30,4 @@ ucsc.o: ucsc.c $(CC) $(Cflags) $(INC_DIR) -c ucsc.c clean: - rm common.o seq.o filesys.o fasta.o list.o hash.o ucsc.o + rm common.o strings.o seq.o filesys.o fasta.o list.o hash.o ucsc.o diff --git a/code_c/Maasha/src/lib/common.c b/code_c/Maasha/src/lib/common.c index c84a02a..7f1c99d 100644 --- a/code_c/Maasha/src/lib/common.c +++ b/code_c/Maasha/src/lib/common.c @@ -143,7 +143,7 @@ void mem_free_zero( void *pt ) if ( pt != NULL ) { - ZERO( pt ); + MEM_ZERO( pt ); free( pt ); pt = NULL; @@ -245,7 +245,7 @@ void split( char *string, char delimit, struct list **fields ) list_add( fields, field_copy ); - ZERO( field ); + MEM_ZERO( field ); j = 0; } diff --git a/code_c/Maasha/src/lib/strings.c b/code_c/Maasha/src/lib/strings.c new file mode 100644 index 0000000..8a4d5fe --- /dev/null +++ b/code_c/Maasha/src/lib/strings.c @@ -0,0 +1,37 @@ +#include "common.h" +#include "strings.h" + +int match_substr( char *str, int str_len, char *substr, int substr_len, int mismatch ) +{ + /* Martin A. Hansen, August 2008. */ + + /* Locate a substr in a str allowing for a given number of mismatches. */ + /* Returns position of match begin or -1 if not found. */ + + int pos; + int i; + int count; + + for ( pos = 0; pos < str_len - substr_len + 1; pos++ ) + { + count = 0; + + for ( i = 0; i < substr_len; i++ ) + { + if ( str[ pos + i ] != substr[ i ] ) + { + count++; + + if ( count > mismatch ) { + break; + } + } + } + + if ( count <= mismatch ) { + return pos; + } + } + + return -1; +} diff --git a/code_c/Maasha/src/repeat-O-matic.c b/code_c/Maasha/src/repeat-O-matic.c index 8ce9232..80a6699 100644 --- a/code_c/Maasha/src/repeat-O-matic.c +++ b/code_c/Maasha/src/repeat-O-matic.c @@ -202,7 +202,7 @@ void oligo_count_output( char *path, uint *array ) if ( block_pos == 0 ) { - ZERO( block ); + MEM_ZERO( block ); block_beg = chr_pos; diff --git a/code_c/Maasha/src/test/Makefile b/code_c/Maasha/src/test/Makefile new file mode 100644 index 0000000..1d62981 --- /dev/null +++ b/code_c/Maasha/src/test/Makefile @@ -0,0 +1,19 @@ +CC = gcc +# Cflags = -Wall -Werror +Cflags = -Wall -Werror -g -pg # for gprof + +INC_DIR = ../inc/ +LIB_DIR = ../lib/ + +INC = -I $(INC_DIR) +LIB = -lm $(LIB_DIR)*.o + +all: test + +test: test_strings + +test_strings: test_strings.c + $(CC) $(Cflags) $(INC) $(LIB) test_strings.c -o test_strings + +clean: + rm -f test_strings diff --git a/code_c/Maasha/src/test/test_strings.c b/code_c/Maasha/src/test/test_strings.c new file mode 100644 index 0000000..15016d6 --- /dev/null +++ b/code_c/Maasha/src/test/test_strings.c @@ -0,0 +1,78 @@ +#include "common.h" + +static void test_strings(); +static void test_match_substr( char *str, int str_len, char *substr, int substr_len, int mismatch ); + + +int main() +{ + test_strings(); + + return 0; +} + + +static void test_strings() +{ + char *str = "MARTIN"; + char *substr = "TXN"; + int str_len = strlen( str ); + int substr_len = strlen( substr ); + int mismatch = 1; + + test_match_substr( str, str_len, substr, substr_len, mismatch ); +} + + +static void test_match_substr( char *str, int str_len, char *substr, int substr_len, int mismatch ) +{ + fprintf( stderr, "Running test_match_substr\n" ); + + fprintf( stderr, " Testing that str is not NULL ... " ); + assert( str != NULL ); + fprintf( stderr, "done\n" ); + + fprintf( stderr, " Testing that substr is not NULL ... " ); + assert( substr != NULL ); + fprintf( stderr, "done\n" ); + + fprintf( stderr, " Testing that str_len > zero ... " ); + assert( str_len > 0 ); + fprintf( stderr, "done\n" ); + + fprintf( stderr, " Testing that substr_len > zero ... " ); + assert( substr_len > 0 ); + fprintf( stderr, "done\n" ); + + fprintf( stderr, " Testing str_len ... " ); + assert( strlen( str ) == str_len ); + fprintf( stderr, "done\n" ); + + fprintf( stderr, " Testing substr_len ... " ); + assert( strlen( substr ) == substr_len ); + fprintf( stderr, "done\n" ); + + fprintf( stderr, " Testing that mismatch >= zero ... " ); + assert( mismatch > 0 ); + fprintf( stderr, "done\n" ); + + fprintf( stderr, " Testing that mismatch < substr_len ... " ); + assert( mismatch < substr_len ); + fprintf( stderr, "done\n" ); + + fprintf( stderr, " Testing that substr_len <= str_len ... " ); + assert( substr_len <= str_len ); + fprintf( stderr, "done\n" ); + + fprintf( stderr, " Testing that str is '\\0' terminated ... " ); + assert( str[ str_len ] == '\0' ); + fprintf( stderr, "done\n" ); + + fprintf( stderr, " Testing that substr is '\\0' terminated ... " ); + assert( substr[ substr_len ] == '\0' ); + fprintf( stderr, "done\n" ); + + fprintf( stderr, "test_match_substr OK\n" ); +} + + diff --git a/code_c/Maasha/src/test_pointer_addys.c b/code_c/Maasha/src/test_pointer_addys.c new file mode 100644 index 0000000..bbc9721 --- /dev/null +++ b/code_c/Maasha/src/test_pointer_addys.c @@ -0,0 +1,49 @@ +#include + +void func1(); +void func2(); + +int main() +{ + char p1 = 0; + char p2 = 0; + char p3 = 0; + char p4 = 0; + + printf( "main p1: val->%c address->%p\n", p1, &p1 ); + printf( "main p2: val->%c address->%p\n", p2, &p2 ); + printf( "main p3: val->%c address->%p\n", p3, &p3 ); + printf( "main p4: val->%c address->%p\n", p4, &p4 ); + + func1(); + func2(); + + return 0; +} + +void func1() +{ + char p1 = 0; + char p2 = 0; + char p3 = 0; + char p4 = 0; + + printf( "func1 p1: val->%c address->%p\n", p1, &p1 ); + printf( "func1 p2: val->%c address->%p\n", p2, &p2 ); + printf( "func1 p3: val->%c address->%p\n", p3, &p3 ); + printf( "func1 p4: val->%c address->%p\n", p4, &p4 ); + +} + +void func2() +{ + char p1 = 0; + char p2 = 0; + char p3 = 0; + char p4 = 0; + + printf( "func2 p1: val->%c address->%p\n", p1, &p1 ); + printf( "func2 p2: val->%c address->%p\n", p2, &p2 ); + printf( "func2 p3: val->%c address->%p\n", p3, &p3 ); + printf( "func2 p4: val->%c address->%p\n", p4, &p4 ); +} -- 2.39.2