1 /*********************************************************************
2  *   Copyright 2010, UCAR/Unidata
3  *   See netcdf/COPYRIGHT file for copying and redistribution conditions.
4  *********************************************************************/
5 
6 /* $Id: ncdispatch.h,v 1.18 2010/06/01 20:11:59 dmh Exp $ */
7 /* $Header: /upc/share/CVS/netcdf-3/libdispatch/ncdispatch.h,v 1.18 2010/06/01 20:11:59 dmh Exp $ */
8 
9 #ifndef _DISPATCH_H
10 #define _DISPATCH_H
11 
12 #include "config.h"
13 #include <stdlib.h>
14 #include <stdio.h>
15 #include <string.h>
16 #include <assert.h>
17 #ifdef USE_PARALLEL
18 #include "netcdf_par.h"
19 #endif
20 #include "xxxnetcdf.h"
21 #include "nc.h"
22 #include "nc_url.h"
23 
24 extern int nc_get_vara_ubyte(int ncid, int varid,
25                   const size_t* start, const size_t* count,
26 		  unsigned char* value);
27 extern int nc_get_vara_ushort(int ncid, int varid,
28                   const size_t* start, const size_t* count,
29 		  unsigned short* value);
30 extern int nc_get_vara_uint(int ncid, int varid,
31                   const size_t* start, const size_t* count,
32 		  unsigned int* value);
33 extern int nc_get_vara_ulonglong(int ncid, int varid,
34                   const size_t* start, const size_t* count,
35 		  unsigned long long* value);
36 
37 extern int nc_put_vara_ushort(int ncid, int varid,
38                   const size_t* start, const size_t* count,
39 		  const unsigned short* value);
40 extern int nc_put_vara_uint(int ncid, int varid,
41                   const size_t* start, const size_t* count,
42 		  const unsigned int* value);
43 extern int nc_put_vara_ulonglong(int ncid, int varid,
44                   const size_t* start, const size_t* count,
45 		  const unsigned long long* value);
46 
47 #define X_INT_MAX	2147483647
48 
49 /* Given a filename, check its magic number */
50 #define MAGIC_NUMBER_LEN 4
51 #define MAGIC_HDF5_FILE 1
52 #define MAGIC_HDF4_FILE 2
53 #define MAGIC_CDF1_FILE 1 /* std classic format */
54 #define MAGIC_CDF2_FILE 2 /* classic 64 bit */
55 
56 /* Define the mappings from fcn name types
57    to corresponding NC types. */
58 #define T_text   NC_CHAR
59 #define T_schar  NC_BYTE
60 #define T_char   NC_CHAR
61 #define T_short  NC_SHORT
62 #define T_int    NC_INT
63 #define T_float  NC_FLOAT
64 #define T_double NC_DOUBLE
65 #define T_ubyte  NC_UBYTE
66 #define T_ushort NC_USHORT
67 #define T_uint   NC_UINT
68 #define T_longlong  NC_INT64
69 #define T_ulonglong  NC_UINT64
70 #ifdef USE_NETCDF4
71 #define T_string NC_STRING
72 #endif
73 
74 /* Synthetic type to handle special memtypes */
75 #define T_uchar  NC_UBYTE
76 #define T_long   longtype
77 #define T_ulong   ulongtype
78 
79 /**************************************************/
80 /* Define the known classes of dispatchers */
81 /* Flags may be or'd => powers of 2*/
82 #define NC_DISPATCH_NC3    1
83 #define NC_DISPATCH_NC4    2
84 #define NC_DISPATCH_NCD    4
85 #define NC_DISPATCH_NCR    8
86 
87 /* Define a type for use when doing e.g. nc_get_vara_long, etc. */
88 /* Should matche values in libsrc4/xxxnetcdf.h */
89 #ifndef NC_UINT64
90 #define	NC_UBYTE 	7	/* unsigned 1 byte int */
91 #define	NC_USHORT 	8	/* unsigned 2-byte int */
92 #define	NC_UINT 	9	/* unsigned 4-byte int */
93 #define	NC_INT64 	10	/* signed 8-byte int */
94 #define	NC_UINT64 	11	/* unsigned 8-byte int */
95 #define	NC_STRING 	12	/* char* */
96 #endif
97 
98 #ifndef HAVE_LONGLONG
99 #define longlong long long
100 #define ulonglong unsigned long long
101 #endif
102 
103 /* Define the range of Atomic types */
104 #ifdef USE_NETCDF4
105 #define ATOMICTYPEMAX NC_STRING
106 #else
107 #define ATOMICTYPEMAX NC_DOUBLE
108 #endif
109 
110 /* Define an alias for int to indicate an error return */
111 typedef int NCerror;
112 
113 /* Define a struct to hold the MPI info so it can be passed down the
114  * call stack. This is used internally by the netCDF library. It
115  * should not be used by netcdf users. */
116 #ifdef USE_PARALLEL
117 typedef struct NC_MPI_INFO {
118     MPI_Comm comm;
119     MPI_Info info;
120 } NC_MPI_INFO;
121 #endif
122 
123 /* Define known dispatch tables */
124 /*Forward*/
125 typedef struct NC_Dispatch NC_Dispatch;
126 
127 extern NC_Dispatch* NC3_dispatch_table;
128 
129 #ifdef USE_NETCDF4
130 extern NC_Dispatch* NC4_dispatch_table;
131 #endif
132 
133 #ifdef USE_DAP
134 extern NC_Dispatch* NCD3_dispatch_table;
135 #endif
136 
137 #if defined(USE_DAP) && defined(USE_NETCDF4)
138 extern NC_Dispatch* NCD4_dispatch_table;
139 #endif
140 
141 #if defined(USE_CDMREMOTE) && defined(USE_NETCDF4)
142 extern NC_Dispatch* NCCR_dispatch_table;
143 #endif
144 
145 /**************************************************/
146 /* Forward */
147 #ifndef USE_NETCDF4
148 /* Taken from libsrc4/xxxnetcdf.h */
149 struct nc_vlen_t;
150 #define NC_NETCDF4 0x1000
151 #define NC_CLASSIC_MODEL 0x0100
152 #define NC_ENOPAR (-114)
153 #endif /*USE_NETCDF4*/
154 
155 struct NC;
156 
157 /* WARNING: this must match libsrc4/xxxnetcdf.h */
158 #ifndef USE_PARALLEL
159 #ifndef MPI_Comm
160 #define MPI_Comm int
161 #define MPI_Info int
162 #define MPI_COMM_WORLD 0
163 #ifndef MPI_INFO_NULL
164 #define MPI_INFO_NULL 0
165 #endif
166 #endif
167 #endif
168 
169 int NC_create(const char *path, int cmode,
170 	      size_t initialsz, int basepe, size_t *chunksizehintp,
171 	      int useparallel,void* mpi_info,
172 	      int *ncidp);
173 int NC_open(const char *path, int cmode,
174 	    int basepe, size_t *chunksizehintp,
175 	    int useparallel, void* mpi_info,
176 	    int *ncidp);
177 
178 /* Expose the default vars and varm dispatch entries */
179 extern int NCDEFAULT_get_vars(int, int, const size_t*,
180 	       const size_t*, const ptrdiff_t*, void*, nc_type);
181 extern int NCDEFAULT_put_vars(int, int, const size_t*,
182 	       const size_t*, const ptrdiff_t*, const void*, nc_type);
183 extern int NCDEFAULT_get_varm(int, int, const size_t*,
184                const size_t*, const ptrdiff_t*, const ptrdiff_t*,
185                void*, nc_type);
186 extern int NCDEFAULT_put_varm(int, int, const size_t*,
187                const size_t*, const ptrdiff_t*, const ptrdiff_t*,
188                const void*, nc_type);
189 
190 /**************************************************/
191 /* Forward */
192 struct NCHDR;
193 
194 struct NC_Dispatch {
195 
196 int model; /* one of the NC_DISPATCH #'s above */
197 
198 int (*new_nc)(struct NC**); /* Create an nc instance;free is not needed,
199 				because it can be done by close and abort*/
200 
201 /* Warning: these two will create appropriate NC instances
202    using new_nc dispatch function
203 */
204 int (*create)(const char *path, int cmode,
205 	  size_t initialsz, int basepe, size_t *chunksizehintp,
206 	  int use_parallel, void* parameters,
207 	  struct NC_Dispatch* table, NC** ncp);
208 int (*open)(const char *path, int mode,
209 	    int basepe, size_t *chunksizehintp,
210 	    int use_parallel, void* parameters,
211 	    struct NC_Dispatch* table, NC** ncp);
212 
213 int (*redef)(int);
214 int (*_enddef)(int,size_t,size_t,size_t,size_t);
215 int (*sync)(int);
216 int (*abort)(int);
217 int (*close)(int);
218 int (*set_fill)(int,int,int*);
219 int (*inq_base_pe)(int,int*);
220 int (*set_base_pe)(int,int);
221 int (*inq_format)(int,int*);
222 
223 int (*inq)(int,int*,int*,int*,int*);
224 int (*inq_type)(int, nc_type, char*, size_t*);
225 
226 int (*def_dim)(int, const char*, size_t, int*);
227 int (*inq_dimid)(int, const char*, int*);
228 int (*inq_dim)(int, int, char*, size_t*);
229 int (*inq_unlimdim)(int ncid,  int *unlimdimidp);
230 int (*rename_dim)(int, int, const char*);
231 
232 int (*inq_att)(int, int, const char*, nc_type*, size_t*);
233 int (*inq_attid)(int, int, const char*, int*);
234 int (*inq_attname)(int, int, int, char*);
235 int (*rename_att)(int, int, const char*, const char*);
236 int (*del_att)(int, int, const char*);
237 int (*get_att)(int, int, const char*, void*, nc_type);
238 int (*put_att)(int, int, const char*, nc_type, size_t, const void*, nc_type);
239 
240 int (*def_var)(int, const char*, nc_type, int, const int*, int*);
241 int (*inq_varid)(int, const char*, int*);
242 int (*rename_var)(int, int, const char*);
243 
244 int (*get_vara)(int, int, const size_t*, const size_t*, void*, nc_type);
245 int (*put_vara)(int, int, const size_t*, const size_t*, const void*, nc_type);
246 
247 /* Added to solve Ferret performance problem with Opendap */
248 int (*get_vars)(int, int, const size_t*, const size_t*, const ptrdiff_t*, void*, nc_type);
249 int (*put_vars)(int, int, const size_t*, const size_t*, const ptrdiff_t*, const void*, nc_type);
250 
251 int (*get_varm)(int, int, const size_t*, const size_t*, const ptrdiff_t*, const ptrdiff_t*, void*, nc_type);
252 int (*put_varm)(int, int, const size_t*, const size_t*, const ptrdiff_t*, const ptrdiff_t*, const void*, nc_type);
253 
254 
255 int (*inq_var_all)(int ncid, int varid, char *name, nc_type *xtypep,
256                int *ndimsp, int *dimidsp, int *nattsp,
257                int *shufflep, int *deflatep, int *deflate_levelp,
258                int *fletcher32p, int *contiguousp, size_t *chunksizesp,
259                int *no_fill, void *fill_valuep, int *endiannessp,
260 	       int *options_maskp, int *pixels_per_blockp);
261 
262 /* Note the following may still be invoked by netcdf client code
263    even when the file is a classic file
264 */
265 #ifdef USE_NETCDF4
266 int (*show_metadata)(int);
267 int (*inq_unlimdims)(int, int*, int*);
268 int (*var_par_access)(int, int, int);
269 int (*inq_ncid)(int, const char*, int*);
270 int (*inq_grps)(int, int*, int*);
271 int (*inq_grpname)(int, char*);
272 int (*inq_grpname_full)(int, size_t*, char*);
273 int (*inq_grp_parent)(int, int*);
274 int (*inq_grp_full_ncid)(int, const char*, int*);
275 int (*inq_varids)(int, int* nvars, int*);
276 int (*inq_dimids)(int, int* ndims, int*, int);
277 int (*inq_typeids)(int, int* ntypes, int*);
278 int (*inq_type_equal)(int, nc_type, int, nc_type, int*);
279 int (*def_grp)(int, const char*, int*);
280 int (*inq_user_type)(int, nc_type, char*, size_t*, nc_type*, size_t*, int*);
281 int (*inq_typeid)(int, const char*, nc_type*);
282 
283 int (*def_compound)(int, size_t, const char*, nc_type*);
284 int (*insert_compound)(int, nc_type, const char*, size_t, nc_type);
285 int (*insert_array_compound)(int, nc_type, const char*, size_t, nc_type, int, const int*);
286 int (*inq_compound_field)(int, nc_type, int, char*, size_t*, nc_type*, int*, int*);
287 int (*inq_compound_fieldindex)(int, nc_type, const char*, int*);
288 int (*def_vlen)(int, const char*, nc_type base_typeid, nc_type*);
289 int (*put_vlen_element)(int, int, void*, size_t, const void*);
290 int (*get_vlen_element)(int, int, const void*, size_t*, void*);
291 int (*def_enum)(int, nc_type, const char*, nc_type*);
292 int (*insert_enum)(int, nc_type, const char*, const void*);
293 int (*inq_enum_member)(int, nc_type, int, char*, void*);
294 int (*inq_enum_ident)(int, nc_type, long long, char*);
295 int (*def_opaque)(int, size_t, const char*, nc_type*);
296 int (*def_var_deflate)(int, int, int, int, int);
297 int (*def_var_fletcher32)(int, int, int);
298 int (*def_var_chunking)(int, int, int, const size_t*);
299 int (*def_var_fill)(int, int, int, const void*);
300 int (*def_var_endian)(int, int, int);
301 int (*set_var_chunk_cache)(int, int, size_t, size_t, float);
302 int (*get_var_chunk_cache)(int ncid, int varid, size_t *sizep, size_t *nelemsp, float *preemptionp);
303 #endif /*USE_NETCDF4*/
304 
305 };
306 
307 /* Following functions must be handled as non-dispatch */
308 #ifdef NONDISPATCH
309 void(*nc_advise)(const char*cdf_routine_name,interr,const char*fmt,...);
310 void(*nc_set_log_level)(int);
311 const char* (*nc_inq_libvers)(void);
312 const char* (*nc_strerror)(int);
313 int(*nc_delete)(const char*path);
314 int(*nc_delete_mp)(const char*path,intbasepe);
315 #endif /*NONDISPATCH*/
316 
317 /* Define the common fields for NC and NC_FILE_INFO_T etc */
318 typedef struct NCcommon {
319 	int ext_ncid; /* uid << 16 */
320 	int int_ncid; /* unspecified other id */
321 	struct NC_Dispatch* dispatch;
322 #ifdef USE_DAP
323 	struct NCDRNO* drno;
324 #endif
325 } NCcommon;
326 
327 extern int NC_atomictypelen(nc_type xtype);
328 extern char* NC_atomictypename(nc_type xtype);
329 
330 /* Provide an initializer */
331 extern int NC_initialize(void);
332 
333 /* Provide a dispatch table overlay facility */
334 extern int NC_dispatch_overlay(const NC_Dispatch* overlay,
335                                         const NC_Dispatch* base,
336 					NC_Dispatch* merge);
337 
338 /* Get/set the override dispatch table */
339 extern NC_Dispatch* NC_get_dispatch_override(void);
340 extern void NC_set_dispatch_override(NC_Dispatch*);
341 
342 /* Does the path look like a url? */
343 extern int NC_testurl(const char* path);
344 /* Return model (0 or 3 or 4) as specified by the url */
345 extern int NC_urlmodel(const char* path);
346 
347 /* allow access dapurlparse and params without exposing dapurl.h */
348 extern int NCDAP_urlparse(const char* s, void** dapurl);
349 extern void NCDAP_urlfree(void* dapurl);
350 extern const char* NCDAP_urllookup(void* dapurl, const char* param);
351 
352 /* Misc */
353 /* Replacement for strdup (in libsrc) */
354 #ifdef HAVE_STRDUP
355 #define nulldup(s) ((s)==NULL?NULL:strdup(s))
356 #else
357 extern char* nulldup(const char*);
358 #endif
359 
360 #define nulllen(s) (s==NULL?0:strlen(s))
361 #define nullstring(s) (s==NULL?"(null)":s)
362 
363 #endif /* _DISPATCH_H */
364 
365