dvlen.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*! \file
  2. Functions for VLEN Types
  3. Copyright 2011 University Corporation for Atmospheric
  4. Research/Unidata. See \ref copyright file for more info. */
  5. #include "ncdispatch.h"
  6. /** \name Variable Length Array Types
  7. Functions to create and learn about VLEN types. */
  8. /*! \{ */ /* All these functions are part of this named group... */
  9. /**
  10. \ingroup user_types
  11. Free memory in a VLEN object.
  12. When you read VLEN type the library will actually allocate the storage
  13. space for the data. This storage space must be freed, so pass the
  14. pointer back to this function, when you're done with the data, and it
  15. will free the vlen memory.
  16. The function nc_free_vlens() is more useful than this function,
  17. because it can free an array of VLEN objects.
  18. \param vl pointer to the vlen object.
  19. \returns ::NC_NOERR No error.
  20. \returns ::NC_EBADID Bad \ref ncid.
  21. \returns ::NC_EBADTYPE Bad type id.
  22. \returns ::NC_ENOTNC4 Not an netCDF-4 file, or classic model enabled.
  23. \returns ::NC_EHDFERR An error was reported by the HDF5 layer.
  24. */
  25. int
  26. nc_free_vlen(nc_vlen_t *vl)
  27. {
  28. free(vl->p);
  29. return NC_NOERR;
  30. }
  31. /**
  32. \ingroup user_types
  33. Free an array of vlens given the number of elements and an array.
  34. When you read VLEN type the library will actually allocate the storage
  35. space for the data. This storage space must be freed, so pass the
  36. pointer back to this function, when you're done with the data, and it
  37. will free the vlen memory.
  38. \param len number of elements in the array.
  39. \param vlens pointer to the vlen object.
  40. \returns ::NC_NOERR No error.
  41. \returns ::NC_EBADID Bad \ref ncid.
  42. \returns ::NC_EBADTYPE Bad type id.
  43. \returns ::NC_ENOTNC4 Not an netCDF-4 file, or classic model enabled.
  44. \returns ::NC_EHDFERR An error was reported by the HDF5 layer.
  45. */
  46. int
  47. nc_free_vlens(size_t len, nc_vlen_t vlens[])
  48. {
  49. int ret;
  50. size_t i;
  51. for(i = 0; i < len; i++)
  52. if ((ret = nc_free_vlen(&vlens[i])))
  53. return ret;
  54. return NC_NOERR;
  55. }
  56. /**
  57. \ingroup user_types
  58. Use this function to define a variable length array type.
  59. \param ncid \ref ncid
  60. \param name \ref object_name of new type.
  61. \param base_typeid The typeid of the base type of the VLEN. For
  62. example, for a VLEN of shorts, the base type is ::NC_SHORT. This can be
  63. a user defined type.
  64. \param xtypep A pointer to an nc_type variable. The typeid of the new
  65. VLEN type will be set here.
  66. \returns ::NC_NOERR No error.
  67. \returns ::NC_EBADID Bad \ref ncid.
  68. \returns ::NC_EBADTYPE Bad type id.
  69. \returns ::NC_ENOTNC4 Not an netCDF-4 file, or classic model enabled.
  70. \returns ::NC_EHDFERR An error was reported by the HDF5 layer.
  71. \returns ::NC_ENAMEINUSE That name is in use.
  72. \returns ::NC_EMAXNAME Name exceeds max length NC_MAX_NAME.
  73. \returns ::NC_EBADNAME Name contains illegal characters.
  74. \returns ::NC_EPERM Attempt to write to a read-only file.
  75. \returns ::NC_ENOTINDEFINE Not in define mode.
  76. */
  77. int
  78. nc_def_vlen(int ncid, const char *name, nc_type base_typeid, nc_type *xtypep)
  79. {
  80. NC* ncp;
  81. int stat = NC_check_id(ncid,&ncp);
  82. if(stat != NC_NOERR) return stat;
  83. return ncp->dispatch->def_vlen(ncid,name,base_typeid,xtypep);
  84. }
  85. /** \ingroup user_types
  86. Learn about a VLEN type.
  87. \param ncid \ref ncid
  88. \param xtype The type of the VLEN to inquire about.
  89. \param name \ref object_name of the type. \ref ignored_if_null.
  90. \param datum_sizep A pointer to a size_t, this will get the size of
  91. one element of this vlen. \ref ignored_if_null.
  92. \param base_nc_typep Pointer to get the base type of the VLEN. \ref
  93. ignored_if_null.
  94. \returns ::NC_NOERR No error.
  95. \returns ::NC_EBADID Bad \ref ncid.
  96. \returns ::NC_EBADTYPE Bad type id.
  97. \returns ::NC_ENOTNC4 Not an netCDF-4 file, or classic model enabled.
  98. \returns ::NC_EHDFERR An error was reported by the HDF5 layer.
  99. */
  100. int
  101. nc_inq_vlen(int ncid, nc_type xtype, char *name, size_t *datum_sizep, nc_type *base_nc_typep)
  102. {
  103. int class = 0;
  104. int stat = nc_inq_user_type(ncid,xtype,name,datum_sizep,base_nc_typep,NULL,&class);
  105. if(stat != NC_NOERR) return stat;
  106. if(class != NC_VLEN) stat = NC_EBADTYPE;
  107. return stat;
  108. }
  109. /*! \} */ /* End of named group ...*/
  110. /** \internal
  111. \ingroup user_types
  112. Put a VLEN element. This function writes an element of a VLEN for the
  113. Fortran APIs.
  114. \param ncid \ref ncid
  115. \param typeid1 Typeid of the VLEN.
  116. \param vlen_element Pointer to the element of the VLEN.
  117. \param len Lenth of the VLEN element.
  118. \param data VLEN data.
  119. \returns ::NC_NOERR No error.
  120. \returns ::NC_EBADID Bad \ref ncid.
  121. \returns ::NC_EBADTYPE Bad type id.
  122. \returns ::NC_ENOTNC4 Not an netCDF-4 file, or classic model enabled.
  123. \returns ::NC_EHDFERR An error was reported by the HDF5 layer.
  124. \returns ::NC_EPERM Attempt to write to a read-only file.
  125. */
  126. int
  127. nc_put_vlen_element(int ncid, int typeid1, void *vlen_element, size_t len, const void *data)
  128. {
  129. NC* ncp;
  130. int stat = NC_check_id(ncid,&ncp);
  131. if(stat != NC_NOERR) return stat;
  132. return ncp->dispatch->put_vlen_element(ncid,typeid1,vlen_element,len,data);
  133. }
  134. /**
  135. \internal
  136. \ingroup user_types
  137. Get a VLEN element. This function reads an element of a VLEN for the
  138. Fortran APIs.
  139. \param ncid \ref ncid
  140. \param typeid1 Typeid of the VLEN.
  141. \param vlen_element Pointer to the element of the VLEN.
  142. \param len Lenth of the VLEN element.
  143. \param data VLEN data.
  144. \returns ::NC_NOERR No error.
  145. \returns ::NC_EBADID Bad \ref ncid.
  146. \returns ::NC_EBADTYPE Bad type id.
  147. \returns ::NC_ENOTNC4 Not an netCDF-4 file, or classic model enabled.
  148. \returns ::NC_EHDFERR An error was reported by the HDF5 layer.
  149. */
  150. int
  151. nc_get_vlen_element(int ncid, int typeid1, const void *vlen_element,
  152. size_t *len, void *data)
  153. {
  154. NC *ncp;
  155. int stat = NC_check_id(ncid,&ncp);
  156. if(stat != NC_NOERR) return stat;
  157. return ncp->dispatch->get_vlen_element(ncid, typeid1, vlen_element,
  158. len, data);
  159. }