util/helper.h

Go to the documentation of this file.
00001 /*----------------------------------------------------------------------------
00002 Name:      xmlBlaster/src/c/util/helper.h
00003 Project:   xmlBlaster.org
00004 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file
00005 Comment:   Generic helper code, used by Queue implementation and xmlBlaster client code
00006            Don't add any queue specific or xmlBlaster client specific code!
00007 Author:    "Marcel Ruff" <xmlBlaster@marcelruff.info>
00008 -----------------------------------------------------------------------------*/
00009 #ifndef _XMLBLASTER_HELPER_H
00010 #define _XMLBLASTER_HELPER_H
00011 
00012 #include <util/basicDefs.h> /* for int64_t (C99), Dll_Export, inline, bool etc. */
00013 
00014 #if defined(_WINDOWS)
00015 #  if defined(XB_USE_PTHREADS)
00016 #     include <pthreads/pthread.h> /* Our pthreads.h: For timespec, for logging output of thread ID, for Windows and WinCE downloaded from http://sources.redhat.com/pthreads-win32 */
00017 #  else
00018 struct timespec {
00019         long tv_sec;
00020         long tv_nsec;
00021 };
00022 #  endif
00023 #else
00024 # include <pthread.h>
00025 # define XB_USE_PTHREADS 1
00026 #endif
00027 
00028 #ifdef __cplusplus
00029 #ifndef XMLBLASTER_C_COMPILE_AS_CPP /* 'g++ -DXMLBLASTER_C_COMPILE_AS_CPP ...' allows to compile the lib as C++ code */
00030 extern "C" {
00031 #endif
00032 #endif
00033 
00035 typedef ssize_t ( * XmlBlasterWriteToSocketFunc)(void *xb, const int fd, const char *ptr, const size_t nbytes);
00037 typedef ssize_t ( * XmlBlasterReadFromSocketFunc)(void *xb, const int fd, char *ptr, const size_t nbytes, XmlBlasterNumReadFunc fpNumRead, void *userP2);
00038 
00042 typedef struct {
00043    XmlBlasterReadFromSocketFunc readFromSocketFuncP;
00044    void *userP;
00057    XmlBlasterNumReadFunc numReadFuncP;
00061    void *numReadUserP;
00062 } XmlBlasterReadFromSocketFuncHolder;
00063 
00067 typedef struct {
00068    XmlBlasterWriteToSocketFunc writeToSocketFuncP;
00069    void *userP;
00070 } XmlBlasterWriteToSocketFuncHolder;
00071 
00075 typedef struct {
00076    size_t dataLen;
00077    char *data;
00078 } BlobHolder;
00079 
00080 #define EXCEPTIONSTRUCT_ERRORCODE_LEN 56
00081 #define EXCEPTIONSTRUCT_MESSAGE_LEN 1024
00082 
00085 typedef struct ExceptionStruct {    /* This name is need for C++ forward declaration 'struct ExceptionStruct; */
00086    int remote; 
00087    char errorCode[EXCEPTIONSTRUCT_ERRORCODE_LEN];
00088    char message[EXCEPTIONSTRUCT_MESSAGE_LEN];
00089    /* ExceptionStruct *embedded;  who allocates/frees it? */
00090 } ExceptionStruct;
00091 Dll_Export extern void initializeExceptionStruct(ExceptionStruct *exception);
00092 Dll_Export extern void embedException(ExceptionStruct *exception, const char *newErrorCode, const char *newMessage, const ExceptionStruct *embed);
00093 Dll_Export extern const char *getExceptionStr(char *out, int outSize, const ExceptionStruct *exception);
00094 
00095 /* Must match XmlBlasterAccess.cs C# LogLevel */
00096 typedef enum XMLBLASTER_LOG_LEVEL_ENUM {
00097    /*XMLBLASTER_LOG_NOLOG=0,  don't use */
00098    XMLBLASTER_LOG_ERROR=1,  
00099    XMLBLASTER_LOG_WARN=2,   
00100    XMLBLASTER_LOG_INFO=3,   
00101    /*XMLBLASTER_LOG_CALL=4,  don't use */
00102    /*XMLBLASTER_LOG_TIME=5,  don't use */
00103    XMLBLASTER_LOG_TRACE=6,  
00104    XMLBLASTER_LOG_DUMP=7    
00105    /*XMLBLASTER_LOG_PLAIN=8  don't use */
00106 } XMLBLASTER_LOG_LEVEL;
00107 typedef void  ( * XmlBlasterLogging)(void *logUserP, XMLBLASTER_LOG_LEVEL currLevel, XMLBLASTER_LOG_LEVEL level, const char *location, const char *fmt, ...);
00108 Dll_Export extern void xmlBlasterDefaultLogging(void *logUserP, 
00109                               XMLBLASTER_LOG_LEVEL currLevel,
00110                               XMLBLASTER_LOG_LEVEL level,
00111                               const char *location, const char *fmt, ...);
00112 Dll_Export extern XMLBLASTER_LOG_LEVEL parseLogLevel(const char *logLevelStr);
00113 Dll_Export extern const char *getLogLevelStr(XMLBLASTER_LOG_LEVEL logLevel);
00114 Dll_Export extern bool doLog(XMLBLASTER_LOG_LEVEL currLevel, XMLBLASTER_LOG_LEVEL level);
00115 
00116 Dll_Export extern char *getStackTrace(int maxNumOfLines);
00117 Dll_Export extern void sleepMillis(long millis);
00118 Dll_Export extern int64_t getTimestamp(void);   /* if no 'int64_t=long long' support we need a workaround */
00119 Dll_Export extern bool getAbsoluteTime(long relativeTimeFromNow, struct timespec *abstime); /* timespec forces pthread */
00120 Dll_Export extern void getCurrentTimeStr(char *timeStr, int bufSize);
00121 
00122 #if defined(__FreeBSD__) || defined(__MacOSX__)
00123 #include <wchar.h>
00124 #endif
00125 Dll_Export extern char **convertWcsArgv(wchar_t **argv_wcs, int argc);
00126 
00127 Dll_Export extern void freeArgv(char **argv, int argc);
00128 Dll_Export extern char *strFromBlobAlloc(const char *blob, const size_t len);
00129 Dll_Export extern char *strcpyAlloc(const char *src);
00130 Dll_Export extern char *strcpyRealloc(char **dest, const char *src);
00131 Dll_Export extern char *strcatAlloc(char **dest, const char *src);
00132 Dll_Export extern char *strncpy0(char * const to, const char * const from, const size_t maxLen);
00133 Dll_Export extern char *strncat0(char * const to, const char * const from, const size_t max);
00134 Dll_Export extern int snprintf0(char *buffer, size_t sizeOfBuffer, const char *format, ...);
00135 Dll_Export extern void trim(char *s);
00136 Dll_Export extern void trimStart(char *s);
00137 Dll_Export extern void trimEnd(char *s);
00138 Dll_Export extern void xb_strerror(char *errnoStr, size_t sizeInBytes, int errnum);
00139 Dll_Export extern char *toReadableDump(char *data, size_t len);
00140 Dll_Export extern const char* int64ToStr(char * const buf, int64_t val);
00141 Dll_Export extern bool strToInt64(int64_t *val, const char * const str);
00142 Dll_Export extern bool strToLong(long *val, const char * const str);
00143 Dll_Export extern bool strToULong(unsigned long *val, const char * const str);
00144 Dll_Export extern bool strToInt(int *val, const char * const str);
00145 Dll_Export extern BlobHolder *blobcpyAlloc(BlobHolder *blob, const char *data, size_t dataLen);
00146 Dll_Export extern BlobHolder *freeBlobHolderContent(BlobHolder *blob);
00147 Dll_Export extern char *blobDump(BlobHolder *blob);
00148 Dll_Export extern void freeBlobDump(char *blobDump); /* deprecated: use xmlBlasterFree() */
00149 #if defined(XB_USE_PTHREADS)
00150 Dll_Export extern long get_pthread_id(pthread_t t);
00151 #endif
00152 
00153 #ifdef __cplusplus
00154 #ifndef XMLBLASTER_C_COMPILE_AS_CPP
00155 }
00156 #endif
00157 #endif
00158 
00159 #endif /* _XMLBLASTER_HELPER_H */
00160