relation.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. #include <opc/opc.h>
  30. #include "internal.h"
  31. opcContainerRelation *opcContainerFindRelationByType(opcContainer *container, opcContainerRelation *relation_array, opc_uint32_t relation_items, const xmlChar *mimeType) {
  32. for(opc_uint32_t i=0;i<relation_items;i++) {
  33. if (0==xmlStrcmp(relation_array[i].relation_type, mimeType)) {
  34. return &relation_array[i];
  35. }
  36. }
  37. return NULL;
  38. }
  39. static opcContainerRelation* _opcRelationFind(opcContainer *container, opcPart part, opcRelation relation) {
  40. if (OPC_PART_INVALID==part) {
  41. return opcContainerFindRelation(container, container->relation_array, container->relation_items, relation);
  42. } else {
  43. opcContainerPart *cp=opcContainerInsertPart(container, part, OPC_FALSE);
  44. return (cp!=NULL?opcContainerFindRelation(container, cp->relation_array, cp->relation_items, relation):NULL);
  45. }
  46. }
  47. opcRelation opcRelationFind(opcContainer *container, opcPart part, const xmlChar *relationId, const xmlChar *mimeType) {
  48. opcContainerRelation *rel=NULL;
  49. if (OPC_PART_INVALID==part) {
  50. if (NULL!=relationId) {
  51. rel=opcContainerFindRelationById(container, container->relation_array, container->relation_items, relationId);
  52. } else if (NULL!=mimeType) {
  53. rel=opcContainerFindRelationByType(container, container->relation_array, container->relation_items, mimeType);
  54. }
  55. } else {
  56. opcContainerPart *cp=opcContainerInsertPart(container, part, OPC_FALSE);
  57. if (NULL!=cp) {
  58. if (NULL!=relationId) {
  59. rel=opcContainerFindRelationById(container, cp->relation_array, cp->relation_items, relationId);
  60. } else if (NULL!=mimeType) {
  61. rel=opcContainerFindRelationByType(container, cp->relation_array, cp->relation_items, mimeType);
  62. }
  63. }
  64. }
  65. return (NULL!=rel?rel->relation_id:OPC_RELATION_INVALID);
  66. }
  67. opcPart opcRelationGetInternalTarget(opcContainer *container, opcPart part, opcRelation relation) {
  68. opcContainerRelation *rel=_opcRelationFind(container, part, relation);
  69. if (rel!=NULL && 0==rel->target_mode) {
  70. return (opcPart)rel->target_ptr;
  71. } else {
  72. return NULL;
  73. }
  74. }
  75. const xmlChar *opcRelationGetExternalTarget(opcContainer *container, opcPart part, opcRelation relation) {
  76. opcContainerRelation *rel=_opcRelationFind(container, part, relation);
  77. if (rel!=NULL && 1==rel->target_mode) {
  78. return rel->target_ptr;
  79. } else {
  80. return NULL;
  81. }
  82. }
  83. opcRelation opcRelationFirst(opcContainer *container, opcPart part) {
  84. if (OPC_PART_INVALID==part) {
  85. return (container->relation_items>0?container->relation_array[0].relation_id:OPC_RELATION_INVALID);
  86. } else {
  87. opcContainerPart *cp=opcContainerInsertPart(container, part, OPC_FALSE);
  88. return (NULL!=cp && cp->relation_items>0?cp->relation_array[0].relation_id:OPC_RELATION_INVALID);
  89. }
  90. }
  91. opcRelation opcRelationNext(opcContainer *container, opcPart part, opcRelation relation) {
  92. opcContainerRelation *relation_array=NULL;
  93. opc_uint32_t relation_items=0;
  94. if (OPC_PART_INVALID==part) {
  95. relation_array=container->relation_array;
  96. relation_items=container->relation_items;
  97. } else {
  98. opcContainerPart *cp=opcContainerInsertPart(container, part, OPC_FALSE);
  99. if (NULL!=cp) {
  100. relation_array=cp->relation_array;
  101. relation_items=cp->relation_items;
  102. }
  103. }
  104. if (relation_items>0) {
  105. opcContainerRelation *rel=opcContainerFindRelation(container, relation_array, relation_items, relation);
  106. if (rel+1<relation_array+relation_items) {
  107. return (rel+1)->relation_id;
  108. } else {
  109. return OPC_RELATION_INVALID;
  110. }
  111. } else {
  112. return OPC_RELATION_INVALID;
  113. }
  114. }
  115. void opcRelationGetInformation(opcContainer *container, opcPart part, opcRelation relation, const xmlChar **prefix, opc_uint32_t *counter, const xmlChar **type) {
  116. opcContainerRelation* rel=NULL;
  117. if (NULL!=prefix) {
  118. OPC_ASSERT(OPC_CONTAINER_RELID_PREFIX(relation)>=0 && OPC_CONTAINER_RELID_PREFIX(relation)<container->relprefix_items);
  119. *prefix=container->relprefix_array[OPC_CONTAINER_RELID_PREFIX(relation)].prefix;
  120. }
  121. if (NULL!=counter) {
  122. *counter=OPC_CONTAINER_RELID_COUNTER(relation);
  123. }
  124. if (NULL!=type) {
  125. if (NULL==rel) rel=_opcRelationFind(container, part, relation);
  126. *type=(NULL!=rel?rel->relation_type:NULL);
  127. }
  128. }
  129. opc_error_t opcRelationDelete(opcContainer *container, opcPart part, const xmlChar *relationId, const xmlChar *mimeType) {
  130. opcRelation relation=opcRelationFind(container, part, relationId, mimeType);
  131. if (OPC_PART_INVALID==part) {
  132. return opcContainerDeleteRelation(container, &container->relation_array, &container->relation_items, relation);
  133. } else {
  134. opcContainerPart *cp=opcContainerInsertPart(container, part, OPC_FALSE);
  135. return (cp!=NULL?opcContainerDeleteRelation(container, &cp->relation_array, &cp->relation_items, relation):OPC_ERROR_STREAM);
  136. }
  137. }