2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
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.
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.
17 * Copyright (C) 1998-1999 The Jabber Team http://jabber.org/
22 void expat_startElement(void* userdata
, const char* name
, const char** atts
)
24 /* get the xmlnode pointed to by the userdata */
25 xmlnode
*x
= userdata
;
30 /* allocate a base node */
31 current
= xmlnode_new_tag(name
);
32 xmlnode_put_expat_attribs(current
, atts
);
37 *x
= xmlnode_insert_tag(current
, name
);
38 xmlnode_put_expat_attribs(*x
, atts
);
42 void expat_endElement(void* userdata
, const char* name
)
44 xmlnode
*x
= userdata
;
47 current
->complete
= 1;
48 current
= xmlnode_get_parent(current
);
50 /* if it's NULL we've hit the top folks, otherwise back up a level */
55 void expat_charData(void* userdata
, const char* s
, int len
)
57 xmlnode
*x
= userdata
;
60 xmlnode_insert_cdata(current
, s
, len
);
64 xmlnode
xmlnode_str(char *str
, int len
)
67 xmlnode
*x
, node
; /* pointer to an xmlnode */
72 x
= malloc(sizeof(void *));
74 *x
= NULL
; /* pointer to NULL */
75 p
= XML_ParserCreate(NULL
);
76 XML_SetUserData(p
, x
);
77 XML_SetElementHandler(p
, expat_startElement
, expat_endElement
);
78 XML_SetCharacterDataHandler(p
, expat_charData
);
79 if(!XML_Parse(p
, str
, len
, 1))
81 /* jdebug(ZONE,"xmlnode_str_error: %s",(char *)XML_ErrorString(XML_GetErrorCode(p)));*/
88 return node
; /* return the xmlnode x points to */
91 xmlnode
xmlnode_file(char *file
)
94 xmlnode
*x
, node
; /* pointer to an xmlnode */
101 fd
= open(file
,O_RDONLY
);
105 x
= malloc(sizeof(void *));
107 *x
= NULL
; /* pointer to NULL */
108 p
= XML_ParserCreate(NULL
);
109 XML_SetUserData(p
, x
);
110 XML_SetElementHandler(p
, expat_startElement
, expat_endElement
);
111 XML_SetCharacterDataHandler(p
, expat_charData
);
113 len
= read(fd
, buf
, BUFSIZ
);
115 if(!XML_Parse(p
, buf
, len
, done
))
117 /* jdebug(ZONE,"xmlnode_file_parseerror: %s",(char *)XML_ErrorString(XML_GetErrorCode(p)));*/
128 return node
; /* return the xmlnode x points to */
131 int xmlnode2file(char *file
, xmlnode node
)
136 if(file
== NULL
|| node
== NULL
)
139 fd
= open(file
, O_CREAT
| O_WRONLY
| O_TRUNC
, 0600);
143 doc
= xmlnode2str(node
);
144 i
= write(fd
,doc
,strlen(doc
));
152 void xmlnode_put_expat_attribs(xmlnode owner
, const char** atts
)
155 if (atts
== NULL
) return;
156 while (atts
[i
] != '\0')
158 xmlnode_put_attrib(owner
, atts
[i
], atts
[i
+1]);