[gaim-migrate @ 3063]
[pidgin-git.git] / src / protocols / jabber / expat.c
blob5e1f428c03082869c719e0990fd90e06fd5b8c11
1 /*
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.
16 * Jabber
17 * Copyright (C) 1998-1999 The Jabber Team http://jabber.org/
20 #include <libxode.h>
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;
26 xmlnode current = *x;
28 if (current == NULL)
30 /* allocate a base node */
31 current = xmlnode_new_tag(name);
32 xmlnode_put_expat_attribs(current, atts);
33 *x = current;
35 else
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;
45 xmlnode current = *x;
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 */
51 if(current != NULL)
52 *x = current;
55 void expat_charData(void* userdata, const char* s, int len)
57 xmlnode *x = userdata;
58 xmlnode current = *x;
60 xmlnode_insert_cdata(current, s, len);
64 xmlnode xmlnode_str(char *str, int len)
66 XML_Parser p;
67 xmlnode *x, node; /* pointer to an xmlnode */
69 if(NULL == str)
70 return NULL;
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)));*/
82 xmlnode_free(*x);
83 *x = NULL;
85 node = *x;
86 free(x);
87 XML_ParserFree(p);
88 return node; /* return the xmlnode x points to */
91 xmlnode xmlnode_file(char *file)
93 XML_Parser p;
94 xmlnode *x, node; /* pointer to an xmlnode */
95 char buf[BUFSIZ];
96 int done, fd, len;
98 if(NULL == file)
99 return NULL;
101 fd = open(file,O_RDONLY);
102 if(fd < 0)
103 return NULL;
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);
114 done = len < BUFSIZ;
115 if(!XML_Parse(p, buf, len, done))
117 /* jdebug(ZONE,"xmlnode_file_parseerror: %s",(char *)XML_ErrorString(XML_GetErrorCode(p)));*/
118 xmlnode_free(*x);
119 *x = NULL;
120 done = 1;
122 }while(!done);
124 node = *x;
125 XML_ParserFree(p);
126 free(x);
127 close(fd);
128 return node; /* return the xmlnode x points to */
131 int xmlnode2file(char *file, xmlnode node)
133 char *doc;
134 int fd, i;
136 if(file == NULL || node == NULL)
137 return -1;
139 fd = open(file, O_CREAT | O_WRONLY | O_TRUNC, 0600);
140 if(fd < 0)
141 return -1;
143 doc = xmlnode2str(node);
144 i = write(fd,doc,strlen(doc));
145 if(i < 0)
146 return -1;
148 close(fd);
149 return 1;
152 void xmlnode_put_expat_attribs(xmlnode owner, const char** atts)
154 int i = 0;
155 if (atts == NULL) return;
156 while (atts[i] != '\0')
158 xmlnode_put_attrib(owner, atts[i], atts[i+1]);
159 i += 2;