00001 #ifndef ASIM_AFTERBASE_H_HEADER_INCLUDED
00002 #define ASIM_AFTERBASE_H_HEADER_INCLUDED
00003
00004 #ifdef HAVE_MALLOC_H
00005 # include <malloc.h>
00006 #endif
00007 #ifdef HAVE_STDLIB_H
00008 # include <stdlib.h>
00009 #endif
00010 #if TIME_WITH_SYS_TIME
00011 # include <sys/time.h>
00012 # include <time.h>
00013 #else
00014 # if HAVE_SYS_TIME_H
00015 # include <sys/time.h>
00016 # else
00017 # include <time.h>
00018 # endif
00019 #endif
00020
00021
00022
00023 #include "xwrap.h"
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 #include <stdio.h>
00034
00035 #ifdef __cplusplus
00036 extern "C" {
00037 #endif
00038
00039
00040 #ifdef __GNUC__
00041
00042 #ifndef MIN
00043 #define MIN(x,y) \
00044 ({ const typeof(x) _x = (x); const typeof(y) _y = (y); \
00045 (void) (&_x == &_y); \
00046 _x < _y ? _x : _y; })
00047 #endif
00048 #ifndef MAX
00049 #define MAX(x,y) \
00050 ({ const typeof(x) _x = (x); const typeof(y) _y = (y); \
00051 (void) (&_x == &_y); \
00052 _x > _y ? _x : _y; })
00053 #endif
00054
00055 #define AS_ASSERT(p) ((p)==(typeof(p))0)
00056 #define AS_ASSERT_NOTVAL(p,v) ((p)!=(typeof(p))v)
00057
00058 #else
00059
00060 #define MIN(a,b) ((a)<(b) ? (a) : (b))
00061 #define MAX(a,b) ((a)>(b) ? (a) : (b))
00062 #define AS_ASSERT(p) ((p)==0)
00063 #define AS_ASSERT_NOTVAL(p,v) ((p)!=(v))
00064 #define inline
00065
00066 #endif
00067
00068 #ifdef __INTEL_COMPILER
00069 #define inline
00070 #endif
00071
00072 #ifndef max
00073 #define max(x,y) MAX(x,y)
00074 #endif
00075
00076 #ifndef min
00077 #define min(x,y) MIN(x,y)
00078 #endif
00079
00080 typedef unsigned long ASFlagType ;
00081 #define ASFLAGS_EVERYTHING 0xFFFFFFFF
00082 typedef ASFlagType ASFlagsXref[5];
00083
00084 #define get_flags(var, val) ((var) & (val))
00085 #define set_flags(var, val) ((var) |= (val))
00086 #define clear_flags(var, val) ((var) &= ~(val))
00087 #define CheckSetFlag(b,f,v) {if((b)) (f) |= (v) ; else (f) &= ~(v);}
00088
00089 #define PTR2CARD32(p) ((CARD32)(p))
00090 #define LONG2CARD32(l) ((CARD32)(l))
00091
00092 typedef struct ASMagic
00093 {
00094 unsigned long magic ;
00095 }ASMagic;
00096
00097
00098 #define get_caller_func() "unknown"
00099
00100
00101
00102 void asim_set_application_name (char *argv0);
00103 const char *asim_get_application_name();
00104
00105 #define set_application_name asim_set_application_name
00106 #define get_application_name asim_get_application_name
00107
00108
00109
00110
00111 #define OUTPUT_LEVEL_INVALID 0
00112 #define OUTPUT_LEVEL_PARSE_ERR 1
00113 #define OUTPUT_LEVEL_ERROR 1
00114 #define OUTPUT_LEVEL_WARNING 4
00115 #define OUTPUT_DEFAULT_THRESHOLD 5
00116 #define OUTPUT_LEVEL_PROGRESS OUTPUT_DEFAULT_THRESHOLD
00117 #define OUTPUT_LEVEL_ACTIVITY OUTPUT_DEFAULT_THRESHOLD
00118 #define OUTPUT_VERBOSE_THRESHOLD 6
00119 #define OUTPUT_LEVEL_DEBUG 10
00120
00121
00122
00123 unsigned int asim_get_output_threshold();
00124 unsigned int asim_set_output_threshold( unsigned int threshold );
00125 #define get_output_threshold asim_get_output_threshold
00126 #define set_output_threshold asim_set_output_threshold
00127
00128 Bool asim_show_error( const char *error_format, ...);
00129 Bool asim_show_warning( const char *warning_format, ...);
00130 Bool asim_show_progress( const char *msg_format, ...);
00131 Bool asim_show_debug( const char *file, const char *func, int line, const char *msg_format, ...);
00132
00133 #define show_error asim_show_error
00134 #define show_warning asim_show_warning
00135 #define show_progress asim_show_progress
00136 #define show_debug asim_show_debug
00137
00138 void asim_nonGNUC_debugout( const char *format, ...);
00139 void asim_nonGNUC_debugout_stub( const char *format, ...);
00140
00141
00142
00143
00144
00145 #ifdef __GNUC__
00146
00147 #if (!defined(NO_DEBUG_OUTPUT))&&(defined(LOCAL_DEBUG)||defined(DEBUG)||defined(DEBUG_ALL))
00148 #define DEBUG_OUT(format,args...) \
00149 do{ fprintf( stderr, "%s:%s:%s:%d:>" format "\n", get_application_name(), __FILE__, __FUNCTION__, __LINE__, ## args );}while(0)
00150 #else
00151 #define DEBUG_OUT(format,args...)
00152 #endif
00153
00154 #if (!defined(NO_DEBUG_OUTPUT))&&(defined(LOCAL_DEBUG)||defined(DEBUG_ALL))
00155 #define LOCAL_DEBUG_OUT(format,args...) \
00156 do{ fprintf( stderr, "%s:%s:%s:%d:>" format "\n", get_application_name(), __FILE__, __FUNCTION__, __LINE__, ## args );}while(0)
00157 #define LOCAL_DEBUG_CALLER_OUT(format,args...) \
00158 do{ fprintf( stderr, "%s:%s:%s:> called from [%s] with args(" format ")\n", get_application_name(), __FILE__, __FUNCTION__, get_caller_func(), ## args );}while(0)
00159 #else
00160 #define LOCAL_DEBUG_OUT(format,args...)
00161 #define LOCAL_DEBUG_CALLER_OUT(format,args...)
00162 #endif
00163
00164 #elif __STDC_VERSION__ >= 199901
00165
00166 #if (!defined(NO_DEBUG_OUTPUT))&&(defined(LOCAL_DEBUG)||defined(DEBUG)||defined(DEBUG_ALL))
00167 #define DEBUG_OUT(...) \
00168 do{ fprintf( stderr, "%s:%s:%s:%d:>", get_application_name(), __FILE__, __FUNCTION__, __LINE__ ); \
00169 fprintf( stderr, __VA_ARGS__); \
00170 fprintf( stderr, "\n"); \
00171 }while(0)
00172 #else
00173 #define DEBUG_OUT(...)
00174 #endif
00175
00176 #if (!defined(NO_DEBUG_OUTPUT))&&(defined(LOCAL_DEBUG)||defined(DEBUG_ALL))
00177 #define LOCAL_DEBUG_OUT(...) \
00178 do{ fprintf( stderr, "%s:%s:%s:%d:>", get_application_name(), __FILE__, __FUNCTION__, __LINE__ ); \
00179 fprintf( stderr, __VA_ARGS__); \
00180 fprintf( stderr, "\n"); \
00181 }while(0)
00182 #define LOCAL_DEBUG_CALLER_OUT(...) \
00183 do{ fprintf( stderr, "%s:%s:%s:> called from [%s] with args(", get_application_name(), __FILE__, get_caller_func() ); \
00184 fprintf( stderr, __VA_ARGS__); \
00185 fprintf( stderr, ")\n"); \
00186 }while(0)
00187 #else
00188 #define LOCAL_DEBUG_OUT(...)
00189 #define LOCAL_DEBUG_CALLER_OUT(...)
00190 #endif
00191
00192 #else
00193
00194 #if (!defined(NO_DEBUG_OUTPUT))&&(defined(LOCAL_DEBUG)||defined(DEBUG)||defined(DEBUG_ALL))
00195 #define DEBUG_OUT asim_nonGNUC_debugout
00196 #else
00197 #define DEBUG_OUT asim_nonGNUC_debugout_stub
00198 #endif
00199
00200 #if (!defined(NO_DEBUG_OUTPUT))&&(defined(LOCAL_DEBUG)||defined(DEBUG_ALL))
00201 #define LOCAL_DEBUG_OUT asim_nonGNUC_debugout
00202 #define LOCAL_DEBUG_CALLER_OUT asim_nonGNUC_debugout_stub
00203 #else
00204 #define LOCAL_DEBUG_OUT asim_nonGNUC_debugout_stub
00205 #define LOCAL_DEBUG_CALLER_OUT asim_nonGNUC_debugout_stub
00206 #endif
00207
00208 #endif
00209
00210 #if defined(DO_CLOCKING) && !defined(NO_DEBUG_OUTPUT)
00211 #define START_TIME(started) time_t started = clock()
00212 #define SHOW_TIME(s,started) fprintf (stderr, "%s " s " time (clocks): %lu mlsec\n", __FUNCTION__, ((clock () - (started))*100)/CLOCKS_PER_SEC)
00213 #else
00214 #define START_TIME(started) unsigned long started = 0
00215 #define SHOW_TIME(s,started) started = 0
00216 #endif
00217
00218
00219 #define safemalloc(s) malloc(s)
00220 #define safecalloc(c,s) calloc(c,s)
00221 #define safefree(m) free(m)
00222 #define NEW(a) ((a *)malloc(sizeof(a)))
00223 #define NEW_ARRAY_ZEROED(a,b) ((a *)calloc((b), sizeof(a)))
00224 #define NEW_ARRAY(a,b) ((a *)malloc((b)*sizeof(a)))
00225
00226
00227
00228 char *asim_mystrdup (const char *str);
00229 char *asim_mystrndup(const char *str, size_t n);
00230 int asim_mystrcasecmp (const char *s1, const char *s2);
00231 int asim_mystrncasecmp (const char *s1, const char *s2, size_t n);
00232 #define mystrdup(s) asim_mystrdup(s)
00233 #define mystrndup(s,n) asim_mystrndup(s,n)
00234 #define mystrncasecmp(s,s2,n) asim_mystrncasecmp(s,s2,n)
00235 #define mystrcasecmp(s,s2) asim_mystrcasecmp(s,s2)
00236
00237
00238 #if !defined(S_IFREG) || !defined(S_IFDIR)
00239 # include <sys/stat.h>
00240 #endif
00241
00242 #ifndef _WIN32
00243 struct direntry
00244 {
00245 mode_t d_mode;
00246 time_t d_mtime;
00247 char d_name[1];
00248 };
00249 #endif
00250 int asim_check_file_mode (const char *file, int mode);
00251 #define CheckFile(f) asim_check_file_mode(f,S_IFREG)
00252 #define CheckDir(d) asim_check_file_mode(d,S_IFDIR)
00253 char *asim_put_file_home (const char *path_with_home);
00254 #define put_file_home(p) asim_put_file_home(p)
00255 char *asim_load_file (const char *realfilename);
00256 #define load_file(r) asim_load_file(r)
00257 char *asim_load_binary_file(const char* realfilename, long *file_size_return);
00258 #define load_binary_file(r,s) asim_load_binary_file(r,s)
00259 #ifndef _WIN32
00260 int asim_my_scandir_ext ( const char *dirname, int (*filter_func) (const char *),
00261 Bool (*handle_direntry_func)( const char *fname, const char *fullname, struct stat *stat_info, void *aux_data),
00262 void *aux_data);
00263 #define my_scandir_ext(d,f,h,a) asim_my_scandir_ext((d),(f),(h),(a))
00264 #endif
00265
00266 void unix_path2dos_path( char *path );
00267 char *asim_find_file (const char *file, const char *pathlist, int type);
00268 #define find_file(f,p,t) asim_find_file(f,p,t)
00269 char *asim_copy_replace_envvar (char *path);
00270 #define copy_replace_envvar(p) asim_copy_replace_envvar(p)
00271
00272 const char *asim_parse_argb_color( const char *color, CARD32 *pargb );
00273 #define parse_argb_color(c,p) asim_parse_argb_color((c),(p))
00274
00275 double asim_parse_math(const char* str, char** endptr, double size);
00276 #define parse_math(s,e,sz) asim_parse_math((s),(e),(sz))
00277
00278 #ifdef __hpux
00279 #define PORTABLE_SELECT(w,i,o,e,t) select((w),(int *)(i),(int *)(o),(e),(t))
00280 #else
00281 #define PORTABLE_SELECT(w,i,o,e,t) select((w),(i),(o),(e),(t))
00282 #endif
00283
00284
00285 #ifdef WORDS_BIGENDIAN
00286 #define as_ntohl(ui32) (ui32)
00287 #define as_hlton(ui32) (ui32)
00288 #define as_ntohl16(ui16) (ui16)
00289 #define as_hlton16(ui16) (ui16)
00290 #else
00291 #define as_ntohl(ui32) ((((ui32)&0x000000FF)<<24)|(((ui32)&0x0000FF00)<<8)|(((ui32)&0x00FF0000)>>8)|(((ui32)&0xFF000000)>>24))
00292 #define as_hlton(ui32) as_ntohl(ui32)
00293 #define as_ntohl16(ui16) ((((ui16)&0x00FF)<<8)|(((ui16)&0xFF00)>>8))
00294 #define as_hlton16(ui16) as_ntohl(ui16)
00295 #endif
00296
00297 #if 0
00298 typedef union ASHashableValue
00299 {
00300 unsigned long long_val;
00301 char *string_val;
00302 void *ptr ;
00303 }
00304 ASHashableValue;
00305 #else
00306 typedef unsigned long ASHashableValue;
00307 #endif
00308
00309 typedef union ASHashData
00310 {
00311 void *vptr ;
00312 int *iptr ;
00313 unsigned int *uiptr ;
00314 long *lptr ;
00315 unsigned long *ulptr ;
00316 char *cptr ;
00317 int i ;
00318 unsigned int ui ;
00319 long l ;
00320 unsigned long ul ;
00321 CARD32 c32 ;
00322 CARD16 c16 ;
00323 CARD8 c8 ;
00324 }ASHashData;
00325
00326 #define AS_HASHABLE(v) ((ASHashableValue)((unsigned long)(v)))
00327
00328 typedef struct ASHashItem
00329 {
00330 struct ASHashItem *next;
00331 ASHashableValue value;
00332 void *data;
00333
00334 }
00335 ASHashItem;
00336
00337 typedef unsigned short ASHashKey;
00338 typedef ASHashItem *ASHashBucket;
00339
00340 typedef struct ASHashTable
00341 {
00342 ASHashKey size;
00343 ASHashBucket *buckets;
00344 ASHashKey buckets_used;
00345 unsigned long items_num;
00346
00347 ASHashItem *most_recent ;
00348
00349 ASHashKey (*hash_func) (ASHashableValue value, ASHashKey hash_size);
00350 long (*compare_func) (ASHashableValue value1, ASHashableValue value2);
00351 void (*item_destroy_func) (ASHashableValue value, void *data);
00352 }
00353 ASHashTable;
00354
00355 typedef enum
00356 {
00357
00358 ASH_BadParameter = -3,
00359 ASH_ItemNotExists = -2,
00360 ASH_ItemExistsDiffer = -1,
00361 ASH_ItemExistsSame = 0,
00362 ASH_Success = 1
00363 }
00364 ASHashResult;
00365
00366 void asim_init_ashash (ASHashTable * hash, Bool freeresources);
00367 ASHashTable *asim_create_ashash (ASHashKey size,
00368 ASHashKey (*hash_func) (ASHashableValue, ASHashKey),
00369 long (*compare_func) (ASHashableValue, ASHashableValue),
00370 void (*item_destroy_func) (ASHashableValue, void *));
00371 void asim_destroy_ashash (ASHashTable ** hash);
00372 ASHashResult asim_add_hash_item (ASHashTable * hash, ASHashableValue value, void *data);
00373 ASHashResult asim_get_hash_item (ASHashTable * hash, ASHashableValue value, void **trg);
00374 ASHashResult asim_remove_hash_item (ASHashTable * hash, ASHashableValue value, void **trg, Bool destroy);
00375
00376 void asim_flush_ashash_memory_pool();
00377 ASHashKey asim_string_hash_value (ASHashableValue value, ASHashKey hash_size);
00378 long asim_string_compare (ASHashableValue value1, ASHashableValue value2);
00379 void asim_string_destroy_without_data (ASHashableValue value, void *data);
00380
00381 ASHashKey asim_casestring_hash_value (ASHashableValue value, ASHashKey hash_size);
00382 long asim_casestring_compare (ASHashableValue value1, ASHashableValue value2);
00383
00384 ASHashKey asim_pointer_hash_value (ASHashableValue value, ASHashKey hash_size);
00385
00386 #define init_ashash(h,f) asim_init_ashash(h,f)
00387 #define create_ashash(s,h,c,d) asim_create_ashash(s,h,c,d)
00388 #define destroy_ashash(h) asim_destroy_ashash(h)
00389 #define add_hash_item(h,v,d) asim_add_hash_item(h,v,d)
00390 #define get_hash_item(h,v,t) asim_get_hash_item(h,v,t)
00391 #define remove_hash_item(h,v,t,d) asim_remove_hash_item(h,v,t,d)
00392 #define flush_ashash_memory_pool asim_flush_ashash_memory_pool
00393
00394 #define string_hash_value asim_string_hash_value
00395 #define pointer_hash_value asim_pointer_hash_value
00396 #define string_compare asim_string_compare
00397 #define string_destroy_without_data asim_string_destroy_without_data
00398 #define casestring_hash_value asim_casestring_hash_value
00399 #define casestring_compare asim_casestring_compare
00400
00401
00402 void asim_start_ticker (unsigned int size);
00403 void asim_wait_tick ();
00404 #define start_ticker asim_start_ticker
00405 #define wait_tick asim_wait_tick
00406
00407
00408
00409
00410 #define xml_tagchar(a) (isalnum(a) || (a) == '-' || (a) == '_')
00411
00412 #define XML_CDATA_STR "CDATA"
00413 #define XML_CONTAINER_STR "CONTAINER"
00414 #define XML_CDATA_ID -2
00415 #define XML_CONTAINER_ID -1
00416 #define XML_UNKNOWN_ID 0
00417
00418 #define IsCDATA(pe) ((pe) && (pe)->tag_id == XML_CDATA_ID)
00419
00420 typedef struct xml_elem_t {
00421 struct xml_elem_t* next;
00422 struct xml_elem_t* child;
00423 char* tag;
00424 int tag_id;
00425 char* parm;
00426 } xml_elem_t;
00427
00428 typedef enum
00429 {
00430 ASXML_Start = 0,
00431 ASXML_TagOpen = 1,
00432 ASXML_TagName = 2,
00433 ASXML_TagAttrOrClose = 3,
00434 ASXML_AttrName = 4,
00435 ASXML_AttrEq = 5,
00436 ASXML_AttrValueStart = 6,
00437 ASXML_AttrValue = 7,
00438 ASXML_AttrSlash = 8
00439 } ASXML_ParserState;
00440
00441 typedef enum
00442 {
00443 ASXML_BadStart = -1,
00444 ASXML_BadTagName = -2,
00445 ASXML_UnexpectedSlash = -3,
00446 ASXML_UnmatchedClose = -4,
00447 ASXML_BadAttrName = -5,
00448 ASXML_MissingAttrEq = -6
00449 } ASXML_ParserError;
00450
00451 typedef struct ASXmlBuffer
00452 {
00453 char *buffer ;
00454 int allocated, used, current ;
00455
00456 int state ;
00457 int level ;
00458 Bool verbatim;
00459 Bool quoted;
00460
00461 enum
00462 {
00463 ASXML_OpeningTag = 0,
00464 ASXML_SimpleTag,
00465 ASXML_ClosingTag
00466 }tag_type ;
00467
00468 int tags_count ;
00469 }ASXmlBuffer;
00470
00471 void asim_asxml_var_insert(const char* name, int value);
00472 int asim_asxml_var_get(const char* name);
00473 void asim_asxml_var_init(void);
00474 void asim_asxml_var_cleanup(void);
00475
00476 xml_elem_t* asim_xml_parse_parm(const char* parm, struct ASHashTable *vocabulary);
00477
00478 void asim_xml_elem_delete(xml_elem_t** list, xml_elem_t* elem);
00479 xml_elem_t* asim_xml_parse_doc(const char* str, struct ASHashTable *vocabulary);
00480 int asim_xml_parse(const char* str, xml_elem_t* current, struct ASHashTable *vocabulary);
00481
00482 void asim_reset_xml_buffer( ASXmlBuffer *xb );
00483 void asim_free_xml_buffer_resources (ASXmlBuffer *xb);
00484
00485 void asim_add_xml_buffer_chars( ASXmlBuffer *xb, char *tmp, int len );
00486 int asim_spool_xml_tag( ASXmlBuffer *xb, char *tmp, int len );
00487
00488 Bool asim_xml_tags2xml_buffer (xml_elem_t *tags, ASXmlBuffer *xb, int tags_count, int depth);
00489 void asim_xml_print (xml_elem_t* root);
00490 xml_elem_t *asim_format_xml_buffer_state (ASXmlBuffer *xb);
00491
00492 char *asim_interpret_ctrl_codes( char *text );
00493
00494 #define asxml_var_insert(n,v) asim_asxml_var_insert((n),(v))
00495 #define asxml_var_get(n) asim_asxml_var_get((n))
00496 #define asxml_var_init asim_asxml_var_init
00497 #define asxml_var_cleanup asim_asxml_var_cleanup
00498
00499 #define xml_parse_parm(p,v) asim_xml_parse_parm((p),(v))
00500
00501 #define xml_elem_delete(l,e) asim_xml_elem_delete((l),(e))
00502 #define xml_parse_doc(s,v) asim_xml_parse_doc((s),(v))
00503 #define xml_parse(s,c,v) asim_xml_parse((s),(c),(v))
00504
00505 #define reset_xml_buffer(xb) asim_reset_xml_buffer((xb))
00506 #define free_xml_buffer_resources(xb) asim_free_xml_buffer_resources((xb))
00507
00508 #define add_xml_buffer_chars(xb,t,l) asim_add_xml_buffer_chars((xb),(t),(l))
00509 #define spool_xml_tag(xb,t,l) asim_spool_xml_tag((xb),(t),(l))
00510
00511 #define xml_tags2xml_buffer(t,xb,tc,d) asim_xml_tags2xml_buffer((t),(xb),(tc),(d))
00512 #define xml_print(r) asim_xml_print((r))
00513 #define format_xml_buffer_state(xb) asim_format_xml_buffer_state((xb))
00514
00515 #define interpret_ctrl_codes(t) asim_interpret_ctrl_codes((t))
00516
00517 #ifdef __cplusplus
00518 }
00519 #endif
00520
00521
00522 #endif
00523