- Fixed WMArray.
[wmaker-crm.git] / WINGs / WUtil.h
blobeba5ce77baeb5a0071a0a8dc7a98a193dc3f4e64
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 */
116 WANotFound = -1 /* element was not found in array */
120 typedef struct W_Array WMArray;
121 typedef struct W_Bag WMBag;
122 typedef struct W_Data WMData;
123 typedef struct W_HashTable WMHashTable;
124 typedef struct W_UserDefaults WMUserDefaults;
125 typedef struct W_Notification WMNotification;
126 typedef struct W_NotificationQueue WMNotificationQueue;
127 typedef struct W_Host WMHost;
128 typedef struct W_Connection WMConnection;
132 typedef void WMFreeDataProc(void *data);
134 /* Used by WMBag or WMArray for matching data */
135 typedef int WMMatchDataProc(void *item, void *cdata);
140 typedef struct {
141 int position;
142 int count;
143 } WMRange;
147 /* DO NOT ACCESS THE CONTENTS OF THIS STRUCT */
148 typedef struct {
149 void *table;
150 void *nextItem;
151 int index;
152 } WMHashEnumerator;
155 typedef struct {
156 /* NULL is pointer hash */
157 unsigned (*hash)(const void *);
158 /* NULL is pointer compare */
159 Bool (*keyIsEqual)(const void *, const void *);
160 /* NULL does nothing */
161 void* (*retainKey)(const void *);
162 /* NULL does nothing */
163 void (*releaseKey)(const void *);
164 } WMHashTableCallbacks;
167 typedef void *WMBagIterator;
170 #if 0
171 typedef struct {
172 char character; /* the escape character */
173 char *value; /* value to place */
174 } WMSEscapes;
175 #endif
178 /* The connection callbacks */
179 typedef struct ConnectionDelegate {
180 void *data;
182 void (*didCatchException)(struct ConnectionDelegate *self,
183 WMConnection *cPtr);
185 void (*didDie)(struct ConnectionDelegate *self, WMConnection *cPtr);
187 void (*didInitialize)(struct ConnectionDelegate *self, WMConnection *cPtr);
189 void (*didReceiveInput)(struct ConnectionDelegate *self, WMConnection *cPtr);
191 void (*didTimeout)(struct ConnectionDelegate *self, WMConnection *cPtr);
193 } ConnectionDelegate;
196 typedef void WMNotificationObserverAction(void *observerData,
197 WMNotification *notification);
201 /*......................................................................*/
203 typedef void (waborthandler)(int);
205 waborthandler *wsetabort(waborthandler*);
208 /* don't free the returned string */
209 char *wstrerror(int errnum);
211 void wfatal(const char *msg, ...);
212 void wwarning(const char *msg, ...);
213 void wsyserror(const char *msg, ...);
214 void wsyserrorwithcode(int error, const char *msg, ...);
216 char *wfindfile(char *paths, char *file);
218 char *wfindfileinlist(char **path_list, char *file);
220 char *wfindfileinarray(proplist_t array, char *file);
222 char *wexpandpath(char *path);
224 /* don't free the returned string */
225 char *wgethomedir();
227 void *wmalloc(size_t size);
228 void *wrealloc(void *ptr, size_t newsize);
229 void wfree(void *ptr);
232 void wrelease(void *ptr);
233 void *wretain(void *ptr);
235 char *wstrdup(char *str);
237 char *wstrappend(char *dst, char *src);
241 void wtokensplit(char *command, char ***argv, int *argc);
243 char *wtokenjoin(char **list, int count);
245 void wtokenfree(char **tokens, int count);
247 char *wtrimspace(char *s);
251 char *wusergnusteppath();
253 char *wdefaultspathfordomain(char *domain);
255 void wusleep(unsigned int microsec);
257 #if 0
258 int wsprintesc(char *buffer, int length, char *format, WMSEscapes **escapes,
259 int count);
260 #endif
262 /*......................................................................*/
264 /* This function is used _only_ if you create a NON-GUI program.
265 * For GUI based programs use WMNextEvent()/WMHandleEvent() instead.
266 * This function will handle all input/timer/idle events, then return.
269 void WHandleEvents();
271 /*......................................................................*/
274 WMHashTable *WMCreateHashTable(WMHashTableCallbacks callbacks);
276 void WMFreeHashTable(WMHashTable *table);
278 void WMResetHashTable(WMHashTable *table);
280 void *WMHashGet(WMHashTable *table, const void *key);
282 /* put data in table, replacing already existing data and returning
283 * the old value */
284 void *WMHashInsert(WMHashTable *table, void *key, void *data);
286 void WMHashRemove(WMHashTable *table, const void *key);
288 /* warning: do not manipulate the table while using these functions */
289 WMHashEnumerator WMEnumerateHashTable(WMHashTable *table);
291 void *WMNextHashEnumeratorItem(WMHashEnumerator *enumerator);
293 unsigned WMCountHashTable(WMHashTable *table);
298 /* some predefined callback sets */
300 extern const WMHashTableCallbacks WMIntHashCallbacks;
301 /* sizeof(keys) are <= sizeof(void*) */
303 extern const WMHashTableCallbacks WMStringHashCallbacks;
304 /* keys are strings. Strings will be copied with wstrdup()
305 * and freed with wfree() */
307 extern const WMHashTableCallbacks WMStringPointerHashCallbacks;
308 /* keys are strings, bug they are not copied */
311 /*......................................................................*/
314 * WMArray use an array to store the elements.
315 * Item indexes may be only positive integer numbers.
316 * The array cannot contain holes in it.
318 * Pros:
319 * Fast [O(1)] access to elements
320 * Fast [O(1)] push/pop
322 * Cons:
323 * A little slower [O(n)] for insertion/deletion of elements that
324 * aren't in the end
327 WMArray* WMCreateArray(int initialSize);
329 WMArray* WMCreateArrayWithDestructor(int initialSize, WMFreeDataProc *destructor);
331 void WMEmptyArray(WMArray *array);
333 void WMFreeArray(WMArray *array);
335 int WMGetArrayItemCount(WMArray *array);
337 /* appends other to array. other remains unchanged */
338 int WMAppendArray(WMArray *array, WMArray *other);
340 /* add will place the element at the end of the array */
341 int WMAddToArray(WMArray *array, void *item);
343 #define WMPushInArray(array, item) WMAddToArray(array, item)
345 /* insert will increment the index of elements after it by 1 */
346 int WMInsertInArray(WMArray *array, int index, void *item);
348 /* replace and set will return the old item WITHOUT calling the
349 * destructor on it even if its available. Free the returned item yourself.
351 void* WMReplaceInArray(WMArray *array, int index, void *item);
353 #define WMSetInArray(array, index, item) WMReplaceInArray(array, index, item)
355 /* delete and remove will remove the elements and cause the elements
356 * after them to decrement their indexes by 1. Also will call the
357 * destructor on the deleted element if there's one available.
359 int WMDeleteFromArray(WMArray *array, int index);
361 int WMRemoveFromArray(WMArray *array, void *item);
363 void* WMGetFromArray(WMArray *array, int index);
365 #define WMGetFirstInArray(array, item) WMFindInArray(array, NULL, item)
367 /* pop will return the last element from the array, also removing it
368 * from the array. The destructor is NOT called, even if available.
369 * Free the returned element if needed by yourself
371 void* WMPopFromArray(WMArray *array);
373 int WMFindInArray(WMArray *array, WMMatchDataProc *match, void *cdata);
375 int WMCountInArray(WMArray *array, void *item);
377 /* comparer must return:
378 * < 0 if a < b
379 * > 0 if a > b
380 * = 0 if a = b
382 int WMSortArray(WMArray *array, int (*comparer)(const void*, const void*));
384 void WMMapArray(WMArray *array, void (*function)(void*, void*), void *data);
388 /*..........................................................................*/
391 * Tree bags use a red-black tree for storage.
392 * Item indexes may be any integer number.
394 * Pros:
395 * O(lg n) insertion/deletion/search
396 * Good for large numbers of elements with sparse indexes
398 * Cons:
399 * O(lg n) insertion/deletion/search
400 * Slow for storing small numbers of elements
403 #define WMCreateBag(size) WMCreateTreeBag()
405 #define WMCreateBagWithDestructor(size, d) WMCreateTreeBagWithDestructor(d)
407 WMBag* WMCreateTreeBag(void);
409 WMBag* WMCreateTreeBagWithDestructor(WMFreeDataProc *destructor);
411 int WMGetBagItemCount(WMBag *bag);
413 int WMAppendBag(WMBag *bag, WMBag *other);
415 int WMPutInBag(WMBag *bag, void *item);
417 /* insert will increment the index of elements after it by 1 */
418 int WMInsertInBag(WMBag *bag, int index, void *item);
420 /* this is slow */
421 /* erase will remove the element from the bag,
422 * but will keep the index of the other elements unchanged */
423 int WMEraseFromBag(WMBag *bag, int index);
425 /* delete and remove will remove the elements and cause the elements
426 * after them to decrement their indexes by 1 */
427 int WMDeleteFromBag(WMBag *bag, int index);
429 int WMRemoveFromBag(WMBag *bag, void *item);
431 void* WMGetFromBag(WMBag *bag, int index);
433 void* WMReplaceInBag(WMBag *bag, int index, void *item);
435 #define WMSetInBag(bag, index, item) WMReplaceInBag(bag, index, item)
437 /* comparer must return:
438 * < 0 if a < b
439 * > 0 if a > b
440 * = 0 if a = b
442 int WMSortBag(WMBag *bag, int (*comparer)(const void*, const void*));
444 void WMEmptyBag(WMBag *bag);
446 void WMFreeBag(WMBag *bag);
448 void WMMapBag(WMBag *bag, void (*function)(void*, void*), void *data);
450 int WMGetFirstInBag(WMBag *bag, void *item);
452 int WMCountInBag(WMBag *bag, void *item);
454 int WMFindInBag(WMBag *bag, WMMatchDataProc *match, void *cdata);
456 void* WMBagFirst(WMBag *bag, WMBagIterator *ptr);
458 void* WMBagLast(WMBag *bag, WMBagIterator *ptr);
460 void* WMBagNext(WMBag *bag, WMBagIterator *ptr);
462 void* WMBagPrevious(WMBag *bag, WMBagIterator *ptr);
464 void* WMBagIteratorAtIndex(WMBag *bag, int index, WMBagIterator *ptr);
466 int WMBagIndexForIterator(WMBag *bag, WMBagIterator ptr);
469 #define WM_ITERATE_BAG(bag, var, i) \
470 for (var = WMBagFirst(bag, &(i)); (i) != NULL; \
471 var = WMBagNext(bag, &(i)))
473 #define WM_ETARETI_BAG(bag, var, i) \
474 for (var = WMBagLast(bag, &(i)); (i) != NULL; \
475 var = WMBagPrevious(bag, &(i)))
479 /*-------------------------------------------------------------------------*/
481 /* WMData handling */
483 /* Creating/destroying data */
485 WMData* WMCreateDataWithCapacity(unsigned capacity);
487 WMData* WMCreateDataWithLength(unsigned length);
489 WMData* WMCreateDataWithBytes(void *bytes, unsigned length);
491 /* destructor is a function called to free the data when releasing the data
492 * object, or NULL if no freeing of data is necesary. */
493 WMData* WMCreateDataWithBytesNoCopy(void *bytes, unsigned length,
494 WMFreeDataProc *destructor);
496 WMData* WMCreateDataWithData(WMData *aData);
498 WMData* WMRetainData(WMData *aData);
500 void WMReleaseData(WMData *aData);
502 /* Adjusting capacity */
504 void WMSetDataCapacity(WMData *aData, unsigned capacity);
506 void WMSetDataLength(WMData *aData, unsigned length);
508 void WMIncreaseDataLengthBy(WMData *aData, unsigned extraLength);
510 /* Accessing data */
512 const void* WMDataBytes(WMData *aData);
514 void WMGetDataBytes(WMData *aData, void *buffer);
516 void WMGetDataBytesWithLength(WMData *aData, void *buffer, unsigned length);
518 void WMGetDataBytesWithRange(WMData *aData, void *buffer, WMRange aRange);
520 WMData* WMGetSubdataWithRange(WMData *aData, WMRange aRange);
522 /* Testing data */
524 Bool WMIsDataEqualToData(WMData *aData, WMData *anotherData);
526 unsigned WMGetDataLength(WMData *aData);
528 /* Adding data */
530 void WMAppendDataBytes(WMData *aData, void *bytes, unsigned length);
532 void WMAppendData(WMData *aData, WMData *anotherData);
534 /* Modifying data */
536 void WMReplaceDataBytesInRange(WMData *aData, WMRange aRange, void *bytes);
538 void WMResetDataBytesInRange(WMData *aData, WMRange aRange);
540 void WMSetData(WMData *aData, WMData *anotherData);
543 void WMSetDataFormat(WMData *aData, unsigned format);
545 unsigned WMGetDataFormat(WMData *aData);
546 /* Storing data */
549 /*--------------------------------------------------------------------------*/
552 WMNotification *WMCreateNotification(char *name, void *object, void *clientData);
554 void WMReleaseNotification(WMNotification *notification);
556 WMNotification *WMRetainNotification(WMNotification *notification);
558 void *WMGetNotificationClientData(WMNotification *notification);
560 void *WMGetNotificationObject(WMNotification *notification);
562 char *WMGetNotificationName(WMNotification *notification);
565 void WMAddNotificationObserver(WMNotificationObserverAction *observerAction,
566 void *observer, char *name, void *object);
568 void WMPostNotification(WMNotification *notification);
570 void WMRemoveNotificationObserver(void *observer);
572 void WMRemoveNotificationObserverWithName(void *observer, char *name,
573 void *object);
575 void WMPostNotificationName(char *name, void *object, void *clientData);
577 WMNotificationQueue *WMGetDefaultNotificationQueue(void);
579 WMNotificationQueue *WMCreateNotificationQueue(void);
581 void WMDequeueNotificationMatching(WMNotificationQueue *queue,
582 WMNotification *notification,
583 unsigned mask);
585 void WMEnqueueNotification(WMNotificationQueue *queue,
586 WMNotification *notification,
587 WMPostingStyle postingStyle);
589 void WMEnqueueCoalesceNotification(WMNotificationQueue *queue,
590 WMNotification *notification,
591 WMPostingStyle postingStyle,
592 unsigned coalesceMask);
595 /*......................................................................*/
597 WMUserDefaults *WMGetStandardUserDefaults(void);
599 WMUserDefaults *WMGetDefaultsFromPath(char *path);
601 void WMSynchronizeUserDefaults(WMUserDefaults *database);
603 void WMSaveUserDefaults(WMUserDefaults *database);
605 void WMEnableUDPeriodicSynchronization(WMUserDefaults *database, Bool enable);
607 /* Returns a PLArray with all keys in the user defaults database.
608 * Free the returned array with PLRelease() when no longer needed,
609 * but do not free the elements of the array! They're just references. */
610 proplist_t WMGetUDAllKeys(WMUserDefaults *database);
612 proplist_t WMGetUDObjectForKey(WMUserDefaults *database, char *defaultName);
614 void WMSetUDObjectForKey(WMUserDefaults *database, proplist_t object,
615 char *defaultName);
617 void WMRemoveUDObjectForKey(WMUserDefaults *database, char *defaultName);
619 char *WMGetUDStringForKey(WMUserDefaults *database, char *defaultName);
621 int WMGetUDIntegerForKey(WMUserDefaults *database, char *defaultName);
623 float WMGetUDFloatForKey(WMUserDefaults *database, char *defaultName);
625 Bool WMGetUDBoolForKey(WMUserDefaults *database, char *defaultName);
627 void WMSetUDStringForKey(WMUserDefaults *database, char *value,
628 char *defaultName);
630 void WMSetUDIntegerForKey(WMUserDefaults *database, int value,
631 char *defaultName);
633 void WMSetUDFloatForKey(WMUserDefaults *database, float value,
634 char *defaultName);
636 void WMSetUDBoolForKey(WMUserDefaults *database, Bool value,
637 char *defaultName);
639 proplist_t WMGetUDSearchList(WMUserDefaults *database);
641 void WMSetUDSearchList(WMUserDefaults *database, proplist_t list);
643 extern char *WMUserDefaultsDidChangeNotification;
646 /*-------------------------------------------------------------------------*/
648 /* WMHost: host handling */
650 WMHost* WMGetCurrentHost();
652 WMHost* WMGetHostWithName(char* name);
654 WMHost* WMGetHostWithAddress(char* address);
656 WMHost* WMRetainHost(WMHost *hPtr);
658 void WMReleaseHost(WMHost *hPtr);
661 * Host cache management
662 * If enabled, only one object representing each host will be created, and
663 * a shared instance will be returned by all methods that return a host.
664 * Enabled by default.
666 void WMSetHostCacheEnabled(Bool flag);
668 Bool WMIsHostCacheEnabled();
670 void WMFlushHostCache();
673 * Compare hosts: Hosts are equal if they share at least one address
675 Bool WMIsHostEqualToHost(WMHost* hPtr, WMHost* anotherHost);
678 * Host names.
679 * WMGetHostName() will return first name (official) if a host has several.
680 * WMGetHostNames() will return a R/O WMBag with all the names of the host.
682 char* WMGetHostName(WMHost* hPtr);
684 /* The returned bag is R/O. Do not modify it, and do not free it! */
685 WMBag* WMGetHostNames(WMHost* hPtr);
688 * Host addresses.
689 * Addresses are represented as "Dotted Decimal" strings, e.g. "192.42.172.1"
690 * WMGetHostAddress() will return an arbitrary address if a host has several.
691 * WMGetHostAddresses() will return a R/O WMBag with all addresses of the host.
693 char* WMGetHostAddress(WMHost* hPtr);
695 /* The returned bag is R/O. Do not modify it, and do not free it! */
696 WMBag* WMGetHostAddresses(WMHost* hPtr);
699 /*-------------------------------------------------------------------------*/
701 /* WMConnection functions */
703 WMConnection* WMCreateConnectionAsServerAtAddress(char *host, char *service,
704 char *protocol);
706 WMConnection* WMCreateConnectionToAddress(char *host, char *service,
707 char *protocol);
709 WMConnection* WMCreateConnectionToAddressAndNotify(char *host, char *service,
710 char *protocol);
712 void WMCloseConnection(WMConnection *cPtr);
714 void WMDestroyConnection(WMConnection *cPtr);
716 WMConnection* WMAcceptConnection(WMConnection *listener);
718 /* Release the returned data! */
719 WMData* WMGetConnectionAvailableData(WMConnection *cPtr);
721 int WMSendConnectionData(WMConnection *cPtr, WMData *data);
723 Bool WMEnqueueConnectionData(WMConnection *cPtr, WMData *data);
725 #define WMFlushConnection(cPtr) WMSendConnectionData((cPtr), NULL)
727 void WMSetConnectionDelegate(WMConnection *cPtr, ConnectionDelegate *delegate);
729 /* Connection info */
731 char* WMGetConnectionAddress(WMConnection *cPtr);
733 char* WMGetConnectionService(WMConnection *cPtr);
735 char* WMGetConnectionProtocol(WMConnection *cPtr);
737 void WMSetConnectionNonBlocking(WMConnection *cPtr, Bool flag);
739 void* WMGetConnectionClientData(WMConnection *cPtr);
741 void WMSetConnectionClientData(WMConnection *cPtr, void *data);
743 unsigned int WMGetConnectionFlags(WMConnection *cPtr);
745 void WMSetConnectionFlags(WMConnection *cPtr, unsigned int flags);
747 int WMGetConnectionSocket(WMConnection *cPtr);
749 WMConnectionState WMGetConnectionState(WMConnection *cPtr);
751 WMConnectionTimeoutState WMGetConnectionTimeoutState(WMConnection *cPtr);
754 * Passing timeout==0 in the SetTimeout functions below, will reset that
755 * timeout to its default value.
758 /* The default timeout inherited by all WMConnection operations, if none set */
759 void WMSetConnectionDefaultTimeout(unsigned int timeout);
761 /* Global timeout for all WMConnection objects, for opening a new connection */
762 void WMSetConnectionOpenTimeout(unsigned int timeout);
764 /* Connection specific timeout for sending out data */
765 void WMSetConnectionSendTimeout(WMConnection *cPtr, unsigned int timeout);
768 /* Global variables */
770 extern int WCErrorCode;
773 /*-------------------------------------------------------------------------*/
777 #ifdef __cplusplus
779 #endif /* __cplusplus */
782 #endif