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