opc_relation.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. Traverse all relations of an OPC container or get the type of an relation.
  31. Ussage:
  32. * Traverse all relations:
  33. opc_relation FILENAME
  34. * Get type of root relation
  35. opc_relation FILENAME RELATIONID
  36. * Get type of part relation
  37. opc_relation FILENAME PARTNAME RELATIONID
  38. Sample:
  39. opc_relation OOXMLI1.docx
  40. opc_relation OOXMLI1.docx "rId1"
  41. opc_relation OOXMLI1.docx "word/document.xml" "rId1"
  42. */
  43. #include <opc/opc.h>
  44. #ifdef WIN32
  45. #include <crtdbg.h>
  46. #endif
  47. static void traverse(opcContainer *c, opcPart source) {
  48. for(opcRelation rel=opcRelationFirst(c, source);OPC_RELATION_INVALID!=rel;rel=opcRelationNext(c, source, rel)) {
  49. opcPart target=opcRelationGetInternalTarget(c, source, rel);
  50. if (OPC_PART_INVALID!=target) {
  51. const xmlChar *prefix=NULL;
  52. opc_uint32_t counter=-1;
  53. const xmlChar *type=NULL;
  54. opcRelationGetInformation(c, source, rel, &prefix, &counter, &type);
  55. char buf[20]="";
  56. if (-1!=counter) {
  57. sprintf(buf, "%i", counter);
  58. }
  59. printf("%s %s%s %s %s\n", source, prefix, buf, target, type);
  60. traverse(c, target);
  61. }
  62. }
  63. }
  64. int main( int argc, const char* argv[] )
  65. {
  66. #ifdef WIN32
  67. _CrtSetDbgFlag (_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
  68. #endif
  69. opcInitLibrary();
  70. opcContainer *c=opcContainerOpen(_X(argv[1]), OPC_OPEN_READ_ONLY, NULL, NULL);
  71. if (NULL!=c) {
  72. if (3==argc) {
  73. opcRelation rel=opcRelationFind(c, OPC_PART_INVALID, _X(argv[2]), NULL);
  74. if (OPC_RELATION_INVALID!=rel) {
  75. const xmlChar *type=NULL;
  76. opcRelationGetInformation(c, OPC_PART_INVALID, rel, NULL, NULL, &type);
  77. printf("type=%s\n", type);
  78. }
  79. } else if (4==argc) {
  80. opcPart part=opcPartFind(c, _X(argv[2]), NULL, 0);
  81. if (OPC_PART_INVALID!=part) {
  82. opcRelation rel=opcRelationFind(c, part, _X(argv[3]), NULL);
  83. if (OPC_RELATION_INVALID!=rel) {
  84. const xmlChar *type=NULL;
  85. opcRelationGetInformation(c, part, rel, NULL, NULL, &type);
  86. printf("type=%s\n", type);
  87. }
  88. }
  89. } else {
  90. traverse(c, OPC_PART_INVALID);
  91. }
  92. opcContainerClose(c, OPC_CLOSE_NOW);
  93. }
  94. opcFreeLibrary();
  95. #ifdef WIN32
  96. OPC_ASSERT(!_CrtDumpMemoryLeaks());
  97. #endif
  98. return 0;
  99. }