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


HyperKWIC - Version 7.20DA executed at 11:37 on 27 Oct 2017 | Polyhedron Solutions - INTERNAL USE | COMMERCIAL (Any O/S) SN 4AKIed