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