HdfFile.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. //
  2. // Created by patrik on 10.08.17.
  3. //
  4. #include <hdf/mfhdf.h>
  5. #include <stdexcept>
  6. #include <hdf4cpp/HdfDefines.h>
  7. #include <hdf4cpp/HdfFile.h>
  8. #include <hdf4cpp/HdfItem.h>
  9. #include <hdf4cpp/HdfAttribute_priv.h>
  10. #include <hdf4cpp/HdfException.h>
  11. hdf4cpp::HdfFile::HdfFile(const std::string& path) : 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. Vstart(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) : HdfObject(file.getType(), file.getClassType()) {
  35. sId = file.sId;
  36. vId = file.vId;
  37. loneRefs = file.loneRefs;
  38. file.sId = file.vId = FAIL;
  39. loneRefs.clear();
  40. }
  41. hdf4cpp::HdfFile& hdf4cpp::HdfFile::operator=(HdfFile&& file) {
  42. setType(file.getType());
  43. setClassType(file.getClassType());
  44. sId = file.sId;
  45. vId = file.vId;
  46. loneRefs = file.loneRefs;
  47. file.sId = file.vId = FAIL;
  48. file.loneRefs.clear();
  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. id = getGroupId(name);
  75. if(id == FAIL) {
  76. id = getDataId(name);
  77. if(id == FAIL) {
  78. raiseException(INVALID_ID);
  79. }
  80. return HdfItem(new HdfItem::HdfDataItem(id, chain), sId, vId);
  81. }
  82. return HdfItem(new HdfItem::HdfGroupItem(id, chain), sId, vId);
  83. }
  84. return HdfItem(new HdfItem::HdfDatasetItem(id, chain), sId, vId);
  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 std::move(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: {
  155. raiseException(INVALID_OPERATION);
  156. }
  157. }
  158. }