HdfFile.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. //
  2. // Created by patrik on 10.08.17.
  3. //
  4. #ifndef GRASP_SEGMENTER_HDFFILE_H
  5. #define GRASP_SEGMENTER_HDFFILE_H
  6. #include <string>
  7. #include <vector>
  8. #include <hdf4cpp/HdfItem.h>
  9. #include <hdf4cpp/HdfAttribute.h>
  10. namespace hdf4cpp {
  11. class HdfFile {
  12. public:
  13. HdfFile(const std::string& path);
  14. ~HdfFile();
  15. bool isValid();
  16. HdfItem get(const std::string& name);
  17. std::vector<HdfItem> getAll(const std::string& name);
  18. HdfAttribute getAttribute(const std::string& name);
  19. class Iterator;
  20. Iterator begin() const;
  21. Iterator end() const;
  22. private:
  23. int32 getDatasetId(const std::string& name);
  24. int32 getGroupId(const std::string& name);
  25. int32 getDataId(const std::string& name);
  26. std::vector<int32> getDatasetIds(const std::string& name);
  27. std::vector<int32> getGroupIds(const std::string& name);
  28. std::vector<int32> getDataIds(const std::string& name);
  29. int32 sId;
  30. int32 vId;
  31. std::vector<std::pair<int32, Type> > loneRefs;
  32. };
  33. class HdfFile::Iterator : public std::iterator<std::bidirectional_iterator_tag, HdfItem> {
  34. public:
  35. Iterator(const HdfFile* file, int32 index) : file(file), index(index) {}
  36. bool operator!=(const Iterator& it) { return index != it.index; }
  37. bool operator==(const Iterator& it) { return index == it.index; }
  38. Iterator& operator++() {
  39. ++index;
  40. return *this;
  41. }
  42. Iterator operator++(int) {
  43. Iterator it(*this);
  44. ++index;
  45. return it;
  46. }
  47. Iterator& operator--() {
  48. --index;
  49. return *this;
  50. }
  51. Iterator operator--(int) {
  52. Iterator it(*this);
  53. --index;
  54. return it;
  55. }
  56. HdfItem operator*() {
  57. if(index < 0 || index >= file->loneRefs.size()) {
  58. throw std::runtime_error("HDF4CPP: cannot access invalid item");
  59. }
  60. int32 ref = file->loneRefs[index].first;
  61. switch(file->loneRefs[index].second) {
  62. case VGROUP: {
  63. int32 id = Vattach(file->vId, ref, "r");
  64. return HdfItem(new HdfGroupItem(id), file->sId, file->vId);
  65. }
  66. case VDATA: {
  67. int32 id = VSattach(file->vId, ref, "r");
  68. return HdfItem(new HdfDataItem(id), file->sId, file->vId);
  69. }
  70. default: {
  71. return HdfItem(nullptr, file->sId, file->vId);
  72. }
  73. }
  74. }
  75. private:
  76. const HdfFile *file;
  77. int32 index;
  78. };
  79. }
  80. #endif //GRASP_SEGMENTER_HDFFILE_H