~drizzle-trunk/drizzle/development

1 by brian
clean slate
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; version 2 of the License.
6
7
   This program is distributed in the hope that it will be useful,
8
   but WITHOUT ANY WARRANTY; without even the implied warranty of
9
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10
   GNU General Public License for more details.
11
12
   You should have received a copy of the GNU General Public License
13
   along with this program; if not, write to the Free Software
14
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
15
16
#include <stdio.h>
17
#include <string.h>
18
#include <unistd.h>
19
#include <fcntl.h>
20
#include "my_xml.h"
21
22
static void mstr(char *str,const char *src,uint l1,uint l2)
23
{
24
  l1 = l1<l2 ? l1 : l2;
25
  memcpy(str,src,l1);
26
  str[l1]='\0';
27
}
28
29
static int dstr(MY_XML_PARSER *st,const char *attr, uint len)
30
{
31
  char str[1024];
32
  
33
  mstr(str,attr,len,sizeof(str)-1);
34
  printf("VALUE '%s'\n",str);
35
  return MY_XML_OK;
36
}
37
38
static int bstr(MY_XML_PARSER *st,const char *attr, uint len)
39
{
40
  char str[1024];
41
  
42
  mstr(str,attr,len,sizeof(str)-1);
43
  printf("ENTER %s\n",str);
44
  return MY_XML_OK;
45
}
46
47
48
static int estr(MY_XML_PARSER *st,const char *attr, uint len)
49
{
50
  char str[1024];
51
  
52
  mstr(str,attr,len,sizeof(str)-1);
53
  printf("LEAVE %s\n",str);
54
  return MY_XML_OK;
55
}
56
57
static void usage(const char *prog)
58
{
59
  printf("Usage:\n");
60
  printf("%s xmlfile\n",prog);
61
}
62
63
int main(int ac, char **av)
64
{
65
  char str[1024*64]="";
66
  const char *fn;
67
  int  f;
68
  uint len;
69
  MY_XML_PARSER p;
70
  
71
  if (ac<2)
72
  {
73
    usage(av[0]);
74
    return 0;
75
  }
76
  
77
  fn=av[1]?av[1]:"test.xml";
78
  if ((f=open(fn,O_RDONLY))<0)
79
  {
80
    fprintf(stderr,"Err '%s'\n",fn);
81
    return 1;
82
  }
83
  
84
  len=read(f,str,sizeof(str)-1);
85
  str[len]='\0';
86
  
87
  my_xml_parser_create(&p);
88
  
89
  my_xml_set_enter_handler(&p,bstr);
90
  my_xml_set_value_handler(&p,dstr);
91
  my_xml_set_leave_handler(&p,estr);
92
  
93
  if (MY_XML_OK!=(f=my_xml_parse(&p,str,len)))
94
  {
95
    printf("ERROR at line %d pos %d '%s'\n",
96
      my_xml_error_lineno(&p)+1,
97
      my_xml_error_pos(&p),
98
      my_xml_error_string(&p));
99
  }
100
  
101
  my_xml_parser_free(&p);
102
  
103
  return 0;
104
}