6 /* >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> ERROR HANDLING <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/
11 /* Martin A. Hansen, May 2008 */
13 /* Print error message and exits. */
15 fprintf( stderr, "ERROR: %s\n", msg );
21 void warn( char *msg )
23 /* Martin A. Hansen, May 2008 */
25 /* Print warning message and exits. */
27 fprintf( stderr, "WARNING: %s\n", msg );
31 /* >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> ARRAYS <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/
34 bool binary_search_array( int *array, int array_size, int val )
36 /* Martin A. Hansen, June 2008 */
38 /* Binary search an array of integers for an integer value. */
49 try = ( ( high + low ) / 2 );
51 if ( val < array[ try ] ) {
53 } else if ( val > array[ try ] ) {
64 /* >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> MISC <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/
67 void split( char *string, char delimit, struct list **fields )
69 /* Martin A. Hansen, June 2008 */
71 /* Split a given line and a delimiter return the split result as a list. */
76 char field[ 256 ] = "";
81 for ( i = 0; string[ i ]; i++ )
83 if ( string[ i ] != delimit )
85 field[ j ] = string[ i ];
91 field_copy = mem_clone( field, j + 1 );
93 list_add( fields, field_copy );
101 field_copy = mem_clone( field, j + 1 );
103 list_add( fields, field_copy );
105 list_reverse( fields );
109 char *substr( char *string, int offset, int len )
111 /* Martin A. Hansen, May 2008 */
113 /* Create equavalent of Perls substr command. */
114 /* Currently implemented without optional length */
115 /* and the replace feature. */
122 string_len = strlen( string );
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." );
134 substr = mem_get( len + 1 );
139 while ( i < offset + len )
141 substr[ j ] = string[ i ];
153 char *bits2string( uint bin )
155 /* Martin A. Hansen, June 2008 */
157 /* Return a binary number as a string of 1's and 0's. */
163 string = mem_get( ( sizeof( uint ) * 8 ) + 1 );
167 for ( i = 0; i < sizeof( uint ) * 8; i++ )
170 if ( ( bin & j ) != 0 ) {
171 string[ 31 - i ] = '1';
173 string[ 31 - i ] = '0';
185 /* >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/