Some more functions for the array class
[wmaker-crm.git] / WINGs / WUtil.h
blobf03495295301abc36721185b3c3b985dbf7233c0
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_Array WMArray;
120 typedef struct W_Bag WMBag;
121 typedef struct W_Data WMData;
122 typedef struct W_HashTable WMHashTable;
123 typedef struct W_UserDefaults WMUserDefaults;
124 typedef struct W_Notification WMNotification;
125 typedef struct W_NotificationQueue WMNotificationQueue;
126 typedef struct W_Host WMHost;
127 typedef struct W_Connection WMConnection;
131 typedef void WMFreeDataProc(void *data);
135 typedef struct {
136 int position;
137 int count;
138 } WMRange;
142 /* DO NOT ACCESS THE CONTENTS OF THIS STRUCT */
143 typedef struct {
144 void *table;
145 void *nextItem;
146 int index;
147 } WMHashEnumerator;
150 typedef struct {
151 /* NULL is pointer hash */
152 unsigned (*hash)(const void *);
153 /* NULL is pointer compare */
154 Bool (*keyIsEqual)(const void *, const void *);
155 /* NULL does nothing */
156 void* (*retainKey)(const void *);
157 /* NULL does nothing */
158 void (*releaseKey)(const void *);
159 } WMHashTableCallbacks;
162 typedef void *WMBagIterator;
165 #if 0
166 struct W_Bag {
167 void *data;
169 void (*destructor)(void *item);
171 W_BagFunctions func;
173 #endif
177 #if 0
178 typedef struct {
179 char character; /* the escape character */
180 char *value; /* value to place */
181 } WMSEscapes;
182 #endif
185 /* The connection callbacks */
186 typedef struct ConnectionDelegate {
187 void *data;
189 void (*didCatchException)(struct ConnectionDelegate *self,
190 WMConnection *cPtr);
192 void (*didDie)(struct ConnectionDelegate *self, WMConnection *cPtr);
194 void (*didInitialize)(struct ConnectionDelegate *self, WMConnection *cPtr);
196 void (*didReceiveInput)(struct ConnectionDelegate *self, WMConnection *cPtr);
198 void (*didTimeout)(struct ConnectionDelegate *self, WMConnection *cPtr);
200 } ConnectionDelegate;
203 typedef void WMNotificationObserverAction(void *observerData,
204 WMNotification *notification);
208 /*......................................................................*/
210 typedef void (waborthandler)(int);
212 waborthandler *wsetabort(waborthandler*);
215 /* don't free the returned string */
216 char *wstrerror(int errnum);
218 void wfatal(const char *msg, ...);
219 void wwarning(const char *msg, ...);
220 void wsyserror(const char *msg, ...);
221 void wsyserrorwithcode(int error, const char *msg, ...);
223 char *wfindfile(char *paths, char *file);
225 char *wfindfileinlist(char **path_list, char *file);
227 char *wfindfileinarray(proplist_t array, char *file);
229 char *wexpandpath(char *path);
231 /* don't free the returned string */
232 char *wgethomedir();
234 void *wmalloc(size_t size);
235 void *wrealloc(void *ptr, size_t newsize);
236 void wfree(void *ptr);
239 void wrelease(void *ptr);
240 void *wretain(void *ptr);
242 char *wstrdup(char *str);
244 char *wstrappend(char *dst, char *src);
248 void wtokensplit(char *command, char ***argv, int *argc);
250 char *wtokenjoin(char **list, int count);
252 void wtokenfree(char **tokens, int count);
254 char *wtrimspace(char *s);
258 char *wusergnusteppath();
260 char *wdefaultspathfordomain(char *domain);
262 void wusleep(unsigned int microsec);
264 #if 0
265 int wsprintesc(char *buffer, int length, char *format, WMSEscapes **escapes,
266 int count);
267 #endif
269 /*......................................................................*/
271 /* This function is used _only_ if you create a NON-GUI program.
272 * For GUI based programs use WMNextEvent()/WMHandleEvent() instead.
273 * This function will handle all input/timer/idle events, then return.
276 void WHandleEvents();
278 /*......................................................................*/
281 WMHashTable *WMCreateHashTable(WMHashTableCallbacks callbacks);
283 void WMFreeHashTable(WMHashTable *table);
285 void WMResetHashTable(WMHashTable *table);
287 void *WMHashGet(WMHashTable *table, const void *key);
289 /* put data in table, replacing already existing data and returning
290 * the old value */
291 void *WMHashInsert(WMHashTable *table, void *key, void *data);
293 void WMHashRemove(WMHashTable *table, const void *key);
295 /* warning: do not manipulate the table while using these functions */
296 WMHashEnumerator WMEnumerateHashTable(WMHashTable *table);
298 void *WMNextHashEnumeratorItem(WMHashEnumerator *enumerator);
300 unsigned WMCountHashTable(WMHashTable *table);
305 /* some predefined callback sets */
307 extern const WMHashTableCallbacks WMIntHashCallbacks;
308 /* sizeof(keys) are <= sizeof(void*) */
310 extern const WMHashTableCallbacks WMStringHashCallbacks;
311 /* keys are strings. Strings will be copied with wstrdup()
312 * and freed with wfree() */
314 extern const WMHashTableCallbacks WMStringPointerHashCallbacks;
315 /* keys are strings, bug they are not copied */
318 /*......................................................................*/
322 * Array bags use an array to store the elements.
323 * Item indexes may be any integer number.
325 * Pros:
326 * Fast [O(1)] access to elements
327 * Fast [O(1)] push/pop
329 * Cons:
330 * A little slower [O(n)] for insertion/deletion of elements that
331 * aren't in the end
332 * Element indexes with large difference will cause large holes
336 * Tree bags use a red-black tree for storage.
337 * Item indexes may be any integer number.
339 * Pros:
340 * O(lg n) insertion/deletion/search
341 * Good for large numbers of elements with sparse indexes
343 * Cons:
344 * O(lg n) insertion/deletion/search
345 * Slow for storing small numbers of elements
348 #define WMCreateBag(size) WMCreateTreeBag()
350 #define WMCreateBagWithDestructor(size, d) WMCreateTreeBagWithDestructor(d)
352 WMBag* WMCreateTreeBag(void);
354 WMBag* WMCreateTreeBagWithDestructor(void (*destructor)(void*));
356 int WMGetBagItemCount(WMBag *self);
358 int WMAppendBag(WMBag *self, WMBag *bag);
360 int WMPutInBag(WMBag *self, void *item);
362 /* insert will increment the index of elements after it by 1 */
363 int WMInsertInBag(WMBag *self, int index, void *item);
365 /* this is slow */
366 /* erase will remove the element from the bag,
367 * but will keep the index of the other elements unchanged */
368 int WMEraseFromBag(WMBag *self, int index);
370 /* delete and remove will remove the elements and cause the elements
371 * after them to decrement their indexes by 1 */
372 int WMRemoveFromBag(WMBag *self, void *item);
374 int WMDeleteFromBag(WMBag *self, int index);
376 void* WMGetFromBag(WMBag *self, int index);
378 void* WMReplaceInBag(WMBag *self, int index, void *item);
380 #define WMSetInBag(bag, index, item) WMReplaceInBag(bag, index, item)
382 /* comparer must return:
383 * < 0 if a < b
384 * > 0 if a > b
385 * = 0 if a = b
387 int WMSortBag(WMBag *self, int (*comparer)(const void*, const void*));
389 void WMEmptyBag(WMBag *self);
391 void WMFreeBag(WMBag *self);
393 void WMMapBag(WMBag *self, void (*function)(void*, void*), void *data);
395 int WMGetFirstInBag(WMBag *self, void *item);
397 int WMCountInBag(WMBag *self, void *item);
399 int WMFindInBag(WMBag *self, int (*match)(void*,void*), void *cdata);
401 void* WMBagFirst(WMBag *self, WMBagIterator *ptr);
403 void* WMBagLast(WMBag *self, WMBagIterator *ptr);
405 void* WMBagNext(WMBag *self, WMBagIterator *ptr);
407 void* WMBagPrevious(WMBag *self, WMBagIterator *ptr);
409 void* WMBagIteratorAtIndex(WMBag *self, int index, WMBagIterator *ptr);
411 int WMBagIndexForIterator(WMBag *bag, WMBagIterator ptr);
414 #define WM_ITERATE_BAG(bag, var, i) \
415 for (var = WMBagFirst(bag, &(i)); (i) != NULL; \
416 var = WMBagNext(bag, &(i)))
418 #define WM_ETARETI_BAG(bag, var, i) \
419 for (var = WMBagLast(bag, &(i)); (i) != NULL; \
420 var = WMBagPrevious(bag, &(i)))
424 /*-------------------------------------------------------------------------*/
426 /* WMData handling */
428 /* Creating/destroying data */
430 WMData* WMCreateDataWithCapacity(unsigned capacity);
432 WMData* WMCreateDataWithLength(unsigned length);
434 WMData* WMCreateDataWithBytes(void *bytes, unsigned length);
436 /* destructor is a function called to free the data when releasing the data
437 * object, or NULL if no freeing of data is necesary. */
438 WMData* WMCreateDataWithBytesNoCopy(void *bytes, unsigned length,
439 WMFreeDataProc *destructor);
441 WMData* WMCreateDataWithData(WMData *aData);
443 WMData* WMRetainData(WMData *aData);
445 void WMReleaseData(WMData *aData);
447 /* Adjusting capacity */
449 void WMSetDataCapacity(WMData *aData, unsigned capacity);
451 void WMSetDataLength(WMData *aData, unsigned length);
453 void WMIncreaseDataLengthBy(WMData *aData, unsigned extraLength);
455 /* Accessing data */
457 const void* WMDataBytes(WMData *aData);
459 void WMGetDataBytes(WMData *aData, void *buffer);
461 void WMGetDataBytesWithLength(WMData *aData, void *buffer, unsigned length);
463 void WMGetDataBytesWithRange(WMData *aData, void *buffer, WMRange aRange);
465 WMData* WMGetSubdataWithRange(WMData *aData, WMRange aRange);
467 /* Testing data */
469 Bool WMIsDataEqualToData(WMData *aData, WMData *anotherData);
471 unsigned WMGetDataLength(WMData *aData);
473 /* Adding data */
475 void WMAppendDataBytes(WMData *aData, void *bytes, unsigned length);
477 void WMAppendData(WMData *aData, WMData *anotherData);
479 /* Modifying data */
481 void WMReplaceDataBytesInRange(WMData *aData, WMRange aRange, void *bytes);
483 void WMResetDataBytesInRange(WMData *aData, WMRange aRange);
485 void WMSetData(WMData *aData, WMData *anotherData);
488 void WMSetDataFormat(WMData *aData, unsigned format);
490 unsigned WMGetDataFormat(WMData *aData);
491 /* Storing data */
494 /*--------------------------------------------------------------------------*/
497 WMNotification *WMCreateNotification(char *name, void *object, void *clientData);
499 void WMReleaseNotification(WMNotification *notification);
501 WMNotification *WMRetainNotification(WMNotification *notification);
503 void *WMGetNotificationClientData(WMNotification *notification);
505 void *WMGetNotificationObject(WMNotification *notification);
507 char *WMGetNotificationName(WMNotification *notification);
510 void WMAddNotificationObserver(WMNotificationObserverAction *observerAction,
511 void *observer, char *name, void *object);
513 void WMPostNotification(WMNotification *notification);
515 void WMRemoveNotificationObserver(void *observer);
517 void WMRemoveNotificationObserverWithName(void *observer, char *name,
518 void *object);
520 void WMPostNotificationName(char *name, void *object, void *clientData);
522 WMNotificationQueue *WMGetDefaultNotificationQueue(void);
524 WMNotificationQueue *WMCreateNotificationQueue(void);
526 void WMDequeueNotificationMatching(WMNotificationQueue *queue,
527 WMNotification *notification,
528 unsigned mask);
530 void WMEnqueueNotification(WMNotificationQueue *queue,
531 WMNotification *notification,
532 WMPostingStyle postingStyle);
534 void WMEnqueueCoalesceNotification(WMNotificationQueue *queue,
535 WMNotification *notification,
536 WMPostingStyle postingStyle,
537 unsigned coalesceMask);
540 /*......................................................................*/
542 WMUserDefaults *WMGetStandardUserDefaults(void);
544 WMUserDefaults *WMGetDefaultsFromPath(char *path);
546 void WMSynchronizeUserDefaults(WMUserDefaults *database);
548 void WMSaveUserDefaults(WMUserDefaults *database);
550 void WMEnableUDPeriodicSynchronization(WMUserDefaults *database, Bool enable);
552 /* Returns a PLArray with all keys in the user defaults database.
553 * Free the returned array with PLRelease() when no longer needed,
554 * but do not free the elements of the array! They're just references. */
555 proplist_t WMGetUDAllKeys(WMUserDefaults *database);
557 proplist_t WMGetUDObjectForKey(WMUserDefaults *database, char *defaultName);
559 void WMSetUDObjectForKey(WMUserDefaults *database, proplist_t object,
560 char *defaultName);
562 void WMRemoveUDObjectForKey(WMUserDefaults *database, char *defaultName);
564 char *WMGetUDStringForKey(WMUserDefaults *database, char *defaultName);
566 int WMGetUDIntegerForKey(WMUserDefaults *database, char *defaultName);
568 float WMGetUDFloatForKey(WMUserDefaults *database, char *defaultName);
570 Bool WMGetUDBoolForKey(WMUserDefaults *database, char *defaultName);
572 void WMSetUDStringForKey(WMUserDefaults *database, char *value,
573 char *defaultName);
575 void WMSetUDIntegerForKey(WMUserDefaults *database, int value,
576 char *defaultName);
578 void WMSetUDFloatForKey(WMUserDefaults *database, float value,
579 char *defaultName);
581 void WMSetUDBoolForKey(WMUserDefaults *database, Bool value,
582 char *defaultName);
584 proplist_t WMGetUDSearchList(WMUserDefaults *database);
586 void WMSetUDSearchList(WMUserDefaults *database, proplist_t list);
588 extern char *WMUserDefaultsDidChangeNotification;
591 /*-------------------------------------------------------------------------*/
593 /* WMHost: host handling */
595 WMHost* WMGetCurrentHost();
597 WMHost* WMGetHostWithName(char* name);
599 WMHost* WMGetHostWithAddress(char* address);
601 WMHost* WMRetainHost(WMHost *hPtr);
603 void WMReleaseHost(WMHost *hPtr);
606 * Host cache management
607 * If enabled, only one object representing each host will be created, and
608 * a shared instance will be returned by all methods that return a host.
609 * Enabled by default.
611 void WMSetHostCacheEnabled(Bool flag);
613 Bool WMIsHostCacheEnabled();
615 void WMFlushHostCache();
618 * Compare hosts: Hosts are equal if they share at least one address
620 Bool WMIsHostEqualToHost(WMHost* hPtr, WMHost* anotherHost);
623 * Host names.
624 * WMGetHostName() will return first name (official) if a host has several.
625 * WMGetHostNames() will return a R/O WMBag with all the names of the host.
627 char* WMGetHostName(WMHost* hPtr);
629 /* The returned bag is R/O. Do not modify it, and do not free it! */
630 WMBag* WMGetHostNames(WMHost* hPtr);
633 * Host addresses.
634 * Addresses are represented as "Dotted Decimal" strings, e.g. "192.42.172.1"
635 * WMGetHostAddress() will return an arbitrary address if a host has several.
636 * WMGetHostAddresses() will return a R/O WMBag with all addresses of the host.
638 char* WMGetHostAddress(WMHost* hPtr);
640 /* The returned bag is R/O. Do not modify it, and do not free it! */
641 WMBag* WMGetHostAddresses(WMHost* hPtr);
644 /*-------------------------------------------------------------------------*/
646 /* WMConnection functions */
648 WMConnection* WMCreateConnectionAsServerAtAddress(char *host, char *service,
649 char *protocol);
651 WMConnection* WMCreateConnectionToAddress(char *host, char *service,
652 char *protocol);
654 WMConnection* WMCreateConnectionToAddressAndNotify(char *host, char *service,
655 char *protocol);
657 void WMCloseConnection(WMConnection *cPtr);
659 void WMDestroyConnection(WMConnection *cPtr);
661 WMConnection* WMAcceptConnection(WMConnection *listener);
663 /* Release the returned data! */
664 WMData* WMGetConnectionAvailableData(WMConnection *cPtr);
666 int WMSendConnectionData(WMConnection *cPtr, WMData *data);
668 Bool WMEnqueueConnectionData(WMConnection *cPtr, WMData *data);
670 #define WMFlushConnection(cPtr) WMSendConnectionData((cPtr), NULL)
672 void WMSetConnectionDelegate(WMConnection *cPtr, ConnectionDelegate *delegate);
674 /* Connection info */
676 char* WMGetConnectionAddress(WMConnection *cPtr);
678 char* WMGetConnectionService(WMConnection *cPtr);
680 char* WMGetConnectionProtocol(WMConnection *cPtr);
682 void WMSetConnectionNonBlocking(WMConnection *cPtr, Bool flag);
684 void* WMGetConnectionClientData(WMConnection *cPtr);
686 void WMSetConnectionClientData(WMConnection *cPtr, void *data);
688 unsigned int WMGetConnectionFlags(WMConnection *cPtr);
690 void WMSetConnectionFlags(WMConnection *cPtr, unsigned int flags);
692 int WMGetConnectionSocket(WMConnection *cPtr);
694 WMConnectionState WMGetConnectionState(WMConnection *cPtr);
696 WMConnectionTimeoutState WMGetConnectionTimeoutState(WMConnection *cPtr);
699 * Passing timeout==0 in the SetTimeout functions below, will reset that
700 * timeout to its default value.
703 /* The default timeout inherited by all WMConnection operations, if none set */
704 void WMSetConnectionDefaultTimeout(unsigned int timeout);
706 /* Global timeout for all WMConnection objects, for opening a new connection */
707 void WMSetConnectionOpenTimeout(unsigned int timeout);
709 /* Connection specific timeout for sending out data */
710 void WMSetConnectionSendTimeout(WMConnection *cPtr, unsigned int timeout);
713 /* Global variables */
715 extern int WCErrorCode;
718 /*-------------------------------------------------------------------------*/
722 #ifdef __cplusplus
724 #endif /* __cplusplus */
727 #endif