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

JExpat.c

Go to the documentation of this file.
00001 /*
00002  * --------------------------------------------------------------------------
00003  * 
00004  * License
00005  * 
00006  * The contents of this file are subject to the Jabber Open Source License
00007  * Version 1.0 (the "JOSL").  You may not copy or use this file, in either
00008  * source code or executable form, except in compliance with the JOSL. You
00009  * may obtain a copy of the JOSL at http://www.jabber.org/ or at
00010  * http://www.opensource.org/.
00011  * 
00012  * Software distributed under the JOSL is distributed on an "AS IS" basis,
00013  * WITHOUT WARRANTY OF ANY KIND, either express or implied.  See the JOSL for
00014  * the specific language governing rights and limitations under the JOSL.
00015  * 
00016  * Copyrights
00017  * 
00018  * Portions created by or assigned to Jabber.com, Inc. are Copyright (c)
00019  * 1999-2002 Jabber.com, Inc.  All Rights Reserved.  Contact information for
00020  * Jabber.com, Inc. is available at http://www.jabber.com/.
00021  * 
00022  * Portions Copyright (c) 1998-1999 Jeremie Miller.
00023  * 
00024  * Acknowledgements
00025  * 
00026  * Special thanks to the Jabber Open Source Contributors for their suggestions
00027  * and support of Jabber.
00028  * 
00029  * Alternatively, the contents of this file may be used under the terms of the
00030  * GNU General Public License Version 2 or later (the "GPL"), in which case
00031  * the provisions of the GPL are applicable instead of those above.  If you
00032  * wish to allow use of your version of this file only under the terms of the
00033  * GPL and not to allow others to use your version of this file under the
00034  * JOSL, indicate your decision by deleting the provisions above and replace
00035  * them with the notice and other provisions required by the GPL.  If you do
00036  * not delete the provisions above, a recipient may use your version of this
00037  * file under either the JOSL or the GPL.
00038  * 
00039  * --------------------------------------------------------------------------
00040  */
00041 
00042 /* $Id: JExpat.c,v 1.2 2003/09/27 02:47:38 jadestorm Exp $ */
00043 
00044 #include "libjwgc.h"
00045 #include "libxode.h"
00046 
00047 void 
00048 expat_startElement(void *userdata, const char *name, const char **atts)
00049 {
00050         /* get the xode pointed to by the userdata */
00051         xode *x = userdata;
00052         xode current = *x;
00053 
00054         if (current == NULL) {
00055                 /* allocate a base node */
00056                 current = xode_new(name);
00057                 xode_put_expat_attribs(current, atts);
00058                 *x = current;
00059         }
00060         else {
00061                 *x = xode_insert_tag(current, name);
00062                 xode_put_expat_attribs(*x, atts);
00063         }
00064 }
00065 
00066 void 
00067 expat_endElement(void *userdata, const char *name)
00068 {
00069         xode *x = userdata;
00070         xode current = *x;
00071 
00072         current->complete = 1;
00073         current = xode_get_parent(current);
00074 
00075         /* if it's NULL we've hit the top folks, otherwise back up a level */
00076         if (current != NULL)
00077                 *x = current;
00078 }
00079 
00080 void 
00081 expat_charData(void *userdata, const char *s, int len)
00082 {
00083         xode *x = userdata;
00084         xode current = *x;
00085 
00086         xode_insert_cdata(current, s, len);
00087 }
00088 
00089 
00090 xode 
00091 xode_str(char *str, int len)
00092 {
00093         XML_Parser p;
00094         xode *x, node;          /* pointer to an xode */
00095 
00096         if (NULL == str)
00097                 return NULL;
00098 
00099         x = malloc(sizeof(void *));
00100 
00101         *x = NULL;              /* pointer to NULL */
00102         p = XML_ParserCreate(NULL);
00103         XML_SetUserData(p, x);
00104         XML_SetElementHandler(p, expat_startElement, expat_endElement);
00105         XML_SetCharacterDataHandler(p, expat_charData);
00106         if (!XML_Parse(p, str, len, 1)) {
00107                 /*
00108                  * jdebug(ZONE,"xode_str_error: %s",(char
00109                  * *)XML_ErrorString(XML_GetErrorCode(p)));
00110                  */
00111                 xode_free(*x);
00112                 *x = NULL;
00113         }
00114         node = *x;
00115         free(x);
00116         XML_ParserFree(p);
00117         return node;            /* return the xode x points to */
00118 }
00119 
00120 xode 
00121 xode_file(char *file)
00122 {
00123         XML_Parser p;
00124         xode *x, node;          /* pointer to an xode */
00125         char buf[BUFSIZ];
00126         int done, fd, len;
00127 
00128         if (NULL == file)
00129                 return NULL;
00130 
00131         fd = open(file, O_RDONLY);
00132         if (fd < 0)
00133                 return NULL;
00134 
00135         x = malloc(sizeof(void *));
00136 
00137         *x = NULL;              /* pointer to NULL */
00138         p = XML_ParserCreate(NULL);
00139         XML_SetUserData(p, x);
00140         XML_SetElementHandler(p, expat_startElement, expat_endElement);
00141         XML_SetCharacterDataHandler(p, expat_charData);
00142         do {
00143                 len = read(fd, buf, BUFSIZ);
00144                 done = len < BUFSIZ;
00145                 if (!XML_Parse(p, buf, len, done)) {
00146                         /*
00147                          * jdebug(ZONE,"xode_file_parseerror: %s",(char
00148                          * *)XML_ErrorString(XML_GetErrorCode(p)));
00149                          */
00150                         xode_free(*x);
00151                         *x = NULL;
00152                         done = 1;
00153                 }
00154         } while (!done);
00155 
00156         node = *x;
00157         XML_ParserFree(p);
00158         free(x);
00159         close(fd);
00160         return node;            /* return the xode x points to */
00161 }
00162 
00163 char *
00164 xode_file_borked(char *file)
00165 {
00166         XML_Parser p;
00167         char buf[BUFSIZ];
00168         static char err[1024];
00169         int fd, len, done;
00170 
00171         if (NULL == file)
00172                 return "no file specified";
00173 
00174         fd = open(file, O_RDONLY);
00175         if (fd < 0)
00176                 return "unable to open file";
00177 
00178         p = XML_ParserCreate(NULL);
00179         while (1) {
00180                 len = read(fd, buf, BUFSIZ);
00181                 done = len < BUFSIZ;
00182                 if (!XML_Parse(p, buf, len, done)) {
00183                         snprintf(err, 1023, "%s at line %d and column %d", XML_ErrorString(XML_GetErrorCode(p)), XML_GetErrorLineNumber(p), XML_GetErrorColumnNumber(p));
00184                         XML_ParserFree(p);
00185                         close(fd);
00186                         return err;
00187                 }
00188         }
00189 }
00190 
00191 int 
00192 xode2file(char *file, xode node)
00193 {
00194         char *doc, *ftmp;
00195         int fd, i;
00196 
00197         if (file == NULL || node == NULL)
00198                 return -1;
00199 
00200         ftmp = spools(xode_get_pool(node), file, ".t.m.p", xode_get_pool(node));
00201         fd = open(ftmp, O_CREAT | O_WRONLY | O_TRUNC, 0600);
00202         if (fd < 0)
00203                 return -1;
00204 
00205         doc = xode_to_str(node);
00206         i = write(fd, doc, strlen(doc));
00207         if (i < 0)
00208                 return -1;
00209 
00210         close(fd);
00211 
00212         if (rename(ftmp, file) < 0) {
00213                 unlink(ftmp);
00214                 return -1;
00215         }
00216         return 1;
00217 }
00218 
00219 void 
00220 xode_put_expat_attribs(xode owner, const char **atts)
00221 {
00222         int i = 0;
00223         if (atts == NULL)
00224                 return;
00225         while (atts[i] != '\0') {
00226                 xode_put_attrib(owner, atts[i], atts[i + 1]);
00227                 i += 2;
00228         }
00229 }


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