libopc
All Data Structures Files Functions Typedefs Enumerations Enumerator Macros Pages
opc_part.c

Demonstrates how to dump a part from an OPC container. Ussage opc_dump [container] [part-name]. E.g. opc_dump sample.docx "word/document.xml".

/*
Dump content of a part using either the binary or the XML interfaces.
Ussage:
opc_part FILENAME PARTNAME
Sample:
opc_part OOXMLI1.docx "word/document.xml"
*/
#include <opc/opc.h>
#include <stdio.h>
#include <time.h>
#ifdef WIN32
#include <crtdbg.h>
#endif
int main( int argc, const char* argv[] )
{
#ifdef WIN32
_CrtSetDbgFlag (_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
#endif
opcContainer *c=opcContainerOpen(_X(argv[1]), OPC_OPEN_READ_ONLY, NULL, NULL);
if (NULL!=c) {
opcPart part=opcPartFind(c, _X(argv[2]), NULL, 0);
if (OPC_PART_INVALID!=part) {
const xmlChar *type=opcPartGetType(c, part);
opc_uint32_t type_len=xmlStrlen(type);
opc_bool_t is_xml=NULL!=type && type_len>=3 && 'x'==type[type_len-3] && 'm'==type[type_len-2] && 'l'==type[type_len-1];
fprintf(stderr, "type=%s is_xml=%i\n", type, is_xml);
if (is_xml) {
if (OPC_ERROR_NONE==opcXmlReaderOpen(c, &reader, part, NULL, 0, 0)) {
xmlTextWriter *writer=xmlNewTextWriterFile(NULL);
xmlTextWriterSetIndent(writer, 1);
if (NULL!=writer) {
mceTextReaderDump(&reader, writer, 1);
}
xmlFreeTextWriter(writer);
}
} else {
if (NULL!=stream) {
opc_uint32_t ret=0;
opc_uint8_t buf[100];
while((ret=opcContainerReadInputStream(stream, buf, sizeof(buf)))>0) {
fwrite(buf, sizeof(opc_uint8_t), ret, stdout);
}
}
}
}
}
#ifdef WIN32
OPC_ASSERT(!_CrtDumpMemoryLeaks());
#endif
return 0;
}