HdfFile.h 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. class HdfFile {
  11. public:
  12. HdfFile(const std::string& path);
  13. ~HdfFile();
  14. bool isValid();
  15. HdfItem get(const std::string& name);
  16. std::vector<HdfItem> getAll(const std::string& name);
  17. HdfAttribute getAttribute(const std::string& name);
  18. class Iterator;
  19. Iterator begin() const;
  20. Iterator end() const;
  21. private:
  22. int32 getDatasetId(const std::string& name);
  23. int32 getGroupId(const std::string& name);
  24. std::vector<int32> getDatasetIds(const std::string& name);
  25. std::vector<int32> getGroupIds(const std::string& name);
  26. int32 sId;
  27. int32 vId;
  28. int32 loneSize;
  29. std::vector<int32> loneRefs;
  30. };
  31. class HdfFile::Iterator : public std::iterator<std::bidirectional_iterator_tag, HdfItem> {
  32. public:
  33. Iterator(const HdfFile* file, int32 index) : file(file), index(index) {}
  34. bool operator!=(const Iterator& it) { return index != it.index; }
  35. bool operator==(const Iterator& it) { return index == it.index; }
  36. Iterator& operator++() {
  37. ++index;
  38. return *this;
  39. }
  40. Iterator operator++(int) {
  41. Iterator it(*this);
  42. ++index;
  43. return it;
  44. }
  45. Iterator& operator--() {
  46. --index;
  47. return *this;
  48. }
  49. Iterator operator--(int) {
  50. Iterator it(*this);
  51. --index;
  52. return it;
  53. }
  54. HdfItem operator*() {
  55. if(index < 0 || index >= file->loneSize) {
  56. throw std::runtime_error("HDF4CPP: cannot access invalid item");
  57. }
  58. int32 ref = file->loneRefs[index];
  59. int32 id = Vattach(file->vId, file->loneRefs[index], "r");
  60. return HdfItem(new HdfGroupItem(id), file->sId, file->vId);
  61. }
  62. private:
  63. const HdfFile *file;
  64. int32 index;
  65. };
  66. #endif //GRASP_SEGMENTER_HDFFILE_H