gwenhywfar  5.10.1
param_fns.c
Go to the documentation of this file.
1 /***************************************************************************
2  begin : Wed Sep 17 2014
3  copyright : (C) 2014 by Martin Preuss
4  email : martin@libchipcard.de
5 
6  ***************************************************************************
7  * *
8  * This library is free software; you can redistribute it and/or *
9  * modify it under the terms of the GNU Lesser General Public *
10  * License as published by the Free Software Foundation; either *
11  * version 2.1 of the License, or (at your option) any later version. *
12  * *
13  * This library is distributed in the hope that it will be useful, *
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
16  * Lesser General Public License for more details. *
17  * *
18  * You should have received a copy of the GNU Lesser General Public *
19  * License along with this library; if not, write to the Free Software *
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, *
21  * MA 02111-1307 USA *
22  * *
23  ***************************************************************************/
24 
25 
26 
27 
28 #include <gwenhywfar/text.h>
29 #include <gwenhywfar/buffer.h>
30 
31 
32 
33 
34 
36 {
37  const char *s;
38 
39  assert(param);
40  s=param->currentValue;
41  if (s && *s) {
42  int b;
43  int v;
44 
45  b=sscanf(s, "%i", &v);
46  if (b==1)
47  return v;
48  /* fall through */
49  }
50 
51  s=param->defaultValue;
52  if (s && *s) {
53  int b;
54  int v;
55 
56  b=sscanf(s, "%i", &v);
57  if (b==1)
58  return v;
59  /* fall through */
60  }
61 
62  return 0;
63 }
64 
65 
66 
68 {
69  char numbuf[64];
70 
71  snprintf(numbuf, sizeof(numbuf)-1, "%i", v);
72  numbuf[sizeof(numbuf)-1]=0;
73  GWEN_Param_SetCurrentValue(param, numbuf);
74 }
75 
76 
77 
78 
80 {
81  const char *s;
82 
83  assert(param);
84  s=param->currentValue;
85  if (s && *s) {
86  double v;
87  int b;
88 
89  b=GWEN_Text_StringToDouble(s, &v);
90  if (b>=0)
91  return v;
92  /* fall through */
93  }
94 
95  s=param->defaultValue;
96  if (s && *s) {
97  double v;
98  int b;
99 
100  b=GWEN_Text_StringToDouble(s, &v);
101  if (b>=0)
102  return v;
103  /* fall through */
104  }
105 
106  return 0.0;
107 }
108 
109 
110 
112 {
113  GWEN_BUFFER *tbuf;
114 
115  tbuf=GWEN_Buffer_new(0, 64, 0, 1);
116  GWEN_Text_DoubleToBuffer(v, tbuf);
118  GWEN_Buffer_free(tbuf);
119 }
120 
121 
122 
123 
124 
125 
126 int GWEN_Param_List_GetCurrentValueAsInt(const GWEN_PARAM_LIST *pl, const char *name, int defVal)
127 {
128  GWEN_PARAM *p;
129 
130  p=GWEN_Param_List_GetByName(pl, name);
131  if (p)
133  return defVal;
134 }
135 
136 
137 
138 void GWEN_Param_List_SetCurrentValueAsInt(GWEN_PARAM_LIST *pl, const char *name, int v)
139 {
140  GWEN_PARAM *p;
141 
142  p=GWEN_Param_List_GetByName(pl, name);
143  if (p)
145 }
146 
147 
148 
149 double GWEN_Param_List_GetCurrentValueAsDouble(const GWEN_PARAM_LIST *pl, const char *name, double defVal)
150 {
151  GWEN_PARAM *p;
152 
153  p=GWEN_Param_List_GetByName(pl, name);
154  if (p)
156  return defVal;
157 }
158 
159 
160 
161 void GWEN_Param_List_SetCurrentValueAsDouble(GWEN_PARAM_LIST *pl, const char *name, double v)
162 {
163  GWEN_PARAM *p;
164 
165  p=GWEN_Param_List_GetByName(pl, name);
166  if (p)
168 }
169 
170 
171 
173 {
174  const GWEN_PARAM *p;
175 
176  p=GWEN_Param_List_First(pl);
177  while (p) {
178  GWEN_XMLNODE *n;
179 
181  GWEN_Param_WriteXml(p, n);
182  GWEN_XMLNode_AddChild(xn, n);
183 
184  /* next */
186  }
187 }
188 
189 
190 
192 {
193  GWEN_XMLNODE *n;
194 
195  n=GWEN_XMLNode_FindFirstTag(xn, "param", NULL, NULL);
196  while (n) {
197  GWEN_PARAM *p;
198 
199  p=GWEN_Param_fromXml(n);
200  if (p)
201  GWEN_Param_List_Add(p, pl);
202 
203  n=GWEN_XMLNode_FindNextTag(n, "param", NULL, NULL);
204  }
205 }
206 
207 
208 
209 
211 {
212  const GWEN_PARAM *p;
213 
214  p=GWEN_Param_List_First(pl);
215  while (p) {
216  const char *sName;
217  const char *sValue;
218 
219  sName=GWEN_Param_GetName(p);
220  sValue=GWEN_Param_GetCurrentValue(p);
221  if (sName && *sName && sValue && *sValue) {
222  GWEN_XMLNODE *n;
223 
225  GWEN_XMLNode_SetProperty(n, "name", sName);
226  GWEN_XMLNode_SetCharValue(n, NULL, sValue);
227  GWEN_XMLNode_AddChild(xn, n);
228  }
229 
230  /* next */
232  }
233 }
234 
235 
236 
238 {
239  GWEN_XMLNODE *n;
240 
241  n=GWEN_XMLNode_FindFirstTag(xn, "param", NULL, NULL);
242  while (n) {
243  const char *sName;
244  const char *sValue;
245 
246  sName=GWEN_XMLNode_GetProperty(n, "name", NULL);
247  sValue=GWEN_XMLNode_GetCharValue(n, NULL, NULL);
248 
249  if (sName && *sName) {
250  GWEN_PARAM *p;
251 
252  p=GWEN_Param_List_GetByName(pl, sName);
253  if (p) {
254  GWEN_Param_SetCurrentValue(p, sValue);
255  }
256  else {
257  DBG_WARN(GWEN_LOGDOMAIN, "Param \"%s\" not found, ignoring", sName);
258  }
259  }
260 
261  n=GWEN_XMLNode_FindNextTag(n, "param", NULL, NULL);
262  }
263 }
264 
265 
266 
267 
268 
269 
270 
GWEN_PARAM * GWEN_Param_List_First(const GWEN_PARAM_LIST *l)
char * GWEN_Buffer_GetStart(const GWEN_BUFFER *bf)
Definition: buffer.c:235
void GWEN_Param_List_ReadXml(GWEN_PARAM_LIST *pl, GWEN_XMLNODE *xn)
Definition: param_fns.c:191
const char * GWEN_XMLNode_GetProperty(const GWEN_XMLNODE *n, const char *name, const char *defaultValue)
Definition: xml.c:239
void GWEN_Param_List_WriteValuesToXml(const GWEN_PARAM_LIST *pl, GWEN_XMLNODE *xn)
Definition: param_fns.c:210
int GWEN_Param_List_GetCurrentValueAsInt(const GWEN_PARAM_LIST *pl, const char *name, int defVal)
Definition: param_fns.c:126
GWEN_XMLNODE * GWEN_XMLNode_FindNextTag(const GWEN_XMLNODE *n, const char *tname, const char *pname, const char *pvalue)
Definition: xml.c:794
#define NULL
Definition: binreloc.c:300
void GWEN_XMLNode_SetProperty(GWEN_XMLNODE *n, const char *name, const char *value)
Definition: xml.c:322
struct GWEN_PARAM GWEN_PARAM
Definition: param.h:116
#define DBG_WARN(dbg_logger, format, args...)
Definition: debug.h:125
#define GWEN_LOGDOMAIN
Definition: logger.h:35
void GWEN_XMLNode_SetCharValue(GWEN_XMLNODE *n, const char *name, const char *value)
Definition: xml.c:897
GWEN_PARAM * GWEN_Param_fromXml(GWEN_XMLNODE *p_db)
Definition: param.c:981
void GWEN_Param_List_ReadValuesFromXml(GWEN_PARAM_LIST *pl, GWEN_XMLNODE *xn)
Definition: param_fns.c:237
GWEN_XMLNODE * GWEN_XMLNode_new(GWEN_XMLNODE_TYPE t, const char *data)
Definition: xml.c:144
GWEN_BUFFER * GWEN_Buffer_new(char *buffer, uint32_t size, uint32_t used, int take)
Definition: buffer.c:42
double GWEN_Param_List_GetCurrentValueAsDouble(const GWEN_PARAM_LIST *pl, const char *name, double defVal)
Definition: param_fns.c:149
void GWEN_Param_List_WriteXml(const GWEN_PARAM_LIST *pl, GWEN_XMLNODE *xn)
Definition: param_fns.c:172
void GWEN_Param_SetCurrentValueAsDouble(GWEN_PARAM *param, double v)
Definition: param_fns.c:111
GWEN_XMLNODE * GWEN_XMLNode_FindFirstTag(const GWEN_XMLNODE *n, const char *tname, const char *pname, const char *pvalue)
Definition: xml.c:776
const char * GWEN_XMLNode_GetCharValue(const GWEN_XMLNODE *n, const char *name, const char *defValue)
Definition: xml.c:812
double GWEN_Param_GetCurrentValueAsDouble(const GWEN_PARAM *param)
Definition: param_fns.c:79
GWEN_PARAM * GWEN_Param_List_Next(const GWEN_PARAM *element)
int GWEN_Param_GetCurrentValueAsInt(const GWEN_PARAM *param)
Definition: param_fns.c:35
void GWEN_Param_SetCurrentValueAsInt(GWEN_PARAM *param, int v)
Definition: param_fns.c:67
void GWEN_Buffer_free(GWEN_BUFFER *bf)
Definition: buffer.c:89
struct GWEN_BUFFER GWEN_BUFFER
A dynamically resizeable text buffer.
Definition: buffer.h:38
void GWEN_Param_List_SetCurrentValueAsDouble(GWEN_PARAM_LIST *pl, const char *name, double v)
Definition: param_fns.c:161
void GWEN_Param_List_SetCurrentValueAsInt(GWEN_PARAM_LIST *pl, const char *name, int v)
Definition: param_fns.c:138
const char * GWEN_Param_GetName(const GWEN_PARAM *p_struct)
Definition: param.c:314
void GWEN_Param_SetCurrentValue(GWEN_PARAM *p_struct, const char *p_src)
Definition: param.c:461
int GWEN_Text_StringToDouble(const char *s, double *num)
Definition: text.c:1688
const char * GWEN_Param_GetCurrentValue(const GWEN_PARAM *p_struct)
Definition: param.c:350
void GWEN_Param_List_Add(GWEN_PARAM *element, GWEN_PARAM_LIST *list)
int GWEN_Text_DoubleToBuffer(double num, GWEN_BUFFER *buf)
Definition: text.c:1663
struct GWEN__XMLNODE GWEN_XMLNODE
Definition: xml.h:156
GWEN_PARAM * GWEN_Param_List_GetByName(const GWEN_PARAM_LIST *p_list, const char *p_cmp)
Definition: param.c:989
void GWEN_Param_WriteXml(const GWEN_PARAM *p_struct, GWEN_XMLNODE *p_db)
Definition: param.c:933
void GWEN_XMLNode_AddChild(GWEN_XMLNODE *n, GWEN_XMLNODE *child)
Definition: xml.c:423