[opendemo-cvs] CVS: opendemo/tools/odcut/odfile_expat odfile_expat_intern.c,1.1,1.2
Status: Beta
Brought to you by:
girlich
From: Uwe G. <gi...@us...> - 2004-03-29 16:33:46
|
Update of /cvsroot/opendemo/opendemo/tools/odcut/odfile_expat In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv24076 Modified Files: odfile_expat_intern.c Log Message: parser implementation started: read file, call expat no handlers defined yet object destructor implemented Index: odfile_expat_intern.c =================================================================== RCS file: /cvsroot/opendemo/opendemo/tools/odcut/odfile_expat/odfile_expat_intern.c,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** odfile_expat_intern.c 29 Mar 2004 10:04:08 -0000 1.1 --- odfile_expat_intern.c 29 Mar 2004 16:22:17 -0000 1.2 *************** *** 3,8 **** /* system includes */ ! #include <string.h> /* for memset() */ ! #include <stdlib.h> /* for malloc() */ --- 3,12 ---- /* system includes */ ! #include <errno.h> /* for errno */ ! #include <fcntl.h> /* for open() */ ! #include <string.h> /* for memset() */ ! #include <stdio.h> /* for fprintf() */ ! #include <stdlib.h> /* for malloc() */ ! #include <unistd.h> /* for read() */ *************** *** 15,28 **** odfile_expat* ofe_init(char *filename) { odfile_expat *self; self = malloc(sizeof(*self)); ! if (self != NULL) { ! memset(self, 0, sizeof(*self)); ! self->infile = strdup(filename); ! /* TODO parse the file */ ! } return self; } --- 19,115 ---- + #define CFREE(x) { if ((x)!=NULL) free(x) ; (x) = NULL; } + #define OFE_DONE(x) { if ((x)!=NULL) ofe_done(x); (x) = NULL; } + + odfile_expat* ofe_init(char *filename) { + const char *fname = "ofe_init"; odfile_expat *self; + XML_Parser p = NULL; + int docfd = -1; + int bytes_read; + + /* allocate object buffer */ self = malloc(sizeof(*self)); ! if (self == NULL) { ! fprintf(stderr,"%s: could not allocate odfile_expat structure: %s\n", fname, strerror(errno)); ! goto out; ! } ! ! /* clean the structure */ ! memset(self, 0, sizeof(*self)); ! ! /* store the file name */ ! self->infile = strdup(filename); ! ! /* create the parser */ ! p = XML_ParserCreate(NULL); ! if (p == NULL) { ! OFE_DONE(self); ! goto out; ! } ! ! /* set the handlers */ ! /* TODO */ ! ! /* open the file for reading */ ! docfd = open(ofe_get_name(self), O_RDONLY); ! if (docfd<0) { ! fprintf(stderr,"%s: could note open '%s' for reading: %s\n", ! fname, ofe_get_name(self), strerror(errno)); ! OFE_DONE(self); ! goto out; ! } ! ! /* parse the file */ ! do { ! void *buff; ! ! /* get the buffer */ ! buff = XML_GetBuffer(p, OFE_BUFFER_SIZE); ! if (buff == NULL) { ! fprintf(stderr,"%s: could not get XML buffer\n", ! fname); ! OFE_DONE(self); ! goto out; ! } ! ! /* read in the file */ ! bytes_read = read(docfd, buff, OFE_BUFFER_SIZE); ! if (bytes_read < 0) { ! fprintf(stderr,"%s: could not read in %d bytes from %s: %s\n", ! fname, OFE_BUFFER_SIZE, ofe_get_name(self), strerror(errno)); ! OFE_DONE(self); ! goto out; ! } ! ! /* parse this buffer */ ! if (!XML_ParseBuffer(p, bytes_read, bytes_read==0)) { ! fprintf(stderr,"%s: XML parse error at line %d, col %d: %s\n", ! fname, ! XML_GetCurrentLineNumber(p), ! XML_GetCurrentColumnNumber(p), ! XML_ErrorString(XML_GetErrorCode(p))); ! OFE_DONE(self); ! goto out; ! } ! ! } while (bytes_read > 0); ! ! out: ! /* remove the parser object */ ! if (p != NULL) { ! XML_ParserFree(p); ! } ! ! /* close the input file */ ! if (docfd != -1) { ! close(docfd); ! docfd = -1; ! } ! ! /* return the object (or a NULL pointer) */ return self; } *************** *** 36,37 **** --- 123,132 ---- + void + ofe_done(odfile_expat *self) + { + CFREE(self->infile); /* was created with strdup() */ + CFREE(self); /* was created with malloc() */ + } + + |