HdfFile.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /// \copyright Copyright (c) Catalysts GmbH
  2. /// \author Patrik Kovacs, Catalysts GmbH
  3. #include <mfhdf.h>
  4. #include <stdexcept>
  5. #include <hdf4cpp/HdfAttribute.h>
  6. #include <hdf4cpp/HdfDefines.h>
  7. #include <hdf4cpp/HdfException.h>
  8. #include <hdf4cpp/HdfFile.h>
  9. #include <hdf4cpp/HdfItem.h>
  10. hdf4cpp::HdfFile::HdfFile(const std::string &path)
  11. : HdfObject(HFILE, FILE) {
  12. sId = SDstart(path.c_str(), DFACC_READ);
  13. vId = Hopen(path.c_str(), DFACC_READ, 0);
  14. if (sId == FAIL || vId == FAIL) {
  15. raiseException(INVALID_ID);
  16. }
  17. Vinitialize(vId);
  18. chain.emplaceBack(&SDend, sId);
  19. chain.emplaceBack(&Vfinish, vId);
  20. chain.emplaceBack(&Hclose, vId);
  21. int32 loneSize = Vlone(vId, nullptr, 0);
  22. std::vector<int32> refs((size_t)loneSize);
  23. Vlone(vId, refs.data(), loneSize);
  24. for (const auto &ref : refs) {
  25. loneRefs.emplace_back(ref, VGROUP);
  26. }
  27. int32 loneVdata = VSlone(vId, nullptr, 0);
  28. refs.resize((size_t)loneVdata);
  29. VSlone(vId, refs.data(), loneVdata);
  30. for (const auto &ref : refs) {
  31. loneRefs.emplace_back(ref, VDATA);
  32. }
  33. }
  34. hdf4cpp::HdfFile::HdfFile(HdfFile &&file) noexcept
  35. : HdfObject(file.getType(), file.getClassType(), std::move(file.chain)) {
  36. sId = file.sId;
  37. vId = file.vId;
  38. loneRefs = std::move(file.loneRefs);
  39. file.sId = file.vId = FAIL;
  40. }
  41. hdf4cpp::HdfFile &hdf4cpp::HdfFile::operator=(HdfFile &&file) noexcept {
  42. setType(file.getType());
  43. setClassType(file.getClassType());
  44. chain = std::move(file.chain);
  45. sId = file.sId;
  46. vId = file.vId;
  47. loneRefs = std::move(file.loneRefs);
  48. file.sId = file.vId = FAIL;
  49. return *this;
  50. }
  51. hdf4cpp::HdfFile::~HdfFile() = default;
  52. int32 hdf4cpp::HdfFile::getSId() const {
  53. return sId;
  54. }
  55. int32 hdf4cpp::HdfFile::getVId() const {
  56. return vId;
  57. }
  58. int32 hdf4cpp::HdfFile::getDatasetId(const std::string &name) const {
  59. int32 index = SDnametoindex(sId, name.c_str());
  60. return (index == FAIL) ? (FAIL) : (SDselect(sId, index));
  61. }
  62. int32 hdf4cpp::HdfFile::getGroupId(const std::string &name) const {
  63. int32 ref = Vfind(vId, name.c_str());
  64. return (ref == 0) ? (FAIL) : (Vattach(vId, ref, "r"));
  65. }
  66. int32 hdf4cpp::HdfFile::getDataId(const std::string &name) const {
  67. int32 ref = VSfind(vId, name.c_str());
  68. return (ref == 0) ? (FAIL) : (VSattach(vId, ref, "r"));
  69. }
  70. hdf4cpp::HdfItem hdf4cpp::HdfFile::get(const std::string &name) const {
  71. int32 id = getDatasetId(name);
  72. if (id != FAIL) {
  73. return HdfItem(new HdfItem::HdfDatasetItem(id, chain), sId, vId);
  74. }
  75. id = getGroupId(name);
  76. if (id != FAIL) {
  77. return HdfItem(new HdfItem::HdfGroupItem(id, chain), sId, vId);
  78. }
  79. id = getDataId(name);
  80. if (id != FAIL) {
  81. return HdfItem(new HdfItem::HdfDataItem(id, chain), sId, vId);
  82. }
  83. raiseException(INVALID_ID);
  84. }
  85. std::vector<int32> hdf4cpp::HdfFile::getDatasetIds(const std::string &name) const {
  86. std::vector<int32> ids;
  87. char nameDataset[MAX_NAME_LENGTH];
  88. int32 datasets, waste;
  89. SDfileinfo(sId, &datasets, &waste);
  90. for (int32 i = 0; i < datasets; ++i) {
  91. int32 id = SDselect(sId, i);
  92. SDgetinfo(id, nameDataset, nullptr, nullptr, nullptr, nullptr);
  93. if (id != FAIL && name == std::string(nameDataset)) {
  94. ids.push_back(id);
  95. } else {
  96. SDendaccess(id);
  97. }
  98. }
  99. return ids;
  100. }
  101. std::vector<int32> hdf4cpp::HdfFile::getGroupDataIds(const std::string &name) const {
  102. std::vector<int32> ids;
  103. char nameGroup[MAX_NAME_LENGTH];
  104. int32 ref = Vgetid(vId, -1);
  105. while (ref != FAIL) {
  106. int32 id = Vattach(vId, ref, "r");
  107. Vgetname(id, nameGroup);
  108. if (name == std::string(nameGroup)) {
  109. ids.push_back(id);
  110. } else {
  111. Vdetach(id);
  112. }
  113. ref = Vgetid(vId, ref);
  114. }
  115. return ids;
  116. }
  117. std::vector<hdf4cpp::HdfItem> hdf4cpp::HdfFile::getAll(const std::string &name) const {
  118. const std::vector<int32> &dataset_ids = getDatasetIds(name);
  119. const std::vector<int32> &group_ids = getGroupDataIds(name);
  120. std::vector<HdfItem> items;
  121. items.reserve(dataset_ids.size() + group_ids.size());
  122. for (auto &id : dataset_ids) {
  123. items.push_back(HdfItem(new HdfItem::HdfDatasetItem(id, chain), sId, vId));
  124. }
  125. for (auto &id : group_ids) {
  126. items.push_back(HdfItem(new HdfItem::HdfGroupItem(id, chain), sId, vId));
  127. }
  128. return items;
  129. }
  130. hdf4cpp::HdfAttribute hdf4cpp::HdfFile::getAttribute(const std::string &name) const {
  131. return HdfAttribute(new HdfAttribute::HdfDatasetAttribute(sId, name, chain));
  132. }
  133. hdf4cpp::HdfFile::Iterator hdf4cpp::HdfFile::begin() const {
  134. return Iterator(this, 0, chain);
  135. }
  136. hdf4cpp::HdfFile::Iterator hdf4cpp::HdfFile::end() const {
  137. return Iterator(this, (int32)loneRefs.size(), chain);
  138. }
  139. hdf4cpp::HdfItem hdf4cpp::HdfFile::Iterator::operator*() {
  140. if (index < 0 || index >= (int)file->loneRefs.size()) {
  141. raiseException(OUT_OF_RANGE);
  142. }
  143. int32 ref = file->loneRefs[index].first;
  144. switch (file->loneRefs[index].second) {
  145. case VGROUP: {
  146. int32 id = Vattach(file->vId, ref, "r");
  147. return HdfItem(new HdfItem::HdfGroupItem(id, chain), file->sId, file->vId);
  148. }
  149. case VDATA: {
  150. int32 id = VSattach(file->vId, ref, "r");
  151. return HdfItem(new HdfItem::HdfDataItem(id, chain), file->sId, file->vId);
  152. }
  153. default: { raiseException(INVALID_OPERATION); }
  154. }
  155. }