HdfFile.cpp 4.4 KB

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