HdfFile.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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/HdfException.h>
  9. hdf4cpp::HdfFile::HdfFile(const std::string& path) : HdfObject(HFILE, FILE) {
  10. sId = SDstart(path.c_str(), DFACC_READ);
  11. vId = Hopen(path.c_str(), DFACC_READ, 0);
  12. if(sId == FAIL || vId == FAIL) {
  13. throw HdfException(getType(), getClassType(), INVALID_ID);
  14. }
  15. Vstart(vId);
  16. int32 loneSize = Vlone(vId, nullptr, 0);
  17. std::vector<int32> refs((size_t) loneSize);
  18. Vlone(vId, refs.data(), loneSize);
  19. for(const auto& ref : refs) {
  20. loneRefs.push_back(std::pair<int32, Type>(ref, VGROUP));
  21. }
  22. int32 loneVdata = VSlone(vId, nullptr, 0);
  23. refs.resize((size_t) loneVdata);
  24. VSlone(vId, refs.data(), loneVdata);
  25. for(const auto& ref : refs) {
  26. loneRefs.push_back(std::pair<int32, Type>(ref, VDATA));
  27. }
  28. }
  29. hdf4cpp::HdfFile::HdfFile(HdfFile&& file) : HdfObject(file.getType(), file.getClassType()) {
  30. sId = file.sId;
  31. vId = file.vId;
  32. loneRefs = file.loneRefs;
  33. file.sId = file.vId = FAIL;
  34. loneRefs.clear();
  35. }
  36. hdf4cpp::HdfFile& hdf4cpp::HdfFile::operator=(HdfFile&& file) {
  37. destroy();
  38. setType(file.getType());
  39. setClassType(file.getClassType());
  40. sId = file.sId;
  41. vId = file.vId;
  42. loneRefs = file.loneRefs;
  43. file.sId = file.vId = FAIL;
  44. file.loneRefs.clear();
  45. return *this;
  46. }
  47. void hdf4cpp::HdfFile::destroy() {
  48. SDend(sId);
  49. Vend(vId);
  50. Hclose(vId);
  51. }
  52. hdf4cpp::HdfFile::~HdfFile() {
  53. destroy();
  54. }
  55. int32 hdf4cpp::HdfFile::getSId() const {
  56. return sId;
  57. }
  58. int32 hdf4cpp::HdfFile::getVId() const {
  59. return vId;
  60. }
  61. int32 hdf4cpp::HdfFile::getDatasetId(const std::string &name) const {
  62. int32 index = SDnametoindex(sId, name.c_str());
  63. return (index == FAIL) ? (FAIL) : (SDselect(sId, index));
  64. }
  65. int32 hdf4cpp::HdfFile::getGroupId(const std::string &name) const {
  66. int32 ref = Vfind(vId, name.c_str());
  67. return (!ref) ? (FAIL) : (Vattach(vId, ref, "r"));
  68. }
  69. int32 hdf4cpp::HdfFile::getDataId(const std::string &name) const {
  70. int32 ref = VSfind(vId, name.c_str());
  71. return (!ref) ? (FAIL) : (VSattach(vId, ref, "r"));
  72. }
  73. hdf4cpp::HdfItem hdf4cpp::HdfFile::get(const std::string& name) const {
  74. int32 id = getDatasetId(name);
  75. if(id == FAIL) {
  76. id = getGroupId(name);
  77. if(id == FAIL) {
  78. id = getDataId(name);
  79. if(id == FAIL) {
  80. raiseException(INVALID_ID);
  81. }
  82. return HdfItem(new HdfDataItem(id), sId, vId);
  83. }
  84. return HdfItem(new HdfGroupItem(id), sId, vId);
  85. }
  86. return HdfItem(new HdfDatasetItem(id), sId, vId);
  87. }
  88. std::vector<int32> hdf4cpp::HdfFile::getDatasetIds(const std::string &name) const {
  89. std::vector<int32> ids;
  90. char nameDataset[MAX_NAME_LENGTH];
  91. int32 datasets, waste;
  92. SDfileinfo(sId, &datasets, &waste);
  93. for(int32 i = 0; i < datasets; ++i) {
  94. int32 id = SDselect(sId, i);
  95. SDgetinfo(id, nameDataset, nullptr, nullptr, nullptr, nullptr);
  96. if(id != FAIL && name == std::string(nameDataset)) {
  97. ids.push_back(id);
  98. } else {
  99. SDendaccess(id);
  100. }
  101. }
  102. return ids;
  103. }
  104. std::vector<int32> hdf4cpp::HdfFile::getGroupIds(const std::string &name) const {
  105. std::vector<int32> ids;
  106. char nameGroup[MAX_NAME_LENGTH];
  107. int32 ref = Vgetid(vId, -1);
  108. while(ref != FAIL) {
  109. int32 id = Vattach(vId, ref, "r");
  110. Vgetname(id, nameGroup);
  111. if(Visvg(id, ref) && name == std::string(nameGroup)) {
  112. ids.push_back(id);
  113. } else {
  114. Vdetach(id);
  115. }
  116. ref = Vgetid(vId, ref);
  117. }
  118. return ids;
  119. }
  120. std::vector<hdf4cpp::HdfItem> hdf4cpp::HdfFile::getAll(const std::string& name) const {
  121. std::vector<HdfItem> items;
  122. std::vector<int32> ids;
  123. ids = getDatasetIds(name);
  124. for(const auto& id : ids) {
  125. items.push_back(HdfItem(new HdfDatasetItem(id), sId, vId));
  126. }
  127. ids = getGroupIds(name);
  128. for(const auto& id : ids) {
  129. items.push_back(HdfItem(new HdfGroupItem(id), sId, vId));
  130. }
  131. return std::move(items);
  132. }
  133. hdf4cpp::HdfAttribute hdf4cpp::HdfFile::getAttribute(const std::string &name) {
  134. return HdfAttribute(new HdfDatasetAttribute(sId, name));
  135. }
  136. hdf4cpp::HdfFile::Iterator hdf4cpp::HdfFile::begin() const {
  137. return Iterator(this, 0);
  138. }
  139. hdf4cpp::HdfFile::Iterator hdf4cpp::HdfFile::end() const {
  140. return Iterator(this, (int32) loneRefs.size());
  141. }