dapcvt.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /*********************************************************************
  2. * Copyright 1993, UCAR/Unidata
  3. * See netcdf/COPYRIGHT file for copying and redistribution conditions.
  4. * $Header: /upc/share/CVS/netcdf-3/libncdap3/dapcvt.c,v 1.14 2009/11/29 00:16:26 dmh Exp $
  5. *********************************************************************/
  6. #include "config.h"
  7. #include "ncdap3.h"
  8. #include "dapodom.h"
  9. NCerror
  10. dapconvert3(nc_type srctype, nc_type dsttype, char* memory0, char* value0, size_t count)
  11. {
  12. NCerror ncstat = NC_NOERR;
  13. size_t i;
  14. char* memory = memory0;
  15. char* value = value0;
  16. /* In order to deal with the DAP upgrade problem,
  17. try to preserve the bit patterns
  18. */
  19. /* Provide space and pointer casts for intermediate results */
  20. signed char ncbyte;
  21. signed char* ncbytep;
  22. char ncchar;
  23. char* nccharp;
  24. short ncshort;
  25. short* ncshortp;
  26. int ncint;
  27. int* ncintp;
  28. float ncfloat;
  29. float* ncfloatp;
  30. double ncdouble;
  31. double* ncdoublep;
  32. unsigned char ncubyte;
  33. unsigned char* ncubytep;
  34. unsigned short ncushort;
  35. unsigned short* ncushortp;
  36. unsigned int ncuint;
  37. unsigned int* ncuintp;
  38. long long ncint64;
  39. long long* ncint64p;
  40. unsigned long long ncuint64;
  41. unsigned long long* ncuint64p;
  42. #define CASE(nc1,nc2) (nc1*256+nc2)
  43. #define CUT8(e) ((unsigned char)((e) & 0xff))
  44. #define CUT16(e) ((unsigned short)((e) & 0xffff))
  45. #define CUT32(e) ((unsigned int)((e) & 0xffffffff))
  46. #define ARM(vs,ncs,ts,vd,ncd,td) \
  47. case CASE(ncs,ncd):\
  48. vs##p = (ts *)value;\
  49. vs = *vs##p;\
  50. vd##p = (td *)memory;\
  51. *vd##p = (td)vs;\
  52. break;
  53. for(i=0;i<count;i++) {
  54. switch (CASE(srctype,dsttype)) {
  55. ARM(ncchar,NC_CHAR,char,ncchar,NC_CHAR,char)
  56. ARM(ncchar,NC_CHAR,char,ncbyte,NC_BYTE,signed char)
  57. ARM(ncchar,NC_CHAR,char,ncubyte,NC_UBYTE,unsigned char)
  58. ARM(ncchar,NC_CHAR,char,ncshort,NC_SHORT,short)
  59. ARM(ncchar,NC_CHAR,char,ncushort,NC_USHORT,unsigned short)
  60. ARM(ncchar,NC_CHAR,char,ncint,NC_INT,int)
  61. ARM(ncchar,NC_CHAR,char,ncuint,NC_UINT,unsigned int)
  62. ARM(ncchar,NC_CHAR,char,ncint64,NC_INT64,long long)
  63. ARM(ncchar,NC_CHAR,char,ncuint64,NC_UINT64,unsigned long long)
  64. ARM(ncchar,NC_CHAR,char,ncfloat,NC_FLOAT,float)
  65. ARM(ncchar,NC_CHAR,char,ncdouble,NC_DOUBLE,double)
  66. ARM(ncbyte,NC_BYTE,signed char,ncchar,NC_CHAR,char)
  67. ARM(ncbyte,NC_BYTE,signed char,ncbyte,NC_BYTE,signed char)
  68. ARM(ncbyte,NC_BYTE,signed char,ncubyte,NC_UBYTE,unsigned char)
  69. ARM(ncbyte,NC_BYTE,signed char,ncshort,NC_SHORT,short)
  70. ARM(ncbyte,NC_BYTE,signed char,ncushort,NC_USHORT,unsigned short)
  71. ARM(ncbyte,NC_BYTE,signed char,ncint,NC_INT,int)
  72. ARM(ncbyte,NC_BYTE,signed char,ncuint,NC_UINT,unsigned int)
  73. ARM(ncbyte,NC_BYTE,signed char,ncint64,NC_INT64,long long)
  74. ARM(ncbyte,NC_BYTE,signed char,ncuint64,NC_UINT64,unsigned long long)
  75. ARM(ncbyte,NC_BYTE,signed char,ncfloat,NC_FLOAT,float)
  76. ARM(ncbyte,NC_BYTE,signed char,ncdouble,NC_DOUBLE,double)
  77. ARM(ncubyte,NC_UBYTE,unsigned char,ncchar,NC_CHAR,char)
  78. ARM(ncubyte,NC_UBYTE,unsigned char,ncbyte,NC_BYTE,signed char)
  79. ARM(ncubyte,NC_UBYTE,unsigned char,ncubyte,NC_UBYTE,unsigned char)
  80. ARM(ncubyte,NC_UBYTE,unsigned char,ncshort,NC_SHORT,short)
  81. ARM(ncubyte,NC_UBYTE,unsigned char,ncushort,NC_USHORT,unsigned short)
  82. ARM(ncubyte,NC_UBYTE,unsigned char,ncint,NC_INT,int)
  83. ARM(ncubyte,NC_UBYTE,unsigned char,ncuint,NC_UINT,unsigned int)
  84. ARM(ncubyte,NC_UBYTE,unsigned char,ncint64,NC_INT64,long long)
  85. ARM(ncubyte,NC_UBYTE,unsigned char,ncuint64,NC_UINT64,unsigned long long)
  86. ARM(ncubyte,NC_UBYTE,unsigned char,ncfloat,NC_FLOAT,float)
  87. ARM(ncubyte,NC_UBYTE,unsigned char,ncdouble,NC_DOUBLE,double)
  88. ARM(ncshort,NC_SHORT,short,ncchar,NC_CHAR,char)
  89. ARM(ncshort,NC_SHORT,short,ncbyte,NC_BYTE,signed char)
  90. ARM(ncshort,NC_SHORT,short,ncubyte,NC_UBYTE,unsigned char)
  91. ARM(ncshort,NC_SHORT,short,ncshort,NC_SHORT,short)
  92. ARM(ncshort,NC_SHORT,short,ncushort,NC_USHORT,unsigned short)
  93. ARM(ncshort,NC_SHORT,short,ncint,NC_INT,int)
  94. ARM(ncshort,NC_SHORT,short,ncuint,NC_UINT,unsigned int)
  95. ARM(ncshort,NC_SHORT,short,ncint64,NC_INT64,long long)
  96. ARM(ncshort,NC_SHORT,short,ncuint64,NC_UINT64,unsigned long long)
  97. ARM(ncshort,NC_SHORT,short,ncfloat,NC_FLOAT,float)
  98. ARM(ncshort,NC_SHORT,short,ncdouble,NC_DOUBLE,double)
  99. ARM(ncushort,NC_USHORT,unsigned short,ncchar,NC_CHAR,char)
  100. ARM(ncushort,NC_USHORT,unsigned short,ncbyte,NC_BYTE,signed char)
  101. ARM(ncushort,NC_USHORT,unsigned short,ncubyte,NC_UBYTE,unsigned char)
  102. ARM(ncushort,NC_USHORT,unsigned short,ncshort,NC_SHORT,short)
  103. ARM(ncushort,NC_USHORT,unsigned short,ncushort,NC_USHORT,unsigned short)
  104. ARM(ncushort,NC_USHORT,unsigned short,ncint,NC_INT,int)
  105. ARM(ncushort,NC_USHORT,unsigned short,ncuint,NC_UINT,unsigned int)
  106. ARM(ncushort,NC_USHORT,unsigned short,ncint64,NC_INT64,long long)
  107. ARM(ncushort,NC_USHORT,unsigned short,ncuint64,NC_UINT64,unsigned long long)
  108. ARM(ncushort,NC_USHORT,unsigned short,ncfloat,NC_FLOAT,float)
  109. ARM(ncushort,NC_USHORT,unsigned short,ncdouble,NC_DOUBLE,double)
  110. ARM(ncint,NC_INT,int,ncchar,NC_CHAR,char)
  111. ARM(ncint,NC_INT,int,ncbyte,NC_BYTE,signed char)
  112. ARM(ncint,NC_INT,int,ncubyte,NC_UBYTE,unsigned char)
  113. ARM(ncint,NC_INT,int,ncshort,NC_SHORT,short)
  114. ARM(ncint,NC_INT,int,ncushort,NC_USHORT,unsigned short)
  115. ARM(ncint,NC_INT,int,ncint,NC_INT,int)
  116. ARM(ncint,NC_INT,int,ncuint,NC_UINT,unsigned int)
  117. ARM(ncint,NC_INT,int,ncint64,NC_INT64,long long)
  118. ARM(ncint,NC_INT,int,ncuint64,NC_UINT64,unsigned long long)
  119. ARM(ncint,NC_INT,int,ncfloat,NC_FLOAT,float)
  120. ARM(ncint,NC_INT,int,ncdouble,NC_DOUBLE,double)
  121. ARM(ncuint,NC_UINT,unsigned int,ncchar,NC_CHAR,char)
  122. ARM(ncuint,NC_UINT,unsigned int,ncbyte,NC_BYTE,signed char)
  123. ARM(ncuint,NC_UINT,unsigned int,ncubyte,NC_UBYTE,unsigned char)
  124. ARM(ncuint,NC_UINT,unsigned int,ncshort,NC_SHORT,short)
  125. ARM(ncuint,NC_UINT,unsigned int,ncushort,NC_USHORT,unsigned short)
  126. ARM(ncuint,NC_UINT,unsigned int,ncint,NC_INT,int)
  127. ARM(ncuint,NC_UINT,unsigned int,ncuint,NC_UINT,unsigned int)
  128. ARM(ncuint,NC_UINT,unsigned int,ncint64,NC_INT64,long long)
  129. ARM(ncuint,NC_UINT,unsigned int,ncuint64,NC_UINT64,unsigned long long)
  130. ARM(ncuint,NC_UINT,unsigned int,ncfloat,NC_FLOAT,float)
  131. ARM(ncuint,NC_UINT,unsigned int,ncdouble,NC_DOUBLE,double)
  132. ARM(ncint64,NC_INT64,long long,ncchar,NC_CHAR,char)
  133. ARM(ncint64,NC_INT64,long long,ncbyte,NC_BYTE,signed char)
  134. ARM(ncint64,NC_INT64,long long,ncubyte,NC_UBYTE,unsigned char)
  135. ARM(ncint64,NC_INT64,long long,ncshort,NC_SHORT,short)
  136. ARM(ncint64,NC_INT64,long long,ncushort,NC_USHORT,unsigned short)
  137. ARM(ncint64,NC_INT64,long long,ncint,NC_INT,int)
  138. ARM(ncint64,NC_INT64,long long,ncuint,NC_UINT,unsigned int)
  139. ARM(ncint64,NC_INT64,long long,ncint64,NC_INT64,long long)
  140. ARM(ncint64,NC_INT64,long long,ncuint64,NC_UINT64,unsigned long long)
  141. ARM(ncint64,NC_INT64,long long,ncfloat,NC_FLOAT,float)
  142. ARM(ncint64,NC_INT64,long long,ncdouble,NC_DOUBLE,double)
  143. ARM(ncuint64,NC_UINT64,unsigned long long,ncchar,NC_CHAR,char)
  144. ARM(ncuint64,NC_UINT64,unsigned long long,ncbyte,NC_BYTE,signed char)
  145. ARM(ncuint64,NC_UINT64,unsigned long long,ncubyte,NC_UBYTE,unsigned char)
  146. ARM(ncuint64,NC_UINT64,unsigned long long,ncshort,NC_SHORT,short)
  147. ARM(ncuint64,NC_UINT64,unsigned long long,ncushort,NC_USHORT,unsigned short)
  148. ARM(ncuint64,NC_UINT64,unsigned long long,ncint,NC_INT,int)
  149. ARM(ncuint64,NC_UINT64,unsigned long long,ncuint,NC_UINT,unsigned int)
  150. ARM(ncuint64,NC_UINT64,unsigned long long,ncint64,NC_INT64,long long)
  151. ARM(ncuint64,NC_UINT64,unsigned long long,ncuint64,NC_UINT64,unsigned long long)
  152. ARM(ncuint64,NC_UINT64,unsigned long long,ncfloat,NC_FLOAT,float)
  153. ARM(ncuint64,NC_UINT64,unsigned long long,ncdouble,NC_DOUBLE,double)
  154. ARM(ncfloat,NC_FLOAT,float,ncchar,NC_CHAR,char)
  155. ARM(ncfloat,NC_FLOAT,float,ncbyte,NC_BYTE,signed char)
  156. ARM(ncfloat,NC_FLOAT,float,ncubyte,NC_UBYTE,unsigned char)
  157. ARM(ncfloat,NC_FLOAT,float,ncshort,NC_SHORT,short)
  158. ARM(ncfloat,NC_FLOAT,float,ncushort,NC_USHORT,unsigned short)
  159. ARM(ncfloat,NC_FLOAT,float,ncint,NC_INT,int)
  160. ARM(ncfloat,NC_FLOAT,float,ncuint,NC_UINT,unsigned int)
  161. ARM(ncfloat,NC_FLOAT,float,ncint64,NC_INT64,long long)
  162. ARM(ncfloat,NC_FLOAT,float,ncuint64,NC_UINT64,unsigned long long)
  163. ARM(ncfloat,NC_FLOAT,float,ncfloat,NC_FLOAT,float)
  164. ARM(ncfloat,NC_FLOAT,float,ncdouble,NC_DOUBLE,double)
  165. ARM(ncdouble,NC_DOUBLE,double,ncchar,NC_CHAR,char)
  166. ARM(ncdouble,NC_DOUBLE,double,ncbyte,NC_BYTE,signed char)
  167. ARM(ncdouble,NC_DOUBLE,double,ncubyte,NC_UBYTE,unsigned char)
  168. ARM(ncdouble,NC_DOUBLE,double,ncshort,NC_SHORT,short)
  169. ARM(ncdouble,NC_DOUBLE,double,ncushort,NC_USHORT,unsigned short)
  170. ARM(ncdouble,NC_DOUBLE,double,ncint,NC_INT,int)
  171. ARM(ncdouble,NC_DOUBLE,double,ncuint,NC_UINT,unsigned int)
  172. ARM(ncdouble,NC_DOUBLE,double,ncint64,NC_INT64,long long)
  173. ARM(ncdouble,NC_DOUBLE,double,ncuint64,NC_UINT64,unsigned long long)
  174. ARM(ncdouble,NC_DOUBLE,double,ncfloat,NC_FLOAT,float)
  175. ARM(ncdouble,NC_DOUBLE,double,ncdouble,NC_DOUBLE,double)
  176. default: ncstat = NC_EINVAL; THROWCHK(ncstat); goto fail;
  177. }
  178. value += nctypesizeof(srctype);
  179. memory += nctypesizeof(dsttype);
  180. }
  181. fail:
  182. return THROW(ncstat);
  183. }
  184. NCerror
  185. dapcvtattrval3(nc_type etype, void* dst, NClist* src)
  186. {
  187. int i,ok;
  188. NCerror ncstat = NC_NOERR;
  189. unsigned int memsize = nctypesizeof(etype);
  190. unsigned int nvalues = nclistlength(src);
  191. char* dstmem = (char*)dst;
  192. for(i=0;i<nvalues;i++) {
  193. char* s = (char*)nclistget(src,i);
  194. ok = 0;
  195. switch (etype) {
  196. case NC_BYTE: {
  197. unsigned char* p = (unsigned char*)dstmem;
  198. ok = sscanf(s,"%hhu",p);
  199. } break;
  200. case NC_CHAR: {
  201. signed char* p = (signed char*)dstmem;
  202. ok = sscanf(s,"%c",p);
  203. } break;
  204. case NC_SHORT: {
  205. short* p = (short*)dstmem;
  206. ok = sscanf(s,"%hd",p);
  207. } break;
  208. case NC_INT: {
  209. int* p = (int*)dstmem;
  210. ok = sscanf(s,"%d",p);
  211. } break;
  212. case NC_FLOAT: {
  213. float* p = (float*)dstmem;
  214. ok = sscanf(s,"%g",p);
  215. } break;
  216. case NC_DOUBLE: {
  217. double* p = (double*)dstmem;
  218. ok = sscanf(s,"%lg",p);
  219. } break;
  220. case NC_UBYTE: {
  221. unsigned char* p = (unsigned char*)dstmem;
  222. ok = sscanf(s,"%hhu",p);
  223. } break;
  224. case NC_USHORT: {
  225. unsigned short* p = (unsigned short*)dstmem;
  226. ok = sscanf(s,"%hu",p);
  227. } break;
  228. case NC_UINT: {
  229. unsigned int* p = (unsigned int*)dstmem;
  230. ok = sscanf(s,"%u",p);
  231. } break;
  232. case NC_INT64: {
  233. long long* p = (long long*)dstmem;
  234. ok = sscanf(s,"%lld",p);
  235. } break;
  236. case NC_UINT64: {
  237. unsigned long long* p = (unsigned long long*)dstmem;
  238. ok = sscanf(s,"%llu",p);
  239. } break;
  240. case NC_STRING: case NC_URL: {
  241. char** p = (char**)dstmem;
  242. *p = nulldup(s);
  243. ok = 1;
  244. } break;
  245. default:
  246. PANIC1("unexpected nc_type: %d",(int)etype);
  247. }
  248. if(ok != 1) {ncstat = NC_EINVAL; goto done;}
  249. dstmem += memsize;
  250. }
  251. done:
  252. return THROW(ncstat);
  253. }