19 WPLString
= 0x57504c01,
21 WPLArray
= 0x57504c03,
22 WPLDictionary
= 0x57504c04
25 typedef struct W_PropList
{
38 typedef struct PLData
{
45 typedef struct StringBuffer
{
50 static unsigned hashPropList(WMPropList
* plist
);
51 static WMPropList
*getPLString(PLData
* pldata
);
52 static WMPropList
*getPLQString(PLData
* pldata
);
53 static WMPropList
*getPLData(PLData
* pldata
);
54 static WMPropList
*getPLArray(PLData
* pldata
);
55 static WMPropList
*getPLDictionary(PLData
* pldata
);
56 static WMPropList
*getPropList(PLData
* pldata
);
58 typedef unsigned (*hashFunc
) (const void *);
59 typedef Bool(*isEqualFunc
) (const void *, const void *);
60 typedef void *(*retainFunc
) (const void *);
61 typedef void (*releaseFunc
) (const void *);
63 static const WMHashTableCallbacks WMPropListHashCallbacks
= {
64 (hashFunc
) hashPropList
,
65 (isEqualFunc
) WMIsPropListEqualTo
,
70 static Bool caseSensitive
= True
;
72 #define BUFFERSIZE 8192
73 #define BUFFERSIZE_INCREMENT 1024
76 # define DPUT(s) puts(s)
81 #define COMPLAIN(pld, msg) wwarning(_("syntax error in %s %s, line %i: %s"),\
82 (pld)->filename ? "file" : "PropList",\
83 (pld)->filename ? (pld)->filename : "description",\
84 (pld)->lineNumber, msg)
86 #define ISSTRINGABLE(c) (isalnum(c) || (c)=='.' || (c)=='_' || (c)=='/' \
89 #define CHECK_BUFFER_SIZE(buf, ptr) \
90 if ((ptr) >= (buf).size-1) {\
91 (buf).size += BUFFERSIZE_INCREMENT;\
92 (buf).str = wrealloc((buf).str, (buf).size);\
95 #define inrange(ch, min, max) ((ch)>=(min) && (ch)<=(max))
96 #define noquote(ch) (inrange(ch, 'a', 'z') || inrange(ch, 'A', 'Z') || inrange(ch, '0', '9') || ((ch)=='_') || ((ch)=='.') || ((ch)=='$'))
97 #define charesc(ch) (inrange(ch, 0x07, 0x0c) || ((ch)=='"') || ((ch)=='\\'))
98 #define numesc(ch) (((ch)<=0x06) || inrange(ch, 0x0d, 0x1f) || ((ch)>0x7e))
99 #define ishexdigit(ch) (inrange(ch, 'a', 'f') || inrange(ch, 'A', 'F') || inrange(ch, '0', '9'))
100 #define char2num(ch) (inrange(ch,'0','9') ? ((ch)-'0') : (inrange(ch,'a','f') ? ((ch)-0x57) : ((ch)-0x37)))
101 #define num2char(num) ((num) < 0xa ? ((num)+'0') : ((num)+0x57))
103 #define MaxHashLength 64
105 static unsigned hashPropList(WMPropList
* plist
)
112 switch (plist
->type
) {
114 key
= plist
->d
.string
;
115 len
= WMIN(strlen(key
), MaxHashLength
);
116 for (i
= 0; i
< len
; i
++) {
117 ret
^= tolower(key
[i
]) << ctr
;
118 ctr
= (ctr
+ 1) % sizeof(char *);
121 ret ^= tolower(*key++) << ctr;
122 ctr = (ctr + 1) % sizeof (char *);
127 key
= WMDataBytes(plist
->d
.data
);
128 len
= WMIN(WMGetDataLength(plist
->d
.data
), MaxHashLength
);
129 for (i
= 0; i
< len
; i
++) {
130 ret
^= key
[i
] << ctr
;
131 ctr
= (ctr
+ 1) % sizeof(char *);
136 wwarning(_("Only string or data is supported for a proplist dictionary key"));
144 static WMPropList
*retainPropListByCount(WMPropList
* plist
, int count
)
146 WMPropList
*key
, *value
;
150 plist
->retainCount
+= count
;
152 switch (plist
->type
) {
157 for (i
= 0; i
< WMGetArrayItemCount(plist
->d
.array
); i
++) {
158 retainPropListByCount(WMGetFromArray(plist
->d
.array
, i
), count
);
162 e
= WMEnumerateHashTable(plist
->d
.dict
);
163 while (WMNextHashEnumeratorItemAndKey(&e
, (void **)&value
, (void **)&key
)) {
164 retainPropListByCount(key
, count
);
165 retainPropListByCount(value
, count
);
169 wwarning(_("Used proplist functions on non-WMPropLists objects"));
170 wassertrv(False
, NULL
);
177 static void releasePropListByCount(WMPropList
* plist
, int count
)
179 WMPropList
*key
, *value
;
183 plist
->retainCount
-= count
;
185 switch (plist
->type
) {
187 if (plist
->retainCount
< 1) {
188 wfree(plist
->d
.string
);
193 if (plist
->retainCount
< 1) {
194 WMReleaseData(plist
->d
.data
);
199 for (i
= 0; i
< WMGetArrayItemCount(plist
->d
.array
); i
++) {
200 releasePropListByCount(WMGetFromArray(plist
->d
.array
, i
), count
);
202 if (plist
->retainCount
< 1) {
203 WMFreeArray(plist
->d
.array
);
208 e
= WMEnumerateHashTable(plist
->d
.dict
);
209 while (WMNextHashEnumeratorItemAndKey(&e
, (void **)&value
, (void **)&key
)) {
210 releasePropListByCount(key
, count
);
211 releasePropListByCount(value
, count
);
213 if (plist
->retainCount
< 1) {
214 WMFreeHashTable(plist
->d
.dict
);
219 wwarning(_("Used proplist functions on non-WMPropLists objects"));
225 static char *dataDescription(WMPropList
* plist
)
227 const unsigned char *data
;
231 data
= WMDataBytes(plist
->d
.data
);
232 length
= WMGetDataLength(plist
->d
.data
);
234 retVal
= (char *)wmalloc(2 * length
+ length
/ 4 + 3);
237 for (i
= 0, j
= 1; i
< length
; i
++) {
238 retVal
[j
++] = num2char((data
[i
] >> 4) & 0x0f);
239 retVal
[j
++] = num2char(data
[i
] & 0x0f);
240 if ((i
& 0x03) == 3 && i
!= length
- 1) {
241 /* if we've just finished a 32-bit int, add a space */
251 static char *stringDescription(WMPropList
* plist
)
254 char *retVal
, *sPtr
, *dPtr
;
258 str
= plist
->d
.string
;
260 if (strlen(str
) == 0) {
261 return wstrdup("\"\"");
264 /* FIXME: make this work with unichars. */
269 while ((ch
= *sPtr
)) {
284 retVal
= (char *)wmalloc(len
+ 1);
292 while ((ch
= *sPtr
)) {
315 *dPtr
= ch
; /* " or \ */
317 } else if (numesc(ch
)) {
319 *(dPtr
++) = '0' + ((ch
>> 6) & 07);
320 *(dPtr
++) = '0' + ((ch
>> 3) & 07);
321 *dPtr
= '0' + (ch
& 07);
337 static char *description(WMPropList
* plist
)
339 WMPropList
*key
, *val
;
341 char *str
, *tmp
, *skey
, *sval
;
345 switch (plist
->type
) {
347 retstr
= stringDescription(plist
);
350 retstr
= dataDescription(plist
);
353 retstr
= wstrdup("(");
354 for (i
= 0; i
< WMGetArrayItemCount(plist
->d
.array
); i
++) {
355 str
= description(WMGetFromArray(plist
->d
.array
, i
));
357 retstr
= wstrappend(retstr
, str
);
359 tmp
= (char *)wmalloc(strlen(retstr
) + strlen(str
) + 3);
360 sprintf(tmp
, "%s, %s", retstr
, str
);
366 retstr
= wstrappend(retstr
, ")");
369 retstr
= wstrdup("{");
370 e
= WMEnumerateHashTable(plist
->d
.dict
);
371 while (WMNextHashEnumeratorItemAndKey(&e
, (void **)&val
, (void **)&key
)) {
372 skey
= description(key
);
373 sval
= description(val
);
374 tmp
= (char *)wmalloc(strlen(retstr
) + strlen(skey
) + strlen(sval
) + 5);
375 sprintf(tmp
, "%s%s = %s;", retstr
, skey
, sval
);
381 retstr
= wstrappend(retstr
, "}");
384 wwarning(_("Used proplist functions on non-WMPropLists objects"));
385 wassertrv(False
, NULL
);
392 static char *indentedDescription(WMPropList
* plist
, int level
)
394 WMPropList
*key
, *val
;
396 char *str
, *tmp
, *skey
, *sval
;
400 if (plist
->type
== WPLArray
/* || plist->type==WPLDictionary */ ) {
401 retstr
= description(plist
);
403 if (retstr
&& ((2 * (level
+ 1) + strlen(retstr
)) <= 77)) {
411 switch (plist
->type
) {
413 retstr
= stringDescription(plist
);
416 retstr
= dataDescription(plist
);
419 retstr
= wstrdup("(\n");
420 for (i
= 0; i
< WMGetArrayItemCount(plist
->d
.array
); i
++) {
421 str
= indentedDescription(WMGetFromArray(plist
->d
.array
, i
), level
+ 1);
423 tmp
= (char *)wmalloc(2 * (level
+ 1) + strlen(retstr
) + strlen(str
) + 1);
424 sprintf(tmp
, "%s%*s%s", retstr
, 2 * (level
+ 1), "", str
);
428 tmp
= (char *)wmalloc(2 * (level
+ 1) + strlen(retstr
) + strlen(str
) + 3);
429 sprintf(tmp
, "%s,\n%*s%s", retstr
, 2 * (level
+ 1), "", str
);
435 tmp
= (char *)wmalloc(strlen(retstr
) + 2 * level
+ 3);
436 sprintf(tmp
, "%s\n%*s)", retstr
, 2 * level
, "");
441 retstr
= wstrdup("{\n");
442 e
= WMEnumerateHashTable(plist
->d
.dict
);
443 while (WMNextHashEnumeratorItemAndKey(&e
, (void **)&val
, (void **)&key
)) {
444 skey
= indentedDescription(key
, level
+ 1);
445 sval
= indentedDescription(val
, level
+ 1);
446 tmp
= (char *)wmalloc(2 * (level
+ 1) + strlen(retstr
) + strlen(skey
)
448 sprintf(tmp
, "%s%*s%s = %s;\n", retstr
, 2 * (level
+ 1), "", skey
, sval
);
454 tmp
= (char *)wmalloc(strlen(retstr
) + 2 * level
+ 2);
455 sprintf(tmp
, "%s%*s}", retstr
, 2 * level
, "");
460 wwarning(_("Used proplist functions on non-WMPropLists objects"));
461 wassertrv(False
, NULL
);
468 static inline int getChar(PLData
* pldata
)
472 c
= pldata
->ptr
[pldata
->pos
];
480 pldata
->lineNumber
++;
485 static inline int getNonSpaceChar(PLData
* pldata
)
490 c
= pldata
->ptr
[pldata
->pos
];
496 pldata
->lineNumber
++;
497 } else if (!isspace(c
)) {
505 static char *unescapestr(const char *src
)
507 char *dest
= wmalloc(strlen(src
) + 1);
511 for (dPtr
= dest
; ; dPtr
++) {
522 } else if ((ch
>= '0') && (ch
<= '7')) {
525 /* Convert octal number to character */
528 if ((ch
>= '0') && (ch
<= '7')) {
530 wch
= (wch
<< 3) | (ch
& 07);
532 if ((ch
>= '0') && (ch
<= '7')) {
534 wch
= (wch
<< 3) | (ch
& 07);
573 static WMPropList
*getPLString(PLData
* pldata
)
580 sBuf
.str
= wmalloc(BUFFERSIZE
);
581 sBuf
.size
= BUFFERSIZE
;
585 if (ISSTRINGABLE(c
)) {
586 CHECK_BUFFER_SIZE(sBuf
, ptr
);
601 char *tmp
= unescapestr(sBuf
.str
);
602 plist
= WMCreatePLString(tmp
);
611 static WMPropList
*getPLQString(PLData
* pldata
)
614 int ptr
= 0, escaping
= 0, ok
= 1;
618 sBuf
.str
= wmalloc(BUFFERSIZE
);
619 sBuf
.size
= BUFFERSIZE
;
627 } else if (c
== '"') {
631 CHECK_BUFFER_SIZE(sBuf
, ptr
);
632 sBuf
.str
[ptr
++] = '\\';
637 COMPLAIN(pldata
, _("unterminated PropList string"));
641 CHECK_BUFFER_SIZE(sBuf
, ptr
);
651 char *tmp
= unescapestr(sBuf
.str
);
652 plist
= WMCreatePLString(tmp
);
661 static WMPropList
*getPLData(PLData
* pldata
)
666 unsigned char buf
[BUFFERSIZE
], byte
;
670 data
= WMCreateDataWithCapacity(0);
673 c1
= getNonSpaceChar(pldata
);
675 COMPLAIN(pldata
, _("unterminated PropList data"));
678 } else if (c1
== '>') {
680 } else if (ishexdigit(c1
)) {
681 c2
= getNonSpaceChar(pldata
);
682 if (c2
== 0 || c2
== '>') {
683 COMPLAIN(pldata
, _("unterminated PropList data (missing hexdigit)"));
686 } else if (ishexdigit(c2
)) {
687 byte
= char2num(c1
) << 4;
688 byte
|= char2num(c2
);
690 if (len
== sizeof(buf
)) {
691 WMAppendDataBytes(data
, buf
, len
);
695 COMPLAIN(pldata
, _("non hexdigit character in PropList data"));
700 COMPLAIN(pldata
, _("non hexdigit character in PropList data"));
712 WMAppendDataBytes(data
, buf
, len
);
714 plist
= WMCreatePLData(data
);
720 static WMPropList
*getPLArray(PLData
* pldata
)
725 WMPropList
*array
, *obj
;
727 array
= WMCreatePLArray(NULL
);
730 c
= getNonSpaceChar(pldata
);
732 COMPLAIN(pldata
, _("unterminated PropList array"));
735 } else if (c
== ')') {
737 } else if (c
== ',') {
738 /* continue normally */
740 COMPLAIN(pldata
, _("missing or unterminated PropList array"));
748 obj
= getPropList(pldata
);
750 COMPLAIN(pldata
, _("could not get PropList array element"));
754 WMAddToPLArray(array
, obj
);
755 WMReleasePropList(obj
);
759 WMReleasePropList(array
);
766 static WMPropList
*getPLDictionary(PLData
* pldata
)
770 WMPropList
*dict
, *key
, *value
;
772 dict
= WMCreatePLDictionary(NULL
, NULL
);
775 c
= getNonSpaceChar(pldata
);
777 COMPLAIN(pldata
, _("unterminated PropList dictionary"));
780 } else if (c
== '}') {
784 DPUT("getting PropList dictionary key");
786 key
= getPLData(pldata
);
787 } else if (c
== '"') {
788 key
= getPLQString(pldata
);
789 } else if (ISSTRINGABLE(c
)) {
791 key
= getPLString(pldata
);
794 COMPLAIN(pldata
, _("missing PropList dictionary key"));
796 COMPLAIN(pldata
, _("missing PropList dictionary entry key "
797 "or unterminated dictionary"));
804 COMPLAIN(pldata
, _("error parsing PropList dictionary key"));
809 c
= getNonSpaceChar(pldata
);
811 WMReleasePropList(key
);
812 COMPLAIN(pldata
, _("missing = in PropList dictionary entry"));
817 DPUT("getting PropList dictionary entry value for key");
818 value
= getPropList(pldata
);
820 COMPLAIN(pldata
, _("error parsing PropList dictionary entry value"));
821 WMReleasePropList(key
);
826 c
= getNonSpaceChar(pldata
);
828 COMPLAIN(pldata
, _("missing ; in PropList dictionary entry"));
829 WMReleasePropList(key
);
830 WMReleasePropList(value
);
835 WMPutInPLDictionary(dict
, key
, value
);
836 WMReleasePropList(key
);
837 WMReleasePropList(value
);
841 WMReleasePropList(dict
);
848 static WMPropList
*getPropList(PLData
* pldata
)
853 c
= getNonSpaceChar(pldata
);
857 DPUT("End of PropList");
862 DPUT("Getting PropList dictionary");
863 plist
= getPLDictionary(pldata
);
867 DPUT("Getting PropList array");
868 plist
= getPLArray(pldata
);
872 DPUT("Getting PropList data");
873 plist
= getPLData(pldata
);
877 DPUT("Getting PropList quoted string");
878 plist
= getPLQString(pldata
);
882 if (ISSTRINGABLE(c
)) {
883 DPUT("Getting PropList string");
885 plist
= getPLString(pldata
);
887 COMPLAIN(pldata
, _("was expecting a string, data, array or "
888 "dictionary. If it's a string, try enclosing " "it with \"."));
889 if (c
== '#' || c
== '/') {
890 wwarning(_("Comments are not allowed inside WindowMaker owned" " domain files."));
900 void WMPLSetCaseSensitive(Bool caseSensitiveness
)
902 caseSensitive
= caseSensitiveness
;
905 WMPropList
*WMCreatePLString(const char *str
)
909 wassertrv(str
!= NULL
, NULL
);
911 plist
= (WMPropList
*) wmalloc(sizeof(W_PropList
));
912 plist
->type
= WPLString
;
913 plist
->d
.string
= wstrdup(str
);
914 plist
->retainCount
= 1;
919 WMPropList
*WMCreatePLData(WMData
* data
)
923 wassertrv(data
!= NULL
, NULL
);
925 plist
= (WMPropList
*) wmalloc(sizeof(W_PropList
));
926 plist
->type
= WPLData
;
927 plist
->d
.data
= WMRetainData(data
);
928 plist
->retainCount
= 1;
933 WMPropList
*WMCreatePLDataWithBytes(const unsigned char *bytes
, unsigned int length
)
937 wassertrv(bytes
!= NULL
, NULL
);
939 plist
= (WMPropList
*) wmalloc(sizeof(W_PropList
));
940 plist
->type
= WPLData
;
941 plist
->d
.data
= WMCreateDataWithBytes(bytes
, length
);
942 plist
->retainCount
= 1;
947 WMPropList
*WMCreatePLDataWithBytesNoCopy(unsigned char *bytes
, unsigned int length
, WMFreeDataProc
* destructor
)
951 wassertrv(bytes
!= NULL
, NULL
);
953 plist
= (WMPropList
*) wmalloc(sizeof(W_PropList
));
954 plist
->type
= WPLData
;
955 plist
->d
.data
= WMCreateDataWithBytesNoCopy(bytes
, length
, destructor
);
956 plist
->retainCount
= 1;
961 WMPropList
*WMCreatePLArray(WMPropList
* elem
, ...)
963 WMPropList
*plist
, *nelem
;
966 plist
= (WMPropList
*) wmalloc(sizeof(W_PropList
));
967 plist
->type
= WPLArray
;
968 plist
->d
.array
= WMCreateArray(4);
969 plist
->retainCount
= 1;
974 WMAddToArray(plist
->d
.array
, WMRetainPropList(elem
));
979 nelem
= va_arg(ap
, WMPropList
*);
984 WMAddToArray(plist
->d
.array
, WMRetainPropList(nelem
));
988 WMPropList
*WMCreatePLDictionary(WMPropList
* key
, WMPropList
* value
, ...)
990 WMPropList
*plist
, *nkey
, *nvalue
, *k
, *v
;
993 plist
= (WMPropList
*) wmalloc(sizeof(W_PropList
));
994 plist
->type
= WPLDictionary
;
995 plist
->d
.dict
= WMCreateHashTable(WMPropListHashCallbacks
);
996 plist
->retainCount
= 1;
1001 WMHashInsert(plist
->d
.dict
, WMRetainPropList(key
), WMRetainPropList(value
));
1003 va_start(ap
, value
);
1006 nkey
= va_arg(ap
, WMPropList
*);
1011 nvalue
= va_arg(ap
, WMPropList
*);
1016 if (WMHashGetItemAndKey(plist
->d
.dict
, nkey
, (void **)&v
, (void **)&k
)) {
1017 WMHashRemove(plist
->d
.dict
, k
);
1018 WMReleasePropList(k
);
1019 WMReleasePropList(v
);
1021 WMHashInsert(plist
->d
.dict
, WMRetainPropList(nkey
), WMRetainPropList(nvalue
));
1025 WMPropList
*WMRetainPropList(WMPropList
* plist
)
1027 WMPropList
*key
, *value
;
1031 plist
->retainCount
++;
1033 switch (plist
->type
) {
1038 for (i
= 0; i
< WMGetArrayItemCount(plist
->d
.array
); i
++) {
1039 WMRetainPropList(WMGetFromArray(plist
->d
.array
, i
));
1043 e
= WMEnumerateHashTable(plist
->d
.dict
);
1044 while (WMNextHashEnumeratorItemAndKey(&e
, (void **)&value
, (void **)&key
)) {
1045 WMRetainPropList(key
);
1046 WMRetainPropList(value
);
1050 wwarning(_("Used proplist functions on non-WMPropLists objects"));
1051 wassertrv(False
, NULL
);
1058 void WMReleasePropList(WMPropList
* plist
)
1060 WMPropList
*key
, *value
;
1064 plist
->retainCount
--;
1066 switch (plist
->type
) {
1068 if (plist
->retainCount
< 1) {
1069 wfree(plist
->d
.string
);
1074 if (plist
->retainCount
< 1) {
1075 WMReleaseData(plist
->d
.data
);
1080 for (i
= 0; i
< WMGetArrayItemCount(plist
->d
.array
); i
++) {
1081 WMReleasePropList(WMGetFromArray(plist
->d
.array
, i
));
1083 if (plist
->retainCount
< 1) {
1084 WMFreeArray(plist
->d
.array
);
1089 e
= WMEnumerateHashTable(plist
->d
.dict
);
1090 while (WMNextHashEnumeratorItemAndKey(&e
, (void **)&value
, (void **)&key
)) {
1091 WMReleasePropList(key
);
1092 WMReleasePropList(value
);
1094 if (plist
->retainCount
< 1) {
1095 WMFreeHashTable(plist
->d
.dict
);
1100 wwarning(_("Used proplist functions on non-WMPropLists objects"));
1106 void WMInsertInPLArray(WMPropList
* plist
, int index
, WMPropList
* item
)
1108 wassertr(plist
->type
== WPLArray
);
1110 retainPropListByCount(item
, plist
->retainCount
);
1111 WMInsertInArray(plist
->d
.array
, index
, item
);
1114 void WMAddToPLArray(WMPropList
* plist
, WMPropList
* item
)
1116 wassertr(plist
->type
== WPLArray
);
1118 retainPropListByCount(item
, plist
->retainCount
);
1119 WMAddToArray(plist
->d
.array
, item
);
1122 void WMDeleteFromPLArray(WMPropList
* plist
, int index
)
1126 wassertr(plist
->type
== WPLArray
);
1128 item
= WMGetFromArray(plist
->d
.array
, index
);
1130 WMDeleteFromArray(plist
->d
.array
, index
);
1131 releasePropListByCount(item
, plist
->retainCount
);
1135 void WMRemoveFromPLArray(WMPropList
* plist
, WMPropList
* item
)
1140 wassertr(plist
->type
== WPLArray
);
1142 for (i
= 0; i
< WMGetArrayItemCount(plist
->d
.array
); i
++) {
1143 iPtr
= WMGetFromArray(plist
->d
.array
, i
);
1144 if (WMIsPropListEqualTo(item
, iPtr
)) {
1145 WMDeleteFromArray(plist
->d
.array
, i
);
1146 releasePropListByCount(iPtr
, plist
->retainCount
);
1152 void WMPutInPLDictionary(WMPropList
* plist
, WMPropList
* key
, WMPropList
* value
)
1154 wassertr(plist
->type
== WPLDictionary
);
1156 /*WMRetainPropList(key); */
1157 WMRemoveFromPLDictionary(plist
, key
);
1158 retainPropListByCount(key
, plist
->retainCount
);
1159 retainPropListByCount(value
, plist
->retainCount
);
1160 WMHashInsert(plist
->d
.dict
, key
, value
);
1161 /*WMReleasePropList(key); */
1164 void WMRemoveFromPLDictionary(WMPropList
* plist
, WMPropList
* key
)
1168 wassertr(plist
->type
== WPLDictionary
);
1170 if (WMHashGetItemAndKey(plist
->d
.dict
, key
, (void **)&v
, (void **)&k
)) {
1171 WMHashRemove(plist
->d
.dict
, k
);
1172 releasePropListByCount(k
, plist
->retainCount
);
1173 releasePropListByCount(v
, plist
->retainCount
);
1177 WMPropList
*WMMergePLDictionaries(WMPropList
* dest
, WMPropList
* source
, Bool recursive
)
1179 WMPropList
*key
, *value
, *dvalue
;
1182 wassertrv(source
->type
== WPLDictionary
&& dest
->type
== WPLDictionary
, NULL
);
1187 e
= WMEnumerateHashTable(source
->d
.dict
);
1188 while (WMNextHashEnumeratorItemAndKey(&e
, (void **)&value
, (void **)&key
)) {
1189 if (recursive
&& value
->type
== WPLDictionary
) {
1190 dvalue
= WMHashGet(dest
->d
.dict
, key
);
1191 if (dvalue
&& dvalue
->type
== WPLDictionary
) {
1192 WMMergePLDictionaries(dvalue
, value
, True
);
1194 WMPutInPLDictionary(dest
, key
, value
);
1197 WMPutInPLDictionary(dest
, key
, value
);
1204 WMPropList
*WMSubtractPLDictionaries(WMPropList
* dest
, WMPropList
* source
, Bool recursive
)
1206 WMPropList
*key
, *value
, *dvalue
;
1209 wassertrv(source
->type
== WPLDictionary
&& dest
->type
== WPLDictionary
, NULL
);
1211 if (source
== dest
) {
1212 WMPropList
*keys
= WMGetPLDictionaryKeys(dest
);
1215 for (i
= 0; i
< WMGetArrayItemCount(keys
->d
.array
); i
++) {
1216 WMRemoveFromPLDictionary(dest
, WMGetFromArray(keys
->d
.array
, i
));
1221 e
= WMEnumerateHashTable(source
->d
.dict
);
1222 while (WMNextHashEnumeratorItemAndKey(&e
, (void **)&value
, (void **)&key
)) {
1223 dvalue
= WMHashGet(dest
->d
.dict
, key
);
1226 if (WMIsPropListEqualTo(value
, dvalue
)) {
1227 WMRemoveFromPLDictionary(dest
, key
);
1228 } else if (recursive
&& value
->type
== WPLDictionary
&& dvalue
->type
== WPLDictionary
) {
1229 WMSubtractPLDictionaries(dvalue
, value
, True
);
1236 int WMGetPropListItemCount(WMPropList
* plist
)
1238 switch (plist
->type
) {
1241 return 0; /* should this be 1 instead? */
1243 return WMGetArrayItemCount(plist
->d
.array
);
1245 return (int)WMCountHashTable(plist
->d
.dict
);
1247 wwarning(_("Used proplist functions on non-WMPropLists objects"));
1248 wassertrv(False
, 0);
1255 Bool
WMIsPLString(WMPropList
* plist
)
1257 return (plist
->type
== WPLString
);
1260 Bool
WMIsPLData(WMPropList
* plist
)
1262 return (plist
->type
== WPLData
);
1265 Bool
WMIsPLArray(WMPropList
* plist
)
1267 return (plist
->type
== WPLArray
);
1270 Bool
WMIsPLDictionary(WMPropList
* plist
)
1272 return (plist
->type
== WPLDictionary
);
1275 Bool
WMIsPropListEqualTo(WMPropList
* plist
, WMPropList
* other
)
1277 WMPropList
*key1
, *item1
, *item2
;
1278 WMHashEnumerator enumerator
;
1281 if (plist
->type
!= other
->type
)
1284 switch (plist
->type
) {
1286 if (caseSensitive
) {
1287 return (strcmp(plist
->d
.string
, other
->d
.string
) == 0);
1289 return (strcasecmp(plist
->d
.string
, other
->d
.string
) == 0);
1292 return WMIsDataEqualToData(plist
->d
.data
, other
->d
.data
);
1294 n
= WMGetArrayItemCount(plist
->d
.array
);
1295 if (n
!= WMGetArrayItemCount(other
->d
.array
))
1297 for (i
= 0; i
< n
; i
++) {
1298 item1
= WMGetFromArray(plist
->d
.array
, i
);
1299 item2
= WMGetFromArray(other
->d
.array
, i
);
1300 if (!WMIsPropListEqualTo(item1
, item2
))
1305 if (WMCountHashTable(plist
->d
.dict
) != WMCountHashTable(other
->d
.dict
))
1307 enumerator
= WMEnumerateHashTable(plist
->d
.dict
);
1308 while (WMNextHashEnumeratorItemAndKey(&enumerator
, (void **)&item1
, (void **)&key1
)) {
1309 item2
= WMHashGet(other
->d
.dict
, key1
);
1310 if (!item2
|| !item1
|| !WMIsPropListEqualTo(item1
, item2
))
1315 wwarning(_("Used proplist functions on non-WMPropLists objects"));
1316 wassertrv(False
, False
);
1323 char *WMGetFromPLString(WMPropList
* plist
)
1325 wassertrv(plist
->type
== WPLString
, NULL
);
1327 return plist
->d
.string
;
1330 WMData
*WMGetFromPLData(WMPropList
* plist
)
1332 wassertrv(plist
->type
== WPLData
, NULL
);
1334 return plist
->d
.data
;
1337 const unsigned char *WMGetPLDataBytes(WMPropList
* plist
)
1339 wassertrv(plist
->type
== WPLData
, NULL
);
1341 return WMDataBytes(plist
->d
.data
);
1344 int WMGetPLDataLength(WMPropList
* plist
)
1346 wassertrv(plist
->type
== WPLData
, 0);
1348 return WMGetDataLength(plist
->d
.data
);
1351 WMPropList
*WMGetFromPLArray(WMPropList
* plist
, int index
)
1353 wassertrv(plist
->type
== WPLArray
, NULL
);
1355 return WMGetFromArray(plist
->d
.array
, index
);
1358 WMPropList
*WMGetFromPLDictionary(WMPropList
* plist
, WMPropList
* key
)
1360 wassertrv(plist
->type
== WPLDictionary
, NULL
);
1362 return WMHashGet(plist
->d
.dict
, key
);
1365 WMPropList
*WMGetPLDictionaryKeys(WMPropList
* plist
)
1367 WMPropList
*array
, *key
;
1368 WMHashEnumerator enumerator
;
1370 wassertrv(plist
->type
== WPLDictionary
, NULL
);
1372 array
= (WMPropList
*) wmalloc(sizeof(W_PropList
));
1373 array
->type
= WPLArray
;
1374 array
->d
.array
= WMCreateArray(WMCountHashTable(plist
->d
.dict
));
1375 array
->retainCount
= 1;
1377 enumerator
= WMEnumerateHashTable(plist
->d
.dict
);
1378 while ((key
= WMNextHashEnumeratorKey(&enumerator
))) {
1379 WMAddToArray(array
->d
.array
, WMRetainPropList(key
));
1385 WMPropList
*WMShallowCopyPropList(WMPropList
* plist
)
1387 WMPropList
*ret
= NULL
;
1388 WMPropList
*key
, *item
;
1393 switch (plist
->type
) {
1395 ret
= WMCreatePLString(plist
->d
.string
);
1398 data
= WMCreateDataWithData(plist
->d
.data
);
1399 ret
= WMCreatePLData(data
);
1400 WMReleaseData(data
);
1403 ret
= (WMPropList
*) wmalloc(sizeof(W_PropList
));
1404 ret
->type
= WPLArray
;
1405 ret
->d
.array
= WMCreateArrayWithArray(plist
->d
.array
);
1406 ret
->retainCount
= 1;
1408 for (i
= 0; i
< WMGetArrayItemCount(ret
->d
.array
); i
++)
1409 WMRetainPropList(WMGetFromArray(ret
->d
.array
, i
));
1413 ret
= WMCreatePLDictionary(NULL
, NULL
);
1414 e
= WMEnumerateHashTable(plist
->d
.dict
);
1415 while (WMNextHashEnumeratorItemAndKey(&e
, (void **)&item
, (void **)&key
)) {
1416 WMPutInPLDictionary(ret
, key
, item
);
1420 wwarning(_("Used proplist functions on non-WMPropLists objects"));
1421 wassertrv(False
, NULL
);
1428 WMPropList
*WMDeepCopyPropList(WMPropList
* plist
)
1430 WMPropList
*ret
= NULL
;
1431 WMPropList
*key
, *item
;
1436 switch (plist
->type
) {
1438 ret
= WMCreatePLString(plist
->d
.string
);
1441 data
= WMCreateDataWithData(plist
->d
.data
);
1442 ret
= WMCreatePLData(data
);
1443 WMReleaseData(data
);
1446 ret
= WMCreatePLArray(NULL
);
1447 for (i
= 0; i
< WMGetArrayItemCount(plist
->d
.array
); i
++) {
1448 item
= WMDeepCopyPropList(WMGetFromArray(plist
->d
.array
, i
));
1449 WMAddToArray(ret
->d
.array
, item
);
1453 ret
= WMCreatePLDictionary(NULL
, NULL
);
1454 e
= WMEnumerateHashTable(plist
->d
.dict
);
1455 /* While we copy an existing dictionary there is no way that we can
1456 * have duplicate keys, so we don't need to first remove a key/value
1457 * pair before inserting the new key/value.
1459 while (WMNextHashEnumeratorItemAndKey(&e
, (void **)&item
, (void **)&key
)) {
1460 WMHashInsert(ret
->d
.dict
, WMDeepCopyPropList(key
), WMDeepCopyPropList(item
));
1464 wwarning(_("Used proplist functions on non-WMPropLists objects"));
1465 wassertrv(False
, NULL
);
1472 WMPropList
*WMCreatePropListFromDescription(const char *desc
)
1474 WMPropList
*plist
= NULL
;
1477 pldata
= (PLData
*) wmalloc(sizeof(PLData
));
1479 pldata
->lineNumber
= 1;
1481 plist
= getPropList(pldata
);
1483 if (getNonSpaceChar(pldata
) != 0 && plist
) {
1484 COMPLAIN(pldata
, _("extra data after end of property list"));
1486 * We can't just ignore garbage after the end of the description
1487 * (especially if the description was read from a file), because
1488 * the "garbage" can be the real data and the real garbage is in
1489 * fact in the beginning of the file (which is now inside plist)
1491 WMReleasePropList(plist
);
1500 char *WMGetPropListDescription(WMPropList
* plist
, Bool indented
)
1502 return (indented
? indentedDescription(plist
, 0) : description(plist
));
1505 WMPropList
*WMReadPropListFromFile(const char *file
)
1507 WMPropList
*plist
= NULL
;
1514 f
= fopen(file
, "rb");
1516 /* let the user print the error message if he really needs to */
1517 /*werror(_("could not open domain file '%s' for reading"), file); */
1521 if (stat(file
, &stbuf
) == 0) {
1522 length
= (size_t) stbuf
.st_size
;
1524 werror(_("could not get size for file '%s'"), file
);
1529 read_buf
= wmalloc(length
+ 1);
1530 if (fread(read_buf
, length
, 1, f
) != 1) {
1532 werror(_("error reading from file '%s'"), file
);
1538 read_buf
[length
] = '\0';
1541 pldata
= (PLData
*) wmalloc(sizeof(PLData
));
1542 pldata
->ptr
= read_buf
;
1543 pldata
->filename
= file
;
1544 pldata
->lineNumber
= 1;
1546 plist
= getPropList(pldata
);
1548 if (getNonSpaceChar(pldata
) != 0 && plist
) {
1549 COMPLAIN(pldata
, _("extra data after end of property list"));
1551 * We can't just ignore garbage after the end of the description
1552 * (especially if the description was read from a file), because
1553 * the "garbage" can be the real data and the real garbage is in
1554 * fact in the beginning of the file (which is now inside plist)
1556 WMReleasePropList(plist
);
1566 WMPropList
*WMReadPropListFromPipe(const char *command
)
1574 file
= popen(command
, "r");
1577 werror(_("%s:could not open menu file"), command
);
1581 pldata
= (PLData
*) wmalloc(sizeof(PLData
));
1583 pldata
->filename
= command
;
1584 pldata
->lineNumber
= 1;
1586 /* read from file till EOF or OOM and fill proplist buffer*/
1588 while (fgets(line
, sizeof(line
), file
) != NULL
) {
1589 if (read_buf
== NULL
) {
1590 read_buf
= wmalloc(strlen(line
)+1);
1593 read_buf
= wrealloc(read_buf
,
1594 strlen(line
) + strlen(read_buf
) + 1);
1597 read_buf
= strncat(read_buf
, line
, strlen(line
));
1599 pldata
->ptr
= read_buf
;
1603 plist
= getPropList(pldata
);
1605 if (getNonSpaceChar(pldata
) != 0 && plist
) {
1606 COMPLAIN(pldata
, _("extra data after end of property list"));
1608 * We can't just ignore garbage after the end of the description
1609 * (especially if the description was read from a file), because
1610 * the "garbage" can be the real data and the real garbage is in
1611 * fact in the beginning of the file (which is now inside plist)
1613 WMReleasePropList(plist
);
1623 /* TODO: review this function's code */
1625 Bool
WMWritePropListToFile(WMPropList
* plist
, const char *path
)
1627 char *thePath
= NULL
;
1634 if (!wmkdirhier(path
))
1637 /* Use the path name of the destination file as a prefix for the
1638 * mkstemp() call so that we can be sure that both files are on
1639 * the same filesystem and the subsequent rename() will work. */
1640 thePath
= wstrconcat(path
, ".XXXXXX");
1643 if ((fd
= mkstemp(thePath
)) < 0) {
1644 werror(_("mkstemp (%s) failed"), thePath
);
1649 fchmod(fd
, 0644 & ~mask
);
1650 if ((theFile
= fdopen(fd
, "wb")) == NULL
) {
1654 if (mktemp(thePath
) == NULL
) {
1655 werror(_("mktemp (%s) failed"), thePath
);
1658 theFile
= fopen(thePath
, "wb");
1661 if (theFile
== NULL
) {
1662 werror(_("open (%s) failed"), thePath
);
1666 desc
= indentedDescription(plist
, 0);
1668 if (fprintf(theFile
, "%s\n", desc
) != strlen(desc
) + 1) {
1669 werror(_("writing to file: %s failed"), thePath
);
1676 (void)fsync(fileno(theFile
));
1677 if (fclose(theFile
) != 0) {
1678 werror(_("fclose (%s) failed"), thePath
);
1682 /* If we used a temporary file, we still need to rename() it be the
1683 * real file. Also, we need to try to retain the file attributes of
1684 * the original file we are overwriting (if we are) */
1685 if (rename(thePath
, path
) != 0) {
1686 werror(_("rename ('%s' to '%s') failed"), thePath
, path
);
1700 * create a directory hierarchy
1702 * if the last octet of `path' is `/', the full path is
1703 * assumed to be a directory; otherwise path is assumed to be a
1704 * file, and the last component is stripped off. the rest is the
1705 * the hierarchy to be created.
1707 * refuses to create anything outside $GNUSTEP_USER_ROOT
1709 * returns 1 on success, 0 on failure
1711 int wmkdirhier(const char *path
)
1714 char *thePath
= NULL
, buf
[1024];
1718 /* Only create directories under $GNUSTEP_USER_ROOT */
1719 if ((t
= wusergnusteppath()) == NULL
)
1721 if (strncmp(path
, t
, strlen(t
)) != 0)
1724 thePath
= wstrdup(path
);
1725 /* Strip the trailing component if it is a file */
1726 p
= strlen(thePath
);
1727 while (p
&& thePath
[p
] != '/')
1728 thePath
[p
--] = '\0';
1732 /* Shortcut if it already exists */
1733 if (stat(thePath
, &st
) == 0) {
1735 if (S_ISDIR(st
.st_mode
)) {
1736 /* Is a directory alright */
1739 /* Exists, but not a directory, the caller
1740 * might just as well abort now */
1745 memset(buf
, 0, sizeof(buf
));
1746 strncpy(buf
, t
, sizeof(buf
) - 1);
1748 plen
= strlen(thePath
);
1751 while (p
++ < plen
&& thePath
[p
] != '/')
1754 strncpy(buf
, thePath
, p
);
1755 if (mkdir(buf
, 0777) == -1 && errno
== EEXIST
&&
1756 stat(buf
, &st
) == 0 && !S_ISDIR(st
.st_mode
)) {
1757 werror(_("Could not create component %s"), buf
);
1768 static int wrmdirhier_fn(const char *path
, const struct stat
*st
,
1769 int type
, struct FTW
*ftw
)
1780 return unlink(path
);
1793 * remove a directory hierarchy
1795 * refuses to remove anything outside $GNUSTEP_USER_ROOT
1797 * returns 1 on success, 0 on failure
1799 * TODO: revisit what's error and what's not
1801 * with inspirations from OpenBSD's bin/rm/rm.c
1803 int wrmdirhier(const char *path
)
1809 /* Only remove directories under $GNUSTEP_USER_ROOT */
1810 if ((t
= wusergnusteppath()) == NULL
)
1812 if (strncmp(path
, t
, strlen(t
)) != 0)
1815 /* Shortcut if it doesn't exist to begin with */
1816 if (stat(path
, &st
) == -1)
1819 error
= nftw(path
, wrmdirhier_fn
, 1, FTW_PHYS
);