HdfFile.cpp 5.3 KB

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