HdfFile.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /// \copyright Copyright (c) Catalysts GmbH
  2. /// \author Patrik Kovacs, Catalysts GmbH
  3. #include <hdf/mfhdf.h>
  4. #include <stdexcept>
  5. #include <hdf4cpp/HdfDefines.h>
  6. #include <hdf4cpp/HdfFile.h>
  7. #include <hdf4cpp/HdfItem.h>
  8. #include <hdf4cpp/HdfAttribute_priv.h>
  9. #include <hdf4cpp/HdfException.h>
  10. hdf4cpp::HdfFile::HdfFile(const std::string& path) : HdfObject(HFILE, FILE) {
  11. sId = SDstart(path.c_str(), DFACC_READ);
  12. vId = Hopen(path.c_str(), DFACC_READ, 0);
  13. if(sId == FAIL || vId == FAIL) {
  14. raiseException(INVALID_ID);
  15. }
  16. Vstart(vId);
  17. chain.pushBack(new HdfDestroyer(&SDend, sId));
  18. chain.pushBack(new HdfDestroyer(&Vfinish, vId));
  19. chain.pushBack(new HdfDestroyer(&Hclose, vId));
  20. int32 loneSize = Vlone(vId, nullptr, 0);
  21. std::vector<int32> refs((size_t) loneSize);
  22. Vlone(vId, refs.data(), loneSize);
  23. for(const auto& ref : refs) {
  24. loneRefs.push_back(std::pair<int32, Type>(ref, VGROUP));
  25. }
  26. int32 loneVdata = VSlone(vId, nullptr, 0);
  27. refs.resize((size_t) loneVdata);
  28. VSlone(vId, refs.data(), loneVdata);
  29. for(const auto& ref : refs) {
  30. loneRefs.push_back(std::pair<int32, Type>(ref, VDATA));
  31. }
  32. }
  33. hdf4cpp::HdfFile::HdfFile(HdfFile&& file) : HdfObject(file.getType(), file.getClassType()) {
  34. sId = file.sId;
  35. vId = file.vId;
  36. loneRefs = file.loneRefs;
  37. file.sId = file.vId = FAIL;
  38. loneRefs.clear();
  39. }
  40. hdf4cpp::HdfFile& hdf4cpp::HdfFile::operator=(HdfFile&& file) {
  41. setType(file.getType());
  42. setClassType(file.getClassType());
  43. sId = file.sId;
  44. vId = file.vId;
  45. loneRefs = file.loneRefs;
  46. file.sId = file.vId = FAIL;
  47. file.loneRefs.clear();
  48. return *this;
  49. }
  50. hdf4cpp::HdfFile::~HdfFile() {
  51. }
  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) ? (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) ? (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. id = getGroupId(name);
  74. if(id == FAIL) {
  75. id = getDataId(name);
  76. if(id == FAIL) {
  77. raiseException(INVALID_ID);
  78. }
  79. return HdfItem(new HdfItem::HdfDataItem(id, chain), sId, vId);
  80. }
  81. return HdfItem(new HdfItem::HdfGroupItem(id, chain), sId, vId);
  82. }
  83. return HdfItem(new HdfItem::HdfDatasetItem(id, chain), sId, vId);
  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. std::vector<HdfItem> items;
  119. std::vector<int32> ids;
  120. ids = getDatasetIds(name);
  121. for(const auto& id : ids) {
  122. items.push_back(HdfItem(new HdfItem::HdfDatasetItem(id, chain), sId, vId));
  123. }
  124. ids = getGroupDataIds(name);
  125. for(const auto& id : ids) {
  126. items.push_back(HdfItem(new HdfItem::HdfGroupItem(id, chain), sId, vId));
  127. }
  128. return std::move(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: {
  154. raiseException(INVALID_OPERATION);
  155. }
  156. }
  157. }