1 /* Copyright 2007-2012 Fredrik Wikstrom. All rights reserved.
3 ** Redistribution and use in source and binary forms, with or without
4 ** modification, are permitted provided that the following conditions
7 ** 1. Redistributions of source code must retain the above copyright
8 ** notice, this list of conditions and the following disclaimer.
10 ** 2. Redistributions in binary form must reproduce the above copyright
11 ** notice, this list of conditions and the following disclaimer in the
12 ** documentation and/or other materials provided with the distribution.
14 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
15 ** AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 ** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 ** ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
18 ** LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19 ** CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20 ** SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21 ** INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22 ** CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23 ** ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24 ** POSSIBILITY OF SUCH DAMAGE.
28 #include <proto/exec.h>
32 PrefsObject
*AllocPrefsDictionary (void) {
34 obj
= AllocVec(sizeof(PrefsObject
), MEMF_CLEAR
);
36 obj
->type
= PREFS_DICTIONARY
;
37 obj
->value
.list
= CreateList(TRUE
);
38 if (obj
->value
.list
) {
46 PrefsObject
*AllocPrefsBoolean (BOOL value
) {
48 obj
= AllocVec(sizeof(PrefsObject
), MEMF_CLEAR
);
50 obj
->type
= PREFS_BOOL
;
51 obj
->value
.bool = value
;
57 PrefsObject
*AllocPrefsInteger (LONG value
) {
59 obj
= AllocVec(sizeof(PrefsObject
), MEMF_CLEAR
);
61 obj
->type
= PREFS_INTEGER
;
62 obj
->value
.integer
= value
;
68 PrefsObject
*AllocPrefsString (CONST_STRPTR value
) {
70 obj
= AllocVec(sizeof(PrefsObject
), MEMF_CLEAR
);
72 obj
->type
= PREFS_STRING
;
73 obj
->value
.string
= ASPrintf("%s", value
);
74 if (obj
->value
.string
) {
82 void FreePrefsObject (PrefsObject
*obj
) {
86 case PREFS_DICTIONARY
:
87 if (obj
->value
.list
) {
89 while ((child
= (PrefsObject
*)RemHead(obj
->value
.list
))) {
90 FreePrefsObject(child
);
92 DeleteList(obj
->value
.list
);
96 FreeVec(obj
->value
.string
);
103 PrefsObject
*DictGetObjectForKey (PrefsObject
*dict
, CONST_STRPTR key
) {
104 if (dict
&& dict
->type
== PREFS_DICTIONARY
&& key
) {
106 child
= (PrefsObject
*)GetHead(dict
->value
.list
);
108 if (!strcmp(child
->key
, key
)) {
111 child
= (PrefsObject
*)GetSucc((struct Node
*)child
);
117 BOOL
DictGetBooleanForKey (PrefsObject
*dict
, CONST_STRPTR key
, BOOL def_val
) {
119 child
= DictGetObjectForKey(dict
, key
);
120 if (child
&& child
->type
== PREFS_BOOL
) {
121 return child
->value
.bool;
127 LONG
DictGetIntegerForKey (PrefsObject
*dict
, CONST_STRPTR key
, LONG def_val
) {
129 child
= DictGetObjectForKey(dict
, key
);
130 if (child
&& child
->type
== PREFS_INTEGER
) {
131 return child
->value
.integer
;
137 CONST_STRPTR
DictGetStringForKey (PrefsObject
*dict
, CONST_STRPTR key
, CONST_STRPTR def_val
) {
139 child
= DictGetObjectForKey(dict
, key
);
140 if (child
&& child
->type
== PREFS_STRING
) {
141 return child
->value
.string
;
147 BOOL
DictSetObjectForKey (PrefsObject
*dict
, PrefsObject
*obj
, CONST_STRPTR key
) {
148 if (dict
&& dict
->type
== PREFS_DICTIONARY
&& obj
&& key
) {
150 obj
->key
= ASPrintf("%s", key
);
152 DictRemoveObjForKey(dict
, key
);
153 AddTail(dict
->value
.list
, (struct Node
*)obj
);
156 FreePrefsObject(obj
);
160 FreePrefsObject(obj
);
165 BOOL
DictRemoveObjForKey (PrefsObject
*dict
, CONST_STRPTR key
) {
167 child
= DictGetObjectForKey(dict
, key
);
169 Remove((struct Node
*)child
);
170 FreePrefsObject(child
);
177 void ClearPrefsDictionary (PrefsObject
*dict
) {
178 if (dict
&& dict
->type
== PREFS_DICTIONARY
) {
180 while ((child
= (PrefsObject
*)RemHead(dict
->value
.list
))) {
181 FreePrefsObject(child
);