]> git.donarmstrong.com Git - biopieces.git/blob - code_c/Maasha/src/test_biotools.c
fixed encoding bug in read_454
[biopieces.git] / code_c / Maasha / src / test_biotools.c
1 #include "common.h"
2 #include "filesys.h"
3 #include "hash.h"
4
5 bool get_record( struct file_buffer *buffer, struct hash *record );
6 void put_record( struct hash *record );
7
8 int main( int argc, char *argv[] )
9 {
10     int                 count;
11     char               *file;
12     struct file_buffer *buffer = NULL;
13     struct hash        *record = NULL;
14
15     file = argv[ 1 ];
16
17     buffer = read_open_buffer( file );
18
19     record = hash_new( 5 );
20
21     count = 0;
22
23     while ( ( get_record( buffer, record ) ) != FALSE )
24     {
25         put_record( record );
26
27         count++;
28     }
29
30     fprintf( stderr, "Count: %d\n", count );
31
32     hash_destroy( record );
33
34     buffer_destroy( buffer );
35
36     return 0;
37 }
38
39
40 bool get_record( struct file_buffer *buffer, struct hash *record )
41 {
42     /* Martin A. Hansen, June 2008 */
43
44     /* Get next record from the stream. */
45
46     char *line = NULL;
47     char *val  = NULL;
48     char  key[ 256 ];
49     int   len;
50     int   i;
51     bool  key_ok;
52
53     while ( ( line = buffer_gets( buffer ) ) )
54     {
55         key_ok = FALSE;
56
57         //printf( "LINE->%s<-", line );
58
59         if ( strcmp( line, "---\n" ) == 0 )
60         {
61 //            printf( "found\n" );
62             
63             return TRUE;
64         }
65         else
66         {
67             len = strlen( line );
68
69             i = 0;
70
71             while ( i < len )
72             {
73                 if ( i < len - 1 && line[ i ] == ':' && line[ i + 1 ] == ' ' )
74                 {
75                     key_ok = TRUE;
76
77                     key[ i ] = '\0';
78
79                     i += 2;
80
81                     break;
82                 }
83
84                 key[ i ] = line[ i ];
85             
86                 i++;
87             }
88
89             if ( ! key_ok ) {
90                 die( "Could not locate key." );
91             }
92
93             val = mem_get( len - i );
94
95             memcpy( val, &line[ i ], len - i - 1 );
96
97             val[ len - i ] = '\0';
98
99 //            printf( "key: ->%s<- val: ->%s<-\n", key, val );
100
101             hash_add( record, key, val );
102         }
103     }
104
105     return FALSE;
106 }
107
108
109 void put_record( struct hash *record )
110 {
111     /* Martin A. Hansen, June 2008 */
112
113     /* Output a record to the stream. */
114
115     int              i;
116     struct hash_elem *bucket;
117
118     for ( i = 0; i < record->table_size; i++ )
119     {
120         for ( bucket = record->table[ i ]; bucket != NULL; bucket = bucket->next ) {
121             printf( "%s: %s\n", ( char * ) bucket->key, ( char * ) bucket->val );
122         }
123     }
124
125     printf( "---\n" );
126 }
127
128
129