HdfFile.h 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. int32 loneSize;
  32. std::vector<int32> loneRefs;
  33. };
  34. class HdfFile::Iterator : public std::iterator<std::bidirectional_iterator_tag, HdfItem> {
  35. public:
  36. Iterator(const HdfFile* file, int32 index) : file(file), index(index) {}
  37. bool operator!=(const Iterator& it) { return index != it.index; }
  38. bool operator==(const Iterator& it) { return index == it.index; }
  39. Iterator& operator++() {
  40. ++index;
  41. return *this;
  42. }
  43. Iterator operator++(int) {
  44. Iterator it(*this);
  45. ++index;
  46. return it;
  47. }
  48. Iterator& operator--() {
  49. --index;
  50. return *this;
  51. }
  52. Iterator operator--(int) {
  53. Iterator it(*this);
  54. --index;
  55. return it;
  56. }
  57. HdfItem operator*() {
  58. if(index < 0 || index >= file->loneSize) {
  59. throw std::runtime_error("HDF4CPP: cannot access invalid item");
  60. }
  61. int32 ref = file->loneRefs[index];
  62. int32 id = Vattach(file->vId, file->loneRefs[index], "r");
  63. return HdfItem(new HdfGroupItem(id), file->sId, file->vId);
  64. }
  65. private:
  66. const HdfFile *file;
  67. int32 index;
  68. };
  69. }
  70. #endif //GRASP_SEGMENTER_HDFFILE_H