gwenhywfar  4.99.8beta
gwen_parser_update.c
Go to the documentation of this file.
1 /***************************************************************************
2  begin : Fri Apr 18 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 
29 int GWEN_Parser__UpdateElementAndChildren(const GWEN_PARSER_ELEMENT *eDefinitions, GWEN_PARSER_ELEMENT *eData, int depth) {
30  int rv;
31  const GWEN_PARSER_ELEMENT *dReal=eDefinitions;
32  const GWEN_PARSER_ELEMENT *eDefChild=NULL;
33  GWEN_PARSER_ELEMENT *eDataChild=NULL;
34  const char *s;
35 
36  /* check choice or direct element */
37  if (GWEN_ParserElement_GetElementType(eDefinitions)==GWEN_ParserElementType_Choice) {
38  /* check choice */
39  dReal=GWEN_Parser__GetChoice(eDefinitions, eData);
40  if (dReal==NULL) {
41  DBG_DEBUG(GWEN_LOGDOMAIN, "No matching choice found");
42  return GWEN_ERROR_BAD_DATA;
43  }
44  }
45  else {
46  /* compare directly */
47  rv=GWEN_Parser__CheckElement(eDefinitions, eData);
48  if (rv<0) {
49  DBG_DEBUG(GWEN_LOGDOMAIN, "here (%d)", rv);
50  return rv;
51  }
52  }
53 
54  /* check children */
55  eDefChild=GWEN_ParserElement_Tree_GetFirstChild(dReal);
56  if (eData)
57  eDataChild=GWEN_ParserElement_Tree_GetFirstChild(eData);
58 
59  rv=GWEN_Parser__UpdateSequence(eDefChild, eDataChild, depth+1);
60  if (rv<0) {
61  DBG_DEBUG(GWEN_LOGDOMAIN, "here (%d)", rv);
62  return rv;
63  }
64 
65  /* element and sub-elements are ok, update data element */
66  s=GWEN_ParserElement_GetDbName(dReal);
67  GWEN_ParserElement_SetDbName(eData, s);
68 
69  return 0;
70 }
71 
72 
73 
74 int GWEN_Parser__UpdateSequence(const GWEN_PARSER_ELEMENT *eDefinitions, GWEN_PARSER_ELEMENT *eData, int depth) {
75  const GWEN_PARSER_ELEMENT *d;
76  GWEN_PARSER_ELEMENT *e;
77  int count=0;
78 
79  d=eDefinitions;
80  e=eData;
81 
82  DBG_VERBOUS(GWEN_LOGDOMAIN, "Entering sequence [%d]", depth);
83 
84  while(d) {
85  int rv;
86 
87  DBG_VERBOUS(GWEN_LOGDOMAIN, "Updateing definition \"%s\" (%s) against \"%s\" (%s) [%d]",
88  d?GWEN_ParserElement_GetName(d):"-?-",
89  GWEN_ParserElementType_toString(GWEN_ParserElement_GetElementType(d)),
90  e?(GWEN_ParserElement_GetName(e)):"-NULL-",
91  e?(GWEN_ParserElementType_toString(GWEN_ParserElement_GetElementType(e))):"-NULL-", depth);
92 
93  rv=GWEN_Parser__UpdateElementAndChildren(d, e, depth+1);
94  if (rv==0) {
95  DBG_VERBOUS(GWEN_LOGDOMAIN, "Matches [%d]", depth);
96  /* does match */
97  if ((GWEN_ParserElement_GetMaxOccurs(d)==-1)|| (count<GWEN_ParserElement_GetMaxOccurs(d))) {
98  /* number is ok, advance to next */
99 
100  DBG_VERBOUS(GWEN_LOGDOMAIN, "Element \"%s\" (%s) is ok (%d) [%d]",
101  e?(GWEN_ParserElement_GetName(e)):"-NULL-",
102  e?(GWEN_ParserElementType_toString(GWEN_ParserElement_GetElementType(e))):"-NULL-",
103  count+1, depth);
104  count++;
105  if (e)
106  e=GWEN_ParserElement_Tree_GetNext(e);
107  }
108  else {
109  DBG_INFO(GWEN_LOGDOMAIN, "Too many counts of this element (%d, maxOccurs=%d)",
110  count, GWEN_ParserElement_GetMaxOccurs(d));
111  return GWEN_ERROR_BAD_DATA;
112  }
113  }
114  else {
115  /* does not match */
116 
117  DBG_VERBOUS(GWEN_LOGDOMAIN, "Does not match [%d]", depth);
118  if (count<GWEN_ParserElement_GetMinOccurs(d)) {
119  /* too few counts */
120  DBG_INFO(GWEN_LOGDOMAIN, "Too few counts of element \"%s\" ([%s], got %d, minOccurs=%d) [%d]",
121  d?GWEN_ParserElement_GetName(d):"-?-",
122  GWEN_ParserElementType_toString(GWEN_ParserElement_GetElementType(d)),
123  count, GWEN_ParserElement_GetMinOccurs(d), depth);
124  return GWEN_ERROR_BAD_DATA;
125  }
126  else {
127  /* ok, advance to next definition */
128  DBG_VERBOUS(GWEN_LOGDOMAIN, "Element \"%s\" (%s) does not match, but that's ok [%d]",
129  e?(GWEN_ParserElement_GetName(e)):"-NULL-",
130  e?(GWEN_ParserElementType_toString(GWEN_ParserElement_GetElementType(e))):"-NULL-", depth);
131  count=0;
132  d=GWEN_ParserElement_Tree_GetNext(d);
133  }
134  }
135  }
136 
137  if (e) {
138  DBG_INFO(GWEN_LOGDOMAIN, "Still data elements but no definition elements");
139  return GWEN_ERROR_BAD_DATA;
140  }
141 
142  DBG_VERBOUS(GWEN_LOGDOMAIN, "Leaving sequence [%d]", depth);
143 
144  return 0;
145 }
146 
147 
148 
149 int GWEN_Parser_UpdateTree(const GWEN_PARSER_ELEMENT_TREE *tDefinitions, GWEN_PARSER_ELEMENT_TREE *tData) {
150  const GWEN_PARSER_ELEMENT *d;
151  GWEN_PARSER_ELEMENT *e;
152  int rv;
153 
154  d=GWEN_ParserElement_Tree_GetFirst(tDefinitions);
155  e=GWEN_ParserElement_Tree_GetFirst(tData);
156  rv=GWEN_Parser__UpdateSequence(d, e, 0);
157  if (rv<0) {
158  DBG_DEBUG(GWEN_LOGDOMAIN, "here (%d)", rv);
159  return rv;
160  }
161 
162  return 0;
163 }
164 
165 
166 
167 
168 
169 
int GWEN_Parser__UpdateElementAndChildren(const GWEN_PARSER_ELEMENT *eDefinitions, GWEN_PARSER_ELEMENT *eData, int depth)
#define NULL
Definition: binreloc.c:290
#define DBG_VERBOUS(dbg_logger, format, args...)
Definition: debug.h:200
int GWEN_Parser_UpdateTree(const GWEN_PARSER_ELEMENT_TREE *tDefinitions, GWEN_PARSER_ELEMENT_TREE *tData)
const GWEN_PARSER_ELEMENT * GWEN_Parser__GetChoice(const GWEN_PARSER_ELEMENT *eDefinitions, const GWEN_PARSER_ELEMENT *eData)
Definition: gwen_parser.c:89
#define GWEN_LOGDOMAIN
Definition: logger.h:35
int GWEN_Parser__UpdateSequence(const GWEN_PARSER_ELEMENT *eDefinitions, GWEN_PARSER_ELEMENT *eData, int depth)
#define GWEN_ERROR_BAD_DATA
Definition: error.h:121
#define DBG_DEBUG(dbg_logger, format, args...)
Definition: debug.h:192
int GWEN_Parser__CheckElement(const GWEN_PARSER_ELEMENT *eDefinitions, const GWEN_PARSER_ELEMENT *eData)
Definition: gwen_parser.c:43
#define DBG_INFO(dbg_logger, format, args...)
Definition: debug.h:164