HdfFile.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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.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(), 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) {
  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() {
  52. }
  53. int32 hdf4cpp::HdfFile::getSId() const {
  54. return sId;
  55. }
  56. int32 hdf4cpp::HdfFile::getVId() const {
  57. return vId;
  58. }
  59. int32 hdf4cpp::HdfFile::getDatasetId(const std::string &name) const {
  60. int32 index = SDnametoindex(sId, name.c_str());
  61. return (index == FAIL) ? (FAIL) : (SDselect(sId, index));
  62. }
  63. int32 hdf4cpp::HdfFile::getGroupId(const std::string &name) const {
  64. int32 ref = Vfind(vId, name.c_str());
  65. return (!ref) ? (FAIL) : (Vattach(vId, ref, "r"));
  66. }
  67. int32 hdf4cpp::HdfFile::getDataId(const std::string &name) const {
  68. int32 ref = VSfind(vId, name.c_str());
  69. return (!ref) ? (FAIL) : (VSattach(vId, ref, "r"));
  70. }
  71. hdf4cpp::HdfItem hdf4cpp::HdfFile::get(const std::string &name) const {
  72. int32 id = getDatasetId(name);
  73. if (id != FAIL) {
  74. return HdfItem(new HdfItem::HdfDatasetItem(id, chain), sId, vId);
  75. }
  76. id = getGroupId(name);
  77. if (id != FAIL) {
  78. return HdfItem(new HdfItem::HdfGroupItem(id, chain), sId, vId);
  79. }
  80. id = getDataId(name);
  81. if (id != FAIL) {
  82. return HdfItem(new HdfItem::HdfDataItem(id, chain), sId, vId);
  83. }
  84. raiseException(INVALID_ID);
  85. }
  86. std::vector<int32> hdf4cpp::HdfFile::getDatasetIds(const std::string &name) const {
  87. std::vector<int32> ids;
  88. char nameDataset[MAX_NAME_LENGTH];
  89. int32 datasets, waste;
  90. SDfileinfo(sId, &datasets, &waste);
  91. for (int32 i = 0; i < datasets; ++i) {
  92. int32 id = SDselect(sId, i);
  93. SDgetinfo(id, nameDataset, nullptr, nullptr, nullptr, nullptr);
  94. if (id != FAIL && name == std::string(nameDataset)) {
  95. ids.push_back(id);
  96. } else {
  97. SDendaccess(id);
  98. }
  99. }
  100. return ids;
  101. }
  102. std::vector<int32> hdf4cpp::HdfFile::getGroupDataIds(const std::string &name) const {
  103. std::vector<int32> ids;
  104. char nameGroup[MAX_NAME_LENGTH];
  105. int32 ref = Vgetid(vId, -1);
  106. while (ref != FAIL) {
  107. int32 id = Vattach(vId, ref, "r");
  108. Vgetname(id, nameGroup);
  109. if (name == std::string(nameGroup)) {
  110. ids.push_back(id);
  111. } else {
  112. Vdetach(id);
  113. }
  114. ref = Vgetid(vId, ref);
  115. }
  116. return ids;
  117. }
  118. std::vector<hdf4cpp::HdfItem> hdf4cpp::HdfFile::getAll(const std::string &name) const {
  119. std::vector<HdfItem> items;
  120. std::vector<int32> ids;
  121. ids = getDatasetIds(name);
  122. for (const auto &id : ids) {
  123. items.push_back(HdfItem(new HdfItem::HdfDatasetItem(id, chain), sId, vId));
  124. }
  125. ids = getGroupDataIds(name);
  126. for (const auto &id : ids) {
  127. items.push_back(HdfItem(new HdfItem::HdfGroupItem(id, chain), sId, vId));
  128. }
  129. return items;
  130. }
  131. hdf4cpp::HdfAttribute hdf4cpp::HdfFile::getAttribute(const std::string &name) const {
  132. return HdfAttribute(new HdfAttribute::HdfDatasetAttribute(sId, name, chain));
  133. }
  134. hdf4cpp::HdfFile::Iterator hdf4cpp::HdfFile::begin() const {
  135. return Iterator(this, 0, chain);
  136. }
  137. hdf4cpp::HdfFile::Iterator hdf4cpp::HdfFile::end() const {
  138. return Iterator(this, (int32)loneRefs.size(), chain);
  139. }
  140. hdf4cpp::HdfItem hdf4cpp::HdfFile::Iterator::operator*() {
  141. if (index < 0 || index >= (int)file->loneRefs.size()) {
  142. raiseException(OUT_OF_RANGE);
  143. }
  144. int32 ref = file->loneRefs[index].first;
  145. switch (file->loneRefs[index].second) {
  146. case VGROUP: {
  147. int32 id = Vattach(file->vId, ref, "r");
  148. return HdfItem(new HdfItem::HdfGroupItem(id, chain), file->sId, file->vId);
  149. }
  150. case VDATA: {
  151. int32 id = VSattach(file->vId, ref, "r");
  152. return HdfItem(new HdfItem::HdfDataItem(id, chain), file->sId, file->vId);
  153. }
  154. default: { raiseException(INVALID_OPERATION); }
  155. }
  156. }