]> git.donarmstrong.com Git - biopieces.git/commitdiff
fixing up c code
authormartinahansen <martinahansen@74ccb610-7750-0410-82ae-013aeee3265d>
Tue, 5 Aug 2008 00:22:07 +0000 (00:22 +0000)
committermartinahansen <martinahansen@74ccb610-7750-0410-82ae-013aeee3265d>
Tue, 5 Aug 2008 00:22:07 +0000 (00:22 +0000)
git-svn-id: http://biopieces.googlecode.com/svn/trunk@203 74ccb610-7750-0410-82ae-013aeee3265d

code_c/Maasha/src/Makefile
code_c/Maasha/src/inc/common.h
code_c/Maasha/src/inc/strings.h [new file with mode: 0644]
code_c/Maasha/src/lib/Makefile
code_c/Maasha/src/lib/common.c
code_c/Maasha/src/lib/strings.c [new file with mode: 0644]
code_c/Maasha/src/repeat-O-matic.c
code_c/Maasha/src/test/Makefile [new file with mode: 0644]
code_c/Maasha/src/test/test_strings.c [new file with mode: 0644]
code_c/Maasha/src/test_pointer_addys.c [new file with mode: 0644]

index c5bf7ba3af2c31e08bdd6d3c9242987d8bd40a19..81fbdf3772268c1e0a08d16d8490380b41faf109 100644 (file)
@@ -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
index 41096bcbd73e66aa58de4b580f9159770a904caa..687fd429defe0f27f30eae540575141908f4f76f 100644 (file)
@@ -6,6 +6,7 @@
 #include <unistd.h>
 #include <ctype.h>
 #include <math.h>
+#include <assert.h>
 
 /* 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 (file)
index 0000000..c2cae78
--- /dev/null
@@ -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 );
index 84dc3a3d9b299855a3dd5cb615254c710c84b0cf..dfdb4f974b987724b2df9c70110bb921e369987d 100644 (file)
@@ -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
index c84a02a876243493507110c3238b6cc4b9b80dc0..7f1c99d4c8674e2a0e8b9e835cba824ba168e5ad 100644 (file)
@@ -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 (file)
index 0000000..8a4d5fe
--- /dev/null
@@ -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;
+}
index 8ce9232011f8624bc1982d7c877160141d00c4ab..80a66990c51b81149163275fa64d642de4b08bfd 100644 (file)
@@ -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 (file)
index 0000000..1d62981
--- /dev/null
@@ -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 (file)
index 0000000..15016d6
--- /dev/null
@@ -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 (file)
index 0000000..bbc9721
--- /dev/null
@@ -0,0 +1,49 @@
+#include <stdio.h>
+
+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 );
+}