gwenhywfar  4.99.8beta
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  const char *s;
37 
38  assert(param);
39  s=param->currentValue;
40  if (s && *s) {
41  int b;
42  int v;
43 
44  b=sscanf(s, "%i", &v);
45  if (b==1)
46  return v;
47  /* fall through */
48  }
49 
50  s=param->defaultValue;
51  if (s && *s) {
52  int b;
53  int v;
54 
55  b=sscanf(s, "%i", &v);
56  if (b==1)
57  return v;
58  /* fall through */
59  }
60 
61  return 0;
62 }
63 
64 
65 
67  char numbuf[64];
68 
69  snprintf(numbuf, sizeof(numbuf)-1, "%i", v);
70  numbuf[sizeof(numbuf)-1]=0;
71  GWEN_Param_SetCurrentValue(param, numbuf);
72 }
73 
74 
75 
76 
78  const char *s;
79 
80  assert(param);
81  s=param->currentValue;
82  if (s && *s) {
83  double v;
84  int b;
85 
86  b=GWEN_Text_StringToDouble(s, &v);
87  if (b>=0)
88  return v;
89  /* fall through */
90  }
91 
92  s=param->defaultValue;
93  if (s && *s) {
94  double v;
95  int b;
96 
97  b=GWEN_Text_StringToDouble(s, &v);
98  if (b>=0)
99  return v;
100  /* fall through */
101  }
102 
103  return 0.0;
104 }
105 
106 
107 
109  GWEN_BUFFER *tbuf;
110 
111  tbuf=GWEN_Buffer_new(0, 64, 0, 1);
112  GWEN_Text_DoubleToBuffer(v, tbuf);
114  GWEN_Buffer_free(tbuf);
115 }
116 
117 
118 
119 
120 
121 
122 int GWEN_Param_List_GetCurrentValueAsInt(const GWEN_PARAM_LIST *pl, const char *name, int defVal) {
123  GWEN_PARAM *p;
124 
125  p=GWEN_Param_List_GetByName(pl, name);
126  if (p)
128  return defVal;
129 }
130 
131 
132 
133 void GWEN_Param_List_SetCurrentValueAsInt(GWEN_PARAM_LIST *pl, const char *name, int v) {
134  GWEN_PARAM *p;
135 
136  p=GWEN_Param_List_GetByName(pl, name);
137  if (p)
139 }
140 
141 
142 
143 double GWEN_Param_List_GetCurrentValueAsDouble(const GWEN_PARAM_LIST *pl, const char *name, double defVal) {
144  GWEN_PARAM *p;
145 
146  p=GWEN_Param_List_GetByName(pl, name);
147  if (p)
149  return defVal;
150 }
151 
152 
153 
154 void GWEN_Param_List_SetCurrentValueAsDouble(GWEN_PARAM_LIST *pl, const char *name, double v) {
155  GWEN_PARAM *p;
156 
157  p=GWEN_Param_List_GetByName(pl, name);
158  if (p)
160 }
161 
162 
163 
165  const GWEN_PARAM *p;
166 
167  p=GWEN_Param_List_First(pl);
168  while(p) {
169  GWEN_XMLNODE *n;
170 
172  GWEN_Param_WriteXml(p, n);
173  GWEN_XMLNode_AddChild(xn, n);
174 
175  /* next */
177  }
178 }
179 
180 
181 
183  GWEN_XMLNODE *n;
184 
185  n=GWEN_XMLNode_FindFirstTag(xn, "param", NULL, NULL);
186  while(n) {
187  GWEN_PARAM *p;
188 
189  p=GWEN_Param_fromXml(n);
190  if (p)
191  GWEN_Param_List_Add(p, pl);
192 
193  n=GWEN_XMLNode_FindNextTag(n, "param", NULL, NULL);
194  }
195 }
196 
197 
198 
199 
201  const GWEN_PARAM *p;
202 
203  p=GWEN_Param_List_First(pl);
204  while(p) {
205  const char *sName;
206  const char *sValue;
207 
208  sName=GWEN_Param_GetName(p);
209  sValue=GWEN_Param_GetCurrentValue(p);
210  if (sName && *sName && sValue && *sValue) {
211  GWEN_XMLNODE *n;
212 
214  GWEN_XMLNode_SetProperty(n, "name", sName);
215  GWEN_XMLNode_SetCharValue(n, NULL, sValue);
216  GWEN_XMLNode_AddChild(xn, n);
217  }
218 
219  /* next */
221  }
222 }
223 
224 
225 
227  GWEN_XMLNODE *n;
228 
229  n=GWEN_XMLNode_FindFirstTag(xn, "param", NULL, NULL);
230  while(n) {
231  const char *sName;
232  const char *sValue;
233 
234  sName=GWEN_XMLNode_GetProperty(n, "name", NULL);
235  sValue=GWEN_XMLNode_GetCharValue(n, NULL, NULL);
236 
237  if (sName && *sName) {
238  GWEN_PARAM *p;
239 
240  p=GWEN_Param_List_GetByName(pl, sName);
241  if (p) {
242  GWEN_Param_SetCurrentValue(p, sValue);
243  }
244  else {
245  DBG_WARN(GWEN_LOGDOMAIN, "Param \"%s\" not found, ignoring", sName);
246  }
247  }
248 
249  n=GWEN_XMLNode_FindNextTag(n, "param", NULL, NULL);
250  }
251 }
252 
253 
254 
255 
256 
257 
258 
GWEN_PARAM * GWEN_Param_List_First(const GWEN_PARAM_LIST *l)
char * GWEN_Buffer_GetStart(const GWEN_BUFFER *bf)
Definition: buffer.c:223
void GWEN_Param_List_ReadXml(GWEN_PARAM_LIST *pl, GWEN_XMLNODE *xn)
Definition: param_fns.c:182
const char * GWEN_XMLNode_GetProperty(const GWEN_XMLNODE *n, const char *name, const char *defaultValue)
Definition: xml.c:228
void GWEN_Param_List_WriteValuesToXml(const GWEN_PARAM_LIST *pl, GWEN_XMLNODE *xn)
Definition: param_fns.c:200
int GWEN_Param_List_GetCurrentValueAsInt(const GWEN_PARAM_LIST *pl, const char *name, int defVal)
Definition: param_fns.c:122
GWEN_XMLNODE * GWEN_XMLNode_FindNextTag(const GWEN_XMLNODE *n, const char *tname, const char *pname, const char *pvalue)
Definition: xml.c:712
#define NULL
Definition: binreloc.c:290
void GWEN_XMLNode_SetProperty(GWEN_XMLNODE *n, const char *name, const char *value)
Definition: xml.c:308
struct GWEN_PARAM GWEN_PARAM
Definition: param.h:34
#define DBG_WARN(dbg_logger, format, args...)
Definition: debug.h:118
#define GWEN_LOGDOMAIN
Definition: logger.h:35
void GWEN_XMLNode_SetCharValue(GWEN_XMLNODE *n, const char *name, const char *value)
Definition: xml.c:812
GWEN_PARAM * GWEN_Param_fromXml(GWEN_XMLNODE *p_db)
Definition: param.c:779
void GWEN_Param_List_ReadValuesFromXml(GWEN_PARAM_LIST *pl, GWEN_XMLNODE *xn)
Definition: param_fns.c:226
GWEN_XMLNODE * GWEN_XMLNode_new(GWEN_XMLNODE_TYPE t, const char *data)
Definition: xml.c:137
GWEN_BUFFER * GWEN_Buffer_new(char *buffer, uint32_t size, uint32_t used, int take)
Definition: buffer.c:38
double GWEN_Param_List_GetCurrentValueAsDouble(const GWEN_PARAM_LIST *pl, const char *name, double defVal)
Definition: param_fns.c:143
void GWEN_Param_List_WriteXml(const GWEN_PARAM_LIST *pl, GWEN_XMLNODE *xn)
Definition: param_fns.c:164
void GWEN_Param_SetCurrentValueAsDouble(GWEN_PARAM *param, double v)
Definition: param_fns.c:108
GWEN_XMLNODE * GWEN_XMLNode_FindFirstTag(const GWEN_XMLNODE *n, const char *tname, const char *pname, const char *pvalue)
Definition: xml.c:695
const char * GWEN_XMLNode_GetCharValue(const GWEN_XMLNODE *n, const char *name, const char *defValue)
Definition: xml.c:729
double GWEN_Param_GetCurrentValueAsDouble(const GWEN_PARAM *param)
Definition: param_fns.c:77
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:66
void GWEN_Buffer_free(GWEN_BUFFER *bf)
Definition: buffer.c:83
struct GWEN_BUFFER GWEN_BUFFER
A dynamically resizeable text buffer.
Definition: buffer.h:41
void GWEN_Param_List_SetCurrentValueAsDouble(GWEN_PARAM_LIST *pl, const char *name, double v)
Definition: param_fns.c:154
void GWEN_Param_List_SetCurrentValueAsInt(GWEN_PARAM_LIST *pl, const char *name, int v)
Definition: param_fns.c:133
const char * GWEN_Param_GetName(const GWEN_PARAM *p_struct)
Definition: param.c:285
void GWEN_Param_SetCurrentValue(GWEN_PARAM *p_struct, const char *p_src)
Definition: param.c:409
int GWEN_Text_StringToDouble(const char *s, double *num)
Definition: text.c:1630
const char * GWEN_Param_GetCurrentValue(const GWEN_PARAM *p_struct)
Definition: param.c:315
void GWEN_Param_List_Add(GWEN_PARAM *element, GWEN_PARAM_LIST *list)
int GWEN_Text_DoubleToBuffer(double num, GWEN_BUFFER *buf)
Definition: text.c:1606
struct GWEN__XMLNODE GWEN_XMLNODE
Definition: xml.h:148
GWEN_PARAM * GWEN_Param_List_GetByName(const GWEN_PARAM_LIST *p_list, const char *p_cmp)
Definition: param.c:786
void GWEN_Param_WriteXml(const GWEN_PARAM *p_struct, GWEN_XMLNODE *p_db)
Definition: param.c:738
void GWEN_XMLNode_AddChild(GWEN_XMLNODE *n, GWEN_XMLNODE *child)
Definition: xml.c:398