1 /*****************************************************************************/
5 /* Config data structure */
9 /* (C) 2002-2003 Ullrich von Bassewitz */
10 /* Römerstrasse 52 */
11 /* D-70794 Filderstadt */
12 /* EMail: uz@cc65.org */
15 /* This software is provided 'as-is', without any expressed or implied */
16 /* warranty. In no event will the authors be held liable for any damages */
17 /* arising from the use of this software. */
19 /* Permission is granted to anyone to use this software for any purpose, */
20 /* including commercial applications, and to alter it and redistribute it */
21 /* freely, subject to the following restrictions: */
23 /* 1. The origin of this software must not be misrepresented; you must not */
24 /* claim that you wrote the original software. If you use this software */
25 /* in a product, an acknowledgment in the product documentation would be */
26 /* appreciated but is not required. */
27 /* 2. Altered source versions must be plainly marked as such, and must not */
28 /* be misrepresented as being the original software. */
29 /* 3. This notice may not be removed or altered from any source */
32 /*****************************************************************************/
49 /*****************************************************************************/
51 /*****************************************************************************/
55 CfgData* NewCfgData (void)
56 /* Create and intialize a new CfgData struct, then return it. The function
57 * uses the current output of the config scanner.
60 /* Get the length of the identifier */
61 unsigned AttrLen = strlen (CfgSVal);
64 CfgData* D = xmalloc (sizeof (CfgData) + AttrLen);
66 /* Initialize the fields */
67 D->Type = CfgDataInvalid;
68 D->Line = CfgErrorLine;
70 memcpy (D->Attr, CfgSVal, AttrLen+1);
72 /* Return the new struct */
78 void FreeCfgData (CfgData* D)
79 /* Free a config data structure */
81 if (D->Type == CfgDataString) {
82 /* Free the string value */
85 /* Free the structure */
91 void CfgDataCheckType (const CfgData* D, unsigned Type)
92 /* Check the config data type and print an error message if it has the wrong
96 if (D->Type != Type) {
97 Error ("%s(%u): Attribute `%s' has invalid type",
98 CfgGetName (), D->Line, D->Attr);
104 int CfgDataFind (const Collection* Attributes, const char* AttrName)
105 /* Find the attribute with the given name and return its index. Return -1 if
106 * the attribute was not found.
111 /* Walk through the attributes checking for a "mirror" attribute */
112 for (I = 0; I < CollCount (Attributes); ++I) {
114 /* Get the next attribute */
115 const CfgData* D = CollConstAt (Attributes, I);
117 /* Compare the name */
118 if (StrCaseCmp (D->Attr, AttrName) == 0) {
130 CfgData* CfgDataGetTyped (Collection* Attributes, const char* Name, unsigned Type)
131 /* Find the attribute with the given name and type. If found, remove it from
132 * Attributes and return it. If not found or wrong type, return NULL.
137 /* Search for the attribute */
138 int I = CfgDataFind (Attributes, Name);
144 /* Get the attribute */
145 D = CollAtUnchecked (Attributes, I);
148 if (D->Type != Type) {
149 /* Wrong type. ### Warn here? */
153 /* Remove the attribute and return it */
154 CollDelete (Attributes, I);
160 int CfgDataGetId (Collection* Attributes, const char* Name, char** Id)
161 /* Search CfgInfo for an attribute with the given name and type "id". If
162 * found, remove it from the configuration, copy it into Buf and return
163 * true. If not found, return false.
166 CfgData* D = CfgDataGetTyped (Attributes, Name, CfgDataId);
168 /* Not found or wrong type */
172 /* Use the string value and invalidate the type, so FreeCfgData won't
176 D->Type = CfgDataInvalid;
178 /* Delete the config data struct */
187 int CfgDataGetStr (Collection* Attributes, const char* Name, char** S)
188 /* Search CfgInfo for an attribute with the given name and type "string".
189 * If found, remove it from the configuration, copy it into Buf and return
190 * true. If not found, return false.
193 CfgData* D = CfgDataGetTyped (Attributes, Name, CfgDataString);
195 /* Not found or wrong type */
199 /* Use the string value and invalidate the type, so FreeCfgData won't
203 D->Type = CfgDataInvalid;
205 /* Delete the config data struct */
214 int CfgDataGetNum (Collection* Attributes, const char* Name, long* Val)
215 /* Search CfgInfo for an attribute with the given name and type "number".
216 * If found, remove it from the configuration, copy it into Val and return
217 * true. If not found, return false.
220 CfgData* D = CfgDataGetTyped (Attributes, Name, CfgDataString);
222 /* Not found or wrong type */
226 /* Return the value to the caller */
229 /* Delete the config data struct */