HdfAttribute.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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::HdfAttribute::HdfAttribute(HdfAttribute &&other) : attribute(std::move(other.attribute)) {
  66. }
  67. bool hdf4cpp::HdfAttribute::isValid() const {
  68. return attribute && attribute->isValid();
  69. }
  70. hdf4cpp::Type hdf4cpp::HdfAttribute::getType() const {
  71. if(isValid()) {
  72. return attribute->getType();
  73. } else {
  74. return NONE;
  75. }
  76. }
  77. intn hdf4cpp::HdfAttribute::size() const {
  78. if(isValid()) {
  79. return attribute->size();
  80. } else {
  81. return FAIL;
  82. }
  83. }