nice additions
[wmaker-crm.git] / WINGs / WUtil.h
blob368c554b3d1b068b2bc3f892fa9a711d5f79592d
1 #ifndef _WUTIL_H_
2 #define _WUTIL_H_
4 #include <X11/Xlib.h>
5 #include <limits.h>
6 #include <sys/types.h>
8 /* SunOS 4.x Blargh.... */
9 #ifndef NULL
10 #define NULL ((void*)0)
11 #endif
14 * Warning: proplist.h #defines BOOL which will clash with the
15 * typedef BOOL in Xmd.h
16 * proplist.h should use Bool (which is a #define in Xlib.h) instead.
19 #include <proplist.h>
22 #ifndef WMAX
23 # define WMAX(a,b) ((a)>(b) ? (a) : (b))
24 #endif
25 #ifndef WMIN
26 # define WMIN(a,b) ((a)<(b) ? (a) : (b))
27 #endif
30 #if (!defined (__GNUC__) || __GNUC__ < 2 || \
31 __GNUC_MINOR__ < (defined (__cplusplus) ? 6 : 4))
32 #define __ASSERT_FUNCTION ((char *) 0)
33 #else
34 #define __ASSERT_FUNCTION __PRETTY_FUNCTION__
35 #endif
38 #ifdef NDEBUG
40 #define wassertr(expr) {}
41 #define wassertrv(expr, val) {}
43 #else /* !NDEBUG */
45 #ifdef DEBUG
47 #include <assert.h>
49 #define wassertr(expr) assert(expr)
51 #define wassertrv(expr, val) assert(expr)
53 #else /* !DEBUG */
55 #define wassertr(expr) \
56 if (!(expr)) { \
57 wwarning("%s line %i (%s): assertion %s failed",\
58 __FILE__, __LINE__, __ASSERT_FUNCTION, #expr);\
59 return;\
62 #define wassertrv(expr, val) \
63 if (!(expr)) { \
64 wwarning("%s line %i (%s): assertion %s failed",\
65 __FILE__, __LINE__, __ASSERT_FUNCTION, #expr);\
66 return (val);\
68 #endif /* !DEBUG */
70 #endif /* !NDEBUG */
73 #ifdef __cplusplus
74 extern "C" {
75 #endif /* __cplusplus */
78 typedef enum {
79 WMPostWhenIdle = 1,
80 WMPostASAP = 2,
81 WMPostNow = 3
82 } WMPostingStyle;
85 typedef enum {
86 WNCNone = 0,
87 WNCOnName = 1,
88 WNCOnSender = 2
89 } WMNotificationCoalescing;
92 /* The possible states for connections */
93 typedef enum {
94 WCNotConnected=0,
95 WCListening,
96 WCInProgress,
97 WCFailed,
98 WCConnected,
99 WCTimedOut,
100 WCDied,
101 WCClosed
102 } WMConnectionState;
105 /* The possible states for connection timeouts */
106 typedef enum {
107 WCTNone=0,
108 WCTWhileOpening,
109 WCTWhileSending
110 } WMConnectionTimeoutState;
114 enum {
115 WBNotFound = INT_MIN /* element was not found in bag */
119 typedef struct W_Bag WMBag;
120 typedef struct W_Data WMData;
121 typedef struct W_HashTable WMHashTable;
122 typedef struct W_UserDefaults WMUserDefaults;
123 typedef struct W_Notification WMNotification;
124 typedef struct W_NotificationQueue WMNotificationQueue;
125 typedef struct W_Host WMHost;
126 typedef struct W_Connection WMConnection;
130 typedef void WMFreeDataProc(void *data);
134 typedef struct {
135 int position;
136 int count;
137 } WMRange;
141 /* DO NOT ACCESS THE CONTENTS OF THIS STRUCT */
142 typedef struct {
143 void *table;
144 void *nextItem;
145 int index;
146 } WMHashEnumerator;
149 typedef struct {
150 /* NULL is pointer hash */
151 unsigned (*hash)(const void *);
152 /* NULL is pointer compare */
153 Bool (*keyIsEqual)(const void *, const void *);
154 /* NULL does nothing */
155 void* (*retainKey)(const void *);
156 /* NULL does nothing */
157 void (*releaseKey)(const void *);
158 } WMHashTableCallbacks;
161 typedef void *WMBagIterator;
163 typedef struct W_BagFunctions {
164 int (*getItemCount)(WMBag *self);
165 int (*appendBag)(WMBag *self, WMBag *bag);
166 int (*putInBag)(WMBag *self, void *item);
167 int (*insertInBag)(WMBag *self, int index, void *item);
168 int (*removeFromBag)(WMBag *bag, void *item);
169 int (*eraseFromBag)(WMBag *bag, int index);
170 int (*deleteFromBag)(WMBag *bag, int index);
171 void *(*getFromBag)(WMBag *bag, int index);
172 int (*firstInBag)(WMBag *bag, void *item);
173 int (*countInBag)(WMBag *bag, void *item);
174 void *(*replaceInBag)(WMBag *bag, int index, void *item);
175 int (*sortBag)(WMBag *bag, int (*comparer)(const void*, const void*));
176 void (*emptyBag)(WMBag *bag);
177 void (*freeBag)(WMBag *bag);
178 void (*mapBag)(WMBag *bag, void (*function)(void*, void*), void *data);
179 int (*findInBag)(WMBag *bag, int (*match)(void*, void*), void *cdata);
180 void *(*first)(WMBag *bag, WMBagIterator *ptr);
181 void *(*last)(WMBag *bag, WMBagIterator *ptr);
182 void *(*next)(WMBag *bag, WMBagIterator *ptr);
183 void *(*previous)(WMBag *bag, WMBagIterator *ptr);
184 void *(*iteratorAtIndex)(WMBag *bag, int index, WMBagIterator *ptr);
185 int (*indexForIterator)(WMBag *bag, WMBagIterator ptr);
186 } W_BagFunctions;
189 struct W_Bag {
190 void *data;
192 void (*destructor)(void *item);
194 W_BagFunctions func;
199 #if 0
200 typedef struct {
201 char character; /* the escape character */
202 char *value; /* value to place */
203 } WMSEscapes;
204 #endif
207 /* The connection callbacks */
208 typedef struct ConnectionDelegate {
209 void *data;
211 void (*didCatchException)(struct ConnectionDelegate *self,
212 WMConnection *cPtr);
214 void (*didDie)(struct ConnectionDelegate *self, WMConnection *cPtr);
216 void (*didInitialize)(struct ConnectionDelegate *self, WMConnection *cPtr);
218 void (*didReceiveInput)(struct ConnectionDelegate *self, WMConnection *cPtr);
220 void (*didTimeout)(struct ConnectionDelegate *self, WMConnection *cPtr);
222 } ConnectionDelegate;
225 typedef void WMNotificationObserverAction(void *observerData,
226 WMNotification *notification);
230 /*......................................................................*/
232 typedef void (waborthandler)(int);
234 waborthandler *wsetabort(waborthandler*);
237 /* don't free the returned string */
238 char *wstrerror(int errnum);
240 void wfatal(const char *msg, ...);
241 void wwarning(const char *msg, ...);
242 void wsyserror(const char *msg, ...);
243 void wsyserrorwithcode(int error, const char *msg, ...);
245 char *wfindfile(char *paths, char *file);
247 char *wfindfileinlist(char **path_list, char *file);
249 char *wfindfileinarray(proplist_t array, char *file);
251 char *wexpandpath(char *path);
253 /* don't free the returned string */
254 char *wgethomedir();
256 void *wmalloc(size_t size);
257 void *wrealloc(void *ptr, size_t newsize);
258 void wfree(void *ptr);
261 void wrelease(void *ptr);
262 void *wretain(void *ptr);
264 char *wstrdup(char *str);
266 char *wstrappend(char *dst, char *src);
271 void wtokensplit(char *command, char ***argv, int *argc);
273 char *wtokenjoin(char **list, int count);
275 void wtokenfree(char **tokens, int count);
277 char *wtrimspace(char *s);
282 char *wusergnusteppath();
284 char *wdefaultspathfordomain(char *domain);
286 void wusleep(unsigned int microsec);
288 #if 0
289 int wsprintesc(char *buffer, int length, char *format, WMSEscapes **escapes,
290 int count);
291 #endif
293 /*......................................................................*/
295 /* This function is used _only_ if you create a NON-GUI program.
296 * For GUI based programs use WMNextEvent()/WMHandleEvent() instead.
297 * This function will handle all input/timer/idle events, then return.
300 void WHandleEvents();
302 /*......................................................................*/
305 WMHashTable *WMCreateHashTable(WMHashTableCallbacks callbacks);
307 void WMFreeHashTable(WMHashTable *table);
309 void WMResetHashTable(WMHashTable *table);
311 void *WMHashGet(WMHashTable *table, const void *key);
313 /* put data in table, replacing already existing data and returning
314 * the old value */
315 void *WMHashInsert(WMHashTable *table, void *key, void *data);
317 void WMHashRemove(WMHashTable *table, const void *key);
319 /* warning: do not manipulate the table while using these functions */
320 WMHashEnumerator WMEnumerateHashTable(WMHashTable *table);
322 void *WMNextHashEnumeratorItem(WMHashEnumerator *enumerator);
324 unsigned WMCountHashTable(WMHashTable *table);
329 /* some predefined callback sets */
331 extern const WMHashTableCallbacks WMIntHashCallbacks;
332 /* sizeof(keys) are <= sizeof(void*) */
334 extern const WMHashTableCallbacks WMStringHashCallbacks;
335 /* keys are strings. Strings will be copied with wstrdup()
336 * and freed with wfree() */
338 extern const WMHashTableCallbacks WMStringPointerHashCallbacks;
339 /* keys are strings, bug they are not copied */
342 /*......................................................................*/
346 * Array bags use an array to store the elements.
347 * Item indexes may be any integer number.
349 * Pros:
350 * Fast [O(1)] access to elements
351 * Fast [O(1)] push/pop
353 * Cons:
354 * A little slower [O(n)] for insertion/deletion of elements that
355 * arent in the end
356 * Element indexes with large difference will cause large holes
358 #if 0
359 WMBag* WMCreateArrayBag(int initialSize);
360 WMBag* WMCreateArrayBagWithDestructor(int initialSize,
361 void (*destructor)(void*));
362 #endif
364 * Tree bags use a red-black tree for storage.
365 * Item indexes may be any integer number.
367 * Pros:
368 * O(lg n) insertion/deletion/search
369 * Good for large numbers of elements with sparse indexes
371 * Cons:
372 * O(lg n) insertion/deletion/search
373 * Slow for storing small numbers of elements
375 WMBag *WMCreateTreeBag(void);
376 WMBag *WMCreateTreeBagWithDestructor(void (*destructor)(void*));
379 #define WMCreateArrayBag(a) WMCreateTreeBag()
380 #define WMCreateArrayBagWithDestructor(a,d) WMCreateTreeBagWithDestructor(d)
382 #define WMCreateBag(size) WMCreateTreeBag()
384 #define WMCreateBagWithDestructor(size, d) WMCreateTreeBagWithDestructor(d)
386 #define WMGetBagItemCount(bag) bag->func.getItemCount(bag)
388 #define WMAppendBag(bag, other) bag->func.appendBag(bag, other)
390 #define WMPutInBag(bag, item) bag->func.putInBag(bag, item)
392 /* insert will increment the index of elements after it by 1 */
393 #define WMInsertInBag(bag, index, item) bag->func.insertInBag(bag, index, item)
395 /* this is slow */
396 /* erase will remove the element from the bag,
397 * but will keep the index of the other elements unchanged */
398 #define WMEraseFromBag(bag, index) bag->func.eraseFromBag(bag, index)
400 /* delete and remove will remove the elements and cause the elements
401 * after them to decrement their indexes by 1 */
402 #define WMRemoveFromBag(bag, item) bag->func.removeFromBag(bag, item)
404 #define WMDeleteFromBag(bag, index) bag->func.deleteFromBag(bag, index)
406 #define WMGetFromBag(bag, index) bag->func.getFromBag(bag, index)
408 #define WMCountInBag(bag, item) bag->func.countInBag(bag, item)
410 #define WMReplaceInBag(bag, index, item) bag->func.replaceInBag(bag, index, item)
411 #define WMSetInBag(bag, index, item) bag->func.replaceInBag(bag, index, item)
413 /* comparer must return:
414 * < 0 if a < b
415 * > 0 if a > b
416 * = 0 if a = b
418 #define WMSortBag(bag, comparer) bag->func.sortBag(bag, comparer)
420 #define WMEmptyBag(bag) bag->func.emptyBag(bag)
422 #define WMFreeBag(bag) bag->func.freeBag(bag)
424 #define WMMapBag(bag, function, cdata) bag->func.mapBag(bag, function, cdata)
426 #define WMGetFirstInBag(bag, item) bag->func.firstInBag(bag, item)
428 #define WMCountInBag(bag, item) bag->func.countInBag(bag, item)
430 #define WMFindInBag(bag, match, cdata) bag->func.findInBag(bag, match, cdata)
432 #define WMBagFirst(bag, ptr) bag->func.first(bag, ptr)
434 #define WMBagLast(bag, ptr) bag->func.last(bag, ptr)
436 #define WMBagNext(bag, ptr) bag->func.next(bag, ptr)
438 #define WMBagPrevious(bag, ptr) bag->func.previous(bag, ptr)
440 #define WMBagIteratorAtIndex(bag, index, ptr) bag->func.iteratorAtIndex(bag, index, ptr)
442 #define WMBagIndexForIterator(bag, ptr) bag->func.indexForIterator(bag, ptr)
446 #define WM_ITERATE_BAG(bag, var, i) \
447 for (var = WMBagFirst(bag, &(i)); (i) != NULL; \
448 var = WMBagNext(bag, &(i)))
450 #define WM_ETARETI_BAG(bag, var, i) \
451 for (var = WMBagLast(bag, &(i)); (i) != NULL; \
452 var = WMBagPrevious(bag, &(i)))
456 /*-------------------------------------------------------------------------*/
458 /* WMData handling */
460 /* Creating/destroying data */
462 WMData* WMCreateDataWithCapacity(unsigned capacity);
464 WMData* WMCreateDataWithLength(unsigned length);
466 WMData* WMCreateDataWithBytes(void *bytes, unsigned length);
468 /* destructor is a function called to free the data when releasing the data
469 * object, or NULL if no freeing of data is necesary. */
470 WMData* WMCreateDataWithBytesNoCopy(void *bytes, unsigned length,
471 WMFreeDataProc *destructor);
473 WMData* WMCreateDataWithData(WMData *aData);
475 WMData* WMRetainData(WMData *aData);
477 void WMReleaseData(WMData *aData);
479 /* Adjusting capacity */
481 void WMSetDataCapacity(WMData *aData, unsigned capacity);
483 void WMSetDataLength(WMData *aData, unsigned length);
485 void WMIncreaseDataLengthBy(WMData *aData, unsigned extraLength);
487 /* Accessing data */
489 const void* WMDataBytes(WMData *aData);
491 void WMGetDataBytes(WMData *aData, void *buffer);
493 void WMGetDataBytesWithLength(WMData *aData, void *buffer, unsigned length);
495 void WMGetDataBytesWithRange(WMData *aData, void *buffer, WMRange aRange);
497 WMData* WMGetSubdataWithRange(WMData *aData, WMRange aRange);
499 /* Testing data */
501 Bool WMIsDataEqualToData(WMData *aData, WMData *anotherData);
503 unsigned WMGetDataLength(WMData *aData);
505 /* Adding data */
507 void WMAppendDataBytes(WMData *aData, void *bytes, unsigned length);
509 void WMAppendData(WMData *aData, WMData *anotherData);
511 /* Modifying data */
513 void WMReplaceDataBytesInRange(WMData *aData, WMRange aRange, void *bytes);
515 void WMResetDataBytesInRange(WMData *aData, WMRange aRange);
517 void WMSetData(WMData *aData, WMData *anotherData);
520 void WMSetDataFormat(WMData *aData, unsigned format);
522 unsigned WMGetDataFormat(WMData *aData);
523 /* Storing data */
526 /*--------------------------------------------------------------------------*/
529 WMNotification *WMCreateNotification(char *name, void *object, void *clientData);
531 void WMReleaseNotification(WMNotification *notification);
533 WMNotification *WMRetainNotification(WMNotification *notification);
535 void *WMGetNotificationClientData(WMNotification *notification);
537 void *WMGetNotificationObject(WMNotification *notification);
539 char *WMGetNotificationName(WMNotification *notification);
542 void WMAddNotificationObserver(WMNotificationObserverAction *observerAction,
543 void *observer, char *name, void *object);
545 void WMPostNotification(WMNotification *notification);
547 void WMRemoveNotificationObserver(void *observer);
549 void WMRemoveNotificationObserverWithName(void *observer, char *name,
550 void *object);
552 void WMPostNotificationName(char *name, void *object, void *clientData);
554 WMNotificationQueue *WMGetDefaultNotificationQueue(void);
556 WMNotificationQueue *WMCreateNotificationQueue(void);
558 void WMDequeueNotificationMatching(WMNotificationQueue *queue,
559 WMNotification *notification,
560 unsigned mask);
562 void WMEnqueueNotification(WMNotificationQueue *queue,
563 WMNotification *notification,
564 WMPostingStyle postingStyle);
566 void WMEnqueueCoalesceNotification(WMNotificationQueue *queue,
567 WMNotification *notification,
568 WMPostingStyle postingStyle,
569 unsigned coalesceMask);
572 /*......................................................................*/
574 WMUserDefaults *WMGetStandardUserDefaults(void);
576 WMUserDefaults *WMGetDefaultsFromPath(char *path);
578 void WMSynchronizeUserDefaults(WMUserDefaults *database);
580 void WMSaveUserDefaults(WMUserDefaults *database);
582 void WMEnableUDPeriodicSynchronization(WMUserDefaults *database, Bool enable);
584 /* Returns a PLArray with all keys in the user defaults database.
585 * Free the returned array with PLRelease() when no longer needed,
586 * but do not free the elements of the array! They're just references. */
587 proplist_t WMGetUDAllKeys(WMUserDefaults *database);
589 proplist_t WMGetUDObjectForKey(WMUserDefaults *database, char *defaultName);
591 void WMSetUDObjectForKey(WMUserDefaults *database, proplist_t object,
592 char *defaultName);
594 void WMRemoveUDObjectForKey(WMUserDefaults *database, char *defaultName);
596 char *WMGetUDStringForKey(WMUserDefaults *database, char *defaultName);
598 int WMGetUDIntegerForKey(WMUserDefaults *database, char *defaultName);
600 float WMGetUDFloatForKey(WMUserDefaults *database, char *defaultName);
602 Bool WMGetUDBoolForKey(WMUserDefaults *database, char *defaultName);
604 void WMSetUDStringForKey(WMUserDefaults *database, char *value,
605 char *defaultName);
607 void WMSetUDIntegerForKey(WMUserDefaults *database, int value,
608 char *defaultName);
610 void WMSetUDFloatForKey(WMUserDefaults *database, float value,
611 char *defaultName);
613 void WMSetUDBoolForKey(WMUserDefaults *database, Bool value,
614 char *defaultName);
616 proplist_t WMGetUDSearchList(WMUserDefaults *database);
618 void WMSetUDSearchList(WMUserDefaults *database, proplist_t list);
620 extern char *WMUserDefaultsDidChangeNotification;
623 /*-------------------------------------------------------------------------*/
625 /* WMHost: host handling */
627 WMHost* WMGetCurrentHost();
629 WMHost* WMGetHostWithName(char* name);
631 WMHost* WMGetHostWithAddress(char* address);
633 WMHost* WMRetainHost(WMHost *hPtr);
635 void WMReleaseHost(WMHost *hPtr);
638 * Host cache management
639 * If enabled, only one object representing each host will be created, and
640 * a shared instance will be returned by all methods that return a host.
641 * Enabled by default.
643 void WMSetHostCacheEnabled(Bool flag);
645 Bool WMIsHostCacheEnabled();
647 void WMFlushHostCache();
650 * Compare hosts: Hosts are equal if they share at least one address
652 Bool WMIsHostEqualToHost(WMHost* hPtr, WMHost* anotherHost);
655 * Host names.
656 * WMGetHostName() will return first name (official) if a host has several.
657 * WMGetHostNames() will return a R/O WMBag with all the names of the host.
659 char* WMGetHostName(WMHost* hPtr);
661 /* The returned bag is R/O. Do not modify it, and do not free it! */
662 WMBag* WMGetHostNames(WMHost* hPtr);
665 * Host addresses.
666 * Addresses are represented as "Dotted Decimal" strings, e.g. "192.42.172.1"
667 * WMGetHostAddress() will return an arbitrary address if a host has several.
668 * WMGetHostAddresses() will return a R/O WMBag with all addresses of the host.
670 char* WMGetHostAddress(WMHost* hPtr);
672 /* The returned bag is R/O. Do not modify it, and do not free it! */
673 WMBag* WMGetHostAddresses(WMHost* hPtr);
676 /*-------------------------------------------------------------------------*/
678 /* WMConnection functions */
680 WMConnection* WMCreateConnectionAsServerAtAddress(char *host, char *service,
681 char *protocol);
683 WMConnection* WMCreateConnectionToAddress(char *host, char *service,
684 char *protocol);
686 WMConnection* WMCreateConnectionToAddressAndNotify(char *host, char *service,
687 char *protocol);
689 void WMCloseConnection(WMConnection *cPtr);
691 void WMDestroyConnection(WMConnection *cPtr);
693 WMConnection* WMAcceptConnection(WMConnection *listener);
695 /* Release the returned data! */
696 WMData* WMGetConnectionAvailableData(WMConnection *cPtr);
698 int WMSendConnectionData(WMConnection *cPtr, WMData *data);
700 Bool WMEnqueueConnectionData(WMConnection *cPtr, WMData *data);
702 #define WMFlushConnection(cPtr) WMSendConnectionData((cPtr), NULL)
704 void WMSetConnectionDelegate(WMConnection *cPtr, ConnectionDelegate *delegate);
706 /* Connection info */
708 char* WMGetConnectionAddress(WMConnection *cPtr);
710 char* WMGetConnectionService(WMConnection *cPtr);
712 char* WMGetConnectionProtocol(WMConnection *cPtr);
714 void WMSetConnectionNonBlocking(WMConnection *cPtr, Bool flag);
716 void* WMGetConnectionClientData(WMConnection *cPtr);
718 void WMSetConnectionClientData(WMConnection *cPtr, void *data);
720 unsigned int WMGetConnectionFlags(WMConnection *cPtr);
722 void WMSetConnectionFlags(WMConnection *cPtr, unsigned int flags);
724 int WMGetConnectionSocket(WMConnection *cPtr);
726 WMConnectionState WMGetConnectionState(WMConnection *cPtr);
728 WMConnectionTimeoutState WMGetConnectionTimeoutState(WMConnection *cPtr);
731 * Passing timeout==0 in the SetTimeout functions below, will reset that
732 * timeout to its default value.
735 /* The default timeout inherited by all WMConnection operations, if none set */
736 void WMSetConnectionDefaultTimeout(unsigned int timeout);
738 /* Global timeout for all WMConnection objects, for opening a new connection */
739 void WMSetConnectionOpenTimeout(unsigned int timeout);
741 /* Connection specific timeout for sending out data */
742 void WMSetConnectionSendTimeout(WMConnection *cPtr, unsigned int timeout);
745 /* Global variables */
747 extern int WCErrorCode;
750 /*-------------------------------------------------------------------------*/
754 #ifdef __cplusplus
756 #endif /* __cplusplus */
759 #endif