opc_xml2.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /**
  2. Copyright (c) 2010, Florian Reuter
  3. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions
  6. are met:
  7. * Redistributions of source code must retain the above copyright
  8. notice, this list of conditions and the following disclaimer.
  9. * Redistributions in binary form must reproduce the above copyright
  10. notice, this list of conditions and the following disclaimer in
  11. the documentation and/or other materials provided with the
  12. distribution.
  13. * Neither the name of Florian Reuter nor the names of its contributors
  14. may be used to endorse or promote products derived from this
  15. software without specific prior written permission.
  16. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  17. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  18. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  19. FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  20. COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  21. INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  22. BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  23. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  24. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  25. STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  26. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  27. OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. /*
  30. Dump some meta information from an Office document using the opc/xmlreader.h APIs.
  31. Ussage:
  32. opc_xml2 FILENAME
  33. Sample:
  34. opc_xml2 OOXMLI1.docx
  35. */
  36. #include <opc/opc.h>
  37. #include <stdio.h>
  38. #include <string.h>
  39. #ifdef WIN32
  40. #include <crtdbg.h>
  41. #endif
  42. const char PROP_NS[]="http://schemas.openxmlformats.org/officeDocument/2006/extended-properties";
  43. typedef struct {
  44. xmlChar *application;
  45. int words;
  46. int lines;
  47. int pages;
  48. } AppDocProps;
  49. int main( int argc, const char* argv[] )
  50. {
  51. #ifdef WIN32
  52. _CrtSetDbgFlag (_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
  53. #endif
  54. if (OPC_ERROR_NONE==opcInitLibrary() && 2==argc) {
  55. opcContainer *c=NULL;
  56. if (NULL!=(c=opcContainerOpen(_X(argv[1]), OPC_OPEN_READ_ONLY, NULL, NULL))) {
  57. AppDocProps props;
  58. memset(&props, 0, sizeof(props));
  59. mceTextReader_t reader;
  60. if (OPC_ERROR_NONE==opcXmlReaderOpen(c, &reader, _X("docProps/app.xml"), NULL, 0, 0)) {
  61. mce_start_document(&reader) {
  62. mce_start_element(&reader, _X(PROP_NS), _X("Properties")) {
  63. mce_start_children (&reader) {
  64. mce_start_element(&reader, _X(PROP_NS), _X("Application")) {
  65. if (props.application!=NULL) {
  66. xmlFree(props.application);
  67. props.application=NULL;
  68. }
  69. mce_skip_attributes(&reader);
  70. mce_start_children(&reader) {
  71. mce_start_text(&reader) {
  72. props.application=xmlStrdup(xmlTextReaderConstValue(reader.reader));
  73. } mce_end_text(&reader);
  74. } mce_end_children(&reader);
  75. } mce_end_element(&reader);
  76. mce_start_element(&reader, _X(PROP_NS), _X("Words")) {
  77. props.words=0;
  78. mce_skip_attributes(&reader);
  79. mce_start_children(&reader) {
  80. mce_start_text(&reader) {
  81. props.words=atoi((char*)xmlTextReaderConstValue(reader.reader));
  82. } mce_end_text(&reader);
  83. } mce_end_children(&reader);
  84. } mce_end_element(&reader);
  85. mce_start_element(&reader, _X(PROP_NS), _X("Lines")) {
  86. props.lines=0;
  87. mce_skip_attributes(&reader);
  88. mce_start_children(&reader) {
  89. mce_start_text(&reader) {
  90. props.lines=atoi((char*)xmlTextReaderConstValue(reader.reader));
  91. } mce_end_text(&reader);
  92. } mce_end_children(&reader);
  93. } mce_end_element(&reader);
  94. mce_start_element(&reader, _X(PROP_NS), _X("Pages")) {
  95. props.pages=0;
  96. mce_skip_attributes(&reader);
  97. mce_start_children(&reader) {
  98. mce_start_text(&reader) {
  99. props.pages=atoi((char*)xmlTextReaderConstValue(reader.reader));
  100. } mce_end_text(&reader);
  101. } mce_end_children(&reader);
  102. } mce_end_element(&reader);
  103. } mce_end_children(&reader);
  104. } mce_end_element(&reader);
  105. } mce_end_document(&reader);
  106. mceTextReaderCleanup(&reader);
  107. }
  108. opcContainerClose(c, OPC_CLOSE_NOW);
  109. printf("application: %s\n", props.application);
  110. printf("words: %i\n", props.words);
  111. printf("lines: %i\n", props.lines);
  112. printf("pages: %i\n", props.pages);
  113. xmlFree(props.application);
  114. } else {
  115. printf("ERROR: file \"%s\" could not be opened.\n", argv[1]);
  116. }
  117. opcFreeLibrary();
  118. } else if (2==argc) {
  119. printf("ERROR: initialization of libopc failed.\n");
  120. } else {
  121. printf("opc_extract FILENAME.\n\n");
  122. printf("Sample: opc_extract test.docx word/document.xml\n");
  123. }
  124. #ifdef WIN32
  125. OPC_ASSERT(!_CrtDumpMemoryLeaks());
  126. #endif
  127. return 0;
  128. }