]> git.donarmstrong.com Git - biopieces.git/blob - code_c/Maasha/src/lib/common.c
05e7793f89549af9ab7cb900ef84c78ccbe17e89
[biopieces.git] / code_c / Maasha / src / lib / common.c
1 #include "common.h"
2 #include "list.h"
3 #include "mem.h"
4
5
6 /* >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> ERROR HANDLING <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/
7
8
9 void die( char *msg )
10 {
11     /* Martin A. Hansen, May 2008 */
12
13     /* Print error message and exits. */
14
15     fprintf( stderr, "ERROR: %s\n", msg );
16
17     exit( 1 );
18 }
19
20
21 void warn( char *msg )
22 {
23     /* Martin A. Hansen, May 2008 */
24
25     /* Print warning message and exits. */
26
27     fprintf( stderr, "WARNING: %s\n", msg );
28 }
29
30
31 /* >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> ARRAYS <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/
32
33
34 bool binary_search_array( int *array, int array_size, int val )
35 {
36     /* Martin A. Hansen, June 2008 */
37
38     /* Binary search an array of integers for an integer value. */
39
40     int high;
41     int low;
42     int try;
43
44     high = array_size; 
45     low  = 0;                                                                                                                   
46
47     while ( low < high )                                                                                                        
48     {                                                                                                                           
49         try = ( ( high + low ) / 2 );                                                                                           
50
51         if ( val < array[ try ] ) {
52             high = try;
53         } else if ( val > array[ try ] ) {
54             low = try + 1;
55         } else {
56             return TRUE;
57         }   
58     }
59
60     return FALSE;
61 }
62
63
64 /* >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> MISC <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/
65
66
67 void split( char *string, char delimit, struct list **fields )
68 {
69     /* Martin A. Hansen, June 2008 */
70     
71     /* Split a given line and a delimiter return the split result as a list. */
72
73     int i;
74     int j;
75
76     char field[ 256 ] = "";
77     char *field_copy; 
78
79     j = 0;
80
81     for ( i = 0; string[ i ]; i++ )
82     {
83         if ( string[ i ] != delimit )
84         {
85             field[ j ] = string[ i ];
86
87             j++;
88         }
89         else
90         {
91             field_copy = mem_clone( field, j + 1 );
92
93             list_add( fields, field_copy );
94
95             MEM_ZERO( field );
96
97             j = 0;
98         }
99     }
100
101     field_copy = mem_clone( field, j + 1 );
102
103     list_add( fields, field_copy );
104
105     list_reverse( fields );
106 }
107
108
109 char *substr( char *string, int offset, int len )
110 {
111     /* Martin A. Hansen, May 2008 */
112
113     /* Create equavalent of Perls substr command. */
114     /* Currently implemented without optional length */
115     /* and the replace feature. */
116
117     int  string_len;
118     int  i;
119     int  j;
120     char *substr;
121
122     string_len = strlen( string );
123
124     if ( offset < 0 ) {
125         die( "substr offset < 0." );
126     } else if ( len < 0 ) {
127         die( "substr length < 0." );
128     } else if ( offset > string_len ) {
129         die( "substr offset outside string." );
130     } else if ( offset + len > string_len ) {
131         die( "substr offset + len outside string." );
132     }
133
134     substr = mem_get( len + 1 );
135
136     i = offset;
137     j = 0;
138
139     while ( i < offset + len )
140     {
141         substr[ j ] = string[ i ];
142
143         i++;
144         j++;
145     }
146
147     substr[ j ] = '\0';
148
149     return substr;
150 }
151
152
153 char *bits2string( uint bin )
154 {
155     /* Martin A. Hansen, June 2008 */
156     
157     /* Return a binary number as a string of 1's and 0's. */
158
159     int   i;
160     uint  j;
161     char *string;
162     
163     string = mem_get( ( sizeof( uint ) * 8 ) + 1 );
164
165     j = 1;
166                                                                                                                                 
167     for ( i = 0; i < sizeof( uint ) * 8; i++ )                                                                          
168     {                                                                                                                           
169                                                                                                                                 
170         if ( ( bin & j ) != 0 ) {                                                                                               
171             string[ 31 - i ] = '1';                                                                                             
172         } else {                                                                                                                
173             string[ 31 - i ] = '0';                                                                                             
174         }                                                                                                                       
175                                                                                                                                 
176         j <<= 1;                                                                                                                
177     }                                                                                                                           
178                                                                                                                                 
179     string[ i ] = '\0';                                                                                                         
180                                                                                                                                 
181     return string;                                                                                                              
182 }
183
184
185 /* >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/