[opendemo-cvs] CVS: opendemo/tools/odcut/odfile_expat odfile_expat_intern.h,1.4,1.5 odfile_expat_int
Status: Beta
Brought to you by:
girlich
From: Uwe G. <gi...@us...> - 2004-03-31 08:54:13
|
Update of /cvsroot/opendemo/opendemo/tools/odcut/odfile_expat In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv15387 Modified Files: odfile_expat_intern.h odfile_expat_intern.c odfile_expat.xs Log Message: implement header and footer footer methods Index: odfile_expat_intern.h =================================================================== RCS file: /cvsroot/opendemo/opendemo/tools/odcut/odfile_expat/odfile_expat_intern.h,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** odfile_expat_intern.h 30 Mar 2004 08:04:49 -0000 1.4 --- odfile_expat_intern.h 31 Mar 2004 08:42:25 -0000 1.5 *************** *** 17,22 **** XML_Parser parser; char *infile; - char *xmldecl; - char *doctype; char *mapname; int snap; --- 17,20 ---- *************** *** 24,28 **** int firstTime; int firstSequence; ! int last_snap_offset; } odfile_expat; --- 22,29 ---- int firstTime; int firstSequence; ! int header_length; ! int footer_offset; ! char *header; ! char *footer; } odfile_expat; *************** *** 39,42 **** --- 40,45 ---- extern int ofe_firstSequence(odfile_expat *self); extern void ofe_set_firstSequence(odfile_expat *self, int firstSequence); + extern const char* ofe_header(odfile_expat *self); + extern const char* ofe_footer(odfile_expat *self); Index: odfile_expat_intern.c =================================================================== RCS file: /cvsroot/opendemo/opendemo/tools/odcut/odfile_expat/odfile_expat_intern.c,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** odfile_expat_intern.c 30 Mar 2004 08:07:03 -0000 1.4 --- odfile_expat_intern.c 31 Mar 2004 08:42:25 -0000 1.5 *************** *** 46,77 **** /* XML file parse handlers */ - void XMLCALL _HandleXMLDecl(void *userData, const XML_Char *version, - const XML_Char *encoding, int standalone) - { - odfile_expat* self = (odfile_expat*)userData; - char buffer[100]; - - CFREE(self->xmldecl); - snprintf(buffer, sizeof(buffer), - "<?xml version=\"%s\" encoding=\"%s\"?>\n", - version, encoding); - self->xmldecl = strdup(buffer); - } - - - void XMLCALL _HandleDoctype(void *userData, const XML_Char *doctypeName, - const XML_Char *sysid, const XML_Char *pubid, int has_internal_subset) - { - odfile_expat* self = (odfile_expat*)userData; - char buffer[500]; - - CFREE(self->doctype); - snprintf(buffer, sizeof(buffer), - "<!DOCTYPE %s PUBLIC \"%s\" \"%s\">", - doctypeName, pubid, sysid); - self->doctype = strdup(buffer); - } - - void XMLCALL _HandleStartElement(void *userData, const XML_Char *name, const XML_Char **atts) --- 46,49 ---- *************** *** 88,91 **** --- 60,64 ---- ofe_set_firstSequence(self, atoi(_getAttribute(atts,"sequence"))); self->snapshotc = 0; + self->header_length = XML_GetCurrentByteIndex(self->parser); } self->snapshotc++; *************** *** 99,105 **** odfile_expat* self = (odfile_expat*)userData; if (strcmp(name, "snapshot") == 0) { ! self->last_snap_offset = XML_GetCurrentByteIndex(self->parser) ! + 11; ! /* + 1 + 1 + strlen("snapshot") + 1; */ } } --- 72,76 ---- odfile_expat* self = (odfile_expat*)userData; if (strcmp(name, "snapshot") == 0) { ! self->footer_offset = XML_GetCurrentByteIndex(self->parser); } } *************** *** 143,148 **** /* set the handlers */ - XML_SetXmlDeclHandler (p, _HandleXMLDecl); - XML_SetStartDoctypeDeclHandler (p, _HandleDoctype); XML_SetStartElementHandler (p, _HandleStartElement); XML_SetEndElementHandler (p, _HandleEndElement); --- 114,117 ---- *************** *** 192,195 **** --- 161,229 ---- } while (bytes_read > 0); + if (self != 0) { + /* post process */ + struct stat buf; + int footer_length; + + self->header = malloc(self->header_length + 1); /* trailing \0 */ + if (self->header == NULL) { + fprintf(stderr,"%s: could not allocate %d bytes for the header: %s\n", + fname, self->header_length + 1, strerror(errno)); + OFE_DONE(self); + goto out; + } + + if (lseek(docfd, 0, SEEK_SET) < 0) { + fprintf(stderr,"%s: could not rewind %s: %s\n", + fname, ofe_name(self), strerror(errno)); + OFE_DONE(self); + goto out; + } + if (read(docfd, self->header, self->header_length) != self->header_length) { + fprintf(stderr, + "%s: could not read header (%d bytes) from %s: %s\n", + fname, self->header_length, ofe_name(self), + strerror(errno)); + OFE_DONE(self); + goto out; + } + self->header[self->header_length] = '\0'; /* trailing \0 */ + + if (fstat(docfd, &buf) < 0) { + fprintf(stderr,"%s: could not stat() the file %s: %s\n", + fname, ofe_name(self), strerror(errno)); + OFE_DONE(self); + goto out; + } + self->footer_offset += 11; /* + </snapshot> */ + footer_length = buf.st_size - self->footer_offset; + + self->footer = malloc(footer_length + 1); /* trailing \0 */ + if (self->footer == NULL) { + fprintf(stderr,"%s: could not allocate %d bytes for the footer: %s\n", + fname, footer_length + 1, strerror(errno)); + OFE_DONE(self); + goto out; + } + + if (lseek(docfd, self->footer_offset, SEEK_SET) < 0) { + fprintf(stderr, + "%s: could not seek %s to the footer (offset %d): %s\n", + fname, ofe_name(self), self->footer_offset, + strerror(errno)); + OFE_DONE(self); + goto out; + } + if (read(docfd, self->footer, footer_length) != footer_length) { + fprintf(stderr, + "%s: could not read footer (%d bytes) from %s: %s\n", + fname, footer_length, ofe_name(self), + strerror(errno)); + OFE_DONE(self); + goto out; + } + self->footer[footer_length] = '\0'; /* trailing \0 */ + } + out: /* remove the parser object */ *************** *** 204,214 **** } - if (self != 0) { - /* post process */ - /* - fprintf(stderr,"last pos = %d\n", self->last_snap_offset); - */ - } - /* return the object (or a NULL pointer) */ return self; --- 238,241 ---- *************** *** 273,283 **** void ofe_done(odfile_expat *self) { CFREE(self->infile); /* was created with strdup() */ - CFREE(self->xmldecl); /* was created with strdup() */ - CFREE(self->doctype); /* was created with strdup() */ CFREE(self->mapname); /* was created with strdup() */ CFREE(self); /* was created with malloc() */ } --- 300,324 ---- + const char* + ofe_header(odfile_expat *self) + { + return self->header; + } + + + const char* + ofe_footer(odfile_expat *self) + { + return self->footer; + } + + void ofe_done(odfile_expat *self) { CFREE(self->infile); /* was created with strdup() */ CFREE(self->mapname); /* was created with strdup() */ + CFREE(self->header); /* was created with malloc() */ + CFREE(self->footer); /* was created with malloc() */ CFREE(self); /* was created with malloc() */ } Index: odfile_expat.xs =================================================================== RCS file: /cvsroot/opendemo/opendemo/tools/odcut/odfile_expat/odfile_expat.xs,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** odfile_expat.xs 30 Mar 2004 08:08:20 -0000 1.4 --- odfile_expat.xs 31 Mar 2004 08:42:25 -0000 1.5 *************** *** 95,96 **** --- 95,114 ---- + const char* + header(self) + odfile_expat *self; + CODE: + RETVAL = ofe_header(self); + OUTPUT: + RETVAL + + + const char* + footer(self) + odfile_expat *self; + CODE: + RETVAL = ofe_footer(self); + OUTPUT: + RETVAL + + |