1 /* Copyright (c) 2000, 2002 MySQL AB
2    Use is subject to license terms
3 
4    This program is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License, version 2.0,
6    as published by the Free Software Foundation.
7 
8    This program is also distributed with certain software (including
9    but not limited to OpenSSL) that is licensed under separate terms,
10    as designated in a particular file or component or in included license
11    documentation.  The authors of MySQL hereby grant you an additional
12    permission to link the program and your derivative works with the
13    separately licensed software that they have included with MySQL.
14 
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License, version 2.0, for more details.
19 
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, write to the Free Software
22    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */
23 
24 #include <stdio.h>
25 #include <string.h>
26 #include <unistd.h>
27 #include <fcntl.h>
28 #include "my_xml.h"
29 
mstr(char * str,const char * src,uint l1,uint l2)30 static void mstr(char *str,const char *src,uint l1,uint l2)
31 {
32   l1 = l1<l2 ? l1 : l2;
33   memcpy(str,src,l1);
34   str[l1]='\0';
35 }
36 
dstr(MY_XML_PARSER * st,const char * attr,uint len)37 static int dstr(MY_XML_PARSER *st,const char *attr, uint len)
38 {
39   char str[1024];
40 
41   mstr(str,attr,len,sizeof(str)-1);
42   printf("VALUE '%s'\n",str);
43   return MY_XML_OK;
44 }
45 
bstr(MY_XML_PARSER * st,const char * attr,uint len)46 static int bstr(MY_XML_PARSER *st,const char *attr, uint len)
47 {
48   char str[1024];
49 
50   mstr(str,attr,len,sizeof(str)-1);
51   printf("ENTER %s\n",str);
52   return MY_XML_OK;
53 }
54 
55 
estr(MY_XML_PARSER * st,const char * attr,uint len)56 static int estr(MY_XML_PARSER *st,const char *attr, uint len)
57 {
58   char str[1024];
59 
60   mstr(str,attr,len,sizeof(str)-1);
61   printf("LEAVE %s\n",str);
62   return MY_XML_OK;
63 }
64 
usage(const char * prog)65 static void usage(const char *prog)
66 {
67   printf("Usage:\n");
68   printf("%s xmlfile\n",prog);
69 }
70 
main(int ac,char ** av)71 int main(int ac, char **av)
72 {
73   char str[1024*64]="";
74   const char *fn;
75   int  f;
76   uint len;
77   MY_XML_PARSER p;
78 
79   if (ac<2)
80   {
81     usage(av[0]);
82     return 0;
83   }
84 
85   fn=av[1]?av[1]:"test.xml";
86   if ((f=open(fn,O_RDONLY))<0)
87   {
88     fprintf(stderr,"Err '%s'\n",fn);
89     return 1;
90   }
91 
92   len=read(f,str,sizeof(str)-1);
93   str[len]='\0';
94 
95   my_xml_parser_create(&p);
96 
97   my_xml_set_enter_handler(&p,bstr);
98   my_xml_set_value_handler(&p,dstr);
99   my_xml_set_leave_handler(&p,estr);
100 
101   if (MY_XML_OK!=(f=my_xml_parse(&p,str,len)))
102   {
103     printf("ERROR at line %d pos %d '%s'\n",
104       my_xml_error_lineno(&p)+1,
105       my_xml_error_pos(&p),
106       my_xml_error_string(&p));
107   }
108 
109   my_xml_parser_free(&p);
110 
111   return 0;
112 }
113