5 /* >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> ERROR HANDLING <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/
10 /* Martin A. Hansen, May 2008 */
12 /* Print error message and exits. */
14 fprintf( stderr, "ERROR: %s\n", msg );
20 void warn( char *msg )
22 /* Martin A. Hansen, May 2008 */
24 /* Print warning message and exits. */
26 fprintf( stderr, "WARNING: %s\n", msg );
30 /* >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> MEMORY HANDLING <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/
33 void *mem_get( size_t size )
35 /* Martin A. Hansen, May 2008 */
37 /* Allocate a given chunk of memory to a pointer that is returned. */
42 die( "could not allocate 0 bytes of memory." );
43 } else if ( ( pt = malloc( size ) ) == NULL ) {
44 die( "could not allocate memory." );
51 void *mem_get_zero( size_t size )
53 /* Martin A. Hansen, May 2008 */
55 /* Allocate a given chunk of zero'ed memory to a pointer that is returned. */
60 die( "could not allocate 0 bytes of memory." );
61 } else if ( ( pt = malloc( size ) ) == NULL ) {
62 die( "could not allocate memory." );
65 memset( pt, '\0', size );
71 void *mem_resize( void *pt, size_t size )
73 /* Martin A. Hansen, May 2008 */
75 /* Resize an allocated chunk of memory for a given pointer and new size. */
80 die( "could not re-allocate 0 bytes of memory." );
81 } else if ( ( pt_new = realloc( pt, size ) ) == NULL ) {
82 die( "could not re-allocate memory." );
89 void *mem_resize_zero( void *pt, size_t old_size, size_t new_size )
91 /* Martin A. Hansen, May 2008 */
93 /* Resize an allocated chunk of memory for a given pointer and zero any extra memory. */
97 pt_new = mem_resize( pt, new_size );
99 if ( new_size > old_size ) {
100 memset( ( ( void * ) pt_new ) + old_size, '\0', new_size - old_size );
107 void *mem_clone( void *old_pt, size_t size )
109 /* Martin A. Hansen, June 2008 */
111 /* Clone a structure in memory and return a pointer to the clone. */
115 new_pt = mem_get( size );
117 memcpy( new_pt, old_pt, size );
123 void mem_free( void *pt )
125 /* Martin A. Hansen, May 2008 */
127 /* Free memory from a given pointer. */
138 /* >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> ARRAYS <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/
141 bool binary_search_array( int *array, int array_size, int val )
143 /* Martin A. Hansen, June 2008 */
145 /* Binary search an array of integers for an integer value. */
156 try = ( ( high + low ) / 2 );
158 if ( val < array[ try ] ) {
160 } else if ( val > array[ try ] ) {
171 /* >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> MISC <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/
174 void chop( char *string )
176 /* Martin A. Hansen, June 2008 */
178 /* Removes the last char from a string. */
182 len = strlen( string );
184 string[ len - 1 ] = '\0';
188 void chomp( char *string )
190 /* Martin A. Hansen, June 2008 */
192 /* Removes the last char from a string if the char is a newline. */
196 len = strlen( string );
198 if ( string[ len - 1 ] == '\n' ) {
199 string[ len - 1 ] = '\0';
204 void split( char *string, char delimit, struct list **fields )
206 /* Martin A. Hansen, June 2008 */
208 /* Split a given line and a delimiter return the split result as a list. */
213 char field[ 256 ] = "";
218 for ( i = 0; string[ i ]; i++ )
220 if ( string[ i ] != delimit )
222 field[ j ] = string[ i ];
228 field_copy = mem_clone( field, j + 1 );
230 list_add( fields, field_copy );
238 field_copy = mem_clone( field, j + 1 );
240 list_add( fields, field_copy );
242 list_reverse( fields );
246 char *substr( char *string, int offset, int len )
248 /* Martin A. Hansen, May 2008 */
250 /* Create equavalent of Perls substr command. */
251 /* Currently implemented without optional length */
252 /* and the replace feature. */
259 string_len = strlen( string );
262 die( "substr offset < 0." );
263 } else if ( len < 0 ) {
264 die( "substr length < 0." );
265 } else if ( offset > string_len ) {
266 die( "substr offset outside string." );
267 } else if ( offset + len > string_len ) {
268 die( "substr offset + len outside string." );
271 substr = mem_get( len + 1 );
276 while ( i < offset + len )
278 substr[ j ] = string[ i ];
290 char *bits2string( uint bin )
292 /* Martin A. Hansen, June 2008 */
294 /* Return a binary number as a string of 1's and 0's. */
300 string = mem_get( ( sizeof( uint ) * 8 ) + 1 );
304 for ( i = 0; i < sizeof( uint ) * 8; i++ )
307 if ( ( bin & j ) != 0 ) {
308 string[ 31 - i ] = '1';
310 string[ 31 - i ] = '0';
322 /* >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/