HdfAttribute.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. //
  2. // Created by patrik on 11.08.17.
  3. //
  4. #include <hdf4cpp/HdfDefines.h>
  5. #include <hdf4cpp/HdfAttribute.h>
  6. #include <stdexcept>
  7. hdf4cpp::HdfDatasetAttribute::HdfDatasetAttribute(int32 id, const std::string& name) : HdfAttributeBase(id, SDfindattr(id, name.c_str())) {
  8. if(index != FAIL) {
  9. int32 nrValues;
  10. char nameRet[MAX_NAME_LENGTH];
  11. SDattrinfo(id, index, nameRet, &dataType, &_size);
  12. } else {
  13. dataType = 0;
  14. _size = FAIL;
  15. }
  16. }
  17. hdf4cpp::Type hdf4cpp::HdfDatasetAttribute::getType() const {
  18. return VDATA;
  19. }
  20. intn hdf4cpp::HdfDatasetAttribute::size() const {
  21. return _size;
  22. }
  23. int32 hdf4cpp::HdfDatasetAttribute::getDataType() const {
  24. return dataType;
  25. }
  26. bool hdf4cpp::HdfDatasetAttribute::get(void *dest) {
  27. if(!isValid()) {
  28. return false;
  29. }
  30. int32 nrValues;
  31. char nameRet[MAX_NAME_LENGTH];
  32. int32 status = SDattrinfo(id, index, nameRet, &dataType, &nrValues);
  33. if(status == FAIL) {
  34. return false;
  35. }
  36. return SDreadattr(id, index, dest) != FAIL;
  37. }
  38. hdf4cpp::HdfGroupAttribute::HdfGroupAttribute(int32 id, const std::string& name) : HdfAttributeBase(id, -1), _size(FAIL) {
  39. int32 nrAtts = Vnattrs2(id);
  40. for(intn i = 0; i < nrAtts; ++i) {
  41. char names[MAX_NAME_LENGTH];
  42. int32 type, count, size, nFields;
  43. uint16 refNum;
  44. Vattrinfo2(id, i, names, &type, &count, &size, &nFields, &refNum);
  45. if(name == std::string(names)) {
  46. index = i;
  47. _size = count;
  48. dataType = type;
  49. break;
  50. }
  51. }
  52. }
  53. hdf4cpp::Type hdf4cpp::HdfGroupAttribute::getType() const {
  54. return VGROUP;
  55. }
  56. intn hdf4cpp::HdfGroupAttribute::size() const {
  57. return _size;
  58. }
  59. int32 hdf4cpp::HdfGroupAttribute::getDataType() const {
  60. return dataType;
  61. }
  62. bool hdf4cpp::HdfGroupAttribute::get(void *dest) {
  63. return Vgetattr2(id, index, dest) != FAIL;
  64. }
  65. hdf4cpp::HdfDataAttribute::HdfDataAttribute(int32 id, const std::string &name) : HdfAttributeBase(id, VSfindattr(id, _HDF_VDATA, name.c_str())) {
  66. if(index != FAIL) {
  67. int32 nrBytes;
  68. char _name[MAX_NAME_LENGTH];
  69. VSattrinfo(id, _HDF_VDATA, index, _name, &dataType, &_size, &nrBytes);
  70. } else {
  71. dataType = 0;
  72. _size = FAIL;
  73. }
  74. }
  75. hdf4cpp::Type hdf4cpp::HdfDataAttribute::getType() const {
  76. return VDATA;
  77. }
  78. intn hdf4cpp::HdfDataAttribute::size() const {
  79. return _size;
  80. }
  81. bool hdf4cpp::HdfDataAttribute::get(void *dest) {
  82. return VSgetattr(id, _HDF_VDATA, index, dest) != FAIL;
  83. }
  84. int32 hdf4cpp::HdfDataAttribute::getDataType() const {
  85. return dataType;
  86. }
  87. hdf4cpp::HdfAttribute::HdfAttribute(HdfAttribute &&other) : attribute(std::move(other.attribute)) {
  88. }
  89. bool hdf4cpp::HdfAttribute::isValid() const {
  90. return attribute && attribute->isValid();
  91. }
  92. hdf4cpp::Type hdf4cpp::HdfAttribute::getType() const {
  93. if(isValid()) {
  94. return attribute->getType();
  95. } else {
  96. return NONE;
  97. }
  98. }
  99. intn hdf4cpp::HdfAttribute::size() const {
  100. if(isValid()) {
  101. return attribute->size();
  102. } else {
  103. return FAIL;
  104. }
  105. }