libopc
opc_extract.c

Demonstrates binary input stream access to an OPC container.

/*
Extract a part from an OPC containers.
Ussage:
opc_extract FILENAME PARTNAME
Sample:
opc_extract OOXMLI1.docx "word/document.xml"
*/
#include <opc/opc.h>
#include <stdio.h>
#ifdef WIN32
#include <io.h>
#include <fcntl.h>
#endif
#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
#ifdef WIN32
_setmode( _fileno( stdout ), _O_BINARY ); // make sure LF are not translated to CR LF on windows...
#endif
if (OPC_ERROR_NONE==opcInitLibrary() && 3==argc) {
opcContainer *c=NULL;
if (NULL!=(c=opcContainerOpen(_X(argv[1]), OPC_OPEN_READ_ONLY, NULL, NULL))) {
if ((part=opcPartFind(c, _X(argv[2]), NULL, 0))!=OPC_PART_INVALID) {
if ((stream=opcContainerOpenInputStream(c, part))!=NULL){
opc_uint8_t buf[100];
opc_uint32_t len=0;
while((len=opcContainerReadInputStream(stream, buf, sizeof(buf)))>0) {
fwrite(buf, sizeof(opc_uint8_t), len, stdout);
}
} else {
fprintf(stderr, "ERROR: part \"%s\" could not be opened for reading.\n", argv[2]);
}
} else {
fprintf(stderr, "ERROR: part \"%s\" could not be opened in \"%s\".\n", argv[2], argv[1]);
}
} else {
fprintf(stderr, "ERROR: file \"%s\" could not be opened.\n", argv[1]);
}
} else if (2==argc) {
fprintf(stderr, "ERROR: initialization of libopc failed.\n");
} else {
fprintf(stderr, "opc_extract FILENAME PART\n\n");
fprintf(stderr, "Sample: opc_extract test.docx word/document.xml\n");
}
#ifdef WIN32
OPC_ASSERT(!_CrtDumpMemoryLeaks());
#endif
return 0;
}