1 /*----------------------------------------------------------------------------
  2 Name:      xmlBlaster/src/c/util/helper.h
  3 Project:   xmlBlaster.org
  4 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file
  5 Comment:   Generic helper code, used by Queue implementation and xmlBlaster client code
  6            Don't add any queue specific or xmlBlaster client specific code!
  7 Author:    "Marcel Ruff" <xmlBlaster@marcelruff.info>
  8 -----------------------------------------------------------------------------*/
  9 #ifndef _XMLBLASTER_HELPER_H
 10 #define _XMLBLASTER_HELPER_H
 11 
 12 #include <util/basicDefs.h> /* for int64_t (C99), Dll_Export, inline, bool etc. */
 13 
 14 #if defined(_WINDOWS)
 15 #  if defined(XB_USE_PTHREADS)
 16 #     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 */
 17 #  else
 18 struct timespec {
 19         long tv_sec;
 20         long tv_nsec;
 21 };
 22 #  endif
 23 #else
 24 # include <pthread.h>
 25 # define XB_USE_PTHREADS 1
 26 #endif
 27 
 28 #ifdef __cplusplus
 29 #ifndef XMLBLASTER_C_COMPILE_AS_CPP /* 'g++ -DXMLBLASTER_C_COMPILE_AS_CPP ...' allows to compile the lib as C++ code */
 30 extern "C" {
 31 #endif
 32 #endif
 33 
 34 /** Declare function pointer to write to socket */
 35 typedef ssize_t ( * XmlBlasterWriteToSocketFunc)(void *xb, const int fd, const char *ptr, const size_t nbytes);
 36 /** Declare function pointer to read from socket */
 37 typedef ssize_t ( * XmlBlasterReadFromSocketFunc)(void *xb, const int fd, char *ptr, const size_t nbytes, XmlBlasterNumReadFunc fpNumRead, void *userP2);
 38 
 39 /**
 40  * Holds a callback function pointer and its user pointer (the 'this' pointer).
 41  */
 42 typedef struct {
 43    XmlBlasterReadFromSocketFunc readFromSocketFuncP;
 44    void *userP;
 45    /** Register listener for socket read progress, can be NULL if nobody is interested */
 46    /**
 47     * You can register a function pointer listener to be informed about the socket read progress.
 48     * The function will be called than and again during reading the socket with the currently read bytes.
 49     * Example:
 50     * <pre>
 51     * static void progress(void *numReadUserP, const size_t currBytesRead, const size_t nbytes) {
 52     *     printf("currBytesRead=%ld nbytes=%ld\n", (long)currBytesRead, (long)nbytes);
 53     * }
 54     * </pre>
 55     * The pointer may remain NULL.
 56     */
 57    XmlBlasterNumReadFunc numReadFuncP;
 58    /**
 59     * This will be looped through to numReadFuncP.
 60     */
 61    void *numReadUserP;
 62 } XmlBlasterReadFromSocketFuncHolder;
 63 
 64 /**
 65  * Holds a callback function pointer and its user pointer (the 'this' pointer, first argument).
 66  */
 67 typedef struct {
 68    XmlBlasterWriteToSocketFunc writeToSocketFuncP;
 69    void *userP;
 70 } XmlBlasterWriteToSocketFuncHolder;
 71 
 72 /**
 73  * Holds arbitrary raw data and its length
 74  */
 75 typedef struct {
 76    size_t dataLen;
 77    char *data;
 78 } BlobHolder;
 79 
 80 #define EXCEPTIONSTRUCT_ERRORCODE_LEN 56
 81 #define EXCEPTIONSTRUCT_MESSAGE_LEN 1024
 82 /**
 83  * Holds error text
 84  */
 85 typedef struct ExceptionStruct {    /* This name is need for C++ forward declaration 'struct ExceptionStruct; */
 86    int remote; /**< true if exception is from remote (changed from bool to int to be C/C++ alignment compatible) */
 87    char errorCode[EXCEPTIONSTRUCT_ERRORCODE_LEN];
 88    char message[EXCEPTIONSTRUCT_MESSAGE_LEN];
 89    /* ExceptionStruct *embedded;  who allocates/frees it? */
 90 } ExceptionStruct;
 91 Dll_Export extern void initializeExceptionStruct(ExceptionStruct *exception);
 92 Dll_Export extern void embedException(ExceptionStruct *exception, const char *newErrorCode, const char *newMessage, const ExceptionStruct *embed);
 93 Dll_Export extern const char *getExceptionStr(char *out, int outSize, const ExceptionStruct *exception);
 94 
 95 /* Must match XmlBlasterAccess.cs C# LogLevel */
 96 typedef enum XMLBLASTER_LOG_LEVEL_ENUM {
 97    /*XMLBLASTER_LOG_NOLOG=0,  don't use */
 98    XMLBLASTER_LOG_ERROR=1,  /**< supported, use for programming errors */
 99    XMLBLASTER_LOG_WARN=2,   /**< supported, use for user errors and wrong configurations */
100    XMLBLASTER_LOG_INFO=3,   /**< supported, use for success information only */
101    /*XMLBLASTER_LOG_CALL=4,  don't use */
102    /*XMLBLASTER_LOG_TIME=5,  don't use */
103    XMLBLASTER_LOG_TRACE=6,  /**< supported, use for debugging purposes */
104    XMLBLASTER_LOG_DUMP=7    /**< supported, use for debugging purposes */
105    /*XMLBLASTER_LOG_PLAIN=8  don't use */
106 } XMLBLASTER_LOG_LEVEL;
107 typedef void  ( * XmlBlasterLogging)(void *logUserP, XMLBLASTER_LOG_LEVEL currLevel, XMLBLASTER_LOG_LEVEL level, const char *location, const char *fmt, ...);
108 Dll_Export extern void xmlBlasterDefaultLogging(void *logUserP,
109                               XMLBLASTER_LOG_LEVEL currLevel,
110                               XMLBLASTER_LOG_LEVEL level,
111                               const char *location, const char *fmt, ...);
112 Dll_Export extern XMLBLASTER_LOG_LEVEL parseLogLevel(const char *logLevelStr);
113 Dll_Export extern const char *getLogLevelStr(XMLBLASTER_LOG_LEVEL logLevel);
114 Dll_Export extern bool doLog(XMLBLASTER_LOG_LEVEL currLevel, XMLBLASTER_LOG_LEVEL level);
115 
116 Dll_Export extern char getInputKey(const char *str);
117 Dll_Export extern char *getStackTrace(int maxNumOfLines);
118 Dll_Export extern void sleepMillis(long millis);
119 
120 #if defined(__FreeBSD__) || defined(__MacOSX__) || defined(__IPhoneOS__) || defined(__hpux__) || defined(__linux__)
121 #include <wchar.h>
122 #endif
123 Dll_Export extern char **convertWcsArgv(wchar_t **argv_wcs, int argc);
124 
125 Dll_Export extern void freeArgv(char **argv, int argc);
126 Dll_Export extern char *strFromBlobAlloc(const char *blob, const size_t len);
127 /**
128  * Extract token from string.
129  * Thread save variant of strtok which returns empty string for two following delimiters.
130  * <pre>
131   char *p, *savePtr, *str = strcpyAlloc("\"H,ello\",joe,,");
132   int count = 0;
133   for (p=str;; count++, p = 0) {
134    if ((token = strtok_r2(p, ",", &savePtr, '"')) == 0)
135       break;
136    printf("%d: %s\n", count, token);
137   }
138   xmlBlasterFree(str);
139  * </pre>
140  * returns
141  * <pre>
142 0: H,ello
143 1: joe
144 2:
145 3:
146  * </pre>
147  * @param src Will be spoiled after this call, must be NULL after first call
148  * @param delim Separator like ","
149  * @param saveptr Holding current position
150  * @param quotechar Typically '"', can be 0 to be ignored
151  * @return the next token
152  * @see strsep
153  */
154 Dll_Export char *strtok_r2(char *src, const char *delim, char **saveptr, const char quotechar);
155 Dll_Export extern char *strcpyAlloc(const char *src);
156 Dll_Export extern char *strcpyAlloc0(const char *src, const size_t maxLen);
157 Dll_Export extern char *strcpyRealloc(char **dest, const char *src);
158 Dll_Export extern char *strcatAlloc(char **dest, const char *src);
159 Dll_Export extern char *strcatAlloc0(char **dest, const char *src, const size_t maxLen);
160 Dll_Export extern char *strncpy0(char * const to, const char * const from, const size_t maxLen);
161 Dll_Export extern char *strncat0(char * const to, const char * const from, const size_t max);
162 Dll_Export extern int snprintf0(char *buffer, size_t sizeOfBuffer, const char *format, ...);
163 Dll_Export extern void trim(char *s);
164 Dll_Export extern void trimStart(char *s);
165 Dll_Export extern void trimEnd(char *s);
166 Dll_Export extern bool startsWith(const char * const str, const char * const token);
167 Dll_Export extern bool endsWith(const char * const str, const char * const token);
168 Dll_Export extern void xb_strerror(char *errnoStr, size_t sizeInBytes, int errnum);
169 Dll_Export extern char *toReadableDump(char *data, size_t len);
170 Dll_Export extern const char* int64ToStr(char * const buf, int64_t val);
171 Dll_Export extern bool strToInt64(int64_t *val, const char * const str);
172 Dll_Export extern bool strToLong(long *val, const char * const str);
173 Dll_Export extern bool strToULong(unsigned long *val, const char * const str);
174 Dll_Export extern bool strToInt(int *val, const char * const str);
175 Dll_Export extern BlobHolder *blobcpyAlloc(BlobHolder *blob, const char *data, size_t dataLen);
176 Dll_Export extern void freeBlobHolder(BlobHolder *blob);
177 Dll_Export extern BlobHolder *freeBlobHolderContent(BlobHolder *blob);
178 Dll_Export extern char *blobDump(BlobHolder *blob);
179 Dll_Export extern void freeBlobDump(char *blobDumpP); /* deprecated: use xmlBlasterFree() */
180 #if defined(XB_USE_PTHREADS)
181 Dll_Export extern unsigned long get_pthread_id(pthread_t t);
182 #endif
183 
184 #ifdef __cplusplus
185 #ifndef XMLBLASTER_C_COMPILE_AS_CPP
186 }
187 #endif
188 #endif
189 
190 #endif /* _XMLBLASTER_HELPER_H */


syntax highlighted by Code2HTML, v. 0.9.1