obsolete/jabber_k/xode_from.c

00001 /*
00002  * $Id$
00003  *
00004  *  This program is free software; you can redistribute it and/or modify
00005  *  it under the terms of the GNU General Public License as published by
00006  *  the Free Software Foundation; either version 2 of the License, or
00007  *  (at your option) any later version.
00008  *
00009  *  This program is distributed in the hope that it will be useful,
00010  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *  GNU General Public License for more details.
00013  *
00014  *  You should have received a copy of the GNU General Public License
00015  *  along with this program; if not, write to the Free Software
00016  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00017  *
00018  *  Jabber
00019  *  Copyright (C) 1998-1999 The Jabber Team http://jabber.org/
00020  */
00021 
00022 #include "xode.h"
00023 #include <sys/types.h>
00024 #include <sys/stat.h>
00025 #include <fcntl.h>
00026 
00027 
00028 static void _xode_put_expatattribs(xode current, const char **atts)
00029 {
00030     int i = 0;
00031     if (atts == NULL) return;
00032     while (atts[i] != '\0')
00033     {
00034         xode_put_attrib(current, atts[i], atts[i+1]);
00035         i += 2;
00036     }
00037 }
00038 
00039 static void _xode_expat_startElement(void* userdata, const char* name, const char** atts)
00040 {
00041     /* get the xmlnode pointed to by the userdata */
00042     xode *x = userdata;
00043     xode current = *x;
00044 
00045     if (current == NULL)
00046     {
00047         /* allocate a base node */
00048         current = xode_new(name);
00049         _xode_put_expatattribs(current, atts);
00050         *x = current;
00051     }
00052     else
00053     {
00054         *x = xode_insert_tag(current, name);
00055         _xode_put_expatattribs(*x, atts);
00056     }
00057 }
00058 
00059 static void _xode_expat_endElement(void* userdata, const char* name)
00060 {
00061     xode *x = userdata;
00062     xode current = *x;
00063 
00064     current->complete = 1;
00065     current = xode_get_parent(current);
00066 
00067     /* if it's NULL we've hit the top folks, otherwise back up a level */
00068     if(current != NULL)
00069         *x = current;
00070 }
00071 
00072 static void _xode_expat_charData(void* userdata, const char* s, int len)
00073 {
00074     xode *x = userdata;
00075     xode current = *x;
00076 
00077     xode_insert_cdata(current, s, len);
00078 }
00079 
00080 
00081 xode xode_from_str(char *str, int len)
00082 {
00083     XML_Parser p;
00084     xode *x, node; /* pointer to an xmlnode */
00085 
00086     if(NULL == str)
00087         return NULL;
00088 
00089     if(len == -1)
00090         len = strlen(str);
00091 
00092     x = malloc(sizeof(void *));
00093 
00094     *x = NULL; /* pointer to NULL */
00095     p = XML_ParserCreate(NULL);
00096     XML_SetUserData(p, x);
00097     XML_SetElementHandler(p, _xode_expat_startElement, _xode_expat_endElement);
00098     XML_SetCharacterDataHandler(p, _xode_expat_charData);
00099     if(!XML_Parse(p, str, len, 1))
00100     {
00101         /*        jdebug(ZONE,"xmlnode_str_error: %s",(char *)XML_ErrorString(XML_GetErrorCode(p)));*/
00102         xode_free(*x);
00103         *x = NULL;
00104     }
00105     node = *x;
00106     free(x);
00107     XML_ParserFree(p);
00108     return node; /* return the xmlnode x points to */
00109 }
00110 
00111 xode xode_from_strx(char *str, int len, int *err, int *pos)
00112 {
00113     XML_Parser p;
00114     xode *x, node; /* pointer to an xmlnode */
00115 
00116     if(NULL == str)
00117         return NULL;
00118 
00119     if(len == -1)
00120         len = strlen(str);
00121 
00122     x = malloc(sizeof(void *));
00123 
00124     *x = NULL; /* pointer to NULL */
00125     p = XML_ParserCreate(NULL);
00126     XML_SetUserData(p, x);
00127     XML_SetElementHandler(p, _xode_expat_startElement, _xode_expat_endElement);
00128     XML_SetCharacterDataHandler(p, _xode_expat_charData);
00129     XML_Parse(p, str, len, 0);
00130         if(err != NULL)
00131                 *err = XML_GetErrorCode(p);
00132         if(pos != NULL)
00133                 *pos = XML_GetCurrentByteIndex(p);              
00134     node = *x;
00135     free(x);
00136     XML_ParserFree(p);
00137     
00138         return node; /* return the xmlnode x points to */
00139 }
00140 
00141 xode xode_from_file(char *file)
00142 {
00143     XML_Parser p;
00144     xode *x, node; /* pointer to an xmlnode */
00145     char buf[BUFSIZ];
00146     int done, fd, len;
00147     char _file[1000];
00148 
00149     if(NULL == file)
00150         return NULL;
00151 
00152     /* perform tilde expansion */
00153     if(*file == '~')
00154     {
00155         char *env = getenv("HOME");
00156         if(env != NULL)
00157             snprintf((char*)_file, 1000, "%s%s", env, file + 1);
00158         else
00159             snprintf((char*)_file, 1000, "%s", file);
00160     }
00161     else
00162     {
00163         snprintf((char*)_file, 1000, "%s", file);
00164     }
00165 
00166     fd = open((char*)&_file,O_RDONLY);
00167     if(fd < 0)
00168         return NULL;
00169 
00170     x = malloc(sizeof(void *));
00171 
00172     *x = NULL; /* pointer to NULL */
00173     p = XML_ParserCreate(NULL);
00174     XML_SetUserData(p, x);
00175     XML_SetElementHandler(p, _xode_expat_startElement, _xode_expat_endElement);
00176     XML_SetCharacterDataHandler(p, _xode_expat_charData);
00177     do{
00178         len = read(fd, buf, BUFSIZ);
00179         done = len < BUFSIZ;
00180         if(!XML_Parse(p, buf, len, done))
00181         {
00182             /*            jdebug(ZONE,"xmlnode_file_parseerror: %s",(char *)XML_ErrorString(XML_GetErrorCode(p)));*/
00183             xode_free(*x);
00184             *x = NULL;
00185             done = 1;
00186         }
00187     }while(!done);
00188 
00189     node = *x;
00190     XML_ParserFree(p);
00191     free(x);
00192     close(fd);
00193     return node; /* return the xmlnode x points to */
00194 }
00195 
00196 int xode_to_file(char *file, xode node)
00197 {
00198     char *doc;
00199     int fd, i;
00200     char _file[1000];
00201 
00202     if(file == NULL || node == NULL)
00203         return -1;
00204 
00205     /* perform tilde expansion */
00206     if(*file == '~')
00207     {
00208         char *env = getenv("HOME");
00209         if(env != NULL)
00210             snprintf((char*)_file, 1000, "%s%s", env, file + 1);
00211         else
00212             snprintf((char*)_file, 1000, "%s", file);
00213     }
00214     else
00215     {
00216         snprintf((char*)_file, 1000, "%s", file);
00217     }
00218 
00219     fd = open((char*)&_file, O_CREAT | O_WRONLY | O_TRUNC, 0600);
00220     if(fd < 0)
00221         return -1;
00222 
00223     doc = xode_to_str(node);
00224     i = write(fd,doc,strlen(doc));
00225     if(i < 0)
00226         return -1;
00227 
00228     close(fd);
00229     return 1;
00230 }
00231