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