1 /*****************************************************************************/
5 /* Command line attributes */
9 /* (C) 2012, Ullrich von Bassewitz */
10 /* Roemerstrasse 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 /*****************************************************************************/
50 /*****************************************************************************/
52 /*****************************************************************************/
56 static int IsNumber (const char* Value)
57 /* Check if Value is an integer number */
59 if (*Value == '-' || *Value == '+') {
62 while (IsDigit (*Value)) {
65 return (*Value == '\0');
70 Attr* NewAttr (const char* Name, const char* Value)
71 /* Create a new attribute */
73 /* Determine the length of Value */
74 unsigned Len = strlen (Value);
77 Attr* A = xmalloc (sizeof (Attr) + Len);
79 /* Initialize the fields */
80 A->Flags = IsNumber (Value)? afInt : afNone;
81 A->Name = xstrdup (Name);
82 memcpy (A->Value, Value, Len + 1);
84 /* Return the new struct */
90 void DumpAttrColl (const Collection* C)
91 /* Dump a collection of attribute/value pairs for debugging */
94 for (I = 0; I < CollCount (C); ++I) {
95 const Attr* A = CollConstAt (C, I);
96 printf ("%s=%s\n", A->Name, A->Value);
102 int FindAttr (const Collection* C, const char* Name, unsigned* Index)
103 /* Search for an attribute with the given name in the collection. If it is
104 * found, the function returns true and Index contains the index of the
105 * entry. If Name isn't found, the function returns false and Index
106 * will contain the insert position.
109 /* Do a binary search */
111 int Hi = (int) CollCount (C) - 1;
115 int Cur = (Lo + Hi) / 2;
118 const Attr* A = CollAt (C, Cur);
121 int Res = strcmp (A->Name, Name);
126 } else if (Res > 0) {
135 /* Pass back the insert position */
142 const Attr* GetAttr (const Collection* C, const char* Name)
143 /* Search for an attribute with the given name and return it. The function
144 * returns NULL if the attribute wasn't found.
147 /* Search for the attribute and return it */
149 if (FindAttr (C, Name, &Index)) {
150 return CollConstAt (C, Index);
159 const Attr* NeedAttr (const Collection* C, const char* Name, const char* Context)
160 /* Search for an attribute with the given name and return it. If the attribute
161 * is not found, the function terminates with an error using Context as
162 * additional context in the error message.
165 /* Search for the attribute and return it */
167 if (!FindAttr (C, Name, &Index)) {
168 Error ("Found no attribute named `%s' in %s", Name, Context);
170 return CollConstAt (C, Index);
175 const char* GetAttrVal (const Collection* C, const char* Name)
176 /* Search for an attribute with the given name and return its value. The
177 * function returns NULL if the attribute wasn't found.
180 const Attr* A = GetAttr (C, Name);
181 return (A == 0)? 0 : A->Value;
186 const char* NeedAttrVal (const Collection* C, const char* Name, const char* Context)
187 /* Search for an attribute with the given name and return its value. If the
188 * attribute wasn't not found, the function terminates with an error using
189 * Context as additional context in the error message.
192 const Attr* A = NeedAttr (C, Name, Context);
193 return (A == 0)? 0 : A->Value;
198 void AddAttr (Collection* C, const char* Name, const char* Value)
199 /* Add an attribute to an alphabetically sorted attribute collection */
201 /* Create a new attribute entry */
202 Attr* A = NewAttr (Name, Value);
204 /* Search for the attribute. If it is there, we have a duplicate, otherwise
205 * we have the insert position.
208 if (FindAttr (C, Name, &Index)) {
209 Error ("Duplicate command line attribute `%s'", Name);
212 /* Insert the attribute */
213 CollInsert (C, A, Index);
218 void SplitAddAttr (Collection* C, const char* Combined, const char* Name)
219 /* Split a combined name/value pair and add it as an attribute to C. Some
220 * attributes may not need a name. If the name is missing, use Name. If
221 * Name is NULL, terminate with an error.
224 /* Name and value are separated by an equal sign */
225 const char* Pos = strchr (Combined, '=');
227 /* Combined is actually a value */
229 Error ("Command line attribute `%s' doesn't contain a name", Combined);
231 AddAttr (C, Name, Combined);
233 /* Must split name and value */
234 StrBuf N = AUTO_STRBUF_INITIALIZER;
235 SB_CopyBuf (&N, Combined, Pos - Combined);
238 /* Add the attribute */
239 AddAttr (C, SB_GetConstBuf (&N), Pos+1);
248 Collection* ParseAttrList (const char* List, const char** NameList, unsigned NameCount)
249 /* Parse a list containing name/value pairs into a sorted collection. Some
250 * attributes may not need a name, so NameList contains these names. If there
251 * were no errors, the function returns a alphabetically sorted collection
252 * containing Attr entries.
257 /* Create a new collection */
258 Collection* C = NewCollection ();
260 /* Name/value pairs are separated by commas */
261 const char* L = List;
262 StrBuf B = AUTO_STRBUF_INITIALIZER;
264 if (*L == ',' || *L == '\0') {
266 /* Terminate the string */
269 /* Determine the default name */
270 if (CollCount (C) >= NameCount) {
273 Name = NameList[CollCount (C)];
276 /* Split and add this attribute/value pair */
277 SplitAddAttr (C, SB_GetConstBuf (&B), Name);
279 /* Done, clear the buffer. */
285 SB_AppendChar (&B, *L);
293 /* Return the collection with the attributes */