00001
00011 #ifndef FISH_UTIL_H
00012 #define FISH_UTIL_H
00013
00014 #include <wchar.h>
00015 #include <stdarg.h>
00016 #include <unistd.h>
00017
00021 typedef void (*func_ptr_t)();
00022
00028 typedef union
00029 {
00033 long long_val;
00037 void *ptr_val;
00041 func_ptr_t func_val;
00042 }
00043 anything_t;
00044
00045
00049 typedef struct dyn_queue
00050 {
00052 void **start;
00054 void **stop;
00056 void **put_pos;
00058 void **get_pos;
00059 }
00060 dyn_queue_t;
00061
00065 typedef struct
00066 {
00068 void *key;
00070 void *data;
00071 }
00072 hash_struct_t;
00073
00089 typedef struct hash_table
00090 {
00092 hash_struct_t *arr;
00094 int cache;
00096 int count;
00098 int size;
00100 int (*hash_func)( void *key );
00102 int (*compare_func)( void *key1, void *key2 );
00103 }
00104 hash_table_t;
00105
00112 typedef struct priority_queue
00113 {
00115 void **arr;
00117 int count;
00119 int size;
00121 int (*compare)(void *e1, void *e2);
00122 }
00123 priority_queue_t;
00124
00129 typedef struct array_list
00130 {
00134 anything_t *arr;
00135
00143 size_t pos;
00144
00148 size_t size;
00149 }
00150 array_list_t;
00151
00155 typedef struct _ll_node
00156 {
00158 struct _ll_node *next, *prev;
00160 void *data;
00161 }
00162 ll_node_t;
00163
00167 typedef struct buffer
00168 {
00169 char *buff;
00170 size_t length;
00171 size_t used;
00172 }
00173 buffer_t;
00174
00175
00180 typedef buffer_t string_buffer_t;
00181
00186 void (*util_set_oom_handler( void (*h)(void *) ))(void *);
00187
00195 void util_die_on_oom( void *p );
00196
00200 int maxi( int a, int b );
00201
00205 int mini( int a, int b );
00206
00207
00208
00209
00210
00211
00212
00218 void q_init( dyn_queue_t *q );
00219
00223 void q_destroy( dyn_queue_t *q );
00224
00228 int q_put( dyn_queue_t *q, void *e );
00229
00233 void *q_get( dyn_queue_t *q);
00234
00238 void *q_peek( dyn_queue_t *q);
00239
00243 int q_empty( dyn_queue_t *q );
00244
00248 void hash_init( hash_table_t *h,
00249 int (*hash_func)( void *key),
00250 int (*compare_func)( void *key1, void *key2 ) );
00251
00255 void hash_init2( hash_table_t *h,
00256 int (*hash_func)( void *key ),
00257 int (*compare_func)( void *key1, void *key2 ),
00258 size_t capacity);
00259
00263 void hash_destroy( hash_table_t *h );
00267 int hash_put( hash_table_t *h,
00268 const void *key,
00269 const void *data );
00273 void *hash_get( hash_table_t *h,
00274 const void *key );
00278 void *hash_get_key( hash_table_t *h,
00279 const void *key );
00280
00284 int hash_get_count( hash_table_t *h);
00293 void hash_remove( hash_table_t *h,
00294 const void *key,
00295 void **old_key,
00296 void **old_data );
00297
00301 int hash_contains( hash_table_t *h,
00302 const void *key );
00303
00307 void hash_get_keys( hash_table_t *h,
00308 array_list_t *arr );
00309
00313 void hash_get_data( hash_table_t *h,
00314 array_list_t *arr );
00315
00319 void hash_foreach( hash_table_t *h,
00320 void (*func)( void *, void * ) );
00321
00326 void hash_foreach2( hash_table_t *h, void (*func)( void *,
00327 void *,
00328 void *),
00329 void *aux );
00330
00334 int hash_str_func( void *data );
00338 int hash_str_cmp( void *a,
00339 void *b );
00340
00346 int hash_wcs_func( void *data );
00347
00351 int hash_wcs_cmp( void *a,
00352 void *b );
00353
00357 int hash_ptr_func( void *data );
00358
00359
00363 int hash_ptr_cmp( void *a,
00364 void *b );
00365
00366
00367
00374 void pq_init( priority_queue_t *q,
00375 int (*compare)(void *e1, void *e2) );
00383 int pq_put( priority_queue_t *q,
00384 void *e );
00388 void *pq_get( priority_queue_t *q );
00389
00393 void *pq_peek( priority_queue_t *q );
00394
00398 int pq_empty( priority_queue_t *q );
00399
00403 int pq_get_count( priority_queue_t *q );
00404
00408 void pq_destroy( priority_queue_t *q );
00409
00414 array_list_t *al_new();
00415
00419 void al_init( array_list_t *l );
00420
00424 void al_destroy( array_list_t *l );
00425
00434 int al_push( array_list_t *l, const void *o );
00443 int al_push_long( array_list_t *l, long o );
00451 int al_push_func( array_list_t *l, func_ptr_t f );
00452
00460 int al_push_all( array_list_t *a, array_list_t *b );
00461
00465 int al_insert( array_list_t *a, int pos, int count );
00466
00474 int al_set( array_list_t *l, int pos, const void *o );
00482 int al_set_long( array_list_t *l, int pos, long v );
00490 int al_set_func( array_list_t *l, int pos, func_ptr_t f );
00491
00499 void *al_get( array_list_t *l, int pos );
00507 long al_get_long( array_list_t *l, int pos );
00515 func_ptr_t al_get_func( array_list_t *l, int pos );
00516
00520 void al_truncate( array_list_t *l, int new_sz );
00521
00525 void *al_pop( array_list_t *l );
00529 long al_pop_long( array_list_t *l );
00533 func_ptr_t al_pop_func( array_list_t *l );
00534
00538 int al_get_count( array_list_t *l );
00539
00543 void *al_peek( array_list_t *l );
00547 long al_peek_long( array_list_t *l );
00551 func_ptr_t al_peek_func( array_list_t *l );
00552
00556 int al_empty( array_list_t *l);
00557
00561 void al_foreach( array_list_t *l, void (*func)( void * ));
00562
00567 void al_foreach2( array_list_t *l, void (*func)( void *, void *), void *aux);
00568
00603 int wcsfilecmp( const wchar_t *a, const wchar_t *b );
00604
00605
00606
00607
00608
00609
00613 void sb_init( string_buffer_t * );
00614
00618 string_buffer_t *sb_new();
00619
00623 void sb_append_substring( string_buffer_t *, const wchar_t *, size_t );
00624
00628 void sb_append_char( string_buffer_t *, wchar_t );
00629
00633 #define sb_append( sb,... ) sb_append_internal( sb, __VA_ARGS__, (void *)0 )
00634
00643 __sentinel void sb_append_internal( string_buffer_t *, ... );
00644
00650 int sb_printf( string_buffer_t *buffer, const wchar_t *format, ... );
00651
00655 int sb_vprintf( string_buffer_t *buffer, const wchar_t *format, va_list va_orig );
00656
00660 void sb_destroy( string_buffer_t * );
00661
00666 void sb_clear( string_buffer_t * );
00667
00672 void sb_truncate( string_buffer_t *, int chars_left );
00673
00677 ssize_t sb_length( string_buffer_t * );
00678
00679
00680
00681
00682
00683
00687 void b_init( buffer_t *b);
00688
00693 void b_destroy( buffer_t *b );
00694
00700 int b_append( buffer_t *b, const void *d, ssize_t len );
00701
00705 long long get_time();
00706
00707 #endif