Jabber WindowGram Client (JWGC)

Introduction Screenshots Installation Downloads
Documentation Browse Source Resources Project Site

Stable Version
-none-

Latest Version
beta5



Main Page | Alphabetical List | Data Structures | Directories | File List | Data Fields | Globals

xode_from.c

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


Last updated at Tue Dec 18 21:07:42 PST 2007. This site and project hosted by...SourceForge.net Logo
Source Perspective by Fisheye