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

xstream.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 
00022 /* xode_stream is a way to have a consistent method of handling incoming XML Stream based events... it doesn't handle the generation of an XML Stream, but provides some facilities to help do that */
00023 
00024 
00025 static void _xode_put_expatattribs(xode owner, const char** atts)
00026 {
00027     int i = 0;
00028     if (atts == NULL) return;
00029     while (atts[i] != '\0')
00030     {
00031         xode_put_attrib(owner, atts[i], atts[i+1]);
00032         i += 2;
00033     }
00034 }
00035 
00036 /******* internal expat callbacks *********/
00037 static void _xode_stream_startElement(xode_stream xs, const char* name, const char** atts)
00038 {
00039     xode_pool p;
00040 
00041     /* if xode_stream is bad, get outa here */
00042     if(xs->status > XODE_STREAM_NODE) return;
00043 
00044     if(xs->node == NULL)
00045     {
00046         p = xode_pool_heap(5*1024); /* 5k, typically 1-2k each plus copy of self and workspace */
00047         xs->node = xode_new_frompool(p,name);
00048         _xode_put_expatattribs(xs->node, atts);
00049 
00050         if(xs->status == XODE_STREAM_ROOT)
00051         {
00052             xs->status = XODE_STREAM_NODE; /* flag status that we're processing nodes now */
00053             (xs->f)(XODE_STREAM_ROOT, xs->node, xs->arg); /* send the root, f must free all nodes */
00054             xs->node = NULL;
00055         }
00056     }else{
00057         xs->node = xode_insert_tag(xs->node, name);
00058         _xode_put_expatattribs(xs->node, atts);
00059     }
00060 
00061     /* depth check */
00062     xs->depth++;
00063     if(xs->depth > XODE_STREAM_MAXDEPTH)
00064         xs->status = XODE_STREAM_ERROR;
00065 }
00066 
00067 
00068 static void _xode_stream_endElement(xode_stream xs, const char* name)
00069 {
00070     xode parent;
00071 
00072     /* if xode_stream is bad, get outa here */
00073     if(xs->status > XODE_STREAM_NODE) return;
00074 
00075     /* if it's already NULL we've received </stream>, tell the app and we're outta here */
00076     if(xs->node == NULL)
00077     {
00078         xs->status = XODE_STREAM_CLOSE;
00079         (xs->f)(XODE_STREAM_CLOSE, NULL, xs->arg);
00080     }else{
00081         parent = xode_get_parent(xs->node);
00082 
00083         /* we are the top-most node, feed to the app who is responsible to delete it */
00084         if(parent == NULL)
00085             (xs->f)(XODE_STREAM_NODE, xs->node, xs->arg);
00086 
00087         xs->node = parent;
00088     }
00089     xs->depth--;
00090 }
00091 
00092 
00093 static void _xode_stream_charData(xode_stream xs, const char *str, int len)
00094 {
00095     /* if xode_stream is bad, get outa here */
00096     if(xs->status > XODE_STREAM_NODE) return;
00097 
00098     if(xs->node == NULL)
00099     {
00100         /* we must be in the root of the stream where CDATA is irrelevant */
00101         return;
00102     }
00103 
00104     xode_insert_cdata(xs->node, str, len);
00105 }
00106 
00107 
00108 static void _xode_stream_cleanup(void *arg)
00109 {
00110     xode_stream xs = (xode_stream)arg;
00111 
00112     xode_free(xs->node); /* cleanup anything left over */
00113     XML_ParserFree(xs->parser);
00114 }
00115 
00116 
00117 /* creates a new xode_stream with given pool, xode_stream will be cleaned up w/ pool */
00118 xode_stream xode_stream_new(xode_pool p, xode_stream_onNode f, void *arg)
00119 {
00120     xode_stream newx;
00121 
00122     if(p == NULL || f == NULL)
00123     {
00124         fprintf(stderr,"Fatal Programming Error: xode_streamnew() was improperly called with NULL.\n");
00125         return NULL;
00126     }
00127 
00128     newx = xode_pool_malloco(p, sizeof(_xode_stream));
00129     newx->p = p;
00130     newx->f = f;
00131     newx->arg = arg;
00132 
00133     /* create expat parser and ensure cleanup */
00134     newx->parser = XML_ParserCreate(NULL);
00135     XML_SetUserData(newx->parser, (void *)newx);
00136     XML_SetElementHandler(newx->parser, (void *)_xode_stream_startElement, (void *)_xode_stream_endElement);
00137     XML_SetCharacterDataHandler(newx->parser, (void *)_xode_stream_charData);
00138     xode_pool_cleanup(p, _xode_stream_cleanup, (void *)newx);
00139 
00140     return newx;
00141 }
00142 
00143 /* attempts to parse the buff onto this stream firing events to the handler, returns the last known status */
00144 int xode_stream_eat(xode_stream xs, char *buff, int len)
00145 {
00146     char *err;
00147     xode xerr;
00148     static char maxerr[] = "maximum node size reached";
00149     static char deeperr[] = "maximum node depth reached";
00150 
00151     if(xs == NULL)
00152     {
00153         fprintf(stderr,"Fatal Programming Error: xode_streameat() was improperly called with NULL.\n");
00154         return XODE_STREAM_ERROR;
00155     }
00156 
00157     if(len == 0 || buff == NULL)
00158         return xs->status;
00159 
00160     if(len == -1) /* easy for hand-fed eat calls */
00161         len = strlen(buff);
00162 
00163     if(!XML_Parse(xs->parser, buff, len, 0))
00164     {
00165         err = (char *)XML_ErrorString(XML_GetErrorCode(xs->parser));
00166         xs->status = XODE_STREAM_ERROR;
00167     }else if(xode_pool_size(xode_get_pool(xs->node)) > XODE_STREAM_MAXNODE || xs->cdata_len > XODE_STREAM_MAXNODE){
00168         err = maxerr;
00169         xs->status = XODE_STREAM_ERROR;
00170     }else if(xs->status == XODE_STREAM_ERROR){ /* set within expat handlers */
00171         err = deeperr;
00172     }
00173 
00174     /* fire parsing error event, make a node containing the error string */
00175     if(xs->status == XODE_STREAM_ERROR)
00176     {
00177         xerr = xode_new("error");
00178         xode_insert_cdata(xerr,err,-1);
00179         (xs->f)(XODE_STREAM_ERROR, xerr, xs->arg);
00180     }
00181 
00182     return xs->status;
00183 }


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