revert between 56095 -> 55830 in arch
[AROS.git] / workbench / devs / diskimage / prefs / prefs.c
blob65267ecef3e3418d04e1327ed35e21cb098cc4f2
1 /* Copyright 2007-2012 Fredrik Wikstrom. All rights reserved.
2 **
3 ** Redistribution and use in source and binary forms, with or without
4 ** modification, are permitted provided that the following conditions
5 ** are met:
6 **
7 ** 1. Redistributions of source code must retain the above copyright
8 ** notice, this list of conditions and the following disclaimer.
9 **
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.
27 #include "prefs.h"
28 #include <proto/exec.h>
29 #include <string.h>
30 #include "support.h"
32 PrefsObject *AllocPrefsDictionary (void) {
33 PrefsObject *obj;
34 obj = AllocVec(sizeof(PrefsObject), MEMF_CLEAR);
35 if (obj) {
36 obj->type = PREFS_DICTIONARY;
37 obj->value.list = CreateList(TRUE);
38 if (obj->value.list) {
39 return obj;
41 FreePrefsObject(obj);
43 return NULL;
46 PrefsObject *AllocPrefsBoolean (BOOL value) {
47 PrefsObject *obj;
48 obj = AllocVec(sizeof(PrefsObject), MEMF_CLEAR);
49 if (obj) {
50 obj->type = PREFS_BOOL;
51 obj->value.bool = value;
52 return obj;
54 return NULL;
57 PrefsObject *AllocPrefsInteger (LONG value) {
58 PrefsObject *obj;
59 obj = AllocVec(sizeof(PrefsObject), MEMF_CLEAR);
60 if (obj) {
61 obj->type = PREFS_INTEGER;
62 obj->value.integer = value;
63 return obj;
65 return NULL;
68 PrefsObject *AllocPrefsString (CONST_STRPTR value) {
69 PrefsObject *obj;
70 obj = AllocVec(sizeof(PrefsObject), MEMF_CLEAR);
71 if (obj) {
72 obj->type = PREFS_STRING;
73 obj->value.string = ASPrintf("%s", value);
74 if (obj->value.string) {
75 return obj;
77 FreePrefsObject(obj);
79 return NULL;
82 void FreePrefsObject (PrefsObject *obj) {
83 if (obj) {
84 FreeVec(obj->key);
85 switch (obj->type) {
86 case PREFS_DICTIONARY:
87 if (obj->value.list) {
88 PrefsObject *child;
89 while ((child = (PrefsObject *)RemHead(obj->value.list))) {
90 FreePrefsObject(child);
92 DeleteList(obj->value.list);
94 break;
95 case PREFS_STRING:
96 FreeVec(obj->value.string);
97 break;
99 FreeVec(obj);
103 PrefsObject *DictGetObjectForKey (PrefsObject *dict, CONST_STRPTR key) {
104 if (dict && dict->type == PREFS_DICTIONARY && key) {
105 PrefsObject *child;
106 child = (PrefsObject *)GetHead(dict->value.list);
107 while (child) {
108 if (!strcmp(child->key, key)) {
109 return child;
111 child = (PrefsObject *)GetSucc((struct Node *)child);
114 return NULL;
117 BOOL DictGetBooleanForKey (PrefsObject *dict, CONST_STRPTR key, BOOL def_val) {
118 PrefsObject *child;
119 child = DictGetObjectForKey(dict, key);
120 if (child && child->type == PREFS_BOOL) {
121 return child->value.bool;
122 } else {
123 return def_val;
127 LONG DictGetIntegerForKey (PrefsObject *dict, CONST_STRPTR key, LONG def_val) {
128 PrefsObject *child;
129 child = DictGetObjectForKey(dict, key);
130 if (child && child->type == PREFS_INTEGER) {
131 return child->value.integer;
132 } else {
133 return def_val;
137 CONST_STRPTR DictGetStringForKey (PrefsObject *dict, CONST_STRPTR key, CONST_STRPTR def_val) {
138 PrefsObject *child;
139 child = DictGetObjectForKey(dict, key);
140 if (child && child->type == PREFS_STRING) {
141 return child->value.string;
142 } else {
143 return def_val;
147 BOOL DictSetObjectForKey (PrefsObject *dict, PrefsObject *obj, CONST_STRPTR key) {
148 if (dict && dict->type == PREFS_DICTIONARY && obj && key) {
149 obj->parent = dict;
150 obj->key = ASPrintf("%s", key);
151 if (obj->key) {
152 DictRemoveObjForKey(dict, key);
153 AddTail(dict->value.list, (struct Node *)obj);
154 return TRUE;
155 } else {
156 FreePrefsObject(obj);
157 return FALSE;
159 } else {
160 FreePrefsObject(obj);
161 return FALSE;
165 BOOL DictRemoveObjForKey (PrefsObject *dict, CONST_STRPTR key) {
166 PrefsObject *child;
167 child = DictGetObjectForKey(dict, key);
168 if (child) {
169 Remove((struct Node *)child);
170 FreePrefsObject(child);
171 return TRUE;
172 } else {
173 return FALSE;
177 void ClearPrefsDictionary (PrefsObject *dict) {
178 if (dict && dict->type == PREFS_DICTIONARY) {
179 PrefsObject *child;
180 while ((child = (PrefsObject *)RemHead(dict->value.list))) {
181 FreePrefsObject(child);