added missing macro for bag
[wmaker-crm.git] / WINGs / WUtil.h
blobd1bc88e9e14161733f32461cfc1359c7a6cfc47e
1 #ifndef _WUTIL_H_
2 #define _WUTIL_H_
4 #include <X11/Xlib.h>
6 #include <sys/types.h>
8 /* SunOS 4.x Blargh.... */
9 #ifndef NULL
10 #define NULL ((void*)0)
11 #endif
15 * Warning: proplist.h #defines BOOL which will clash with the
16 * typedef BOOL in Xmd.h
17 * proplist.h should use Bool (which is a #define in Xlib.h) instead.
20 #include <proplist.h>
23 #ifndef WMAX
24 # define WMAX(a,b) ((a)>(b) ? (a) : (b))
25 #endif
26 #ifndef WMIN
27 # define WMIN(a,b) ((a)<(b) ? (a) : (b))
28 #endif
31 #if (!defined (__GNUC__) || __GNUC__ < 2 || \
32 __GNUC_MINOR__ < (defined (__cplusplus) ? 6 : 4))
33 #define __ASSERT_FUNCTION ((char *) 0)
34 #else
35 #define __ASSERT_FUNCTION __PRETTY_FUNCTION__
36 #endif
39 #ifdef NDEBUG
41 #define wassertr(expr) {}
42 #define wassertrv(expr, val) {}
44 #else /* !NDEBUG */
46 #ifdef DEBUG
48 #include <assert.h>
50 #define wassertr(expr) assert(expr)
52 #define wassertrv(expr, val) assert(expr)
54 #else /* !DEBUG */
56 #define wassertr(expr) \
57 if (!(expr)) { \
58 wwarning("%s line %i (%s): assertion %s failed",\
59 __FILE__, __LINE__, __ASSERT_FUNCTION, #expr);\
60 return;\
63 #define wassertrv(expr, val) \
64 if (!(expr)) { \
65 wwarning("%s line %i (%s): assertion %s failed",\
66 __FILE__, __LINE__, __ASSERT_FUNCTION, #expr);\
67 return (val);\
69 #endif /* !DEBUG */
71 #endif /* !NDEBUG */
74 #ifdef __cplusplus
75 extern "C" {
76 #endif /* __cplusplus */
79 typedef enum {
80 WMPostWhenIdle = 1,
81 WMPostASAP = 2,
82 WMPostNow = 3
83 } WMPostingStyle;
86 typedef enum {
87 WNCNone = 0,
88 WNCOnName = 1,
89 WNCOnSender = 2
90 } WMNotificationCoalescing;
93 /* The possible states for connections */
94 typedef enum {
95 WCNotConnected=0,
96 WCListening,
97 WCInProgress,
98 WCFailed,
99 WCConnected,
100 WCDied,
101 WCClosed
102 } WMConnectionState;
105 typedef struct W_Bag WMBag;
106 typedef struct W_Data WMData;
107 typedef struct W_HashTable WMHashTable;
108 typedef struct W_UserDefaults WMUserDefaults;
109 typedef struct W_Notification WMNotification;
110 typedef struct W_NotificationQueue WMNotificationQueue;
111 typedef struct W_Host WMHost;
112 typedef struct W_Connection WMConnection;
116 typedef struct {
117 int position;
118 int count;
119 } WMRange;
123 /* DO NOT ACCESS THE CONTENTS OF THIS STRUCT */
124 typedef struct {
125 void *table;
126 void *nextItem;
127 int index;
128 } WMHashEnumerator;
131 typedef struct {
132 /* NULL is pointer hash */
133 unsigned (*hash)(const void *);
134 /* NULL is pointer compare */
135 Bool (*keyIsEqual)(const void *, const void *);
136 /* NULL does nothing */
137 void* (*retainKey)(const void *);
138 /* NULL does nothing */
139 void (*releaseKey)(const void *);
140 } WMHashTableCallbacks;
143 typedef struct W_BagFunctions {
144 int (*getItemCount)(WMBag *self);
145 int (*appendBag)(WMBag *self, WMBag *bag);
146 int (*putInBag)(WMBag *self, void *item);
147 int (*insertInBag)(WMBag *self, int index, void *item);
148 int (*removeFromBag)(WMBag *bag, void *item);
149 int (*deleteFromBag)(WMBag *bag, int index);
150 void *(*getFromBag)(WMBag *bag, int index);
151 int (*firstInBag)(WMBag *bag, void *item);
152 int (*countInBag)(WMBag *bag, void *item);
153 void *(*replaceInBag)(WMBag *bag, int index, void *item);
154 void (*sortBag)(WMBag *bag, int (*comparer)(const void*, const void*));
155 void (*emptyBag)(WMBag *bag);
156 void (*freeBag)(WMBag *bag);
157 WMBag *(*mapBag)(WMBag *bag, void * (*function)(void*));
158 int (*findInBag)(WMBag *bag, int (*match)(void*));
159 void *(*first)(WMBag *bag, void **ptr);
160 void *(*last)(WMBag *bag, void **ptr);
161 void *(*next)(WMBag *bag, void **ptr);
162 void *(*previous)(WMBag *bag, void **ptr);
163 } W_BagFunctions;
166 struct W_Bag {
167 void *data;
169 void (*destructor)(void *item);
171 W_BagFunctions func;
176 #if 0
177 typedef struct {
178 char character; /* the escape character */
179 char *value; /* value to place */
180 } WMSEscapes;
181 #endif
184 /* The connection callbacks */
185 typedef struct ConnectionDelegate {
186 void *data;
188 void (*didCatchException)(struct ConnectionDelegate *self,
189 WMConnection *cPtr);
191 void (*didDie)(struct ConnectionDelegate *self, WMConnection *cPtr);
193 void (*didInitialize)(struct ConnectionDelegate *self, WMConnection *cPtr);
195 void (*didReceiveInput)(struct ConnectionDelegate *self, WMConnection *cPtr);
197 void (*didTimeout)(struct ConnectionDelegate *self, WMConnection *cPtr);
199 } ConnectionDelegate;
202 typedef void WMNotificationObserverAction(void *observerData,
203 WMNotification *notification);
207 /*......................................................................*/
209 typedef void (waborthandler)(int);
211 waborthandler *wsetabort(waborthandler*);
214 /* don't free the returned string */
215 char *wstrerror(int errnum);
217 void wfatal(const char *msg, ...);
218 void wwarning(const char *msg, ...);
219 void wsyserror(const char *msg, ...);
220 void wsyserrorwithcode(int error, const char *msg, ...);
222 char *wfindfile(char *paths, char *file);
224 char *wfindfileinlist(char **path_list, char *file);
226 char *wfindfileinarray(proplist_t array, char *file);
228 char *wexpandpath(char *path);
230 /* don't free the returned string */
231 char *wgethomedir();
233 void *wmalloc(size_t size);
234 void *wrealloc(void *ptr, size_t newsize);
235 void wfree(void *ptr);
238 void wrelease(void *ptr);
239 void *wretain(void *ptr);
241 char *wstrdup(char *str);
243 char *wstrappend(char *dst, char *src);
245 char *wusergnusteppath();
247 char *wdefaultspathfordomain(char *domain);
249 void wusleep(unsigned int microsec);
251 #if 0
252 int wsprintesc(char *buffer, int length, char *format, WMSEscapes **escapes,
253 int count);
254 #endif
256 /*......................................................................*/
258 /* This function is used _only_ if you create a NON-GUI program.
259 * For GUI based programs use WMNextEvent()/WMHandleEvent() instead.
260 * This function will handle all input/timer/idle events, then return.
263 void WHandleEvents();
265 /*......................................................................*/
268 WMHashTable *WMCreateHashTable(WMHashTableCallbacks callbacks);
270 void WMFreeHashTable(WMHashTable *table);
272 void WMResetHashTable(WMHashTable *table);
274 void *WMHashGet(WMHashTable *table, const void *key);
276 /* put data in table, replacing already existing data and returning
277 * the old value */
278 void *WMHashInsert(WMHashTable *table, void *key, void *data);
280 void WMHashRemove(WMHashTable *table, const void *key);
282 /* warning: do not manipulate the table while using these functions */
283 WMHashEnumerator WMEnumerateHashTable(WMHashTable *table);
285 void *WMNextHashEnumeratorItem(WMHashEnumerator *enumerator);
287 unsigned WMCountHashTable(WMHashTable *table);
292 /* some predefined callback sets */
294 extern const WMHashTableCallbacks WMIntHashCallbacks;
295 /* sizeof(keys) are <= sizeof(void*) */
297 extern const WMHashTableCallbacks WMStringHashCallbacks;
298 /* keys are strings. Strings will be copied with wstrdup()
299 * and freed with free() */
301 extern const WMHashTableCallbacks WMStringPointerHashCallbacks;
302 /* keys are strings, bug they are not copied */
305 /*......................................................................*/
308 WMBag* WMCreateArrayBag(int size);
309 WMBag* WMCreateArrayBagWithDestructor(int size, void (*destructor)(void*));
312 WMBag *WMCreateListBag(void);
313 WMBag *WMCreateListBagWithDestructor(void (*destructor)(void*));
315 #define WMCreateBag(size) WMCreateArrayBag(size)
317 #define WMGetBagItemCount(bag) bag->func.getItemCount(bag)
319 #define WMAppendBag(bag, other) bag->func.appendBag(bag, other)
321 #define WMPutInBag(bag, item) bag->func.putInBag(bag, item)
323 #define WMInsertInBag(bag, index, item) bag->func.insertInBag(bag, index, item)
325 #define WMRemoveFromBag(bag, item) bag->func.removeFromBag(bag, item)
327 #define WMDeleteFromBag(bag, index) bag->func.deleteFromBag(bag, index)
329 #define WMGetFromBag(bag, index) bag->func.getFromBag(bag, index)
331 #define WMCountInBag(bag, item) bag->func.countInBag(bag, item)
333 #define WMReplaceInBag(bag, index, item) bag->func.replaceInBag(bag, index, item)
335 /* comparer must return:
336 * < 0 if a < b
337 * > 0 if a > b
338 * = 0 if a = b
340 #define WMSortBag(bag, comparer) bag->func.sortBag(bag, comparer)
342 #define WMEmptyBag(bag) bag->func.emptyBag(bag)
344 #define WMFreeBag(bag) bag->func.freeBag(bag)
346 #define WMMapBag(bag, function) bag->func.mapBag(bag, function)
348 #define WMGetFirstInBag(bag, item) bag->func.firstInBag(bag, item)
350 #define WMCountInBag(bag, item) bag->func.countInBag(bag, item)
352 #define WMFindInBag(bag, match) bag->func.findInBag(bag, match)
354 #define WMBagFirst(bag, ptr) bag->func.first(bag, ptr)
356 #define WMBagLast(bag, ptr) bag->func.last(bag, ptr)
358 #define WMBagNext(bag, ptr) bag->func.next(bag, ptr)
360 #define WMBagPrevious(bag, ptr) bag->func.previous(bag, ptr)
363 /*-------------------------------------------------------------------------*/
365 /* WMData handling */
367 /* Creating/destroying data */
369 WMData* WMCreateDataWithCapacity(unsigned capacity);
371 WMData* WMCreateDataWithLength(unsigned length);
373 WMData* WMCreateDataWithBytes(void *bytes, unsigned length);
375 WMData* WMCreateDataWithBytesNoCopy(void *bytes, unsigned length);
377 WMData* WMCreateDataWithData(WMData *aData);
379 WMData* WMRetainData(WMData *aData);
381 void WMReleaseData(WMData *aData);
383 /* Adjusting capacity */
385 void WMSetDataCapacity(WMData *aData, unsigned capacity);
387 void WMSetDataLength(WMData *aData, unsigned length);
389 void WMIncreaseDataLengthBy(WMData *aData, unsigned extraLength);
391 /* Accessing data */
393 const void* WMDataBytes(WMData *aData);
395 void WMGetDataBytes(WMData *aData, void *buffer);
397 void WMGetDataBytesWithLength(WMData *aData, void *buffer, unsigned length);
399 void WMGetDataBytesWithRange(WMData *aData, void *buffer, WMRange aRange);
401 WMData* WMGetSubdataWithRange(WMData *aData, WMRange aRange);
403 /* Testing data */
405 Bool WMIsDataEqualToData(WMData *aData, WMData *anotherData);
407 unsigned WMGetDataLength(WMData *aData);
409 unsigned WMGetDataHash(WMData *aData);
411 /* Adding data */
413 void WMAppendDataBytes(WMData *aData, void *bytes, unsigned length);
415 void WMAppendData(WMData *aData, WMData *anotherData);
417 /* Modifying data */
419 void WMReplaceDataBytesInRange(WMData *aData, WMRange aRange, void *bytes);
421 void WMResetDataBytesInRange(WMData *aData, WMRange aRange);
423 void WMSetData(WMData *aData, WMData *anotherData);
425 /* Storing data */
428 /*--------------------------------------------------------------------------*/
431 WMNotification *WMCreateNotification(char *name, void *object, void *clientData);
433 void WMReleaseNotification(WMNotification *notification);
435 WMNotification *WMRetainNotification(WMNotification *notification);
437 void *WMGetNotificationClientData(WMNotification *notification);
439 void *WMGetNotificationObject(WMNotification *notification);
441 char *WMGetNotificationName(WMNotification *notification);
444 void WMAddNotificationObserver(WMNotificationObserverAction *observerAction,
445 void *observer, char *name, void *object);
447 void WMPostNotification(WMNotification *notification);
449 void WMRemoveNotificationObserver(void *observer);
451 void WMRemoveNotificationObserverWithName(void *observer, char *name,
452 void *object);
454 void WMPostNotificationName(char *name, void *object, void *clientData);
456 WMNotificationQueue *WMGetDefaultNotificationQueue(void);
458 WMNotificationQueue *WMCreateNotificationQueue(void);
460 void WMDequeueNotificationMatching(WMNotificationQueue *queue,
461 WMNotification *notification,
462 unsigned mask);
464 void WMEnqueueNotification(WMNotificationQueue *queue,
465 WMNotification *notification,
466 WMPostingStyle postingStyle);
468 void WMEnqueueCoalesceNotification(WMNotificationQueue *queue,
469 WMNotification *notification,
470 WMPostingStyle postingStyle,
471 unsigned coalesceMask);
474 /*......................................................................*/
476 WMUserDefaults *WMGetStandardUserDefaults(void);
478 WMUserDefaults *WMGetDefaultsFromPath(char *path);
480 void WMSynchronizeUserDefaults(WMUserDefaults *database);
482 void WMSaveUserDefaults(WMUserDefaults *database);
484 void WMEnableUDPeriodicSynchronization(WMUserDefaults *database, Bool enable);
486 proplist_t WMGetUDObjectForKey(WMUserDefaults *database, char *defaultName);
488 void WMSetUDObjectForKey(WMUserDefaults *database, proplist_t object,
489 char *defaultName);
491 void WMRemoveUDObjectForKey(WMUserDefaults *database, char *defaultName);
493 /* you can free the returned string */
494 char *WMGetUDStringForKey(WMUserDefaults *database, char *defaultName);
496 int WMGetUDIntegerForKey(WMUserDefaults *database, char *defaultName);
498 float WMGetUDFloatForKey(WMUserDefaults *database, char *defaultName);
500 Bool WMGetUDBoolForKey(WMUserDefaults *database, char *defaultName);
502 void WMSetUDStringForKey(WMUserDefaults *database, char *value,
503 char *defaultName);
505 void WMSetUDIntegerForKey(WMUserDefaults *database, int value,
506 char *defaultName);
508 void WMSetUDFloatForKey(WMUserDefaults *database, float value,
509 char *defaultName);
511 void WMSetUDBoolForKey(WMUserDefaults *database, Bool value,
512 char *defaultName);
514 proplist_t WMGetUDSearchList(WMUserDefaults *database);
516 void WMSetUDSearchList(WMUserDefaults *database, proplist_t list);
518 extern char *WMUserDefaultsDidChangeNotification;
521 /*-------------------------------------------------------------------------*/
523 /* WMHost: host handling */
525 WMHost* WMGetCurrentHost();
527 WMHost* WMGetHostWithName(char* name);
529 WMHost* WMGetHostWithAddress(char* address);
531 WMHost* WMRetainHost(WMHost *hPtr);
533 void WMReleaseHost(WMHost *hPtr);
536 * Host cache management
537 * If enabled, only one object representing each host will be created, and
538 * a shared instance will be returned by all methods that return a host.
539 * Enabled by default.
541 void WMSetHostCacheEnabled(Bool flag);
543 Bool WMIsHostCacheEnabled();
545 void WMFlushHostCache();
548 * Compare hosts: Hosts are equal if they share at least one address
550 Bool WMIsHostEqualToHost(WMHost* hPtr, WMHost* anotherHost);
553 * Host names.
554 * WMGetHostName() will return first name (official) if a host has several.
555 * WMGetHostNames() will return a R/O WMBag with all the names of the host.
557 char* WMGetHostName(WMHost* hPtr);
559 /* The returned bag is R/O. Do not modify it, and do not free it! */
560 WMBag* WMGetHostNames(WMHost* hPtr);
563 * Host addresses.
564 * Addresses are represented as "Dotted Decimal" strings, e.g. "192.42.172.1"
565 * WMGetHostAddress() will return an arbitrary address if a host has several.
566 * WMGetHostAddresses() will return a R/O WMBag with all addresses of the host.
568 char* WMGetHostAddress(WMHost* hPtr);
570 /* The returned bag is R/O. Do not modify it, and do not free it! */
571 WMBag* WMGetHostAddresses(WMHost* hPtr);
574 /*-------------------------------------------------------------------------*/
576 /* WMConnection functions */
578 WMConnection* WMCreateConnectionAsServerAtAddress(char *host, char *service,
579 char *protocol);
581 WMConnection* WMCreateConnectionToAddress(char *host, char *service,
582 char *protocol);
584 WMConnection* WMCreateConnectionToAddressAndNotify(char *host, char *service,
585 char *protocol);
587 void WMCloseConnection(WMConnection *cPtr);
589 void WMDestroyConnection(WMConnection *cPtr);
591 WMConnection* WMAcceptConnection(WMConnection *listener);
593 /* Release the returned data! */
594 WMData* WMGetConnectionAvailableData(WMConnection *cPtr);
596 int WMSendConnectionData(WMConnection *cPtr, WMData *data);
598 Bool WMEnqueueConnectionData(WMConnection *cPtr, WMData *data);
600 #define WMFlushConnection(cPtr) WMSendConnectionData((cPtr), NULL)
602 void WMSetConnectionDelegate(WMConnection *cPtr, ConnectionDelegate *delegate);
604 /* Connection info */
606 char* WMGetConnectionAddress(WMConnection *cPtr);
608 char* WMGetConnectionService(WMConnection *cPtr);
610 char* WMGetConnectionProtocol(WMConnection *cPtr);
612 void WMSetConnectionNonBlocking(WMConnection *cPtr, Bool flag);
614 void* WMGetConnectionClientData(WMConnection *cPtr);
616 void WMSetConnectionClientData(WMConnection *cPtr, void *data);
618 unsigned int WMGetConnectionFlags(WMConnection *cPtr);
620 void WMSetConnectionFlags(WMConnection *cPtr, unsigned int flags);
622 int WMGetConnectionSocket(WMConnection *cPtr);
624 WMConnectionState WMGetConnectionState(WMConnection *cPtr);
626 void WMSetConnectionSendTimeout(WMConnection *cPtr, unsigned int timeout);
629 /* Global variables */
631 extern int WCErrorCode;
634 /*-------------------------------------------------------------------------*/
638 #ifdef __cplusplus
640 #endif /* __cplusplus */
643 #endif