]> git.sur5r.net Git - cc65/blob - src/sim65/cfgdata.c
Fixed a bug
[cc65] / src / sim65 / cfgdata.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                 cfgdata.c                                 */
4 /*                                                                           */
5 /*                           Config data structure                           */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 2002-2003 Ullrich von Bassewitz                                       */
10 /*               Römerstrasse 52                                             */
11 /*               D-70794 Filderstadt                                         */
12 /* EMail:        uz@cc65.org                                                 */
13 /*                                                                           */
14 /*                                                                           */
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.                                    */
18 /*                                                                           */
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:                            */
22 /*                                                                           */
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              */
30 /*    distribution.                                                          */
31 /*                                                                           */
32 /*****************************************************************************/
33
34
35
36 #include <string.h>
37
38 /* common */
39 #include "strutil.h"
40 #include "xmalloc.h"
41
42 /* sim65 */
43 #include "error.h"
44 #include "scanner.h"
45 #include "cfgdata.h"
46
47
48
49 /*****************************************************************************/
50 /*                                     Code                                  */
51 /*****************************************************************************/
52
53
54
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.
58  */
59 {
60     /* Get the length of the identifier */
61     unsigned AttrLen = strlen (CfgSVal);
62
63     /* Allocate memory */
64     CfgData* D = xmalloc (sizeof (CfgData) + AttrLen);
65
66     /* Initialize the fields */
67     D->Type = CfgDataInvalid;
68     D->Line = CfgErrorLine;
69     D->Col  = CfgErrorCol;
70     memcpy (D->Attr, CfgSVal, AttrLen+1);
71
72     /* Return the new struct */
73     return D;
74 }
75
76
77
78 void FreeCfgData (CfgData* D)
79 /* Free a config data structure */
80 {
81     if (D->Type == CfgDataString) {
82         /* Free the string value */
83         xfree (D->V.SVal);
84     }
85     /* Free the structure */
86     xfree (D);
87 }
88
89
90
91 void CfgDataCheckType (const CfgData* D, unsigned Type)
92 /* Check the config data type and print an error message if it has the wrong
93  * type.
94  */
95 {
96     if (D->Type != Type) {
97         Error ("%s(%u): Attribute `%s' has invalid type",
98                CfgGetName (), D->Line, D->Attr);
99     }
100 }
101
102
103
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.
107  */
108 {
109     unsigned I;
110
111     /* Walk through the attributes checking for a "mirror" attribute */
112     for (I = 0; I < CollCount (Attributes); ++I) {
113
114         /* Get the next attribute */
115         const CfgData* D = CollConstAt (Attributes, I);
116
117         /* Compare the name */
118         if (StrCaseCmp (D->Attr, AttrName) == 0) {
119             /* Found */
120             return I;
121         }
122     }
123
124     /* Not found */
125     return -1;
126 }
127
128
129
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.
133  */
134 {
135     CfgData* D;
136
137     /* Search for the attribute */
138     int I = CfgDataFind (Attributes, Name);
139     if (I < 0) {
140         /* Not found */
141         return 0;
142     }
143
144     /* Get the attribute */
145     D = CollAtUnchecked (Attributes, I);
146
147     /* Check the type */
148     if (D->Type != Type) {
149         /* Wrong type. ### Warn here? */
150         return 0;
151     }
152
153     /* Remove the attribute and return it */
154     CollDelete (Attributes, I);
155     return D;
156 }
157
158
159
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.
164  */
165 {
166     CfgData* D = CfgDataGetTyped (Attributes, Name, CfgDataId);
167     if (D == 0) {
168         /* Not found or wrong type */
169         return 0;
170     }
171
172     /* Use the string value and invalidate the type, so FreeCfgData won't
173      * delete the string.
174      */
175     *Id = D->V.SVal;
176     D->Type = CfgDataInvalid;
177
178     /* Delete the config data struct */
179     FreeCfgData (D);
180
181     /* Success */
182     return 1;
183 }
184
185
186
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.
191  */
192 {
193     CfgData* D = CfgDataGetTyped (Attributes, Name, CfgDataString);
194     if (D == 0) {
195         /* Not found or wrong type */
196         return 0;
197     }
198
199     /* Use the string value and invalidate the type, so FreeCfgData won't
200      * delete the string.
201      */
202     *S = D->V.SVal;
203     D->Type = CfgDataInvalid;
204
205     /* Delete the config data struct */
206     FreeCfgData (D);
207
208     /* Success */
209     return 1;
210 }
211
212
213
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.
218  */
219 {
220     CfgData* D = CfgDataGetTyped (Attributes, Name, CfgDataString);
221     if (D == 0) {
222         /* Not found or wrong type */
223         return 0;
224     }
225
226     /* Return the value to the caller */
227     *Val = D->V.IVal;
228
229     /* Delete the config data struct */
230     FreeCfgData (D);
231
232     /* Success */
233     return 1;
234 }
235
236
237