gwenhywfar 5.10.1
p_options.c
Go to the documentation of this file.
1/***************************************************************************
2 begin : Mon Feb 08 2021
3 copyright : (C) 2021 by Martin Preuss
4 email : martin@libchipcard.de
5
6 ***************************************************************************
7 * Please see toplevel file COPYING for license details *
8 ***************************************************************************/
9
10#ifdef HAVE_CONFIG_H
11# include <config.h>
12#endif
13
14
18
19#include <gwenhywfar/debug.h>
20#include <gwenhywfar/text.h>
21
22#include <ctype.h>
23
24
25
26int _checkAgainstGivenOption(GWB_PROJECT *project, GWB_CONTEXT *currentContext, GWB_OPTION *option);
27int _checkStringOption(GWB_OPTION *option, GWB_CONTEXT *currentContext, const char *givenValue);
28int _checkStringListOption(GWB_PROJECT *project, GWB_OPTION *option, GWB_CONTEXT *currentContext, const char *givenValue);
29
30
31
32
33
34
35int GWB_ParseOption(GWB_PROJECT *project, GWB_CONTEXT *currentContext, GWEN_XMLNODE *xmlNode)
36{
37 const char *sId;
38 int otype;
39 GWB_OPTION *option;
40 const char *s;
41 GWEN_XMLNODE *n;
42 int rv;
43
44 rv=GWEN_XMLNode_ExpandProperties(xmlNode, GWB_Context_GetVars(currentContext));
45 if (rv<0) {
46 DBG_INFO(NULL, "here (%d)", rv);
47 return rv;
48 }
49
50 sId=GWEN_XMLNode_GetProperty(xmlNode, "id", NULL);
51 if (!(sId && *sId)) {
52 DBG_ERROR(NULL, "Option has no id");
53 GWEN_XMLNode_Dump(xmlNode, 2);
54 return GWEN_ERROR_GENERIC;
55 }
56
57 otype=GWB_OptionType_fromString(GWEN_XMLNode_GetProperty(xmlNode, "type", "string"));
58 if (!(otype>GWB_OptionType_None)) {
59 DBG_ERROR(NULL, "Invalid option type (id=%s)", sId);
60 GWEN_XMLNode_Dump(xmlNode, 2);
61 return GWEN_ERROR_GENERIC;
62 }
63
64 option=GWB_Option_new(sId);
65 GWB_Option_SetOptionType(option, otype);
66
67 if (1) {
68 GWEN_BUFFER *buf;
69
70 buf=GWB_Parser_ReadNamedXmlDataIntoBufferAndExpand(GWB_Context_GetVars(currentContext), xmlNode, "default");
71 if (buf) {
75 }
76 }
77
78 s=GWEN_XMLNode_GetProperty(xmlNode, "definePrefix", NULL);
79 if (s)
81
82 s=GWEN_XMLNode_GetCharValue(xmlNode, "choices", NULL);
83 if (s) {
85
87 if (sl) {
90 }
91 }
92
93 n=GWEN_XMLNode_FindFirstTag(xmlNode, "alias", NULL, NULL);
94 while(n) {
95 const char *sName;
96 GWEN_BUFFER *valueBuffer;
97
98 sName=GWEN_XMLNode_GetProperty(n, "name", NULL);
100 if (valueBuffer) {
101 GWB_Option_AddAlias(option, sName, GWEN_Buffer_GetStart(valueBuffer));
102 GWEN_Buffer_free(valueBuffer);
103 }
104 n=GWEN_XMLNode_FindNextTag(n, "alias", NULL, NULL);
105 }
106
107 _checkAgainstGivenOption(project, currentContext, option);
108
109 GWB_Project_AddOption(project, option);
110 return 0;
111}
112
113
114
115int _checkAgainstGivenOption(GWB_PROJECT *project, GWB_CONTEXT *currentContext, GWB_OPTION *option)
116{
117 const char *optionId;
118 GWB_KEYVALUEPAIR *kvp;
119 const char *givenValue=NULL;
120
121 optionId=GWB_Option_GetId(option);
123 if (kvp)
124 givenValue=GWB_KeyValuePair_GetValue(kvp);
125 if (givenValue==NULL)
126 givenValue=GWB_Option_GetDefaultValue(option);
127 if (givenValue) {
128 int rv=GWEN_ERROR_GENERIC;
129
130 switch(GWB_Option_GetOptionType(option)) {
133 DBG_ERROR(NULL, "Bad option type in option %s", optionId);
135 break;
137 rv=_checkStringOption(option, currentContext, givenValue);
138 break;
140 rv=_checkStringListOption(project, option, currentContext, givenValue);
141 break;
142 }
143 if (rv<0)
144 return rv;
145 }
146
147 if (kvp) {
148 GWB_KeyValuePair_List_Del(kvp);
150 }
151
152 return 0;
153}
154
155
156
157int _checkStringOption(GWB_OPTION *option, GWB_CONTEXT *currentContext, const char *givenValue)
158{
159 const char *optionId;
160
161 optionId=GWB_Option_GetId(option);
162 fprintf(stdout, " option %s: ", optionId);
163 if (givenValue) {
164 const char *s;
165 GWEN_BUFFER *nameBuffer;
166
167 s=GWB_Option_GetAlias(option, givenValue);
168 if (s==NULL)
169 s=givenValue;
170
171 if (!GWB_Option_IsValidChoice(option, s)) {
173 "Value \"%s\" (given value \"%s\") is not a valid choice for option \"%s\"",
174 s, givenValue, optionId);
175 return GWEN_ERROR_INVALID;
176 }
177
178 nameBuffer=GWEN_Buffer_new(0, 256, 0, 1);
179 GWEN_Buffer_AppendString(nameBuffer, "option_");
180 GWEN_Buffer_AppendString(nameBuffer, optionId);
183 GWEN_Buffer_GetStart(nameBuffer), s);
184 GWEN_Buffer_free(nameBuffer);
185 fprintf(stdout, "%s\n", s);
186 }
187
188 return 0;
189}
190
191
192
193int _checkStringListOption(GWB_PROJECT *project, GWB_OPTION *option, GWB_CONTEXT *currentContext, const char *givenValue)
194{
195 const char *optionId;
196 const char *definePrefix;
197
198 optionId=GWB_Option_GetId(option);
199 definePrefix=GWB_Option_GetDefinePrefix(option);
200 fprintf(stdout, " option %s: ", optionId);
201 if (givenValue) {
202 const char *s;
203 GWEN_STRINGLIST *sl;
204
205 s=GWB_Option_GetAlias(option, givenValue);
206 if (s==NULL)
207 s=givenValue;
208
210 if (sl) {
212 GWEN_BUFFER *nameBuffer;
213
214 nameBuffer=GWEN_Buffer_new(0, 256, 0, 1);
215 GWEN_Buffer_AppendString(nameBuffer, "option_");
216 GWEN_Buffer_AppendString(nameBuffer, optionId);
217
219 while(se) {
220 const char *sCurrentGivenValue;
221
222 sCurrentGivenValue=GWEN_StringListEntry_Data(se);
223 if (sCurrentGivenValue) {
224 if (!GWB_Option_IsValidChoice(option, sCurrentGivenValue)) {
226 "Value \"%s\" is not a valid choice for option \"%s\"",
227 sCurrentGivenValue, optionId);
228 GWEN_Buffer_free(nameBuffer);
230 return GWEN_ERROR_INVALID;
231 }
233 0,
234 GWEN_Buffer_GetStart(nameBuffer), sCurrentGivenValue);
235 fprintf(stdout, "%s ", sCurrentGivenValue);
236 if (definePrefix) {
237 GWEN_BUFFER *dbuf;
238
239 dbuf=GWEN_Buffer_new(0, 64, 0, 1);
240 GWEN_Buffer_AppendString(dbuf, definePrefix);
241 s=sCurrentGivenValue;
242 while(*s)
243 GWEN_Buffer_AppendByte(dbuf, toupper(*(s++)));
244 GWB_Project_SetDefine(project, GWEN_Buffer_GetStart(dbuf), "1");
245 GWEN_Buffer_free(dbuf);
246 }
247 }
248
250 } /* while */
251 GWEN_Buffer_free(nameBuffer);
253 } /* if sl */
254
255 } /* if givenValue */
256 fprintf(stdout, "\n");
257 return 0;
258}
259
260
261
262
263
264
#define NULL
Definition: binreloc.c:300
GWEN_BUFFER * GWEN_Buffer_new(char *buffer, uint32_t size, uint32_t used, int take)
Definition: buffer.c:42
void GWEN_Buffer_free(GWEN_BUFFER *bf)
Definition: buffer.c:89
int GWEN_Buffer_AppendString(GWEN_BUFFER *bf, const char *buffer)
Definition: buffer.c:989
uint32_t GWEN_Buffer_GetUsedBytes(const GWEN_BUFFER *bf)
Definition: buffer.c:277
char * GWEN_Buffer_GetStart(const GWEN_BUFFER *bf)
Definition: buffer.c:235
int GWEN_Buffer_AppendByte(GWEN_BUFFER *bf, char c)
Definition: buffer.c:394
GWEN_DB_NODE * GWB_Context_GetVars(const GWB_CONTEXT *ctx)
Definition: context.c:427
struct GWB_CONTEXT GWB_CONTEXT
Definition: context.h:17
int GWEN_DB_SetCharValue(GWEN_DB_NODE *n, uint32_t flags, const char *path, const char *val)
Definition: db.c:997
#define GWEN_DB_FLAGS_OVERWRITE_VARS
Definition: db.h:121
#define DBG_INFO(dbg_logger, format, args...)
Definition: debug.h:181
#define DBG_ERROR(dbg_logger, format, args...)
Definition: debug.h:97
#define GWEN_ERROR_INVALID
Definition: error.h:67
#define GWEN_ERROR_GENERIC
Definition: error.h:62
struct GWEN_BUFFER GWEN_BUFFER
A dynamically resizeable text buffer.
Definition: buffer.h:38
const char * GWB_KeyValuePair_GetValue(const GWB_KEYVALUEPAIR *kvp)
Definition: keyvaluepair.c:97
void GWB_KeyValuePair_free(GWB_KEYVALUEPAIR *kvp)
Definition: keyvaluepair.c:68
GWB_KEYVALUEPAIR * GWB_KeyValuePair_List_GetFirstByKey(const GWB_KEYVALUEPAIR_LIST *kvpList, const char *key)
Definition: keyvaluepair.c:144
struct GWB_KEYVALUEPAIR GWB_KEYVALUEPAIR
Definition: keyvaluepair.h:19
GWB_OPTION * GWB_Option_new(const char *id)
Definition: option.c:30
const char * GWB_Option_GetId(const GWB_OPTION *option)
Definition: option.c:63
void GWB_Option_SetOptionType(GWB_OPTION *option, int i)
Definition: option.c:77
int GWB_Option_GetOptionType(const GWB_OPTION *option)
Definition: option.c:70
void GWB_Option_SetDefinePrefix(GWB_OPTION *option, const char *s)
Definition: option.c:163
int GWB_OptionType_fromString(const char *s)
Definition: option.c:205
const char * GWB_Option_GetDefinePrefix(const GWB_OPTION *option)
Definition: option.c:156
int GWB_Option_IsValidChoice(const GWB_OPTION *option, const char *s)
Definition: option.c:137
const char * GWB_Option_GetDefaultValue(const GWB_OPTION *option)
Definition: option.c:84
void GWB_Option_AddAlias(GWB_OPTION *option, const char *name, const char *value)
Definition: option.c:106
void GWB_Option_SetDefaultValue(GWB_OPTION *option, const char *s)
Definition: option.c:91
const char * GWB_Option_GetAlias(const GWB_OPTION *option, const char *name)
Definition: option.c:116
GWEN_STRINGLIST * GWB_Option_GetChoiceList(const GWB_OPTION *option)
Definition: option.c:123
@ GWB_OptionType_String
Definition: option.h:24
@ GWB_OptionType_StringList
Definition: option.h:25
@ GWB_OptionType_Unknown
Definition: option.h:22
@ GWB_OptionType_None
Definition: option.h:23
struct GWB_OPTION GWB_OPTION
Definition: option.h:17
int _checkStringOption(GWB_OPTION *option, GWB_CONTEXT *currentContext, const char *givenValue)
Definition: p_options.c:157
int GWB_ParseOption(GWB_PROJECT *project, GWB_CONTEXT *currentContext, GWEN_XMLNODE *xmlNode)
Definition: p_options.c:35
int _checkAgainstGivenOption(GWB_PROJECT *project, GWB_CONTEXT *currentContext, GWB_OPTION *option)
Definition: p_options.c:115
int _checkStringListOption(GWB_PROJECT *project, GWB_OPTION *option, GWB_CONTEXT *currentContext, const char *givenValue)
Definition: p_options.c:193
GWEN_BUFFER * GWB_Parser_ReadNamedXmlDataIntoBufferAndExpand(GWEN_DB_NODE *db, GWEN_XMLNODE *xmlNode, const char *elem)
Definition: parser.c:345
GWEN_BUFFER * GWB_Parser_ReadXmlDataIntoBufferAndExpand(GWEN_DB_NODE *db, GWEN_XMLNODE *xmlNode)
Definition: parser.c:307
void GWB_Project_AddOption(GWB_PROJECT *project, GWB_OPTION *option)
Definition: project.c:348
GWB_KEYVALUEPAIR_LIST * GWB_Project_GetGivenOptionList(const GWB_PROJECT *project)
Definition: project.c:374
void GWB_Project_SetDefine(GWB_PROJECT *project, const char *name, const char *value)
Definition: project.c:298
struct GWB_PROJECT GWB_PROJECT
Definition: project.h:14
void GWEN_StringList_free(GWEN_STRINGLIST *sl)
Definition: stringlist.c:62
GWEN_STRINGLIST * GWEN_StringList_fromString2(const char *str, const char *delimiters, int checkDouble, uint32_t flags)
Definition: stringlist.c:801
const char * GWEN_StringListEntry_Data(const GWEN_STRINGLISTENTRY *se)
Definition: stringlist.c:406
GWEN_STRINGLISTENTRY * GWEN_StringListEntry_Next(const GWEN_STRINGLISTENTRY *se)
Definition: stringlist.c:398
void GWEN_StringList_AppendStringList(GWEN_STRINGLIST *slDest, const GWEN_STRINGLIST *slSource, int checkDouble)
Definition: stringlist.c:898
GWEN_STRINGLISTENTRY * GWEN_StringList_FirstEntry(const GWEN_STRINGLIST *sl)
Definition: stringlist.c:390
struct GWEN_STRINGLISTENTRYSTRUCT GWEN_STRINGLISTENTRY
Definition: stringlist.h:53
struct GWEN_STRINGLISTSTRUCT GWEN_STRINGLIST
Definition: stringlist.h:56
#define GWEN_TEXT_FLAGS_DEL_QUOTES
Definition: text.h:49
#define GWEN_TEXT_FLAGS_CHECK_BACKSLASH
Definition: text.h:50
const char * GWEN_XMLNode_GetProperty(const GWEN_XMLNODE *n, const char *name, const char *defaultValue)
Definition: xml.c:239
GWEN_XMLNODE * GWEN_XMLNode_FindFirstTag(const GWEN_XMLNODE *n, const char *tname, const char *pname, const char *pvalue)
Definition: xml.c:776
void GWEN_XMLNode_Dump(const GWEN_XMLNODE *n, int ind)
Definition: xml.c:472
GWEN_XMLNODE * GWEN_XMLNode_FindNextTag(const GWEN_XMLNODE *n, const char *tname, const char *pname, const char *pvalue)
Definition: xml.c:794
const char * GWEN_XMLNode_GetCharValue(const GWEN_XMLNODE *n, const char *name, const char *defValue)
Definition: xml.c:812
int GWEN_XMLNode_ExpandProperties(const GWEN_XMLNODE *n, GWEN_DB_NODE *dbVars)
Definition: xml.c:634
struct GWEN__XMLNODE GWEN_XMLNODE
Definition: xml.h:156