Mercurial > pidgin
annotate src/protocols/jabber/expat.c @ 4982:fb232515839a
[gaim-migrate @ 5317]
funny, I thought we did this once already
committer: Tailor Script <tailor@pidgin.im>
author | Nathan Walp <nwalp@pidgin.im> |
---|---|
date | Thu, 03 Apr 2003 15:57:08 +0000 |
parents | 988485669631 |
children |
rev | line source |
---|---|
3127 | 1 /* -------------------------------------------------------------------------- |
2 * | |
3 * License | |
4 * | |
5 * The contents of this file are subject to the Jabber Open Source License | |
6 * Version 1.0 (the "JOSL"). You may not copy or use this file, in either | |
7 * source code or executable form, except in compliance with the JOSL. You | |
8 * may obtain a copy of the JOSL at http://www.jabber.org/ or at | |
9 * http://www.opensource.org/. | |
10 * | |
11 * Software distributed under the JOSL is distributed on an "AS IS" basis, | |
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the JOSL | |
13 * for the specific language governing rights and limitations under the | |
14 * JOSL. | |
15 * | |
16 * Copyrights | |
17 * | |
18 * Portions created by or assigned to Jabber.com, Inc. are | |
19 * Copyright (c) 1999-2002 Jabber.com, Inc. All Rights Reserved. Contact | |
20 * information for Jabber.com, Inc. is available at http://www.jabber.com/. | |
2086 | 21 * |
3127 | 22 * Portions Copyright (c) 1998-1999 Jeremie Miller. |
23 * | |
24 * Acknowledgements | |
25 * | |
26 * Special thanks to the Jabber Open Source Contributors for their | |
27 * suggestions and support of Jabber. | |
28 * | |
29 * Alternatively, the contents of this file may be used under the terms of the | |
30 * GNU General Public License Version 2 or later (the "GPL"), in which case | |
31 * the provisions of the GPL are applicable instead of those above. If you | |
32 * wish to allow use of your version of this file only under the terms of the | |
33 * GPL and not to allow others to use your version of this file under the JOSL, | |
34 * indicate your decision by deleting the provisions above and replace them | |
35 * with the notice and other provisions required by the GPL. If you do not | |
36 * delete the provisions above, a recipient may use your version of this file | |
37 * under either the JOSL or the GPL. | |
38 * | |
39 * | |
40 * --------------------------------------------------------------------------*/ | |
2086 | 41 |
3127 | 42 #include "lib.h" |
2086 | 43 |
3717
988485669631
[gaim-migrate @ 3850]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
3127
diff
changeset
|
44 #ifdef _WIN32 |
988485669631
[gaim-migrate @ 3850]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
3127
diff
changeset
|
45 #include "win32dep.h" |
988485669631
[gaim-migrate @ 3850]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
3127
diff
changeset
|
46 #endif |
988485669631
[gaim-migrate @ 3850]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
3127
diff
changeset
|
47 |
2086 | 48 void expat_startElement(void* userdata, const char* name, const char** atts) |
49 { | |
50 /* get the xmlnode pointed to by the userdata */ | |
51 xmlnode *x = userdata; | |
52 xmlnode current = *x; | |
53 | |
54 if (current == NULL) | |
55 { | |
56 /* allocate a base node */ | |
57 current = xmlnode_new_tag(name); | |
58 xmlnode_put_expat_attribs(current, atts); | |
59 *x = current; | |
60 } | |
61 else | |
62 { | |
63 *x = xmlnode_insert_tag(current, name); | |
64 xmlnode_put_expat_attribs(*x, atts); | |
65 } | |
66 } | |
67 | |
68 void expat_endElement(void* userdata, const char* name) | |
69 { | |
70 xmlnode *x = userdata; | |
71 xmlnode current = *x; | |
72 | |
73 current->complete = 1; | |
74 current = xmlnode_get_parent(current); | |
75 | |
76 /* if it's NULL we've hit the top folks, otherwise back up a level */ | |
77 if(current != NULL) | |
78 *x = current; | |
79 } | |
80 | |
81 void expat_charData(void* userdata, const char* s, int len) | |
82 { | |
83 xmlnode *x = userdata; | |
84 xmlnode current = *x; | |
85 | |
86 xmlnode_insert_cdata(current, s, len); | |
87 } | |
88 | |
89 | |
90 xmlnode xmlnode_str(char *str, int len) | |
91 { | |
92 XML_Parser p; | |
93 xmlnode *x, node; /* pointer to an xmlnode */ | |
94 | |
95 if(NULL == str) | |
96 return NULL; | |
97 | |
98 x = malloc(sizeof(void *)); | |
99 | |
100 *x = NULL; /* pointer to NULL */ | |
101 p = XML_ParserCreate(NULL); | |
102 XML_SetUserData(p, x); | |
103 XML_SetElementHandler(p, expat_startElement, expat_endElement); | |
104 XML_SetCharacterDataHandler(p, expat_charData); | |
105 if(!XML_Parse(p, str, len, 1)) | |
106 { | |
107 /* jdebug(ZONE,"xmlnode_str_error: %s",(char *)XML_ErrorString(XML_GetErrorCode(p)));*/ | |
108 xmlnode_free(*x); | |
109 *x = NULL; | |
110 } | |
111 node = *x; | |
112 free(x); | |
113 XML_ParserFree(p); | |
114 return node; /* return the xmlnode x points to */ | |
115 } | |
116 | |
117 xmlnode xmlnode_file(char *file) | |
118 { | |
119 XML_Parser p; | |
120 xmlnode *x, node; /* pointer to an xmlnode */ | |
121 char buf[BUFSIZ]; | |
122 int done, fd, len; | |
123 | |
124 if(NULL == file) | |
125 return NULL; | |
126 | |
127 fd = open(file,O_RDONLY); | |
128 if(fd < 0) | |
129 return NULL; | |
130 | |
131 x = malloc(sizeof(void *)); | |
132 | |
133 *x = NULL; /* pointer to NULL */ | |
134 p = XML_ParserCreate(NULL); | |
135 XML_SetUserData(p, x); | |
136 XML_SetElementHandler(p, expat_startElement, expat_endElement); | |
137 XML_SetCharacterDataHandler(p, expat_charData); | |
138 do{ | |
139 len = read(fd, buf, BUFSIZ); | |
140 done = len < BUFSIZ; | |
141 if(!XML_Parse(p, buf, len, done)) | |
142 { | |
143 /* jdebug(ZONE,"xmlnode_file_parseerror: %s",(char *)XML_ErrorString(XML_GetErrorCode(p)));*/ | |
144 xmlnode_free(*x); | |
145 *x = NULL; | |
146 done = 1; | |
147 } | |
148 }while(!done); | |
149 | |
150 node = *x; | |
151 XML_ParserFree(p); | |
152 free(x); | |
153 close(fd); | |
154 return node; /* return the xmlnode x points to */ | |
155 } | |
156 | |
3127 | 157 char* xmlnode_file_borked(char *file) |
158 { | |
159 XML_Parser p; | |
160 char buf[BUFSIZ]; | |
161 static char err[1024]; | |
162 int fd, len, done; | |
163 | |
164 if(NULL == file) | |
165 return "no file specified"; | |
166 | |
167 fd = open(file,O_RDONLY); | |
168 if(fd < 0) | |
169 return "unable to open file"; | |
170 | |
171 p = XML_ParserCreate(NULL); | |
172 while(1) | |
173 { | |
174 len = read(fd, buf, BUFSIZ); | |
175 done = len < BUFSIZ; | |
176 if(!XML_Parse(p, buf, len, done)) | |
177 { | |
178 snprintf(err,1023,"%s at line %d and column %d",XML_ErrorString(XML_GetErrorCode(p)),XML_GetErrorLineNumber(p),XML_GetErrorColumnNumber(p)); | |
179 XML_ParserFree(p); | |
180 close(fd); | |
181 return err; | |
182 } | |
183 } | |
184 } | |
185 | |
2086 | 186 int xmlnode2file(char *file, xmlnode node) |
187 { | |
3127 | 188 char *doc, *ftmp; |
2086 | 189 int fd, i; |
190 | |
191 if(file == NULL || node == NULL) | |
192 return -1; | |
193 | |
3127 | 194 ftmp = spools(xmlnode_pool(node),file,".t.m.p",xmlnode_pool(node)); |
195 fd = open(ftmp, O_CREAT | O_WRONLY | O_TRUNC, 0600); | |
2086 | 196 if(fd < 0) |
197 return -1; | |
198 | |
199 doc = xmlnode2str(node); | |
200 i = write(fd,doc,strlen(doc)); | |
201 if(i < 0) | |
202 return -1; | |
203 | |
204 close(fd); | |
3127 | 205 |
206 if(rename(ftmp,file) < 0) | |
207 { | |
208 unlink(ftmp); | |
209 return -1; | |
210 } | |
2086 | 211 return 1; |
212 } | |
213 | |
214 void xmlnode_put_expat_attribs(xmlnode owner, const char** atts) | |
215 { | |
216 int i = 0; | |
217 if (atts == NULL) return; | |
218 while (atts[i] != '\0') | |
219 { | |
220 xmlnode_put_attrib(owner, atts[i], atts[i+1]); | |
221 i += 2; | |
222 } | |
223 } |