part.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. static opcPart opcPartOpenEx(opcContainer *container,
  32. const xmlChar *absolutePath,
  33. const xmlChar *type,
  34. int flags,
  35. opc_bool_t create_part) {
  36. opcContainerPart *part=opcContainerInsertPart(container, (absolutePath[0]=='/'?absolutePath+1:absolutePath), create_part);
  37. if (NULL!=part) {
  38. if (create_part && NULL==part->type) {
  39. opcContainerType *ct=insertType(container, type, OPC_TRUE);
  40. OPC_ASSERT(NULL!=ct && 0==xmlStrcmp(ct->type, type));
  41. part->type=ct->type;
  42. }
  43. return part->name;
  44. } else {
  45. return OPC_PART_INVALID;
  46. }
  47. }
  48. opcPart opcPartFind(opcContainer *container,
  49. const xmlChar *absolutePath,
  50. const xmlChar *type,
  51. int flags) {
  52. return opcPartOpenEx(container, absolutePath, type, flags, OPC_FALSE);
  53. }
  54. opcPart opcPartCreate(opcContainer *container,
  55. const xmlChar *absolutePath,
  56. const xmlChar *type,
  57. int flags) {
  58. return opcPartOpenEx(container, absolutePath, type, flags, OPC_TRUE);
  59. }
  60. const xmlChar *opcPartGetType(opcContainer *c, opcPart part) {
  61. return opcPartGetTypeEx(c, part, OPC_FALSE);
  62. }
  63. const xmlChar *opcPartGetTypeEx(opcContainer *c, opcPart part, opc_bool_t override_only) {
  64. OPC_ASSERT(OPC_PART_INVALID!=part);
  65. opcContainerPart *cp=(OPC_PART_INVALID!=part?opcContainerInsertPart(c, part, OPC_FALSE):NULL);
  66. const xmlChar *type=(NULL!=cp?cp->type:NULL);
  67. if (NULL==type && NULL!=cp && !override_only) {
  68. xmlChar *name=cp->name;
  69. int l=(NULL!=name?xmlStrlen(name):0);
  70. while(l>0 && name[l]!='.') l--;
  71. if (l>0) {
  72. l++;
  73. opcContainerExtension *ct=opcContainerInsertExtension(c, name+l, OPC_FALSE);
  74. type=(NULL!=ct?ct->type:NULL);
  75. } else {
  76. type=NULL; // no extension
  77. }
  78. }
  79. return type;
  80. }
  81. opc_error_t opcPartDelete(opcContainer *container, const xmlChar *absolutePath) {
  82. return opcContainerDeletePart(container, (absolutePath[0]=='/'?absolutePath+1:absolutePath));
  83. }
  84. opcPart opcPartGetFirst(opcContainer *container) {
  85. return (NULL!=container && container->part_items>0?container->part_array[0].name:OPC_PART_INVALID);
  86. }
  87. opcPart opcPartGetNext(opcContainer *container, opcPart part) {
  88. opcContainerPart *cp=(NULL!=container?opcContainerInsertPart(container, part, OPC_FALSE):NULL);
  89. if (NULL!=cp) {
  90. do { cp++; } while(cp<container->part_array+container->part_items && -1==cp->first_segment_id);
  91. }
  92. return (NULL!=cp && cp<container->part_array+container->part_items?cp->name:NULL);
  93. }
  94. opc_ofs_t opcPartGetSize(opcContainer *c, opcPart part) {
  95. OPC_ASSERT(OPC_PART_INVALID!=part);
  96. opcContainerPart *cp=(OPC_PART_INVALID!=part?opcContainerInsertPart(c, part, OPC_FALSE):NULL);
  97. if (NULL!=cp && cp->first_segment_id>=0 && cp->first_segment_id<c->storage->segment_items) {
  98. return c->storage->segment_array[cp->first_segment_id].uncompressed_size;
  99. } else {
  100. return 0;
  101. }
  102. }