gwenhywfar  4.99.8beta
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  char name[256];
57  char *np;
58  char *p;
59  const char *g;
60  int i;
61  int quotes;
62  int esc;
63  int firstval;
64  GWEN_BUFFER *vbuf=NULL;
65 
66  assert(s);
67  name[0]=0;
68 
69  /* check for group definition */
70  g=s;
71  while(*g && (unsigned char)(*g)<33)
72  g++;
73  if (*g=='[') {
74  /* ok, parse group name */
75  GWEN_DB_NODE *grp;
76 
77  s=g;
78  s++;
79  while(*s && (unsigned char)(*s)<33)
80  s++;
81  p=name;
82  i=sizeof(name)-1;
83  while ((unsigned char)(*s)>31 && i && *s!=']' && *s!='#') {
84  *p=*s;
85  p++;
86  s++;
87  } /* while */
88  if (!i) {
89  DBG_ERROR(0, "Groupname is too long (limit is %zd chars)",
90  sizeof(name)-1);
91  return 0;
92  }
93  if (*s!=']') {
94  DBG_ERROR(0, "\"]\" expected");
95  return 0;
96  }
97  *p=0;
98  DBG_VERBOUS(0, "Selecting group \"%s\"",name);
99  grp=GWEN_DB_GetGroup(root, mode, name);
100  if (!grp) {
101  DBG_DEBUG(0, "Group \"%s\" is not available", name);
102  return 0;
103  }
104  return grp;
105  }
106 
107  /* get name */
108  while(*s && (unsigned char)(*s)<33)
109  s++;
110  i=sizeof(name)-1;
111  p=name;
112  while ((unsigned char)(*s)>31 && i-- && *s!='=' && *s!='#') {
113  *p=*s;
114  p++;
115  s++;
116  } /* while */
117  if (!i) {
118  DBG_ERROR(0, "Name is too long (limit is %zd chars)", sizeof(name)-1);
119  return 0;
120  }
121  *p=0;
122  np=name;
123 
124  /* post process name */
125  i=strlen(name)-1;
126  while (i>=0) {
127  if ((unsigned char)(name[i])<33)
128  name[i]=0;
129  else
130  break;
131  i--;
132  }
133 
134  i=strlen(name);
135  if (i>1) {
136  if (name[i-1]=='"' &&
137  name[0]=='"') {
138  name[i-1]=0;
139  np++;
140  }
141  }
142 
143  if ((unsigned char)(*s)<31 || *s=='#') {
144  DBG_VERBOUS(0, "Empty line");
145  return group;
146  }
147 
148  /* get equation mark */
149  if (*s!='=') {
150  DBG_ERROR(0, "\"=\" expected");
151  return 0;
152  }
153  s++;
154 
155  if (strlen(np)==0) {
156  DBG_ERROR(0, "Variable name must not be empty");
157  return 0;
158  }
159 
160  DBG_VERBOUS(0, "Creating variable \"%s\"",np);
161 
162 
163  firstval=1;
164  /* read komma separated values */
165 
166  vbuf=GWEN_Buffer_new(0, 64, 0, 1);
167  while ((unsigned char)(*s)>31) {
168  char *vp;
169 
170  /* skip komma that may occur */
171  while(*s && (unsigned char)(*s)<33)
172  s++;
173  if (*s==0) {
174  break;
175  }
176  if (*s==',') {
177  if (firstval) {
178  DBG_ERROR(0, "Unexpected comma");
179  GWEN_Buffer_free(vbuf);
180  return 0;
181  }
182  s++;
183  }
184  else {
185  if (!firstval) {
186  DBG_ERROR(0, "Comma expected");
187  GWEN_Buffer_free(vbuf);
188  return 0;
189  }
190  }
191 
192  /* get value */
193  while(*s && (unsigned char)(*s)<33)
194  s++;
195  /* copy value */
196  quotes=0;
197  esc=0;
198  i=GWEN_DBIO_OLDDB_MAXVALUE_LEN-1;
199  while ((unsigned char)(*s)>31 && i) {
200  if (esc) {
201  GWEN_Buffer_AppendByte(vbuf, *s);
202  i--;
203  esc=0;
204  }
205  else {
206  if (*s=='\\')
207  esc=1;
208  else if (*s=='"') {
209  quotes++;
210  if (quotes==2) {
211  s++;
212  break;
213  }
214  }
215  else if (*s=='#' && !(quotes&1))
216  break;
217  else if (*s==',' && !(quotes&1))
218  break;
219  else {
220  GWEN_Buffer_AppendByte(vbuf, *s);
221  i--;
222  }
223  }
224  s++;
225  } /* while */
226  if (!i) {
227  DBG_ERROR(0, "Value is too long (limit is %d chars)",
228  GWEN_DBIO_OLDDB_MAXVALUE_LEN-1);
229  GWEN_Buffer_free(vbuf);
230  return 0;
231  }
232  if (quotes&1) {
233  DBG_ERROR(0, "Unbalanced quotation marks");
234  GWEN_Buffer_free(vbuf);
235  return 0;
236  }
237  if (esc)
238  DBG_WARN(0, "Backslash at the end of the line");
239  *p=0;
240  vp=GWEN_Buffer_GetStart(vbuf);
241  /* post process value */
242  if (quotes==0) {
243  i=GWEN_Buffer_GetUsedBytes(vbuf);
244  if (i) {
245  i--;
246  while (i>=0) {
247  if ((unsigned char)(vp[i])<33)
248  vp[i]=0;
249  else
250  break;
251  i--;
252  }
253  }
254  i=strlen(vp);
255  if (i>1) {
256  if (vp[i-1]=='"' &&
257  vp[0]=='"') {
258  vp[i-1]=0;
259  vp++;
260  }
261  }
262  }
263 
264  /* create value, append it */
265  DBG_VERBOUS(0, " Creating value \"%s\"", vp);
266  GWEN_DB_SetCharValue(group, mode, np, vp);
267  GWEN_Buffer_Reset(vbuf);
268 
269  if (*s=='#')
270  break;
271  firstval=0;
272  } /* while (reading values) */
273 
274  GWEN_Buffer_free(vbuf);
275 
276  return group;
277 }
278 
279 
280 
282  GWEN_SYNCIO *sio,
283  GWEN_DB_NODE *data,
284  GWEN_DB_NODE *cfg,
285  uint32_t flags) {
286  GWEN_DB_NODE *curr;
287  int ln;
288  int gerr;
289  GWEN_BUFFER *lbuffer;
290  GWEN_FAST_BUFFER *fb;
291 
292  assert(data);
293 
294  fb=GWEN_FastBuffer_new(512, sio);
295  lbuffer=GWEN_Buffer_new(0, 256, 0, 1);
296  curr=data;
297  ln=1;
298 
299  for (;;) {
300  GWEN_Buffer_Reset(lbuffer);
301  gerr=GWEN_FastBuffer_ReadLineToBuffer(fb, lbuffer);
302  if (gerr) {
303  GWEN_Buffer_free(lbuffer);
304  if (gerr==GWEN_ERROR_EOF && ln) {
306  return 0;
307  }
308  else {
309  DBG_ERROR_ERR(0, gerr);
310  return gerr;
311  }
312  }
313  curr=GWEN_DBIO_OldDb__ParseLine(data, curr, GWEN_Buffer_GetStart(lbuffer), flags);
314  if (!curr) {
315  DBG_ERROR(0, "Error in input stream (line %d)", ln);
316  GWEN_Buffer_free(lbuffer);
318  return GWEN_ERROR_BAD_DATA;
319  }
320  ln++;
321  } /* while */
322 }
323 
324 
325 
327  GWEN_SYNCIO *sio,
328  GWEN_DB_NODE *data,
329  GWEN_DB_NODE *cfg,
330  uint32_t flags) {
331  DBG_ERROR(GWEN_LOGDOMAIN, "Export function not supported");
332  return GWEN_ERROR_GENERIC;
333 }
334 
335 
336 
338  int rv;
339  GWEN_SYNCIO *sio;
340  GWEN_DB_NODE *dbTmp;
341  GWEN_DB_NODE *dbCfg;
342 
345  rv=GWEN_SyncIo_Connect(sio);
346  if (rv<0) {
347  DBG_INFO(GWEN_LOGDOMAIN, "here (%d)", rv);
348  GWEN_SyncIo_free(sio);
350  }
351 
352  dbTmp=GWEN_DB_Group_new("tmp");
353  dbCfg=GWEN_DB_Group_new("cfg");
354  rv=GWEN_DBIO_OldDb_Import(dbio, sio, dbTmp, dbCfg ,GWEN_DB_FLAGS_DEFAULT);
355 
356  GWEN_DB_Group_free(dbCfg);
357  GWEN_DB_Group_free(dbTmp);
358 
360  GWEN_SyncIo_free(sio);
361 
362  if (rv) {
364  }
366 }
367 
368 
369 
371  GWEN_DBIO *dbio;
372 
373  dbio=GWEN_DBIO_new("OldDb", "Imports and exports Old OpenHBCI data");
377  return dbio;
378 }
379 
380 
381 
383  const char *modName,
384  const char *fileName) {
385  GWEN_PLUGIN *pl;
386 
387  pl=GWEN_DBIO_Plugin_new(pm, modName, fileName);
388  assert(pl);
389 
391 
392  return pl;
393 
394 }
395 
396 
397 
struct GWEN_PLUGIN_MANAGER GWEN_PLUGIN_MANAGER
Definition: plugin.h:40
char * GWEN_Buffer_GetStart(const GWEN_BUFFER *bf)
Definition: buffer.c:223
int GWEN_SyncIo_Connect(GWEN_SYNCIO *sio)
Definition: syncio.c:94
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:201
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:369
uint32_t GWEN_Buffer_GetUsedBytes(const GWEN_BUFFER *bf)
Definition: buffer.c:266
#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:337
#define NULL
Definition: binreloc.c:290
int GWEN_FastBuffer_ReadLineToBuffer(GWEN_FAST_BUFFER *fb, GWEN_BUFFER *buf)
Definition: fastbuffer.c:92
#define DBG_VERBOUS(dbg_logger, format, args...)
Definition: debug.h:200
void GWEN_DBIO_SetCheckFileFn(GWEN_DBIO *dbio, GWEN_DBIO_CHECKFILEFN f)
Definition: dbio.c:327
void GWEN_FastBuffer_free(GWEN_FAST_BUFFER *fb)
Definition: fastbuffer.c:45
#define DBG_WARN(dbg_logger, format, args...)
Definition: debug.h:118
#define DBG_ERROR_ERR(dbg_logger, dbg_err)
Definition: debug.h:108
#define GWEN_LOGDOMAIN
Definition: logger.h:35
void GWEN_DBIO_SetImportFn(GWEN_DBIO *dbio, GWEN_DBIO_IMPORTFN f)
Definition: dbio.c:314
GWEN_DBIO * GWEN_DBIO_OldDb_Factory(GWEN_PLUGIN *pl)
Definition: olddb.c:370
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:684
#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:192
#define GWEN_ERROR_GENERIC
Definition: error.h:62
int GWEN_Buffer_AppendByte(GWEN_BUFFER *bf, char c)
Definition: buffer.c:380
GWEN_DB_NODE * GWEN_DB_GetGroup(GWEN_DB_NODE *n, uint32_t flags, const char *path)
Definition: db.c:1260
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_SyncIo_free(GWEN_SYNCIO *sio)
Definition: syncio.c:76
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:281
void GWEN_SyncIo_AddFlags(GWEN_SYNCIO *sio, uint32_t fl)
Definition: syncio.c:169
void GWEN_DBIO_Plugin_SetFactoryFn(GWEN_PLUGIN *pl, GWEN_DBIO_PLUGIN_FACTORYFN f)
Definition: dbio.c:183
#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:105
void GWEN_DBIO_SetExportFn(GWEN_DBIO *dbio, GWEN_DBIO_EXPORTFN f)
Definition: dbio.c:321
GWEN_PLUGIN * dbio_olddb_factory(GWEN_PLUGIN_MANAGER *pm, const char *modName, const char *fileName)
Definition: olddb.c:382
int GWEN_DB_SetCharValue(GWEN_DB_NODE *n, uint32_t flags, const char *path, const char *val)
Definition: db.c:922
GWEN_DBIO_CHECKFILE_RESULT
Definition: dbio.h:79
#define DBG_INFO(dbg_logger, format, args...)
Definition: debug.h:164
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:326
GWEN_DB_NODE * GWEN_DB_Group_new(const char *name)
Definition: db.c:131
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:145