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(char *src
)
507 char *dest
= wmalloc(strlen(src
) + 1);
511 for (sPtr
= src
, dPtr
= dest
; *sPtr
; sPtr
++, dPtr
++) {
516 if ((ch
>= '0') && (ch
<= '3')) {
517 /* assume next 2 chars are octal too */
518 *dPtr
= ((ch
& 07) << 6);
519 *dPtr
|= ((*(++sPtr
) & 07) << 3);
520 *dPtr
|= *(++sPtr
) & 07;
556 static WMPropList
*getPLString(PLData
* pldata
)
563 sBuf
.str
= wmalloc(BUFFERSIZE
);
564 sBuf
.size
= BUFFERSIZE
;
568 if (ISSTRINGABLE(c
)) {
569 CHECK_BUFFER_SIZE(sBuf
, ptr
);
584 char *tmp
= unescapestr(sBuf
.str
);
585 plist
= WMCreatePLString(tmp
);
594 static WMPropList
*getPLQString(PLData
* pldata
)
597 int ptr
= 0, escaping
= 0, ok
= 1;
601 sBuf
.str
= wmalloc(BUFFERSIZE
);
602 sBuf
.size
= BUFFERSIZE
;
610 } else if (c
== '"') {
614 CHECK_BUFFER_SIZE(sBuf
, ptr
);
615 sBuf
.str
[ptr
++] = '\\';
620 COMPLAIN(pldata
, _("unterminated PropList string"));
624 CHECK_BUFFER_SIZE(sBuf
, ptr
);
634 char *tmp
= unescapestr(sBuf
.str
);
635 plist
= WMCreatePLString(tmp
);
644 static WMPropList
*getPLData(PLData
* pldata
)
649 unsigned char buf
[BUFFERSIZE
], byte
;
653 data
= WMCreateDataWithCapacity(0);
656 c1
= getNonSpaceChar(pldata
);
658 COMPLAIN(pldata
, _("unterminated PropList data"));
661 } else if (c1
== '>') {
663 } else if (ishexdigit(c1
)) {
664 c2
= getNonSpaceChar(pldata
);
665 if (c2
== 0 || c2
== '>') {
666 COMPLAIN(pldata
, _("unterminated PropList data (missing hexdigit)"));
669 } else if (ishexdigit(c2
)) {
670 byte
= char2num(c1
) << 4;
671 byte
|= char2num(c2
);
673 if (len
== sizeof(buf
)) {
674 WMAppendDataBytes(data
, buf
, len
);
678 COMPLAIN(pldata
, _("non hexdigit character in PropList data"));
683 COMPLAIN(pldata
, _("non hexdigit character in PropList data"));
695 WMAppendDataBytes(data
, buf
, len
);
697 plist
= WMCreatePLData(data
);
703 static WMPropList
*getPLArray(PLData
* pldata
)
708 WMPropList
*array
, *obj
;
710 array
= WMCreatePLArray(NULL
);
713 c
= getNonSpaceChar(pldata
);
715 COMPLAIN(pldata
, _("unterminated PropList array"));
718 } else if (c
== ')') {
720 } else if (c
== ',') {
721 /* continue normally */
723 COMPLAIN(pldata
, _("missing or unterminated PropList array"));
731 obj
= getPropList(pldata
);
733 COMPLAIN(pldata
, _("could not get PropList array element"));
737 WMAddToPLArray(array
, obj
);
738 WMReleasePropList(obj
);
742 WMReleasePropList(array
);
749 static WMPropList
*getPLDictionary(PLData
* pldata
)
753 WMPropList
*dict
, *key
, *value
;
755 dict
= WMCreatePLDictionary(NULL
, NULL
);
758 c
= getNonSpaceChar(pldata
);
760 COMPLAIN(pldata
, _("unterminated PropList dictionary"));
763 } else if (c
== '}') {
767 DPUT("getting PropList dictionary key");
769 key
= getPLData(pldata
);
770 } else if (c
== '"') {
771 key
= getPLQString(pldata
);
772 } else if (ISSTRINGABLE(c
)) {
774 key
= getPLString(pldata
);
777 COMPLAIN(pldata
, _("missing PropList dictionary key"));
779 COMPLAIN(pldata
, _("missing PropList dictionary entry key "
780 "or unterminated dictionary"));
787 COMPLAIN(pldata
, _("error parsing PropList dictionary key"));
792 c
= getNonSpaceChar(pldata
);
794 WMReleasePropList(key
);
795 COMPLAIN(pldata
, _("missing = in PropList dictionary entry"));
800 DPUT("getting PropList dictionary entry value for key");
801 value
= getPropList(pldata
);
803 COMPLAIN(pldata
, _("error parsing PropList dictionary entry value"));
804 WMReleasePropList(key
);
809 c
= getNonSpaceChar(pldata
);
811 COMPLAIN(pldata
, _("missing ; in PropList dictionary entry"));
812 WMReleasePropList(key
);
813 WMReleasePropList(value
);
818 WMPutInPLDictionary(dict
, key
, value
);
819 WMReleasePropList(key
);
820 WMReleasePropList(value
);
824 WMReleasePropList(dict
);
831 static WMPropList
*getPropList(PLData
* pldata
)
836 c
= getNonSpaceChar(pldata
);
840 DPUT("End of PropList");
845 DPUT("Getting PropList dictionary");
846 plist
= getPLDictionary(pldata
);
850 DPUT("Getting PropList array");
851 plist
= getPLArray(pldata
);
855 DPUT("Getting PropList data");
856 plist
= getPLData(pldata
);
860 DPUT("Getting PropList quoted string");
861 plist
= getPLQString(pldata
);
865 if (ISSTRINGABLE(c
)) {
866 DPUT("Getting PropList string");
868 plist
= getPLString(pldata
);
870 COMPLAIN(pldata
, _("was expecting a string, data, array or "
871 "dictionary. If it's a string, try enclosing " "it with \"."));
872 if (c
== '#' || c
== '/') {
873 wwarning(_("Comments are not allowed inside WindowMaker owned" " domain files."));
883 void WMPLSetCaseSensitive(Bool caseSensitiveness
)
885 caseSensitive
= caseSensitiveness
;
888 WMPropList
*WMCreatePLString(char *str
)
892 wassertrv(str
!= NULL
, NULL
);
894 plist
= (WMPropList
*) wmalloc(sizeof(W_PropList
));
895 plist
->type
= WPLString
;
896 plist
->d
.string
= wstrdup(str
);
897 plist
->retainCount
= 1;
902 WMPropList
*WMCreatePLData(WMData
* data
)
906 wassertrv(data
!= NULL
, NULL
);
908 plist
= (WMPropList
*) wmalloc(sizeof(W_PropList
));
909 plist
->type
= WPLData
;
910 plist
->d
.data
= WMRetainData(data
);
911 plist
->retainCount
= 1;
916 WMPropList
*WMCreatePLDataWithBytes(unsigned char *bytes
, unsigned int length
)
920 wassertrv(bytes
!= NULL
, NULL
);
922 plist
= (WMPropList
*) wmalloc(sizeof(W_PropList
));
923 plist
->type
= WPLData
;
924 plist
->d
.data
= WMCreateDataWithBytes(bytes
, length
);
925 plist
->retainCount
= 1;
930 WMPropList
*WMCreatePLDataWithBytesNoCopy(unsigned char *bytes
, unsigned int length
, WMFreeDataProc
* destructor
)
934 wassertrv(bytes
!= NULL
, NULL
);
936 plist
= (WMPropList
*) wmalloc(sizeof(W_PropList
));
937 plist
->type
= WPLData
;
938 plist
->d
.data
= WMCreateDataWithBytesNoCopy(bytes
, length
, destructor
);
939 plist
->retainCount
= 1;
944 WMPropList
*WMCreatePLArray(WMPropList
* elem
, ...)
946 WMPropList
*plist
, *nelem
;
949 plist
= (WMPropList
*) wmalloc(sizeof(W_PropList
));
950 plist
->type
= WPLArray
;
951 plist
->d
.array
= WMCreateArray(4);
952 plist
->retainCount
= 1;
957 WMAddToArray(plist
->d
.array
, WMRetainPropList(elem
));
962 nelem
= va_arg(ap
, WMPropList
*);
967 WMAddToArray(plist
->d
.array
, WMRetainPropList(nelem
));
971 WMPropList
*WMCreatePLDictionary(WMPropList
* key
, WMPropList
* value
, ...)
973 WMPropList
*plist
, *nkey
, *nvalue
, *k
, *v
;
976 plist
= (WMPropList
*) wmalloc(sizeof(W_PropList
));
977 plist
->type
= WPLDictionary
;
978 plist
->d
.dict
= WMCreateHashTable(WMPropListHashCallbacks
);
979 plist
->retainCount
= 1;
984 WMHashInsert(plist
->d
.dict
, WMRetainPropList(key
), WMRetainPropList(value
));
989 nkey
= va_arg(ap
, WMPropList
*);
994 nvalue
= va_arg(ap
, WMPropList
*);
999 if (WMHashGetItemAndKey(plist
->d
.dict
, nkey
, (void **)&v
, (void **)&k
)) {
1000 WMHashRemove(plist
->d
.dict
, k
);
1001 WMReleasePropList(k
);
1002 WMReleasePropList(v
);
1004 WMHashInsert(plist
->d
.dict
, WMRetainPropList(nkey
), WMRetainPropList(nvalue
));
1008 WMPropList
*WMRetainPropList(WMPropList
* plist
)
1010 WMPropList
*key
, *value
;
1014 plist
->retainCount
++;
1016 switch (plist
->type
) {
1021 for (i
= 0; i
< WMGetArrayItemCount(plist
->d
.array
); i
++) {
1022 WMRetainPropList(WMGetFromArray(plist
->d
.array
, i
));
1026 e
= WMEnumerateHashTable(plist
->d
.dict
);
1027 while (WMNextHashEnumeratorItemAndKey(&e
, (void **)&value
, (void **)&key
)) {
1028 WMRetainPropList(key
);
1029 WMRetainPropList(value
);
1033 wwarning(_("Used proplist functions on non-WMPropLists objects"));
1034 wassertrv(False
, NULL
);
1041 void WMReleasePropList(WMPropList
* plist
)
1043 WMPropList
*key
, *value
;
1047 plist
->retainCount
--;
1049 switch (plist
->type
) {
1051 if (plist
->retainCount
< 1) {
1052 wfree(plist
->d
.string
);
1057 if (plist
->retainCount
< 1) {
1058 WMReleaseData(plist
->d
.data
);
1063 for (i
= 0; i
< WMGetArrayItemCount(plist
->d
.array
); i
++) {
1064 WMReleasePropList(WMGetFromArray(plist
->d
.array
, i
));
1066 if (plist
->retainCount
< 1) {
1067 WMFreeArray(plist
->d
.array
);
1072 e
= WMEnumerateHashTable(plist
->d
.dict
);
1073 while (WMNextHashEnumeratorItemAndKey(&e
, (void **)&value
, (void **)&key
)) {
1074 WMReleasePropList(key
);
1075 WMReleasePropList(value
);
1077 if (plist
->retainCount
< 1) {
1078 WMFreeHashTable(plist
->d
.dict
);
1083 wwarning(_("Used proplist functions on non-WMPropLists objects"));
1089 void WMInsertInPLArray(WMPropList
* plist
, int index
, WMPropList
* item
)
1091 wassertr(plist
->type
== WPLArray
);
1093 retainPropListByCount(item
, plist
->retainCount
);
1094 WMInsertInArray(plist
->d
.array
, index
, item
);
1097 void WMAddToPLArray(WMPropList
* plist
, WMPropList
* item
)
1099 wassertr(plist
->type
== WPLArray
);
1101 retainPropListByCount(item
, plist
->retainCount
);
1102 WMAddToArray(plist
->d
.array
, item
);
1105 void WMDeleteFromPLArray(WMPropList
* plist
, int index
)
1109 wassertr(plist
->type
== WPLArray
);
1111 item
= WMGetFromArray(plist
->d
.array
, index
);
1113 WMDeleteFromArray(plist
->d
.array
, index
);
1114 releasePropListByCount(item
, plist
->retainCount
);
1118 void WMRemoveFromPLArray(WMPropList
* plist
, WMPropList
* item
)
1123 wassertr(plist
->type
== WPLArray
);
1125 for (i
= 0; i
< WMGetArrayItemCount(plist
->d
.array
); i
++) {
1126 iPtr
= WMGetFromArray(plist
->d
.array
, i
);
1127 if (WMIsPropListEqualTo(item
, iPtr
)) {
1128 WMDeleteFromArray(plist
->d
.array
, i
);
1129 releasePropListByCount(iPtr
, plist
->retainCount
);
1135 void WMPutInPLDictionary(WMPropList
* plist
, WMPropList
* key
, WMPropList
* value
)
1137 wassertr(plist
->type
== WPLDictionary
);
1139 /*WMRetainPropList(key); */
1140 WMRemoveFromPLDictionary(plist
, key
);
1141 retainPropListByCount(key
, plist
->retainCount
);
1142 retainPropListByCount(value
, plist
->retainCount
);
1143 WMHashInsert(plist
->d
.dict
, key
, value
);
1144 /*WMReleasePropList(key); */
1147 void WMRemoveFromPLDictionary(WMPropList
* plist
, WMPropList
* key
)
1151 wassertr(plist
->type
== WPLDictionary
);
1153 if (WMHashGetItemAndKey(plist
->d
.dict
, key
, (void **)&v
, (void **)&k
)) {
1154 WMHashRemove(plist
->d
.dict
, k
);
1155 releasePropListByCount(k
, plist
->retainCount
);
1156 releasePropListByCount(v
, plist
->retainCount
);
1160 WMPropList
*WMMergePLDictionaries(WMPropList
* dest
, WMPropList
* source
, Bool recursive
)
1162 WMPropList
*key
, *value
, *dvalue
;
1165 wassertr(source
->type
== WPLDictionary
&& dest
->type
== WPLDictionary
);
1170 e
= WMEnumerateHashTable(source
->d
.dict
);
1171 while (WMNextHashEnumeratorItemAndKey(&e
, (void **)&value
, (void **)&key
)) {
1172 if (recursive
&& value
->type
== WPLDictionary
) {
1173 dvalue
= WMHashGet(dest
->d
.dict
, key
);
1174 if (dvalue
&& dvalue
->type
== WPLDictionary
) {
1175 WMMergePLDictionaries(dvalue
, value
, True
);
1177 WMPutInPLDictionary(dest
, key
, value
);
1180 WMPutInPLDictionary(dest
, key
, value
);
1187 WMPropList
*WMSubtractPLDictionaries(WMPropList
* dest
, WMPropList
* source
, Bool recursive
)
1189 WMPropList
*key
, *value
, *dvalue
;
1192 wassertr(source
->type
== WPLDictionary
&& dest
->type
== WPLDictionary
);
1194 if (source
== dest
) {
1195 WMPropList
*keys
= WMGetPLDictionaryKeys(dest
);
1198 for (i
= 0; i
< WMGetArrayItemCount(keys
->d
.array
); i
++) {
1199 WMRemoveFromPLDictionary(dest
, WMGetFromArray(keys
->d
.array
, i
));
1204 e
= WMEnumerateHashTable(source
->d
.dict
);
1205 while (WMNextHashEnumeratorItemAndKey(&e
, (void **)&value
, (void **)&key
)) {
1206 dvalue
= WMHashGet(dest
->d
.dict
, key
);
1209 if (WMIsPropListEqualTo(value
, dvalue
)) {
1210 WMRemoveFromPLDictionary(dest
, key
);
1211 } else if (recursive
&& value
->type
== WPLDictionary
&& dvalue
->type
== WPLDictionary
) {
1212 WMSubtractPLDictionaries(dvalue
, value
, True
);
1219 int WMGetPropListItemCount(WMPropList
* plist
)
1221 switch (plist
->type
) {
1224 return 0; /* should this be 1 instead? */
1226 return WMGetArrayItemCount(plist
->d
.array
);
1228 return (int)WMCountHashTable(plist
->d
.dict
);
1230 wwarning(_("Used proplist functions on non-WMPropLists objects"));
1231 wassertrv(False
, 0);
1238 Bool
WMIsPLString(WMPropList
* plist
)
1240 return (plist
->type
== WPLString
);
1243 Bool
WMIsPLData(WMPropList
* plist
)
1245 return (plist
->type
== WPLData
);
1248 Bool
WMIsPLArray(WMPropList
* plist
)
1250 return (plist
->type
== WPLArray
);
1253 Bool
WMIsPLDictionary(WMPropList
* plist
)
1255 return (plist
->type
== WPLDictionary
);
1258 Bool
WMIsPropListEqualTo(WMPropList
* plist
, WMPropList
* other
)
1260 WMPropList
*key1
, *item1
, *item2
;
1261 WMHashEnumerator enumerator
;
1264 if (plist
->type
!= other
->type
)
1267 switch (plist
->type
) {
1269 if (caseSensitive
) {
1270 return (strcmp(plist
->d
.string
, other
->d
.string
) == 0);
1272 return (strcasecmp(plist
->d
.string
, other
->d
.string
) == 0);
1275 return WMIsDataEqualToData(plist
->d
.data
, other
->d
.data
);
1277 n
= WMGetArrayItemCount(plist
->d
.array
);
1278 if (n
!= WMGetArrayItemCount(other
->d
.array
))
1280 for (i
= 0; i
< n
; i
++) {
1281 item1
= WMGetFromArray(plist
->d
.array
, i
);
1282 item2
= WMGetFromArray(other
->d
.array
, i
);
1283 if (!WMIsPropListEqualTo(item1
, item2
))
1288 if (WMCountHashTable(plist
->d
.dict
) != WMCountHashTable(other
->d
.dict
))
1290 enumerator
= WMEnumerateHashTable(plist
->d
.dict
);
1291 while (WMNextHashEnumeratorItemAndKey(&enumerator
, (void **)&item1
, (void **)&key1
)) {
1292 item2
= WMHashGet(other
->d
.dict
, key1
);
1293 if (!item2
|| !item1
|| !WMIsPropListEqualTo(item1
, item2
))
1298 wwarning(_("Used proplist functions on non-WMPropLists objects"));
1299 wassertrv(False
, False
);
1306 char *WMGetFromPLString(WMPropList
* plist
)
1308 wassertrv(plist
->type
== WPLString
, NULL
);
1310 return plist
->d
.string
;
1313 WMData
*WMGetFromPLData(WMPropList
* plist
)
1315 wassertrv(plist
->type
== WPLData
, NULL
);
1317 return plist
->d
.data
;
1320 const unsigned char *WMGetPLDataBytes(WMPropList
* plist
)
1322 wassertrv(plist
->type
== WPLData
, NULL
);
1324 return WMDataBytes(plist
->d
.data
);
1327 int WMGetPLDataLength(WMPropList
* plist
)
1329 wassertrv(plist
->type
== WPLData
, 0);
1331 return WMGetDataLength(plist
->d
.data
);
1334 WMPropList
*WMGetFromPLArray(WMPropList
* plist
, int index
)
1336 wassertrv(plist
->type
== WPLArray
, NULL
);
1338 return WMGetFromArray(plist
->d
.array
, index
);
1341 WMPropList
*WMGetFromPLDictionary(WMPropList
* plist
, WMPropList
* key
)
1343 wassertrv(plist
->type
== WPLDictionary
, NULL
);
1345 return WMHashGet(plist
->d
.dict
, key
);
1348 WMPropList
*WMGetPLDictionaryKeys(WMPropList
* plist
)
1350 WMPropList
*array
, *key
;
1351 WMHashEnumerator enumerator
;
1353 wassertrv(plist
->type
== WPLDictionary
, NULL
);
1355 array
= (WMPropList
*) wmalloc(sizeof(W_PropList
));
1356 array
->type
= WPLArray
;
1357 array
->d
.array
= WMCreateArray(WMCountHashTable(plist
->d
.dict
));
1358 array
->retainCount
= 1;
1360 enumerator
= WMEnumerateHashTable(plist
->d
.dict
);
1361 while ((key
= WMNextHashEnumeratorKey(&enumerator
))) {
1362 WMAddToArray(array
->d
.array
, WMRetainPropList(key
));
1368 WMPropList
*WMShallowCopyPropList(WMPropList
* plist
)
1370 WMPropList
*ret
= NULL
;
1371 WMPropList
*key
, *item
;
1376 switch (plist
->type
) {
1378 ret
= WMCreatePLString(plist
->d
.string
);
1381 data
= WMCreateDataWithData(plist
->d
.data
);
1382 ret
= WMCreatePLData(data
);
1383 WMReleaseData(data
);
1386 ret
= (WMPropList
*) wmalloc(sizeof(W_PropList
));
1387 ret
->type
= WPLArray
;
1388 ret
->d
.array
= WMCreateArrayWithArray(plist
->d
.array
);
1389 ret
->retainCount
= 1;
1391 for (i
= 0; i
< WMGetArrayItemCount(ret
->d
.array
); i
++)
1392 WMRetainPropList(WMGetFromArray(ret
->d
.array
, i
));
1396 ret
= WMCreatePLDictionary(NULL
, NULL
);
1397 e
= WMEnumerateHashTable(plist
->d
.dict
);
1398 while (WMNextHashEnumeratorItemAndKey(&e
, (void **)&item
, (void **)&key
)) {
1399 WMPutInPLDictionary(ret
, key
, item
);
1403 wwarning(_("Used proplist functions on non-WMPropLists objects"));
1404 wassertrv(False
, NULL
);
1411 WMPropList
*WMDeepCopyPropList(WMPropList
* plist
)
1413 WMPropList
*ret
= NULL
;
1414 WMPropList
*key
, *item
;
1419 switch (plist
->type
) {
1421 ret
= WMCreatePLString(plist
->d
.string
);
1424 data
= WMCreateDataWithData(plist
->d
.data
);
1425 ret
= WMCreatePLData(data
);
1426 WMReleaseData(data
);
1429 ret
= WMCreatePLArray(NULL
);
1430 for (i
= 0; i
< WMGetArrayItemCount(plist
->d
.array
); i
++) {
1431 item
= WMDeepCopyPropList(WMGetFromArray(plist
->d
.array
, i
));
1432 WMAddToArray(ret
->d
.array
, item
);
1436 ret
= WMCreatePLDictionary(NULL
, NULL
);
1437 e
= WMEnumerateHashTable(plist
->d
.dict
);
1438 /* While we copy an existing dictionary there is no way that we can
1439 * have duplicate keys, so we don't need to first remove a key/value
1440 * pair before inserting the new key/value.
1442 while (WMNextHashEnumeratorItemAndKey(&e
, (void **)&item
, (void **)&key
)) {
1443 WMHashInsert(ret
->d
.dict
, WMDeepCopyPropList(key
), WMDeepCopyPropList(item
));
1447 wwarning(_("Used proplist functions on non-WMPropLists objects"));
1448 wassertrv(False
, NULL
);
1455 WMPropList
*WMCreatePropListFromDescription(char *desc
)
1457 WMPropList
*plist
= NULL
;
1460 pldata
= (PLData
*) wmalloc(sizeof(PLData
));
1462 pldata
->lineNumber
= 1;
1464 plist
= getPropList(pldata
);
1466 if (getNonSpaceChar(pldata
) != 0 && plist
) {
1467 COMPLAIN(pldata
, _("extra data after end of property list"));
1469 * We can't just ignore garbage after the end of the description
1470 * (especially if the description was read from a file), because
1471 * the "garbage" can be the real data and the real garbage is in
1472 * fact in the beginning of the file (which is now inside plist)
1474 WMReleasePropList(plist
);
1483 char *WMGetPropListDescription(WMPropList
* plist
, Bool indented
)
1485 return (indented
? indentedDescription(plist
, 0) : description(plist
));
1488 WMPropList
*WMReadPropListFromFile(char *file
)
1490 WMPropList
*plist
= NULL
;
1496 f
= fopen(file
, "rb");
1498 /* let the user print the error message if he really needs to */
1499 /*werror(_("could not open domain file '%s' for reading"), file); */
1503 if (stat(file
, &stbuf
) == 0) {
1504 length
= (size_t) stbuf
.st_size
;
1506 werror(_("could not get size for file '%s'"), file
);
1511 pldata
= (PLData
*) wmalloc(sizeof(PLData
));
1512 pldata
->ptr
= (char *)wmalloc(length
+ 1);
1513 pldata
->filename
= file
;
1514 pldata
->lineNumber
= 1;
1516 if (fread(pldata
->ptr
, length
, 1, f
) != 1) {
1518 werror(_("error reading from file '%s'"), file
);
1524 pldata
->ptr
[length
] = 0;
1526 plist
= getPropList(pldata
);
1528 if (getNonSpaceChar(pldata
) != 0 && plist
) {
1529 COMPLAIN(pldata
, _("extra data after end of property list"));
1531 * We can't just ignore garbage after the end of the description
1532 * (especially if the description was read from a file), because
1533 * the "garbage" can be the real data and the real garbage is in
1534 * fact in the beginning of the file (which is now inside plist)
1536 WMReleasePropList(plist
);
1548 WMPropList
*WMReadPropListFromPipe(char *command
)
1555 file
= popen(command
, "r");
1558 werror(_("%s:could not open menu file"), command
);
1562 pldata
= (PLData
*) wmalloc(sizeof(PLData
));
1564 pldata
->filename
= command
;
1565 pldata
->lineNumber
= 1;
1567 /* read from file till EOF or OOM and fill proplist buffer*/
1568 while (fgets(line
, sizeof(line
), file
) != NULL
) {
1569 if (pldata
->ptr
== NULL
) {
1570 pldata
->ptr
= wmalloc(strlen(line
)+1);
1571 pldata
->ptr
[0] = '\0';
1573 pldata
->ptr
= wrealloc(pldata
->ptr
,
1574 strlen(line
) + strlen(pldata
->ptr
) + 1);
1577 pldata
->ptr
= strncat(pldata
->ptr
, line
, strlen(line
));
1582 plist
= getPropList(pldata
);
1584 if (getNonSpaceChar(pldata
) != 0 && plist
) {
1585 COMPLAIN(pldata
, _("extra data after end of property list"));
1587 * We can't just ignore garbage after the end of the description
1588 * (especially if the description was read from a file), because
1589 * the "garbage" can be the real data and the real garbage is in
1590 * fact in the beginning of the file (which is now inside plist)
1592 WMReleasePropList(plist
);
1602 /* TODO: review this function's code */
1604 Bool
WMWritePropListToFile(WMPropList
* plist
, char *path
)
1606 char *thePath
= NULL
;
1613 if (!wmkdirhier(path
))
1616 /* Use the path name of the destination file as a prefix for the
1617 * mkstemp() call so that we can be sure that both files are on
1618 * the same filesystem and the subsequent rename() will work. */
1619 thePath
= wstrconcat(path
, ".XXXXXX");
1622 if ((fd
= mkstemp(thePath
)) < 0) {
1623 werror(_("mkstemp (%s) failed"), thePath
);
1628 fchmod(fd
, 0644 & ~mask
);
1629 if ((theFile
= fdopen(fd
, "wb")) == NULL
) {
1633 if (mktemp(thePath
) == NULL
) {
1634 werror(_("mktemp (%s) failed"), thePath
);
1637 theFile
= fopen(thePath
, "wb");
1640 if (theFile
== NULL
) {
1641 werror(_("open (%s) failed"), thePath
);
1645 desc
= indentedDescription(plist
, 0);
1647 if (fprintf(theFile
, "%s\n", desc
) != strlen(desc
) + 1) {
1648 werror(_("writing to file: %s failed"), thePath
);
1655 (void)fsync(fileno(theFile
));
1656 if (fclose(theFile
) != 0) {
1657 werror(_("fclose (%s) failed"), thePath
);
1661 /* If we used a temporary file, we still need to rename() it be the
1662 * real file. Also, we need to try to retain the file attributes of
1663 * the original file we are overwriting (if we are) */
1664 if (rename(thePath
, path
) != 0) {
1665 werror(_("rename ('%s' to '%s') failed"), thePath
, path
);
1679 * create a directory hierarchy
1681 * if the last octet of `path' is `/', the full path is
1682 * assumed to be a directory; otherwise path is assumed to be a
1683 * file, and the last component is stripped off. the rest is the
1684 * the hierarchy to be created.
1686 * refuses to create anything outside $GNUSTEP_USER_ROOT
1688 * returns 1 on success, 0 on failure
1690 int wmkdirhier(const char *path
)
1692 char *t
, *thePath
= NULL
, buf
[1024];
1696 /* Only create directories under $GNUSTEP_USER_ROOT */
1697 if ((t
= wusergnusteppath()) == NULL
)
1699 if (strncmp(path
, t
, strlen(t
)) != 0)
1702 thePath
= wstrdup(path
);
1703 /* Strip the trailing component if it is a file */
1704 p
= strlen(thePath
);
1705 while (p
&& thePath
[p
] != '/')
1706 thePath
[p
--] = '\0';
1710 /* Shortcut if it already exists */
1711 if (stat(thePath
, &st
) == 0) {
1713 if (S_ISDIR(st
.st_mode
)) {
1714 /* Is a directory alright */
1717 /* Exists, but not a directory, the caller
1718 * might just as well abort now */
1723 memset(buf
, 0, sizeof(buf
));
1724 strncpy(buf
, t
, sizeof(buf
) - 1);
1726 plen
= strlen(thePath
);
1729 while (p
++ < plen
&& thePath
[p
] != '/')
1732 strncpy(buf
, thePath
, p
);
1733 if (mkdir(buf
, 0777) == -1 && errno
== EEXIST
&&
1734 stat(buf
, &st
) == 0 && !S_ISDIR(st
.st_mode
)) {
1735 werror(_("Could not create component %s"), buf
);
1746 static int wrmdirhier_fn(const char *path
, const struct stat
*st
,
1747 int type
, struct FTW
*ftw
)
1758 return unlink(path
);
1771 * remove a directory hierarchy
1773 * refuses to remove anything outside $GNUSTEP_USER_ROOT
1775 * returns 1 on success, 0 on failure
1777 * TODO: revisit what's error and what's not
1779 * with inspirations from OpenBSD's bin/rm/rm.c
1781 int wrmdirhier(const char *path
)
1787 /* Only remove directories under $GNUSTEP_USER_ROOT */
1788 if ((t
= wusergnusteppath()) == NULL
)
1790 if (strncmp(path
, t
, strlen(t
)) != 0)
1793 /* Shortcut if it doesn't exist to begin with */
1794 if (stat(path
, &st
) == -1)
1797 error
= nftw(path
, wrmdirhier_fn
, 1, FTW_PHYS
);