source: trunk/third/jwgc/lib/libxode/xode_from.c @ 22406

Revision 22406, 4.8 KB checked in by ghudson, 19 years ago (diff)
This commit was generated by cvs2svn to compensate for changes in r22405, which included commits to RCS files with non-trunk default branches.
Line 
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.
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 *  Jabber
17 *  Copyright (C) 1998-1999 The Jabber Team http://jabber.org/
18 */
19
20#include <libxode.h>
21#include <sys/types.h>
22#include <sys/stat.h>
23#include <fcntl.h>
24
25
26static void _xode_put_expatattribs(xode current, const char **atts)
27{
28    int i = 0;
29    if (atts == NULL) return;
30    while (atts[i] != '\0')
31    {
32        xode_put_attrib(current, atts[i], atts[i+1]);
33        i += 2;
34    }
35}
36
37static void _xode_expat_startElement(void* userdata, const char* name, const char** atts)
38{
39    /* get the xmlnode pointed to by the userdata */
40    xode *x = userdata;
41    xode current = *x;
42
43    if (current == NULL)
44    {
45        /* allocate a base node */
46        current = xode_new(name);
47        _xode_put_expatattribs(current, atts);
48        *x = current;
49    }
50    else
51    {
52        *x = xode_insert_tag(current, name);
53        _xode_put_expatattribs(*x, atts);
54    }
55}
56
57static void _xode_expat_endElement(void* userdata, const char* name)
58{
59    xode *x = userdata;
60    xode current = *x;
61
62    current->complete = 1;
63    current = xode_get_parent(current);
64
65    /* if it's NULL we've hit the top folks, otherwise back up a level */
66    if(current != NULL)
67        *x = current;
68}
69
70static void _xode_expat_charData(void* userdata, const char* s, int len)
71{
72    xode *x = userdata;
73    xode current = *x;
74
75    xode_insert_cdata(current, s, len);
76}
77
78
79xode xode_from_str(char *str, int len)
80{
81    XML_Parser p;
82    xode *x, node; /* pointer to an xmlnode */
83
84    if(NULL == str)
85        return NULL;
86
87    if(len == -1)
88        len = strlen(str);
89
90    x = malloc(sizeof(void *));
91
92    *x = NULL; /* pointer to NULL */
93    p = XML_ParserCreate(NULL);
94    XML_SetUserData(p, x);
95    XML_SetElementHandler(p, _xode_expat_startElement, _xode_expat_endElement);
96    XML_SetCharacterDataHandler(p, _xode_expat_charData);
97    if(!XML_Parse(p, str, len, 1))
98    {
99        dprintf(dXML, "xmlnode_str_error: %s",(char *)XML_ErrorString(XML_GetErrorCode(p)));
100        xode_free(*x);
101        *x = NULL;
102    }
103    node = *x;
104    free(x);
105    XML_ParserFree(p);
106    return node; /* return the xmlnode x points to */
107}
108
109xode xode_from_file(char *file)
110{
111    XML_Parser p;
112    xode *x, node; /* pointer to an xmlnode */
113    char buf[BUFSIZ];
114    int done, fd, len;
115    char _file[1000];
116
117    if(NULL == file)
118        return NULL;
119
120    /* perform tilde expansion */
121    if(*file == '~')
122    {
123        char *env = getenv("HOME");
124        if(env != NULL)
125            snprintf((char*)_file, 1000, "%s%s", env, file + 1);
126        else
127            snprintf((char*)_file, 1000, "%s", file);
128    }
129    else
130    {
131        snprintf((char*)_file, 1000, "%s", file);
132    }
133
134    fd = open((char*)_file,O_RDONLY);
135    if(fd < 0) {
136        dprintf(dXML, "unable to open file [%d]: %s\n", errno, _file);
137        return NULL;
138    }
139
140    x = malloc(sizeof(void *));
141
142    *x = NULL; /* pointer to NULL */
143    p = XML_ParserCreate(NULL);
144    XML_SetUserData(p, x);
145    XML_SetElementHandler(p, _xode_expat_startElement, _xode_expat_endElement);
146    XML_SetCharacterDataHandler(p, _xode_expat_charData);
147    do{
148        len = read(fd, buf, BUFSIZ);
149        done = len < BUFSIZ;
150        if(!XML_Parse(p, buf, len, done))
151        {
152            dprintf(dXML, "xmlnode_file_parseerror: %s",(char *)XML_ErrorString(XML_GetErrorCode(p)));
153            xode_free(*x);
154            *x = NULL;
155            done = 1;
156        }
157    }while(!done);
158
159    node = *x;
160    XML_ParserFree(p);
161    free(x);
162    close(fd);
163    return node; /* return the xmlnode x points to */
164}
165
166int xode_to_file(char *file, xode node)
167{
168    char *doc;
169    int fd, i;
170    char _file[1000];
171
172    if(file == NULL || node == NULL)
173        return -1;
174
175    /* perform tilde expansion */
176    if(*file == '~')
177    {
178        char *env = getenv("HOME");
179        if(env != NULL)
180            snprintf((char*)_file, 1000, "%s%s", env, file + 1);
181        else
182            snprintf((char*)_file, 1000, "%s", file);
183    }
184    else
185    {
186        snprintf((char*)_file, 1000, "%s", file);
187    }
188
189    fd = open((char*)&_file, O_CREAT | O_WRONLY | O_TRUNC, 0600);
190    if(fd < 0)
191        return -1;
192
193    doc = xode_to_str(node);
194    i = write(fd,doc,strlen(doc));
195    if(i < 0)
196        return -1;
197
198    close(fd);
199    return 1;
200}
201
Note: See TracBrowser for help on using the repository browser.