gwenhywfar  4.99.15beta
olddb.c
Go to the documentation of this file.
1 /***************************************************************************
2  $RCSfile$
3  -------------------
4  cvs : $Id$
5  begin : Thu Oct 30 2003
6  copyright : (C) 2003 by Martin Preuss
7  email : martin@libchipcard.de
8 
9  ***************************************************************************
10  * *
11  * This library is free software; you can redistribute it and/or *
12  * modify it under the terms of the GNU Lesser General Public *
13  * License as published by the Free Software Foundation; either *
14  * version 2.1 of the License, or (at your option) any later version. *
15  * *
16  * This library is distributed in the hope that it will be useful, *
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
19  * Lesser General Public License for more details. *
20  * *
21  * You should have received a copy of the GNU Lesser General Public *
22  * License along with this library; if not, write to the Free Software *
23  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, *
24  * MA 02111-1307 USA *
25  * *
26  ***************************************************************************/
27 
28 
29 #ifdef HAVE_CONFIG_H
30 # include <config.h>
31 #endif
32 
33 #include "olddb_p.h"
34 #include <gwenhywfar/text.h>
35 #include <gwenhywfar/debug.h>
36 #include <gwenhywfar/stringlist.h>
37 #include <gwenhywfar/dbio_be.h>
38 #include <gwenhywfar/syncio_file.h>
39 #include <gwenhywfar/fastbuffer.h>
40 
41 #include <stdlib.h>
42 #include <string.h>
43 #include <assert.h>
44 #include <sys/types.h>
45 #include <sys/stat.h>
46 #include <fcntl.h>
47 #include <string.h>
48 #include <errno.h>
49 
50 
51 
53  GWEN_DB_NODE *group,
54  const char *s,
55  uint32_t mode)
56 {
57  char name[256];
58  char *np;
59  char *p;
60  const char *g;
61  int i;
62  int quotes;
63  int esc;
64  int firstval;
65  GWEN_BUFFER *vbuf=NULL;
66 
67  assert(s);
68  name[0]=0;
69 
70  /* check for group definition */
71  g=s;
72  while (*g && (unsigned char)(*g)<33)
73  g++;
74  if (*g=='[') {
75  /* ok, parse group name */
76  GWEN_DB_NODE *grp;
77 
78  s=g;
79  s++;
80  while (*s && (unsigned char)(*s)<33)
81  s++;
82  p=name;
83  i=sizeof(name)-1;
84  while ((unsigned char)(*s)>31 && i && *s!=']' && *s!='#') {
85  *p=*s;
86  p++;
87  s++;
88  } /* while */
89  if (!i) {
90  DBG_ERROR(0, "Groupname is too long (limit is %zd chars)",
91  sizeof(name)-1);
92  return 0;
93  }
94  if (*s!=']') {
95  DBG_ERROR(0, "\"]\" expected");
96  return 0;
97  }
98  *p=0;
99  DBG_VERBOUS(0, "Selecting group \"%s\"", name);
100  grp=GWEN_DB_GetGroup(root, mode, name);
101  if (!grp) {
102  DBG_DEBUG(0, "Group \"%s\" is not available", name);
103  return 0;
104  }
105  return grp;
106  }
107 
108  /* get name */
109  while (*s && (unsigned char)(*s)<33)
110  s++;
111  i=sizeof(name)-1;
112  p=name;
113  while ((unsigned char)(*s)>31 && i-- && *s!='=' && *s!='#') {
114  *p=*s;
115  p++;
116  s++;
117  } /* while */
118  if (!i) {
119  DBG_ERROR(0, "Name is too long (limit is %zd chars)", sizeof(name)-1);
120  return 0;
121  }
122  *p=0;
123  np=name;
124 
125  /* post process name */
126  i=strlen(name)-1;
127  while (i>=0) {
128  if ((unsigned char)(name[i])<33)
129  name[i]=0;
130  else
131  break;
132  i--;
133  }
134 
135  i=strlen(name);
136  if (i>1) {
137  if (name[i-1]=='"' &&
138  name[0]=='"') {
139  name[i-1]=0;
140  np++;
141  }
142  }
143 
144  if ((unsigned char)(*s)<31 || *s=='#') {
145  DBG_VERBOUS(0, "Empty line");
146  return group;
147  }
148 
149  /* get equation mark */
150  if (*s!='=') {
151  DBG_ERROR(0, "\"=\" expected");
152  return 0;
153  }
154  s++;
155 
156  if (strlen(np)==0) {
157  DBG_ERROR(0, "Variable name must not be empty");
158  return 0;
159  }
160 
161  DBG_VERBOUS(0, "Creating variable \"%s\"", np);
162 
163 
164  firstval=1;
165  /* read komma separated values */
166 
167  vbuf=GWEN_Buffer_new(0, 64, 0, 1);
168  while ((unsigned char)(*s)>31) {
169  char *vp;
170 
171  /* skip komma that may occur */
172  while (*s && (unsigned char)(*s)<33)
173  s++;
174  if (*s==0) {
175  break;
176  }
177  if (*s==',') {
178  if (firstval) {
179  DBG_ERROR(0, "Unexpected comma");
180  GWEN_Buffer_free(vbuf);
181  return 0;
182  }
183  s++;
184  }
185  else {
186  if (!firstval) {
187  DBG_ERROR(0, "Comma expected");
188  GWEN_Buffer_free(vbuf);
189  return 0;
190  }
191  }
192 
193  /* get value */
194  while (*s && (unsigned char)(*s)<33)
195  s++;
196  /* copy value */
197  quotes=0;
198  esc=0;
199  i=GWEN_DBIO_OLDDB_MAXVALUE_LEN-1;
200  while ((unsigned char)(*s)>31 && i) {
201  if (esc) {
202  GWEN_Buffer_AppendByte(vbuf, *s);
203  i--;
204  esc=0;
205  }
206  else {
207  if (*s=='\\')
208  esc=1;
209  else if (*s=='"') {
210  quotes++;
211  if (quotes==2) {
212  s++;
213  break;
214  }
215  }
216  else if (*s=='#' && !(quotes&1))
217  break;
218  else if (*s==',' && !(quotes&1))
219  break;
220  else {
221  GWEN_Buffer_AppendByte(vbuf, *s);
222  i--;
223  }
224  }
225  s++;
226  } /* while */
227  if (!i) {
228  DBG_ERROR(0, "Value is too long (limit is %d chars)",
229  GWEN_DBIO_OLDDB_MAXVALUE_LEN-1);
230  GWEN_Buffer_free(vbuf);
231  return 0;
232  }
233  if (quotes&1) {
234  DBG_ERROR(0, "Unbalanced quotation marks");
235  GWEN_Buffer_free(vbuf);
236  return 0;
237  }
238  if (esc)
239  DBG_WARN(0, "Backslash at the end of the line");
240  *p=0;
241  vp=GWEN_Buffer_GetStart(vbuf);
242  /* post process value */
243  if (quotes==0) {
244  i=GWEN_Buffer_GetUsedBytes(vbuf);
245  if (i) {
246  i--;
247  while (i>=0) {
248  if ((unsigned char)(vp[i])<33)
249  vp[i]=0;
250  else
251  break;
252  i--;
253  }
254  }
255  i=strlen(vp);
256  if (i>1) {
257  if (vp[i-1]=='"' &&
258  vp[0]=='"') {
259  vp[i-1]=0;
260  vp++;
261  }
262  }
263  }
264 
265  /* create value, append it */
266  DBG_VERBOUS(0, " Creating value \"%s\"", vp);
267  GWEN_DB_SetCharValue(group, mode, np, vp);
268  GWEN_Buffer_Reset(vbuf);
269 
270  if (*s=='#')
271  break;
272  firstval=0;
273  } /* while (reading values) */
274 
275  GWEN_Buffer_free(vbuf);
276 
277  return group;
278 }
279 
280 
281 
283  GWEN_SYNCIO *sio,
284  GWEN_DB_NODE *data,
285  GWEN_DB_NODE *cfg,
286  uint32_t flags)
287 {
288  GWEN_DB_NODE *curr;
289  int ln;
290  int gerr;
291  GWEN_BUFFER *lbuffer;
292  GWEN_FAST_BUFFER *fb;
293 
294  assert(data);
295 
296  fb=GWEN_FastBuffer_new(512, sio);
297  lbuffer=GWEN_Buffer_new(0, 256, 0, 1);
298  curr=data;
299  ln=1;
300 
301  for (;;) {
302  GWEN_Buffer_Reset(lbuffer);
303  gerr=GWEN_FastBuffer_ReadLineToBuffer(fb, lbuffer);
304  if (gerr) {
305  GWEN_Buffer_free(lbuffer);
306  if (gerr==GWEN_ERROR_EOF && ln) {
308  return 0;
309  }
310  else {
311  DBG_ERROR_ERR(0, gerr);
312  return gerr;
313  }
314  }
315  curr=GWEN_DBIO_OldDb__ParseLine(data, curr, GWEN_Buffer_GetStart(lbuffer), flags);
316  if (!curr) {
317  DBG_ERROR(0, "Error in input stream (line %d)", ln);
318  GWEN_Buffer_free(lbuffer);
320  return GWEN_ERROR_BAD_DATA;
321  }
322  ln++;
323  } /* while */
324 }
325 
326 
327 
329  GWEN_SYNCIO *sio,
330  GWEN_DB_NODE *data,
331  GWEN_DB_NODE *cfg,
332  uint32_t flags)
333 {
334  DBG_ERROR(GWEN_LOGDOMAIN, "Export function not supported");
335  return GWEN_ERROR_GENERIC;
336 }
337 
338 
339 
341 {
342  int rv;
343  GWEN_SYNCIO *sio;
344  GWEN_DB_NODE *dbTmp;
345  GWEN_DB_NODE *dbCfg;
346 
349  rv=GWEN_SyncIo_Connect(sio);
350  if (rv<0) {
351  DBG_INFO(GWEN_LOGDOMAIN, "here (%d)", rv);
352  GWEN_SyncIo_free(sio);
354  }
355 
356  dbTmp=GWEN_DB_Group_new("tmp");
357  dbCfg=GWEN_DB_Group_new("cfg");
358  rv=GWEN_DBIO_OldDb_Import(dbio, sio, dbTmp, dbCfg, GWEN_DB_FLAGS_DEFAULT);
359 
360  GWEN_DB_Group_free(dbCfg);
361  GWEN_DB_Group_free(dbTmp);
362 
364  GWEN_SyncIo_free(sio);
365 
366  if (rv) {
368  }
370 }
371 
372 
373 
375 {
376  GWEN_DBIO *dbio;
377 
378  dbio=GWEN_DBIO_new("OldDb", "Imports and exports Old OpenHBCI data");
382  return dbio;
383 }
384 
385 
386 
388  const char *modName,
389  const char *fileName)
390 {
391  GWEN_PLUGIN *pl;
392 
393  pl=GWEN_DBIO_Plugin_new(pm, modName, fileName);
394  assert(pl);
395 
397 
398  return pl;
399 
400 }
401 
402 
403 
struct GWEN_PLUGIN_MANAGER GWEN_PLUGIN_MANAGER
Definition: plugin.h:40
char * GWEN_Buffer_GetStart(const GWEN_BUFFER *bf)
Definition: buffer.c:235
int GWEN_SyncIo_Connect(GWEN_SYNCIO *sio)
Definition: syncio.c:97
struct GWEN_DB_NODE GWEN_DB_NODE
Definition: db.h:228
struct GWEN_PLUGIN GWEN_PLUGIN
Definition: plugin.h:39
GWEN_DBIO * GWEN_DBIO_new(const char *name, const char *descr)
Definition: dbio.c:207
GWEN_DB_NODE * GWEN_DBIO_OldDb__ParseLine(GWEN_DB_NODE *root, GWEN_DB_NODE *group, const char *s, uint32_t mode)
Definition: olddb.c:52
void GWEN_DB_Group_free(GWEN_DB_NODE *n)
Definition: db.c:408
uint32_t GWEN_Buffer_GetUsedBytes(const GWEN_BUFFER *bf)
Definition: buffer.c:282
#define GWEN_SYNCIO_FILE_FLAGS_READ
Definition: syncio_file.h:53
GWEN_DBIO_CHECKFILE_RESULT GWEN_DBIO_OldDb_CheckFile(GWEN_DBIO *dbio, const char *fname)
Definition: olddb.c:340
#define NULL
Definition: binreloc.c:297
int GWEN_FastBuffer_ReadLineToBuffer(GWEN_FAST_BUFFER *fb, GWEN_BUFFER *buf)
Definition: fastbuffer.c:95
#define DBG_VERBOUS(dbg_logger, format, args...)
Definition: debug.h:216
void GWEN_DBIO_SetCheckFileFn(GWEN_DBIO *dbio, GWEN_DBIO_CHECKFILEFN f)
Definition: dbio.c:344
void GWEN_FastBuffer_free(GWEN_FAST_BUFFER *fb)
Definition: fastbuffer.c:46
#define DBG_WARN(dbg_logger, format, args...)
Definition: debug.h:123
#define DBG_ERROR_ERR(dbg_logger, dbg_err)
Definition: debug.h:113
#define GWEN_LOGDOMAIN
Definition: logger.h:35
void GWEN_DBIO_SetImportFn(GWEN_DBIO *dbio, GWEN_DBIO_IMPORTFN f)
Definition: dbio.c:329
GWEN_DBIO * GWEN_DBIO_OldDb_Factory(GWEN_PLUGIN *pl)
Definition: olddb.c:374
GWEN_BUFFER * GWEN_Buffer_new(char *buffer, uint32_t size, uint32_t used, int take)
Definition: buffer.c:38
void GWEN_Buffer_Reset(GWEN_BUFFER *bf)
Definition: buffer.c:719
#define GWEN_ERROR_BAD_DATA
Definition: error.h:121
struct GWEN_SYNCIO GWEN_SYNCIO
Definition: syncio.h:40
#define DBG_DEBUG(dbg_logger, format, args...)
Definition: debug.h:208
#define GWEN_ERROR_GENERIC
Definition: error.h:62
int GWEN_Buffer_AppendByte(GWEN_BUFFER *bf, char c)
Definition: buffer.c:399
GWEN_DB_NODE * GWEN_DB_GetGroup(GWEN_DB_NODE *n, uint32_t flags, const char *path)
Definition: db.c:1368
void GWEN_Buffer_free(GWEN_BUFFER *bf)
Definition: buffer.c:85
struct GWEN_BUFFER GWEN_BUFFER
A dynamically resizeable text buffer.
Definition: buffer.h:41
void GWEN_SyncIo_free(GWEN_SYNCIO *sio)
Definition: syncio.c:78
int GWEN_DBIO_OldDb_Import(GWEN_DBIO *dbio, GWEN_SYNCIO *sio, GWEN_DB_NODE *data, GWEN_DB_NODE *cfg, uint32_t flags)
Definition: olddb.c:282
void GWEN_SyncIo_AddFlags(GWEN_SYNCIO *sio, uint32_t fl)
Definition: syncio.c:179
void GWEN_DBIO_Plugin_SetFactoryFn(GWEN_PLUGIN *pl, GWEN_DBIO_PLUGIN_FACTORYFN f)
Definition: dbio.c:188
#define DBG_ERROR(dbg_logger, format, args...)
Definition: debug.h:97
#define GWEN_ERROR_EOF
Definition: error.h:96
struct GWEN_DBIO GWEN_DBIO
Definition: dbio.h:43
int GWEN_SyncIo_Disconnect(GWEN_SYNCIO *sio)
Definition: syncio.c:109
void GWEN_DBIO_SetExportFn(GWEN_DBIO *dbio, GWEN_DBIO_EXPORTFN f)
Definition: dbio.c:337
GWEN_PLUGIN * dbio_olddb_factory(GWEN_PLUGIN_MANAGER *pm, const char *modName, const char *fileName)
Definition: olddb.c:387
int GWEN_DB_SetCharValue(GWEN_DB_NODE *n, uint32_t flags, const char *path, const char *val)
Definition: db.c:984
GWEN_DBIO_CHECKFILE_RESULT
Definition: dbio.h:79
#define DBG_INFO(dbg_logger, format, args...)
Definition: debug.h:177
int GWEN_DBIO_OldDb_Export(GWEN_DBIO *dbio, GWEN_SYNCIO *sio, GWEN_DB_NODE *data, GWEN_DB_NODE *cfg, uint32_t flags)
Definition: olddb.c:328
GWEN_DB_NODE * GWEN_DB_Group_new(const char *name)
Definition: db.c:160
GWENHYWFAR_API GWEN_SYNCIO * GWEN_SyncIo_File_new(const char *path, GWEN_SYNCIO_FILE_CREATIONMODE cm)
GWEN_FAST_BUFFER * GWEN_FastBuffer_new(uint32_t bsize, GWEN_SYNCIO *io)
Definition: fastbuffer.c:27
#define GWEN_DB_FLAGS_DEFAULT
Definition: db.h:168
GWEN_PLUGIN * GWEN_DBIO_Plugin_new(GWEN_PLUGIN_MANAGER *pm, const char *name, const char *fileName)
Definition: dbio.c:147