Skip to content

Commit eb87b8a

Browse files
author
bar@bar.mysql.r18.ru
committed
XML parser
1 parent c7ba7c9 commit eb87b8a

File tree

6 files changed

+522
-2
lines changed

6 files changed

+522
-2
lines changed

include/Makefile.am

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
# MA 02111-1307, USA
1717

1818
BUILT_SOURCES = mysql_version.h m_ctype.h my_config.h
19-
pkginclude_HEADERS = dbug.h m_string.h my_sys.h my_list.h \
19+
pkginclude_HEADERS = dbug.h m_string.h my_sys.h my_list.h my_xml.h \
2020
mysql.h mysql_com.h mysqld_error.h mysql_embed.h \
2121
my_semaphore.h my_pthread.h my_no_pthread.h raid.h \
2222
errmsg.h my_global.h my_net.h my_alloc.h \

include/my_xml.h

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/* Copyright (C) 2000 MySQL AB
2+
3+
This program is free software; you can redistribute it and/or modify
4+
it under the terms of the GNU General Public License as published by
5+
the Free Software Foundation; either version 2 of the License, or
6+
(at your option) any later version.
7+
8+
This program is distributed in the hope that it will be useful,
9+
but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+
GNU General Public License for more details.
12+
13+
You should have received a copy of the GNU General Public License
14+
along with this program; if not, write to the Free Software
15+
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
16+
17+
18+
#ifndef _my_xml_h
19+
#define _my_xml_h
20+
21+
#ifdef __cplusplus
22+
extern "C" {
23+
#endif
24+
25+
26+
#define MY_XML_OK 0
27+
#define MY_XML_ERROR 1
28+
29+
typedef struct xml_stack_st
30+
{
31+
char errstr[128];
32+
char attr[128];
33+
char *attrend;
34+
const char *beg;
35+
const char *cur;
36+
const char *end;
37+
int (*enter)(struct xml_stack_st *st,const char *val, uint len);
38+
int (*value)(struct xml_stack_st *st,const char *val, uint len);
39+
int (*leave)(struct xml_stack_st *st,const char *val, uint len);
40+
} MY_XML_PARSER;
41+
42+
void my_xml_parser_create(MY_XML_PARSER *st);
43+
void my_xml_parser_free(MY_XML_PARSER *st);
44+
int my_xml_parse(MY_XML_PARSER *st,const char *str, uint len);
45+
46+
void my_xml_set_value_handler(MY_XML_PARSER *st, int (*)(MY_XML_PARSER *, const char *, uint len));
47+
void my_xml_set_enter_handler(MY_XML_PARSER *st, int (*)(MY_XML_PARSER *, const char *, uint len));
48+
void my_xml_set_leave_handler(MY_XML_PARSER *st, int (*)(MY_XML_PARSER *, const char *, uint len));
49+
50+
uint my_xml_error_pos(MY_XML_PARSER *st);
51+
uint my_xml_error_lineno(MY_XML_PARSER *st);
52+
53+
const char *my_xml_error_string(MY_XML_PARSER *st);
54+
55+
#ifdef __cplusplus
56+
}
57+
#endif
58+
59+
#endif /* _my_xml_h */

mysys/Makefile.am

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ libmysys_a_SOURCES = my_init.c my_getwd.c mf_getdate.c\
5050
my_getopt.c my_mkdir.c \
5151
default.c my_compress.c checksum.c raid.cc \
5252
my_net.c my_semaphore.c my_port.c \
53-
my_vsnprintf.c charset.c my_bitmap.c my_bit.c md5.c \
53+
my_vsnprintf.c charset.c xml.c my_bitmap.c my_bit.c md5.c \
5454
my_gethostbyname.c rijndael.c my_aes.c sha1.c \
5555
my_handler.c
5656
EXTRA_DIST = thr_alarm.c thr_lock.c my_pthread.c my_thr_init.c \
@@ -104,6 +104,9 @@ test_dir: test_dir.c $(LIBRARIES)
104104
test_charset$(EXEEXT): test_charset.c $(LIBRARIES)
105105
$(LINK) $(FLAGS) -DMAIN $(srcdir)/test_charset.c $(LDADD) $(LIBS)
106106

107+
test_xml$(EXEEXT): test_xml.c $(LIBRARIES)
108+
$(LINK) $(FLAGS) -DMAIN $(srcdir)/test_xml.c $(LDADD) $(LIBS)
109+
107110
charset2html$(EXEEXT): charset2html.c $(LIBRARIES)
108111
$(LINK) $(FLAGS) -DMAIN $(srcdir)/charset2html.c $(LDADD) $(LIBS)
109112

mysys/test_xml

34.7 KB
Binary file not shown.

mysys/test_xml.c

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
#include <stdio.h>
2+
#include <string.h>
3+
#include <unistd.h>
4+
#include <fcntl.h>
5+
#include "my_xml.h"
6+
7+
static void mstr(char *str,const char *src,uint l1,uint l2)
8+
{
9+
l1 = l1<l2 ? l1 : l2;
10+
memcpy(str,src,l1);
11+
str[l1]='\0';
12+
}
13+
14+
static int dstr(MY_XML_PARSER *st,const char *attr, uint len)
15+
{
16+
char str[1024];
17+
18+
mstr(str,attr,len,sizeof(str)-1);
19+
printf("VALUE '%s'\n",str);
20+
return MY_XML_OK;
21+
}
22+
23+
static int bstr(MY_XML_PARSER *st,const char *attr, uint len)
24+
{
25+
char str[1024];
26+
27+
mstr(str,attr,len,sizeof(str)-1);
28+
printf("ENTER %s\n",str);
29+
return MY_XML_OK;
30+
}
31+
32+
33+
static int estr(MY_XML_PARSER *st,const char *attr, uint len)
34+
{
35+
char str[1024];
36+
37+
mstr(str,attr,len,sizeof(str)-1);
38+
printf("LEAVE %s\n",str);
39+
return MY_XML_OK;
40+
}
41+
42+
static void usage(const char *prog)
43+
{
44+
printf("Usage:\n");
45+
printf("%s xmlfile\n",prog);
46+
}
47+
48+
int main(int ac, char **av)
49+
{
50+
char str[1024*64]="";
51+
const char *fn;
52+
int f;
53+
uint len;
54+
MY_XML_PARSER p;
55+
56+
if (ac<2)
57+
{
58+
usage(av[0]);
59+
return 0;
60+
}
61+
62+
fn=av[1]?av[1]:"test.xml";
63+
if ((f=open(fn,O_RDONLY))<0)
64+
{
65+
fprintf(stderr,"Err '%s'\n",fn);
66+
return 1;
67+
}
68+
69+
len=read(f,str,sizeof(str)-1);
70+
str[len]='\0';
71+
72+
my_xml_parser_create(&p);
73+
74+
my_xml_set_enter_handler(&p,bstr);
75+
my_xml_set_value_handler(&p,dstr);
76+
my_xml_set_leave_handler(&p,estr);
77+
78+
if (MY_XML_OK!=(f=my_xml_parse(&p,str,len)))
79+
{
80+
printf("ERROR at line %d pos %d '%s'\n",
81+
my_xml_error_lineno(&p)+1,
82+
my_xml_error_pos(&p),
83+
my_xml_error_string(&p));
84+
}
85+
86+
my_xml_parser_free(&p);
87+
88+
return 0;
89+
}

0 commit comments

Comments
 (0)