WPrefs: Fix crash due to wtokenfree() memory leak fix
[wmaker-crm.git] / WINGs / WINGs / WUtil.h
blob1a76b174a951f3732288162fc20571a2c62bb9aa
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 #ifndef WMAX
15 # define WMAX(a,b) ((a)>(b) ? (a) : (b))
16 #endif
17 #ifndef WMIN
18 # define WMIN(a,b) ((a)<(b) ? (a) : (b))
19 #endif
22 #ifndef __ASSERT_FUNCTION
23 # if (!defined (__GNUC__) || (__GNUC__ < 2 && \
24 __GNUC_MINOR__ < (defined (__cplusplus) ? 6 : 4)))
25 # define __ASSERT_FUNCTION ((char *) 0)
26 # else
27 # define __ASSERT_FUNCTION __PRETTY_FUNCTION__
28 # endif
29 #endif
32 #ifdef NDEBUG
34 #define wassertr(expr) {}
35 #define wassertrv(expr, val) {}
37 #else /* !NDEBUG */
39 #ifdef DEBUG
41 #include <assert.h>
43 #define wassertr(expr) assert(expr)
45 #define wassertrv(expr, val) assert(expr)
47 #else /* !DEBUG */
49 #define wassertr(expr) \
50 if (!(expr)) { \
51 wwarning("%s line %i (%s): assertion %s failed",\
52 __FILE__, __LINE__, __ASSERT_FUNCTION, #expr);\
53 return;\
56 #define wassertrv(expr, val) \
57 if (!(expr)) { \
58 wwarning("%s line %i (%s): assertion %s failed",\
59 __FILE__, __LINE__, __ASSERT_FUNCTION, #expr);\
60 return (val);\
62 #endif /* !DEBUG */
64 #endif /* !NDEBUG */
67 #ifdef __cplusplus
68 extern "C" {
69 #endif /* __cplusplus */
72 typedef enum {
73 WMPostWhenIdle = 1,
74 WMPostASAP = 2,
75 WMPostNow = 3
76 } WMPostingStyle;
79 typedef enum {
80 WNCNone = 0,
81 WNCOnName = 1,
82 WNCOnSender = 2
83 } WMNotificationCoalescing;
86 /* The possible states for connections */
87 typedef enum {
88 WCNotConnected=0,
89 WCListening,
90 WCInProgress,
91 WCFailed,
92 WCConnected,
93 WCTimedOut,
94 WCDied,
95 WCClosed
96 } WMConnectionState;
99 /* The possible states for connection timeouts */
100 typedef enum {
101 WCTNone=0,
102 WCTWhileOpening,
103 WCTWhileSending
104 } WMConnectionTimeoutState;
108 enum {
109 WBNotFound = INT_MIN, /* element was not found in WMBag */
110 WANotFound = -1 /* element was not found in WMArray */
114 typedef struct W_Array WMArray;
115 typedef struct W_Bag WMBag;
116 typedef struct W_Data WMData;
117 typedef struct W_TreeNode WMTreeNode;
118 typedef struct W_HashTable WMHashTable;
119 typedef struct W_UserDefaults WMUserDefaults;
120 typedef struct W_Notification WMNotification;
121 typedef struct W_NotificationQueue WMNotificationQueue;
122 typedef struct W_Host WMHost;
123 typedef struct W_Connection WMConnection;
124 typedef struct W_PropList WMPropList;
128 /* Some typedefs for the handler stuff */
129 typedef void *WMHandlerID;
131 typedef void WMCallback(void *data);
133 typedef void WMInputProc(int fd, int mask, void *clientData);
137 typedef int WMCompareDataProc(const void *item1, const void *item2);
139 typedef void WMFreeDataProc(void *data);
141 /* Used by WMBag or WMArray for matching data */
142 typedef int WMMatchDataProc(void *item, void *cdata);
146 typedef struct {
147 int position;
148 int count;
149 } WMRange;
153 /* DO NOT ACCESS THE CONTENTS OF THIS STRUCT */
154 typedef struct {
155 void *table;
156 void *nextItem;
157 int index;
158 } WMHashEnumerator;
161 typedef struct {
162 /* NULL is pointer hash */
163 unsigned (*hash)(const void *);
164 /* NULL is pointer compare */
165 Bool (*keyIsEqual)(const void *, const void *);
166 /* NULL does nothing */
167 void* (*retainKey)(const void *);
168 /* NULL does nothing */
169 void (*releaseKey)(const void *);
170 } WMHashTableCallbacks;
173 typedef int WMArrayIterator;
174 typedef void *WMBagIterator;
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 (*canResumeSending)(struct ConnectionDelegate *self, WMConnection *cPtr);
191 void (*didCatchException)(struct ConnectionDelegate *self, WMConnection *cPtr);
193 void (*didDie)(struct ConnectionDelegate *self, WMConnection *cPtr);
195 void (*didInitialize)(struct ConnectionDelegate *self, WMConnection *cPtr);
197 void (*didReceiveInput)(struct ConnectionDelegate *self, WMConnection *cPtr);
199 void (*didTimeout)(struct ConnectionDelegate *self, WMConnection *cPtr);
201 } ConnectionDelegate;
204 typedef void WMNotificationObserverAction(void *observerData,
205 WMNotification *notification);
209 /*......................................................................*/
211 typedef void waborthandler(int);
213 waborthandler* wsetabort(waborthandler* handler);
216 /* don't free the returned string */
217 char* wstrerror(int errnum);
219 void wmessage(const char *msg, ...);
220 void wwarning(const char *msg, ...);
221 void wfatal(const char *msg, ...);
222 void wsyserror(const char *msg, ...);
223 void wsyserrorwithcode(int error, const char *msg, ...);
225 char* wfindfile(char *paths, char *file);
227 char* wfindfileinlist(char **path_list, char *file);
229 char* wfindfileinarray(WMPropList* array, char *file);
231 char* wexpandpath(char *path);
233 /* don't free the returned string */
234 char* wgethomedir();
236 void* wmalloc(size_t size);
237 void* wmalloc0(size_t size);
238 void* wrealloc(void *ptr, size_t newsize);
239 void wfree(void *ptr);
241 #define wnew(type, count) wmalloc(sizeof(type)*count)
242 #define wnew0(type, count) wmalloc0(sizeof(type)*count)
244 void wrelease(void *ptr);
245 void* wretain(void *ptr);
247 char* wstrdup(char *str);
248 char* wstrndup(char *str, size_t len);
250 /* Concatenate str1 with str2 and return that in a newly malloc'ed string.
251 * str1 and str2 can be any strings including static and constant strings.
252 * str1 and str2 will not be modified.
253 * Free the returned string when you're done with it. */
254 char* wstrconcat(char *str1, char *str2);
256 /* This will append src to dst, and return dst. dst MUST be either NULL
257 * or a string that was a result of a dynamic allocation (malloc, realloc
258 * wmalloc, wrealloc, ...). dst CANNOT be a static or a constant string!
259 * Modifies dst (no new string is created except if dst==NULL in which case
260 * it is equivalent to calling wstrdup(src) ).
261 * The returned address may be different from the original address of dst,
262 * so always assign the returned address to avoid dangling pointers. */
263 char* wstrappend(char *dst, char *src);
266 void wtokensplit(char *command, char ***argv, int *argc);
268 char* wtokennext(char *word, char **next);
270 char* wtokenjoin(char **list, int count);
272 void wtokenfree(char **tokens, int count);
274 char* wtrimspace(char *s);
277 WMRange wmkrange(int start, int count);
278 #ifdef ANSI_C_DOESNT_LIKE_IT_THIS_WAY
279 #define wmkrange(position, count) (WMRange){(position), (count)}
280 #endif
283 char* wusergnusteppath();
285 char* wdefaultspathfordomain(char *domain);
287 void wusleep(unsigned int microsec);
289 #if 0
290 int wsprintesc(char *buffer, int length, char *format, WMSEscapes **escapes,
291 int count);
292 #endif
294 /*......................................................................*/
296 /* Event handlers: timer, idle, input */
298 WMHandlerID WMAddTimerHandler(int milliseconds, WMCallback *callback,
299 void *cdata);
301 WMHandlerID WMAddPersistentTimerHandler(int milliseconds, WMCallback *callback,
302 void *cdata);
304 void WMDeleteTimerWithClientData(void *cdata);
306 void WMDeleteTimerHandler(WMHandlerID handlerID);
308 WMHandlerID WMAddIdleHandler(WMCallback *callback, void *cdata);
310 void WMDeleteIdleHandler(WMHandlerID handlerID);
312 WMHandlerID WMAddInputHandler(int fd, int condition, WMInputProc *proc,
313 void *clientData);
315 void WMDeleteInputHandler(WMHandlerID handlerID);
318 /* This function is used _only_ if you create a non-GUI program.
319 * For GUI based programs use WMNextEvent()/WMHandleEvent() instead.
320 * This function will handle all input/timer/idle events, then return.
323 void WHandleEvents();
325 /*......................................................................*/
328 WMHashTable* WMCreateHashTable(WMHashTableCallbacks callbacks);
330 void WMFreeHashTable(WMHashTable *table);
332 void WMResetHashTable(WMHashTable *table);
334 unsigned WMCountHashTable(WMHashTable *table);
336 void* WMHashGet(WMHashTable *table, const void *key);
338 /* Returns True if there is a value associated with <key> in the table, in
339 * which case <retKey> and <retItem> will contain the item's internal key
340 * address and the item's value respectively.
341 * If there is no value associated with <key> it will return False and in
342 * this case <retKey> and <retItem> will be undefined.
343 * Use this when you need to perform your own custom retain/release mechanism
344 * which requires access to the keys too.
346 Bool WMHashGetItemAndKey(WMHashTable *table, const void *key,
347 void **retItem, void **retKey);
349 /* put data in table, replacing already existing data and returning
350 * the old value */
351 void* WMHashInsert(WMHashTable *table, const void *key, const void *data);
353 void WMHashRemove(WMHashTable *table, const void *key);
355 /* warning: do not manipulate the table while using the enumerator functions */
356 WMHashEnumerator WMEnumerateHashTable(WMHashTable *table);
358 void* WMNextHashEnumeratorItem(WMHashEnumerator *enumerator);
360 void* WMNextHashEnumeratorKey(WMHashEnumerator *enumerator);
362 /* Returns True if there is a next element, in which case key and item
363 * will contain the next element's key and value respectively.
364 * If there is no next element available it will return False and in this
365 * case key and item will be undefined.
367 Bool WMNextHashEnumeratorItemAndKey(WMHashEnumerator *enumerator,
368 void **item, void **key);
373 /* some predefined callback sets */
375 extern const WMHashTableCallbacks WMIntHashCallbacks;
376 /* sizeof(keys) are <= sizeof(void*) */
378 extern const WMHashTableCallbacks WMStringHashCallbacks;
379 /* keys are strings. Strings will be copied with wstrdup()
380 * and freed with wfree() */
382 extern const WMHashTableCallbacks WMStringPointerHashCallbacks;
383 /* keys are strings, but they are not copied */
386 /*......................................................................*/
389 * WMArray use an array to store the elements.
390 * Item indexes may be only positive integer numbers.
391 * The array cannot contain holes in it.
393 * Pros:
394 * Fast [O(1)] access to elements
395 * Fast [O(1)] push/pop
397 * Cons:
398 * A little slower [O(n)] for insertion/deletion of elements that
399 * aren't in the end
402 WMArray* WMCreateArray(int initialSize);
404 WMArray* WMCreateArrayWithDestructor(int initialSize, WMFreeDataProc *destructor);
406 WMArray* WMCreateArrayWithArray(WMArray *array);
408 #define WMDuplicateArray(array) WMCreateArrayWithArray(array)
410 void WMEmptyArray(WMArray *array);
412 void WMFreeArray(WMArray *array);
414 int WMGetArrayItemCount(WMArray *array);
416 /* appends other to array. other remains unchanged */
417 void WMAppendArray(WMArray *array, WMArray *other);
419 /* add will place the element at the end of the array */
420 void WMAddToArray(WMArray *array, void *item);
422 #define WMPushInArray(array, item) WMAddToArray(array, item)
424 /* insert will increment the index of elements after it by 1 */
425 void WMInsertInArray(WMArray *array, int index, void *item);
427 /* replace and set will return the old item WITHOUT calling the
428 * destructor on it even if its available. Free the returned item yourself.
430 void* WMReplaceInArray(WMArray *array, int index, void *item);
432 #define WMSetInArray(array, index, item) WMReplaceInArray(array, index, item)
434 /* delete and remove will remove the elements and cause the elements
435 * after them to decrement their indexes by 1. Also will call the
436 * destructor on the deleted element if there's one available.
438 int WMDeleteFromArray(WMArray *array, int index);
440 #define WMRemoveFromArray(array, item) WMRemoveFromArrayMatching(array, NULL, item)
442 int WMRemoveFromArrayMatching(WMArray *array, WMMatchDataProc *match, void *cdata);
444 void* WMGetFromArray(WMArray *array, int index);
446 #define WMGetFirstInArray(array, item) WMFindInArray(array, NULL, item)
448 /* pop will return the last element from the array, also removing it
449 * from the array. The destructor is NOT called, even if available.
450 * Free the returned element if needed by yourself
452 void* WMPopFromArray(WMArray *array);
454 int WMFindInArray(WMArray *array, WMMatchDataProc *match, void *cdata);
456 int WMCountInArray(WMArray *array, void *item);
458 /* comparer must return:
459 * < 0 if a < b
460 * > 0 if a > b
461 * = 0 if a = b
463 void WMSortArray(WMArray *array, WMCompareDataProc *comparer);
465 void WMMapArray(WMArray *array, void (*function)(void*, void*), void *data);
467 WMArray* WMGetSubarrayWithRange(WMArray* array, WMRange aRange);
469 void* WMArrayFirst(WMArray *array, WMArrayIterator *iter);
471 void* WMArrayLast(WMArray *array, WMArrayIterator *iter);
473 /* The following 2 functions assume that the array doesn't change between calls */
474 void* WMArrayNext(WMArray *array, WMArrayIterator *iter);
476 void* WMArrayPrevious(WMArray *array, WMArrayIterator *iter);
479 /* The following 2 macros assume that the array doesn't change in the for loop */
480 #define WM_ITERATE_ARRAY(array, var, i) \
481 for (var = WMArrayFirst(array, &(i)); (i) != WANotFound; \
482 var = WMArrayNext(array, &(i)))
484 #define WM_ETARETI_ARRAY(array, var, i) \
485 for (var = WMArrayLast(array, &(i)); (i) != WANotFound; \
486 var = WMArrayPrevious(array, &(i)))
488 /*..........................................................................*/
491 * Tree bags use a red-black tree for storage.
492 * Item indexes may be any integer number.
494 * Pros:
495 * O(lg n) insertion/deletion/search
496 * Good for large numbers of elements with sparse indexes
498 * Cons:
499 * O(lg n) insertion/deletion/search
500 * Slow for storing small numbers of elements
503 #define WMCreateBag(size) WMCreateTreeBag()
505 #define WMCreateBagWithDestructor(size, d) WMCreateTreeBagWithDestructor(d)
507 WMBag* WMCreateTreeBag(void);
509 WMBag* WMCreateTreeBagWithDestructor(WMFreeDataProc *destructor);
511 int WMGetBagItemCount(WMBag *bag);
513 void WMAppendBag(WMBag *bag, WMBag *other);
515 void WMPutInBag(WMBag *bag, void *item);
517 /* insert will increment the index of elements after it by 1 */
518 void WMInsertInBag(WMBag *bag, int index, void *item);
520 /* erase will remove the element from the bag,
521 * but will keep the index of the other elements unchanged */
522 int WMEraseFromBag(WMBag *bag, int index);
524 /* delete and remove will remove the elements and cause the elements
525 * after them to decrement their indexes by 1 */
526 int WMDeleteFromBag(WMBag *bag, int index);
528 int WMRemoveFromBag(WMBag *bag, void *item);
530 void* WMGetFromBag(WMBag *bag, int index);
532 void* WMReplaceInBag(WMBag *bag, int index, void *item);
534 #define WMSetInBag(bag, index, item) WMReplaceInBag(bag, index, item)
536 /* comparer must return:
537 * < 0 if a < b
538 * > 0 if a > b
539 * = 0 if a = b
541 void WMSortBag(WMBag *bag, WMCompareDataProc *comparer);
543 void WMEmptyBag(WMBag *bag);
545 void WMFreeBag(WMBag *bag);
547 void WMMapBag(WMBag *bag, void (*function)(void*, void*), void *data);
549 int WMGetFirstInBag(WMBag *bag, void *item);
551 int WMCountInBag(WMBag *bag, void *item);
553 int WMFindInBag(WMBag *bag, WMMatchDataProc *match, void *cdata);
555 void* WMBagFirst(WMBag *bag, WMBagIterator *ptr);
557 void* WMBagLast(WMBag *bag, WMBagIterator *ptr);
559 /* The following 4 functions assume that the bag doesn't change between calls */
560 void* WMBagNext(WMBag *bag, WMBagIterator *ptr);
562 void* WMBagPrevious(WMBag *bag, WMBagIterator *ptr);
564 void* WMBagIteratorAtIndex(WMBag *bag, int index, WMBagIterator *ptr);
566 int WMBagIndexForIterator(WMBag *bag, WMBagIterator ptr);
569 /* The following 2 macros assume that the bag doesn't change in the for loop */
570 #define WM_ITERATE_BAG(bag, var, i) \
571 for (var = WMBagFirst(bag, &(i)); (i) != NULL; \
572 var = WMBagNext(bag, &(i)))
574 #define WM_ETARETI_BAG(bag, var, i) \
575 for (var = WMBagLast(bag, &(i)); (i) != NULL; \
576 var = WMBagPrevious(bag, &(i)))
580 /*-------------------------------------------------------------------------*/
582 /* WMData handling */
584 /* Creating/destroying data */
586 WMData* WMCreateDataWithCapacity(unsigned capacity);
588 WMData* WMCreateDataWithLength(unsigned length);
590 WMData* WMCreateDataWithBytes(void *bytes, unsigned length);
592 /* destructor is a function called to free the data when releasing the data
593 * object, or NULL if no freeing of data is necesary. */
594 WMData* WMCreateDataWithBytesNoCopy(void *bytes, unsigned length,
595 WMFreeDataProc *destructor);
597 WMData* WMCreateDataWithData(WMData *aData);
599 WMData* WMRetainData(WMData *aData);
601 void WMReleaseData(WMData *aData);
603 /* Adjusting capacity */
605 void WMSetDataCapacity(WMData *aData, unsigned capacity);
607 void WMSetDataLength(WMData *aData, unsigned length);
609 void WMIncreaseDataLengthBy(WMData *aData, unsigned extraLength);
611 /* Accessing data */
613 const void* WMDataBytes(WMData *aData);
615 void WMGetDataBytes(WMData *aData, void *buffer);
617 void WMGetDataBytesWithLength(WMData *aData, void *buffer, unsigned length);
619 void WMGetDataBytesWithRange(WMData *aData, void *buffer, WMRange aRange);
621 WMData* WMGetSubdataWithRange(WMData *aData, WMRange aRange);
623 /* Testing data */
625 Bool WMIsDataEqualToData(WMData *aData, WMData *anotherData);
627 unsigned WMGetDataLength(WMData *aData);
629 /* Adding data */
631 void WMAppendDataBytes(WMData *aData, void *bytes, unsigned length);
633 void WMAppendData(WMData *aData, WMData *anotherData);
635 /* Modifying data */
637 void WMReplaceDataBytesInRange(WMData *aData, WMRange aRange, void *bytes);
639 void WMResetDataBytesInRange(WMData *aData, WMRange aRange);
641 void WMSetData(WMData *aData, WMData *anotherData);
644 void WMSetDataFormat(WMData *aData, unsigned format);
646 unsigned WMGetDataFormat(WMData *aData);
647 /* Storing data */
650 /*--------------------------------------------------------------------------*/
652 /* Generic Tree and TreeNode */
654 WMTreeNode* WMCreateTreeNode(void *data);
656 WMTreeNode* WMCreateTreeNodeWithDestructor(void *data, WMFreeDataProc *destructor);
658 WMTreeNode* WMInsertItemInTree(WMTreeNode *parent, int index, void *item);
660 #define WMAddItemToTree(parent, item) WMInsertItemInTree(parent, -1, item)
662 WMTreeNode* WMInsertNodeInTree(WMTreeNode *parent, int index, WMTreeNode *aNode);
664 #define WMAddNodeToTree(parent, aNode) WMInsertNodeInTree(parent, -1, aNode)
666 void WMDestroyTreeNode(WMTreeNode *aNode);
668 void WMDeleteLeafForTreeNode(WMTreeNode *aNode, int index);
670 void WMRemoveLeafForTreeNode(WMTreeNode *aNode, void *leaf);
672 void* WMReplaceDataForTreeNode(WMTreeNode *aNode, void *newData);
674 void* WMGetDataForTreeNode(WMTreeNode *aNode);
676 int WMGetTreeNodeDepth(WMTreeNode *aNode);
678 WMTreeNode* WMGetParentForTreeNode(WMTreeNode *aNode);
680 /* Sort only the leaves of the passed node */
681 void WMSortLeavesForTreeNode(WMTreeNode *aNode, WMCompareDataProc *comparer);
683 /* Sort all tree recursively starting from the passed node */
684 void WMSortTree(WMTreeNode *aNode, WMCompareDataProc *comparer);
686 /* Returns the first node which matches node's data with cdata by 'match' */
687 WMTreeNode* WMFindInTree(WMTreeNode *aTree, WMMatchDataProc *match, void *cdata);
689 /* Returns first tree node that has data == cdata */
690 #define WMGetFirstInTree(aTree, cdata) WMFindInTree(aTree, NULL, cdata)
693 /*--------------------------------------------------------------------------*/
696 WMNotification* WMCreateNotification(const char *name, void *object, void *clientData);
698 void WMReleaseNotification(WMNotification *notification);
700 WMNotification* WMRetainNotification(WMNotification *notification);
702 void* WMGetNotificationClientData(WMNotification *notification);
704 void* WMGetNotificationObject(WMNotification *notification);
706 const char* WMGetNotificationName(WMNotification *notification);
709 void WMAddNotificationObserver(WMNotificationObserverAction *observerAction,
710 void *observer, const char *name, void *object);
712 void WMPostNotification(WMNotification *notification);
714 void WMRemoveNotificationObserver(void *observer);
716 void WMRemoveNotificationObserverWithName(void *observer, const char *name,
717 void *object);
719 void WMPostNotificationName(const char *name, void *object, void *clientData);
721 WMNotificationQueue* WMGetDefaultNotificationQueue(void);
723 WMNotificationQueue* WMCreateNotificationQueue(void);
725 void WMDequeueNotificationMatching(WMNotificationQueue *queue,
726 WMNotification *notification,
727 unsigned mask);
729 void WMEnqueueNotification(WMNotificationQueue *queue,
730 WMNotification *notification,
731 WMPostingStyle postingStyle);
733 void WMEnqueueCoalesceNotification(WMNotificationQueue *queue,
734 WMNotification *notification,
735 WMPostingStyle postingStyle,
736 unsigned coalesceMask);
739 /*......................................................................*/
741 /* Property Lists handling */
743 void WMPLSetCaseSensitive(Bool caseSensitive);
745 WMPropList* WMCreatePLString(char *str);
747 WMPropList* WMCreatePLData(WMData *data);
749 WMPropList* WMCreatePLDataWithBytes(unsigned char *bytes, unsigned int length);
751 WMPropList* WMCreatePLDataWithBytesNoCopy(unsigned char *bytes,
752 unsigned int length,
753 WMFreeDataProc *destructor);
755 WMPropList* WMCreatePLArray(WMPropList *elem, ...);
757 WMPropList* WMCreatePLDictionary(WMPropList *key, WMPropList *value, ...);
759 WMPropList* WMRetainPropList(WMPropList *plist);
761 void WMReleasePropList(WMPropList *plist);
763 /* Objects inserted in arrays and dictionaries will be retained,
764 * so you can safely release them after insertion.
765 * For dictionaries both the key and value are retained.
766 * Objects removed from arrays or dictionaries are released */
767 void WMInsertInPLArray(WMPropList *plist, int index, WMPropList *item);
769 void WMAddToPLArray(WMPropList *plist, WMPropList *item);
771 void WMDeleteFromPLArray(WMPropList *plist, int index);
773 void WMRemoveFromPLArray(WMPropList *plist, WMPropList *item);
775 void WMPutInPLDictionary(WMPropList *plist, WMPropList *key, WMPropList *value);
777 void WMRemoveFromPLDictionary(WMPropList *plist, WMPropList *key);
779 /* It will insert all key/value pairs from source into dest, overwriting
780 * the values with the same keys from dest, keeping all values with keys
781 * only present in dest unchanged */
782 WMPropList* WMMergePLDictionaries(WMPropList *dest, WMPropList *source,
783 Bool recursive);
785 /* It will remove all key/value pairs from dest for which there is an
786 * identical key/value present in source. Entires only present in dest, or
787 * which have different values in dest than in source will be preserved. */
788 WMPropList* WMSubtractPLDictionaries(WMPropList *dest, WMPropList *source,
789 Bool recursive);
791 int WMGetPropListItemCount(WMPropList *plist);
793 Bool WMIsPLString(WMPropList *plist);
795 Bool WMIsPLData(WMPropList *plist);
797 Bool WMIsPLArray(WMPropList *plist);
799 Bool WMIsPLDictionary(WMPropList *plist);
801 Bool WMIsPropListEqualTo(WMPropList *plist, WMPropList *other);
803 /* Returns a reference. Do not free it! */
804 char* WMGetFromPLString(WMPropList *plist);
806 /* Returns a reference. Do not free it! */
807 WMData* WMGetFromPLData(WMPropList *plist);
809 /* Returns a reference. Do not free it! */
810 const unsigned char* WMGetPLDataBytes(WMPropList *plist);
812 int WMGetPLDataLength(WMPropList *plist);
814 /* Returns a reference. */
815 WMPropList* WMGetFromPLArray(WMPropList *plist, int index);
817 /* Returns a reference. */
818 WMPropList* WMGetFromPLDictionary(WMPropList *plist, WMPropList *key);
820 /* Returns a PropList array with all the dictionary keys. Release it when
821 * you're done. Keys in array are retained from the original dictionary
822 * not copied and need NOT to be released individually. */
823 WMPropList* WMGetPLDictionaryKeys(WMPropList *plist);
825 /* Creates only the first level deep object. All the elements inside are
826 * retained from the original */
827 WMPropList* WMShallowCopyPropList(WMPropList *plist);
829 /* Makes a completely separate replica of the original proplist */
830 WMPropList* WMDeepCopyPropList(WMPropList *plist);
832 WMPropList* WMCreatePropListFromDescription(char *desc);
834 /* Free the returned string when you no longer need it */
835 char* WMGetPropListDescription(WMPropList *plist, Bool indented);
837 WMPropList* WMReadPropListFromFile(char *file);
839 Bool WMWritePropListToFile(WMPropList *plist, char *path, Bool atomically);
841 /*......................................................................*/
843 WMUserDefaults* WMGetStandardUserDefaults(void);
845 WMUserDefaults* WMGetDefaultsFromPath(char *path);
847 void WMSynchronizeUserDefaults(WMUserDefaults *database);
849 void WMSaveUserDefaults(WMUserDefaults *database);
851 void WMEnableUDPeriodicSynchronization(WMUserDefaults *database, Bool enable);
853 /* Returns a WMPropList array with all the keys in the user defaults database.
854 * Free the array with WMReleasePropList() when no longer needed.
855 * Keys in array are just retained references to the original keys */
856 WMPropList* WMGetUDKeys(WMUserDefaults *database);
858 WMPropList* WMGetUDObjectForKey(WMUserDefaults *database, char *defaultName);
860 void WMSetUDObjectForKey(WMUserDefaults *database, WMPropList *object,
861 char *defaultName);
863 void WMRemoveUDObjectForKey(WMUserDefaults *database, char *defaultName);
865 char* WMGetUDStringForKey(WMUserDefaults *database, char *defaultName);
867 int WMGetUDIntegerForKey(WMUserDefaults *database, char *defaultName);
869 float WMGetUDFloatForKey(WMUserDefaults *database, char *defaultName);
871 Bool WMGetUDBoolForKey(WMUserDefaults *database, char *defaultName);
873 void WMSetUDStringForKey(WMUserDefaults *database, char *value,
874 char *defaultName);
876 void WMSetUDIntegerForKey(WMUserDefaults *database, int value,
877 char *defaultName);
879 void WMSetUDFloatForKey(WMUserDefaults *database, float value,
880 char *defaultName);
882 void WMSetUDBoolForKey(WMUserDefaults *database, Bool value,
883 char *defaultName);
885 WMPropList* WMGetUDSearchList(WMUserDefaults *database);
887 void WMSetUDSearchList(WMUserDefaults *database, WMPropList *list);
889 extern char *WMUserDefaultsDidChangeNotification;
892 /*-------------------------------------------------------------------------*/
894 /* WMHost: host handling */
896 WMHost* WMGetCurrentHost();
898 WMHost* WMGetHostWithName(char* name);
900 WMHost* WMGetHostWithAddress(char* address);
902 WMHost* WMRetainHost(WMHost *hPtr);
904 void WMReleaseHost(WMHost *hPtr);
907 * Host cache management
908 * If enabled, only one object representing each host will be created, and
909 * a shared instance will be returned by all methods that return a host.
910 * Enabled by default.
912 void WMSetHostCacheEnabled(Bool flag);
914 Bool WMIsHostCacheEnabled();
916 void WMFlushHostCache();
919 * Compare hosts: Hosts are equal if they share at least one address
921 Bool WMIsHostEqualToHost(WMHost* hPtr, WMHost* anotherHost);
924 * Host names.
925 * WMGetHostName() will return first name (official) if a host has several.
926 * WMGetHostNames() will return a R/O WMArray with all the names of the host.
928 char* WMGetHostName(WMHost* hPtr);
930 /* The returned array is R/O. Do not modify it, and do not free it! */
931 WMArray* WMGetHostNames(WMHost* hPtr);
934 * Host addresses.
935 * Addresses are represented as "Dotted Decimal" strings, e.g. "192.42.172.1"
936 * WMGetHostAddress() will return an arbitrary address if a host has several.
937 * WMGetHostAddresses() will return a R/O WMArray with all addresses of the host.
939 char* WMGetHostAddress(WMHost* hPtr);
941 /* The returned array is R/O. Do not modify it, and do not free it! */
942 WMArray* WMGetHostAddresses(WMHost* hPtr);
945 /*-------------------------------------------------------------------------*/
947 /* WMConnection functions */
949 WMConnection* WMCreateConnectionAsServerAtAddress(char *host, char *service,
950 char *protocol);
952 WMConnection* WMCreateConnectionToAddress(char *host, char *service,
953 char *protocol);
955 WMConnection* WMCreateConnectionToAddressAndNotify(char *host, char *service,
956 char *protocol);
958 void WMCloseConnection(WMConnection *cPtr);
960 void WMDestroyConnection(WMConnection *cPtr);
962 WMConnection* WMAcceptConnection(WMConnection *listener);
964 /* Release the returned data! */
965 WMData* WMGetConnectionAvailableData(WMConnection *cPtr);
967 int WMSendConnectionData(WMConnection *cPtr, WMData *data);
969 Bool WMEnqueueConnectionData(WMConnection *cPtr, WMData *data);
971 #define WMFlushConnection(cPtr) WMSendConnectionData((cPtr), NULL)
973 void WMSetConnectionDelegate(WMConnection *cPtr, ConnectionDelegate *delegate);
975 /* Connection info */
977 char* WMGetConnectionAddress(WMConnection *cPtr);
979 char* WMGetConnectionService(WMConnection *cPtr);
981 char* WMGetConnectionProtocol(WMConnection *cPtr);
983 Bool WMSetConnectionNonBlocking(WMConnection *cPtr, Bool flag);
985 Bool WMSetConnectionCloseOnExec(WMConnection *cPtr, Bool flag);
987 void WMSetConnectionShutdownOnClose(WMConnection *cPtr, Bool flag);
989 void* WMGetConnectionClientData(WMConnection *cPtr);
991 void WMSetConnectionClientData(WMConnection *cPtr, void *data);
993 unsigned int WMGetConnectionFlags(WMConnection *cPtr);
995 void WMSetConnectionFlags(WMConnection *cPtr, unsigned int flags);
997 int WMGetConnectionSocket(WMConnection *cPtr);
999 WMConnectionState WMGetConnectionState(WMConnection *cPtr);
1001 WMConnectionTimeoutState WMGetConnectionTimeoutState(WMConnection *cPtr);
1003 WMArray* WMGetConnectionUnsentData(WMConnection *cPtr);
1005 #define WMGetConnectionQueuedData(cPtr) WMGetConnectionUnsentData(cPtr)
1009 * Passing timeout==0 in the SetTimeout functions below, will reset that
1010 * timeout to its default value.
1013 /* The default timeout inherited by all WMConnection operations, if none set */
1014 void WMSetConnectionDefaultTimeout(unsigned int timeout);
1016 /* Global timeout for all WMConnection objects, for opening a new connection */
1017 void WMSetConnectionOpenTimeout(unsigned int timeout);
1019 /* Connection specific timeout for sending out data */
1020 void WMSetConnectionSendTimeout(WMConnection *cPtr, unsigned int timeout);
1023 /* Global variables */
1025 extern int WCErrorCode;
1028 /*-------------------------------------------------------------------------*/
1032 #ifdef __cplusplus
1034 #endif /* __cplusplus */
1037 #endif