xmpp/xode_from.c

Go to the documentation of this file.
00001 /*
00002  * $Id: xode_from.c 4494 2008-07-22 13:43:48Z henningw $
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 /*! \file
00023  * \ingroup xmpp
00024  */
00025 
00026 
00027 #include "xode.h"
00028 #include <sys/types.h>
00029 #include <sys/stat.h>
00030 #include <fcntl.h>
00031 
00032 
00033 static void _xode_put_expatattribs(xode current, const char **atts)
00034 {
00035     int i = 0;
00036     if (atts == NULL) return;
00037     while (atts[i] != '\0')
00038     {
00039         xode_put_attrib(current, atts[i], atts[i+1]);
00040         i += 2;
00041     }
00042 }
00043 
00044 static void _xode_expat_startElement(void* userdata, const char* name, const char** atts)
00045 {
00046     /* get the xmlnode pointed to by the userdata */
00047     xode *x = userdata;
00048     xode current = *x;
00049 
00050     if (current == NULL)
00051     {
00052         /* allocate a base node */
00053         current = xode_new(name);
00054         _xode_put_expatattribs(current, atts);
00055         *x = current;
00056     }
00057     else
00058     {
00059         *x = xode_insert_tag(current, name);
00060         _xode_put_expatattribs(*x, atts);
00061     }
00062 }
00063 
00064 static void _xode_expat_endElement(void* userdata, const char* name)
00065 {
00066     xode *x = userdata;
00067     xode current = *x;
00068 
00069     current->complete = 1;
00070     current = xode_get_parent(current);
00071 
00072     /* if it's NULL we've hit the top folks, otherwise back up a level */
00073     if(current != NULL)
00074         *x = current;
00075 }
00076 
00077 static void _xode_expat_charData(void* userdata, const char* s, int len)
00078 {
00079     xode *x = userdata;
00080     xode current = *x;
00081 
00082     xode_insert_cdata(current, s, len);
00083 }
00084 
00085 
00086 xode xode_from_str(char *str, int len)
00087 {
00088     XML_Parser p;
00089     xode *x, node; /* pointer to an xmlnode */
00090 
00091     if(NULL == str)
00092         return NULL;
00093 
00094     if(len == -1)
00095         len = strlen(str);
00096 
00097     x = malloc(sizeof(void *));
00098 
00099     *x = NULL; /* pointer to NULL */
00100     p = XML_ParserCreate(NULL);
00101     XML_SetUserData(p, x);
00102     XML_SetElementHandler(p, _xode_expat_startElement, _xode_expat_endElement);
00103     XML_SetCharacterDataHandler(p, _xode_expat_charData);
00104     if(!XML_Parse(p, str, len, 1))
00105     {
00106         /*        jdebug(ZONE,"xmlnode_str_error: %s",(char *)XML_ErrorString(XML_GetErrorCode(p)));*/
00107         xode_free(*x);
00108         *x = NULL;
00109     }
00110     node = *x;
00111     free(x);
00112     XML_ParserFree(p);
00113     return node; /* return the xmlnode x points to */
00114 }
00115 
00116 xode xode_from_strx(char *str, int len, int *err, int *pos)
00117 {
00118     XML_Parser p;
00119     xode *x, node; /* pointer to an xmlnode */
00120 
00121     if(NULL == str)
00122         return NULL;
00123 
00124     if(len == -1)
00125         len = strlen(str);
00126 
00127     x = malloc(sizeof(void *));
00128 
00129     *x = NULL; /* pointer to NULL */
00130     p = XML_ParserCreate(NULL);
00131     XML_SetUserData(p, x);
00132     XML_SetElementHandler(p, _xode_expat_startElement, _xode_expat_endElement);
00133     XML_SetCharacterDataHandler(p, _xode_expat_charData);
00134     XML_Parse(p, str, len, 0);
00135    if(err != NULL)
00136       *err = XML_GetErrorCode(p);
00137    if(pos != NULL)
00138       *pos = XML_GetCurrentByteIndex(p);     
00139     node = *x;
00140     free(x);
00141     XML_ParserFree(p);
00142     
00143    return node; /* return the xmlnode x points to */
00144 }
00145 
00146 xode xode_from_file(char *file)
00147 {
00148     XML_Parser p;
00149     xode *x, node; /* pointer to an xmlnode */
00150     char buf[BUFSIZ];
00151     int done, fd, len;
00152     char _file[1000];
00153 
00154     if(NULL == file)
00155         return NULL;
00156 
00157     /* perform tilde expansion */
00158     if(*file == '~')
00159     {
00160         char *env = getenv("HOME");
00161         if(env != NULL)
00162             snprintf((char*)_file, 1000, "%s%s", env, file + 1);
00163         else
00164             snprintf((char*)_file, 1000, "%s", file);
00165     }
00166     else
00167     {
00168         snprintf((char*)_file, 1000, "%s", file);
00169     }
00170 
00171     fd = open((char*)&_file,O_RDONLY);
00172     if(fd < 0)
00173         return NULL;
00174 
00175     x = malloc(sizeof(void *));
00176 
00177     *x = NULL; /* pointer to NULL */
00178     p = XML_ParserCreate(NULL);
00179     XML_SetUserData(p, x);
00180     XML_SetElementHandler(p, _xode_expat_startElement, _xode_expat_endElement);
00181     XML_SetCharacterDataHandler(p, _xode_expat_charData);
00182     do{
00183         len = read(fd, buf, BUFSIZ);
00184         done = len < BUFSIZ;
00185         if(!XML_Parse(p, buf, len, done))
00186         {
00187             /*            jdebug(ZONE,"xmlnode_file_parseerror: %s",(char *)XML_ErrorString(XML_GetErrorCode(p)));*/
00188             xode_free(*x);
00189             *x = NULL;
00190             done = 1;
00191         }
00192     }while(!done);
00193 
00194     node = *x;
00195     XML_ParserFree(p);
00196     free(x);
00197     close(fd);
00198     return node; /* return the xmlnode x points to */
00199 }
00200 
00201 int xode_to_file(char *file, xode node)
00202 {
00203     char *doc;
00204     int fd, i;
00205     char _file[1000];
00206 
00207     if(file == NULL || node == NULL)
00208         return -1;
00209 
00210     /* perform tilde expansion */
00211     if(*file == '~')
00212     {
00213         char *env = getenv("HOME");
00214         if(env != NULL)
00215             snprintf((char*)_file, 1000, "%s%s", env, file + 1);
00216         else
00217             snprintf((char*)_file, 1000, "%s", file);
00218     }
00219     else
00220     {
00221         snprintf((char*)_file, 1000, "%s", file);
00222     }
00223 
00224     fd = open((char*)&_file, O_CREAT | O_WRONLY | O_TRUNC, 0600);
00225     if(fd < 0)
00226         return -1;
00227 
00228     doc = xode_to_str(node);
00229     i = write(fd,doc,strlen(doc));
00230     if(i < 0)
00231         return -1;
00232 
00233     close(fd);
00234     return 1;
00235 }
00236 

Generated on Fri May 25 00:00:35 2012 for Kamailio - The Open Source SIP Server by  doxygen 1.5.6