HdfItem.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. //
  2. // Created by patrik on 11.08.17.
  3. //
  4. #include <hdf4cpp/HdfFile.h>
  5. #include <hdf4cpp/HdfItem.h>
  6. #include <hdf/mfhdf.h>
  7. #include <sstream>
  8. hdf4cpp::HdfDatasetItem::HdfDatasetItem(int32 id, const HdfDestroyerChain& chain) : HdfItemBase(id, SDATA, chain) {
  9. _size = 1;
  10. std::for_each(dims.begin(), dims.end(), [this](const int32 &t) {
  11. _size *= t;
  12. });
  13. int32 dim[MAX_DIMENSION];
  14. int32 size;
  15. char _name[MAX_NAME_LENGTH];
  16. SDgetinfo(id, _name, &size, dim, &dataType, nullptr);
  17. dims = std::vector<int32>(dim, dim + size);
  18. name = std::string(_name);
  19. this->chain.push_back(new HdfDatasetItemDestroyer(id));
  20. }
  21. std::vector<int32> hdf4cpp::HdfDatasetItem::getDims() {
  22. return dims;
  23. }
  24. hdf4cpp::HdfAttribute hdf4cpp::HdfDatasetItem::getAttribute(const std::string &name) const {
  25. return HdfAttribute(new HdfDatasetAttribute(id, name, chain));
  26. }
  27. std::string hdf4cpp::HdfDatasetItem::getName() const {
  28. return name;
  29. }
  30. int32 hdf4cpp::HdfDatasetItem::getId() const {
  31. return id;
  32. }
  33. int32 hdf4cpp::HdfDatasetItem::getDataType() const {
  34. return dataType;
  35. }
  36. hdf4cpp::HdfDatasetItem::~HdfDatasetItem() {
  37. }
  38. intn hdf4cpp::HdfDatasetItem::size() const {
  39. return _size;
  40. }
  41. hdf4cpp::HdfGroupItem::HdfGroupItem(int32 id, const HdfDestroyerChain& chain) : HdfItemBase(id, VGROUP, chain) {
  42. char _name[MAX_NAME_LENGTH];
  43. Vgetname(id, _name);
  44. name = std::string(_name);
  45. this->chain.push_back(new HdfGroupItemDestroyer(id));
  46. }
  47. std::vector<int32> hdf4cpp::HdfGroupItem::getDims() {
  48. raiseException(INVALID_OPERATION);
  49. }
  50. hdf4cpp::HdfAttribute hdf4cpp::HdfGroupItem::getAttribute(const std::string &name) const {
  51. return HdfAttribute(new HdfGroupAttribute(id, name, chain));
  52. }
  53. std::string hdf4cpp::HdfGroupItem::getName() const {
  54. return name;
  55. }
  56. int32 hdf4cpp::HdfGroupItem::getId() const {
  57. return id;
  58. }
  59. hdf4cpp::HdfGroupItem::~HdfGroupItem() {
  60. }
  61. intn hdf4cpp::HdfGroupItem::size() const {
  62. raiseException(INVALID_OPERATION);
  63. }
  64. int32 hdf4cpp::HdfGroupItem::getDataType() const {
  65. raiseException(INVALID_OPERATION);
  66. }
  67. hdf4cpp::HdfDataItem::HdfDataItem(int32 id, const HdfDestroyerChain& chain) : HdfItemBase(id, VDATA, chain) {
  68. char fieldNameList[MAX_NAME_LENGTH];
  69. char _name[MAX_NAME_LENGTH];
  70. VSinquire(id, &nrRecords, &interlace, fieldNameList, &recordSize, _name);
  71. name = std::string(_name);
  72. std::istringstream in(fieldNameList);
  73. std::string token;
  74. while(getline(in, token, ',')) {
  75. fieldNames.push_back(token);
  76. }
  77. this->chain.push_back(new HdfDataItemDestroyer(id));
  78. }
  79. hdf4cpp::HdfDataItem::~HdfDataItem() {
  80. }
  81. hdf4cpp::HdfAttribute hdf4cpp::HdfDataItem::getAttribute(const std::string &name) const {
  82. return HdfAttribute(new HdfDataAttribute(id, name, chain));
  83. }
  84. int32 hdf4cpp::HdfDataItem::getId() const {
  85. return id;
  86. }
  87. std::string hdf4cpp::HdfDataItem::getName() const {
  88. return name;
  89. }
  90. std::vector<int32> hdf4cpp::HdfDataItem::getDims() {
  91. std::vector<int32> dims;
  92. dims.push_back(nrRecords);
  93. dims.push_back((int32) fieldNames.size());
  94. return dims;
  95. }
  96. intn hdf4cpp::HdfDataItem::size() const {
  97. return nrRecords * (int32) fieldNames.size();
  98. }
  99. int32 hdf4cpp::HdfDataItem::getDataType() const {
  100. return 0;
  101. }
  102. hdf4cpp::HdfItem::HdfItem(HdfItemBase *item, int32 sId, int32 vId) : HdfObject(item),
  103. item(item),
  104. sId(sId),
  105. vId(vId) {}
  106. hdf4cpp::HdfItem::HdfItem(HdfItem&& other) : HdfObject(other.getType(), other.getClassType(), std::move(other.chain)),
  107. item(std::move(other.item)),
  108. sId(other.sId),
  109. vId(other.vId) {}
  110. hdf4cpp::HdfItem& hdf4cpp::HdfItem::operator=(HdfItem&& it) {
  111. item = std::move(it.item);
  112. return *this;
  113. }
  114. std::vector<int32> hdf4cpp::HdfItem::getDims() {
  115. return item->getDims();
  116. }
  117. hdf4cpp::HdfAttribute hdf4cpp::HdfItem::getAttribute(const std::string &name) const {
  118. return item->getAttribute(name);
  119. }
  120. hdf4cpp::Type hdf4cpp::HdfItem::getType() const {
  121. return item->getType();
  122. }
  123. std::string hdf4cpp::HdfItem::getName() const {
  124. return item->getName();
  125. }
  126. intn hdf4cpp::HdfItem::size() const {
  127. return item->size();
  128. }
  129. hdf4cpp::HdfItem::Iterator hdf4cpp::HdfItem::begin() const {
  130. return Iterator(sId, vId, item->getId(), 0, getType(), chain);
  131. }
  132. hdf4cpp::HdfItem::Iterator hdf4cpp::HdfItem::end() const {
  133. switch(item->getType()) {
  134. case VGROUP: {
  135. int32 size = Vntagrefs(item->getId());
  136. return Iterator(sId, vId, item->getId(), size, getType(), chain);
  137. }
  138. default: {
  139. return Iterator(sId, vId, item->getId(), 0, getType(), chain);
  140. }
  141. }
  142. }