netcdf-c/ncgen/cvt.c

668 lines
19 KiB
C
Raw Normal View History

2010-06-03 21:24:43 +08:00
/*********************************************************************
2018-12-07 06:40:43 +08:00
* Copyright 2018, UCAR/Unidata
2010-06-03 21:24:43 +08:00
* See netcdf/COPYRIGHT file for copying and redistribution conditions.
*********************************************************************/
/* $Id: cvt.c,v 1.2 2010/05/24 19:59:56 dmh Exp $ */
/* $Header: /upc/share/CVS/netcdf-3/ncgen/cvt.c,v 1.2 2010/05/24 19:59:56 dmh Exp $ */
This PR adds EXPERIMENTAL support for accessing data in the cloud using a variant of the Zarr protocol and storage format. This enhancement is generically referred to as "NCZarr". The data model supported by NCZarr is netcdf-4 minus the user-defined types and the String type. In this sense it is similar to the CDF-5 data model. More detailed information about enabling and using NCZarr is described in the document NUG/nczarr.md and in a [Unidata Developer's blog entry](https://www.unidata.ucar.edu/blogs/developer/en/entry/overview-of-zarr-support-in). WARNING: this code has had limited testing, so do use this version for production work. Also, performance improvements are ongoing. Note especially the following platform matrix of successful tests: Platform | Build System | S3 support ------------------------------------ Linux+gcc | Automake | yes Linux+gcc | CMake | yes Visual Studio | CMake | no Additionally, and as a consequence of the addition of NCZarr, major changes have been made to the Filter API. NOTE: NCZarr does not yet support filters, but these changes are enablers for that support in the future. Note that it is possible (probable?) that there will be some accidental reversions if the changes here did not correctly mimic the existing filter testing. In any case, previously filter ids and parameters were of type unsigned int. In order to support the more general zarr filter model, this was all converted to char*. The old HDF5-specific, unsigned int operations are still supported but they are wrappers around the new, char* based nc_filterx_XXX functions. This entailed at least the following changes: 1. Added the files libdispatch/dfilterx.c and include/ncfilter.h 2. Some filterx utilities have been moved to libdispatch/daux.c 3. A new entry, "filter_actions" was added to the NCDispatch table and the version bumped. 4. An overly complex set of structs was created to support funnelling all of the filterx operations thru a single dispatch "filter_actions" entry. 5. Move common code to from libhdf5 to libsrc4 so that it is accessible to nczarr. Changes directly related to Zarr: 1. Modified CMakeList.txt and configure.ac to support both C and C++ -- this is in support of S3 support via the awd-sdk libraries. 2. Define a size64_t type to support nczarr. 3. More reworking of libdispatch/dinfermodel.c to support zarr and to regularize the structure of the fragments section of a URL. Changes not directly related to Zarr: 1. Make client-side filter registration be conditional, with default off. 2. Hack include/nc4internal.h to make some flags added by Ed be unique: e.g. NC_CREAT, NC_INDEF, etc. 3. cleanup include/nchttp.h and libdispatch/dhttp.c. 4. Misc. changes to support compiling under Visual Studio including: * Better testing under windows for dirent.h and opendir and closedir. 5. Misc. changes to the oc2 code to support various libcurl CURLOPT flags and to centralize error reporting. 6. By default, suppress the vlen tests that have unfixed memory leaks; add option to enable them. 7. Make part of the nc_test/test_byterange.sh test be contingent on remotetest.unidata.ucar.edu being accessible. Changes Left TO-DO: 1. fix provenance code, it is too HDF5 specific.
2020-06-29 08:02:47 +08:00
#include "includes.h"
#include "bytebuffer.h"
#include "isnan.h"
This PR adds EXPERIMENTAL support for accessing data in the cloud using a variant of the Zarr protocol and storage format. This enhancement is generically referred to as "NCZarr". The data model supported by NCZarr is netcdf-4 minus the user-defined types and the String type. In this sense it is similar to the CDF-5 data model. More detailed information about enabling and using NCZarr is described in the document NUG/nczarr.md and in a [Unidata Developer's blog entry](https://www.unidata.ucar.edu/blogs/developer/en/entry/overview-of-zarr-support-in). WARNING: this code has had limited testing, so do use this version for production work. Also, performance improvements are ongoing. Note especially the following platform matrix of successful tests: Platform | Build System | S3 support ------------------------------------ Linux+gcc | Automake | yes Linux+gcc | CMake | yes Visual Studio | CMake | no Additionally, and as a consequence of the addition of NCZarr, major changes have been made to the Filter API. NOTE: NCZarr does not yet support filters, but these changes are enablers for that support in the future. Note that it is possible (probable?) that there will be some accidental reversions if the changes here did not correctly mimic the existing filter testing. In any case, previously filter ids and parameters were of type unsigned int. In order to support the more general zarr filter model, this was all converted to char*. The old HDF5-specific, unsigned int operations are still supported but they are wrappers around the new, char* based nc_filterx_XXX functions. This entailed at least the following changes: 1. Added the files libdispatch/dfilterx.c and include/ncfilter.h 2. Some filterx utilities have been moved to libdispatch/daux.c 3. A new entry, "filter_actions" was added to the NCDispatch table and the version bumped. 4. An overly complex set of structs was created to support funnelling all of the filterx operations thru a single dispatch "filter_actions" entry. 5. Move common code to from libhdf5 to libsrc4 so that it is accessible to nczarr. Changes directly related to Zarr: 1. Modified CMakeList.txt and configure.ac to support both C and C++ -- this is in support of S3 support via the awd-sdk libraries. 2. Define a size64_t type to support nczarr. 3. More reworking of libdispatch/dinfermodel.c to support zarr and to regularize the structure of the fragments section of a URL. Changes not directly related to Zarr: 1. Make client-side filter registration be conditional, with default off. 2. Hack include/nc4internal.h to make some flags added by Ed be unique: e.g. NC_CREAT, NC_INDEF, etc. 3. cleanup include/nchttp.h and libdispatch/dhttp.c. 4. Misc. changes to support compiling under Visual Studio including: * Better testing under windows for dirent.h and opendir and closedir. 5. Misc. changes to the oc2 code to support various libcurl CURLOPT flags and to centralize error reporting. 6. By default, suppress the vlen tests that have unfixed memory leaks; add option to enable them. 7. Make part of the nc_test/test_byterange.sh test be contingent on remotetest.unidata.ucar.edu being accessible. Changes Left TO-DO: 1. fix provenance code, it is too HDF5 specific.
2020-06-29 08:02:47 +08:00
#include <math.h>
2010-06-03 21:24:43 +08:00
#ifndef nulldup
#define nulldup(x) ((x)?strdup(x):(x))
#endif
2010-06-03 21:24:43 +08:00
static char stmp[256];
2010-06-03 21:24:43 +08:00
void
convert1(NCConstant* src, NCConstant* dst)
2010-06-03 21:24:43 +08:00
{
Constvalue tmp;
unsigned char* bytes = NULL;
size_t bytelen;
#ifdef _MSC_VER
int byteval;
#endif
Fix more memory leaks in netcdf-c library This is a follow up to PR https://github.com/Unidata/netcdf-c/pull/1173 Sorry that it is so big, but leak suppression can be complex. This PR fixes all remaining memory leaks -- as determined by -fsanitize=address, and with the exceptions noted below. Unfortunately. there remains a significant leak that I cannot solve. It involves vlens, and it is unclear if the leak is occurring in the netcdf-c library or the HDF5 library. I have added a check_PROGRAM to the ncdump directory to show the problem. The program is called tst_vlen_demo.c To exercise it, build the netcdf library with -fsanitize=address enabled. Then go into ncdump and do a "make clean check". This should build tst_vlen_demo without actually executing it. Then do the command "./tst_vlen_demo" to see the output of the memory checker. Note the the lost malloc is deep in the HDF5 library (in H5Tvlen.c). I am temporarily working around this error in the following way. 1. I modified several test scripts to not execute known vlen tests that fail as described above. 2. Added an environment variable called NC_VLEN_NOTEST. If set, then those specific tests are suppressed. This should mean that the --disable-utilities option to ./configure should not need to be set to get a memory leak clean build. This should allow for detection of any new leaks. Note: I used an environment variable rather than a ./configure option to control the vlen tests. This is because it is temporary (I hope) and because it is a bit tricky for shell scripts to access ./configure options. Finally, as before, this only been tested with netcdf-4 and hdf5 support.
2018-11-16 01:00:38 +08:00
memset(&tmp,0,sizeof(tmp));
dst->lineno = src->lineno;
2010-06-03 21:24:43 +08:00
/* Need to translate all possible sources to all possible sinks.*/
/* Rather than have a nested switch, combine the src and target into*/
/* a single value so we can do a single n*n-way switch*/
/* special case for src being NC_FILLVALUE*/
if(src->nctype == NC_FILLVALUE) {
if(dst->nctype != NC_FILLVALUE) {
Fix more memory leaks in netcdf-c library This is a follow up to PR https://github.com/Unidata/netcdf-c/pull/1173 Sorry that it is so big, but leak suppression can be complex. This PR fixes all remaining memory leaks -- as determined by -fsanitize=address, and with the exceptions noted below. Unfortunately. there remains a significant leak that I cannot solve. It involves vlens, and it is unclear if the leak is occurring in the netcdf-c library or the HDF5 library. I have added a check_PROGRAM to the ncdump directory to show the problem. The program is called tst_vlen_demo.c To exercise it, build the netcdf library with -fsanitize=address enabled. Then go into ncdump and do a "make clean check". This should build tst_vlen_demo without actually executing it. Then do the command "./tst_vlen_demo" to see the output of the memory checker. Note the the lost malloc is deep in the HDF5 library (in H5Tvlen.c). I am temporarily working around this error in the following way. 1. I modified several test scripts to not execute known vlen tests that fail as described above. 2. Added an environment variable called NC_VLEN_NOTEST. If set, then those specific tests are suppressed. This should mean that the --disable-utilities option to ./configure should not need to be set to get a memory leak clean build. This should allow for detection of any new leaks. Note: I used an environment variable rather than a ./configure option to control the vlen tests. This is because it is temporary (I hope) and because it is a bit tricky for shell scripts to access ./configure options. Finally, as before, this only been tested with netcdf-4 and hdf5 support.
2018-11-16 01:00:38 +08:00
nc_getfill(dst,NULL);
}
2010-06-03 21:24:43 +08:00
return;
}
/* special case handling for src being NC_ECONST*/
if(src->nctype == NC_ECONST) {
if(dst->nctype == NC_ECONST) {
dst->value = src->value;
} else {
Symbol* econst;
econst = src->value.enumv;
Fix more memory leaks in netcdf-c library This is a follow up to PR https://github.com/Unidata/netcdf-c/pull/1173 Sorry that it is so big, but leak suppression can be complex. This PR fixes all remaining memory leaks -- as determined by -fsanitize=address, and with the exceptions noted below. Unfortunately. there remains a significant leak that I cannot solve. It involves vlens, and it is unclear if the leak is occurring in the netcdf-c library or the HDF5 library. I have added a check_PROGRAM to the ncdump directory to show the problem. The program is called tst_vlen_demo.c To exercise it, build the netcdf library with -fsanitize=address enabled. Then go into ncdump and do a "make clean check". This should build tst_vlen_demo without actually executing it. Then do the command "./tst_vlen_demo" to see the output of the memory checker. Note the the lost malloc is deep in the HDF5 library (in H5Tvlen.c). I am temporarily working around this error in the following way. 1. I modified several test scripts to not execute known vlen tests that fail as described above. 2. Added an environment variable called NC_VLEN_NOTEST. If set, then those specific tests are suppressed. This should mean that the --disable-utilities option to ./configure should not need to be set to get a memory leak clean build. This should allow for detection of any new leaks. Note: I used an environment variable rather than a ./configure option to control the vlen tests. This is because it is temporary (I hope) and because it is a bit tricky for shell scripts to access ./configure options. Finally, as before, this only been tested with netcdf-4 and hdf5 support.
2018-11-16 01:00:38 +08:00
convert1(econst->typ.econst,dst);
2010-06-03 21:24:43 +08:00
}
return;
} else if(dst->nctype == NC_ECONST) {
/* special case for dst being NC_ECONST*/
semerror(lineno,"Conversion to enum not supported (yet)");
return;
}
if(src->nctype == NC_OPAQUE) {
bytes = makebytestring(src->value.opaquev.stringv,&bytelen);
}
#define CASE(nc1,nc2) (nc1*256+nc2)
switch (CASE(src->nctype,dst->nctype)) {
case CASE(NC_CHAR,NC_CHAR):
tmp.charv = src->value.charv;
break;
case CASE(NC_CHAR,NC_BYTE):
tmp.int8v = (unsigned char)src->value.charv;
break;
case CASE(NC_CHAR,NC_UBYTE):
tmp.uint8v = (unsigned char)src->value.charv;
break;
case CASE(NC_CHAR,NC_USHORT):
tmp.uint16v = (unsigned short)src->value.charv;
break;
case CASE(NC_CHAR,NC_UINT):
tmp.uint32v = (unsigned int)src->value.charv;
break;
case CASE(NC_CHAR,NC_UINT64):
tmp.uint64v = (unsigned long long)src->value.charv;
break;
case CASE(NC_CHAR,NC_SHORT):
tmp.int16v = (short)src->value.charv;
break;
case CASE(NC_CHAR,NC_INT):
tmp.int32v = (int)src->value.charv;
break;
case CASE(NC_CHAR,NC_INT64):
tmp.int64v = (long long)src->value.charv;
break;
case CASE(NC_CHAR,NC_FLOAT):
tmp.floatv = (float)src->value.charv;
break;
case CASE(NC_CHAR,NC_DOUBLE):
tmp.doublev = (double)src->value.charv;
break;
case CASE(NC_BYTE,NC_CHAR):
tmp.charv = (char)src->value.uint8v;
break;
case CASE(NC_BYTE,NC_BYTE):
tmp.uint8v = (unsigned char)src->value.uint8v;
break;
case CASE(NC_BYTE,NC_UBYTE):
tmp.uint8v = (unsigned char)src->value.uint8v;
break;
case CASE(NC_BYTE,NC_USHORT):
tmp.uint16v = (unsigned short)src->value.uint8v;
break;
case CASE(NC_BYTE,NC_UINT):
tmp.uint32v = (unsigned int)src->value.uint8v;
break;
case CASE(NC_BYTE,NC_UINT64):
tmp.uint64v = (unsigned long long)src->value.uint8v;
break;
case CASE(NC_BYTE,NC_SHORT):
tmp.int16v = (short)src->value.uint8v;
break;
case CASE(NC_BYTE,NC_INT):
tmp.int32v = (int)src->value.uint8v;
break;
case CASE(NC_BYTE,NC_INT64):
tmp.int64v = (long long)src->value.uint8v;
break;
case CASE(NC_BYTE,NC_FLOAT):
tmp.floatv = (float)src->value.uint8v;
break;
case CASE(NC_BYTE,NC_DOUBLE):
tmp.doublev = (double)src->value.uint8v;
break;
case CASE(NC_UBYTE,NC_CHAR):
tmp.charv = (char)src->value.uint8v;
break;
case CASE(NC_UBYTE,NC_BYTE):
tmp.uint8v = (unsigned char)src->value.uint8v;
break;
case CASE(NC_UBYTE,NC_UBYTE):
tmp.uint8v = (unsigned char)src->value.uint8v;
break;
case CASE(NC_UBYTE,NC_USHORT):
tmp.uint16v = (unsigned short)src->value.uint8v;
break;
case CASE(NC_UBYTE,NC_UINT):
tmp.uint32v = (unsigned int)src->value.uint8v;
break;
case CASE(NC_UBYTE,NC_UINT64):
tmp.uint64v = (unsigned long long)src->value.uint8v;
break;
case CASE(NC_UBYTE,NC_SHORT):
tmp.int16v = (short)src->value.uint8v;
break;
case CASE(NC_UBYTE,NC_INT):
tmp.int32v = (int)src->value.uint8v;
break;
case CASE(NC_UBYTE,NC_INT64):
tmp.int64v = (long long)src->value.uint8v;
break;
case CASE(NC_UBYTE,NC_FLOAT):
tmp.floatv = (float)src->value.uint8v;
break;
case CASE(NC_UBYTE,NC_DOUBLE):
tmp.doublev = (double)src->value.uint8v;
break;
case CASE(NC_USHORT,NC_BYTE):
tmp.uint8v = (unsigned char)src->value.uint16v;
break;
case CASE(NC_USHORT,NC_UBYTE):
tmp.uint8v = (unsigned char)src->value.uint16v;
break;
case CASE(NC_USHORT,NC_USHORT):
tmp.uint16v = (unsigned short)src->value.uint16v;
break;
case CASE(NC_USHORT,NC_UINT):
tmp.uint32v = (unsigned int)src->value.uint16v;
break;
case CASE(NC_USHORT,NC_UINT64):
tmp.uint64v = (unsigned long long)src->value.uint16v;
break;
case CASE(NC_USHORT,NC_SHORT):
tmp.int16v = (short)src->value.uint16v;
break;
case CASE(NC_USHORT,NC_INT):
tmp.int32v = (int)src->value.uint16v;
break;
case CASE(NC_USHORT,NC_INT64):
tmp.int64v = (long long)src->value.uint16v;
break;
case CASE(NC_USHORT,NC_FLOAT):
tmp.floatv = (float)src->value.uint16v;
break;
case CASE(NC_USHORT,NC_DOUBLE):
tmp.doublev = (double)src->value.uint16v;
break;
case CASE(NC_UINT,NC_BYTE):
tmp.uint8v = (unsigned char)src->value.uint32v;
break;
case CASE(NC_UINT,NC_UBYTE):
tmp.uint8v = (unsigned char)src->value.uint32v;
break;
case CASE(NC_UINT,NC_USHORT):
tmp.uint16v = (unsigned short)src->value.uint32v;
break;
case CASE(NC_UINT,NC_UINT):
tmp.uint32v = (unsigned int)src->value.uint32v;
break;
case CASE(NC_UINT,NC_UINT64):
tmp.uint64v = (unsigned long long)src->value.uint32v;
break;
case CASE(NC_UINT,NC_SHORT):
tmp.int16v = (short)src->value.uint32v;
break;
case CASE(NC_UINT,NC_INT):
tmp.int32v = (int)src->value.uint32v;
break;
case CASE(NC_UINT,NC_INT64):
tmp.int64v = (long long)src->value.uint32v;
break;
case CASE(NC_UINT,NC_FLOAT):
tmp.floatv = (float)src->value.uint32v;
break;
case CASE(NC_UINT,NC_DOUBLE):
tmp.doublev = (double)src->value.uint32v;
break;
case CASE(NC_UINT64,NC_BYTE):
tmp.uint8v = (unsigned char)src->value.uint64v;
break;
case CASE(NC_UINT64,NC_UBYTE):
tmp.uint8v = (unsigned char)src->value.uint64v;
break;
case CASE(NC_UINT64,NC_USHORT):
tmp.uint16v = (unsigned short)src->value.uint64v;
break;
case CASE(NC_UINT64,NC_UINT):
tmp.uint32v = (unsigned int)src->value.uint64v;
break;
case CASE(NC_UINT64,NC_UINT64):
tmp.uint64v = (unsigned long long)src->value.uint64v;
break;
case CASE(NC_UINT64,NC_SHORT):
tmp.int16v = (short)src->value.uint64v;
break;
case CASE(NC_UINT64,NC_INT):
tmp.int32v = (int)src->value.uint64v;
break;
case CASE(NC_UINT64,NC_INT64):
tmp.int64v = (long long)src->value.uint64v;
break;
case CASE(NC_UINT64,NC_FLOAT):
tmp.floatv = (float)src->value.uint64v;
break;
case CASE(NC_UINT64,NC_DOUBLE):
tmp.doublev = (double)src->value.uint64v;
break;
case CASE(NC_SHORT,NC_BYTE):
tmp.uint8v = (unsigned char)src->value.int16v;
break;
case CASE(NC_SHORT,NC_UBYTE):
tmp.uint8v = (unsigned char)src->value.int16v;
break;
case CASE(NC_SHORT,NC_USHORT):
tmp.uint16v = (unsigned short)src->value.int16v;
break;
case CASE(NC_SHORT,NC_UINT):
tmp.uint32v = (unsigned int)src->value.int16v;
break;
case CASE(NC_SHORT,NC_UINT64):
tmp.uint64v = (unsigned long long)src->value.int16v;
break;
case CASE(NC_SHORT,NC_SHORT):
tmp.int16v = (short)src->value.int16v;
break;
case CASE(NC_SHORT,NC_INT):
tmp.int32v = (int)src->value.int16v;
break;
case CASE(NC_SHORT,NC_INT64):
tmp.int64v = (long long)src->value.int16v;
break;
case CASE(NC_SHORT,NC_FLOAT):
tmp.floatv = (float)src->value.int16v;
break;
case CASE(NC_SHORT,NC_DOUBLE):
tmp.doublev = (double)src->value.int16v;
break;
case CASE(NC_INT,NC_BYTE):
tmp.uint8v = (unsigned char)src->value.int32v;
break;
case CASE(NC_INT,NC_UBYTE):
tmp.uint8v = (unsigned char)src->value.int32v;
break;
case CASE(NC_INT,NC_USHORT):
tmp.uint16v = (unsigned short)src->value.int32v;
break;
case CASE(NC_INT,NC_UINT):
tmp.uint32v = (unsigned int)src->value.int32v;
break;
case CASE(NC_INT,NC_UINT64):
tmp.uint64v = (unsigned long long)src->value.int32v;
break;
case CASE(NC_INT,NC_SHORT):
tmp.int16v = (short)src->value.int32v;
break;
case CASE(NC_INT,NC_INT):
tmp.int32v = (int)src->value.int32v;
break;
case CASE(NC_INT,NC_INT64):
tmp.int64v = (long long)src->value.int32v;
break;
case CASE(NC_INT,NC_FLOAT):
tmp.floatv = (float)src->value.int32v;
break;
case CASE(NC_INT,NC_DOUBLE):
tmp.doublev = (double)src->value.int32v;
break;
case CASE(NC_INT64,NC_BYTE):
tmp.uint8v = (unsigned char)src->value.int64v;
break;
case CASE(NC_INT64,NC_UBYTE):
tmp.uint8v = (unsigned char)src->value.int64v;
break;
case CASE(NC_INT64,NC_USHORT):
tmp.uint16v = (unsigned short)src->value.int64v;
break;
case CASE(NC_INT64,NC_UINT):
tmp.uint32v = (unsigned int)src->value.int64v;
break;
case CASE(NC_INT64,NC_UINT64):
tmp.uint64v = (unsigned long long)src->value.int64v;
break;
case CASE(NC_INT64,NC_SHORT):
tmp.int16v = (short)src->value.int64v;
break;
case CASE(NC_INT64,NC_INT):
tmp.int32v = (int)src->value.int64v;
break;
case CASE(NC_INT64,NC_INT64):
tmp.int64v = (long long)src->value.int64v;
break;
case CASE(NC_INT64,NC_FLOAT):
tmp.floatv = (float)src->value.int64v;
break;
case CASE(NC_INT64,NC_DOUBLE):
tmp.doublev = (double)src->value.int64v;
break;
case CASE(NC_FLOAT,NC_BYTE):
tmp.uint8v = (unsigned char)src->value.floatv;
break;
case CASE(NC_FLOAT,NC_UBYTE):
tmp.uint8v = (unsigned char)src->value.floatv;
break;
case CASE(NC_FLOAT,NC_USHORT):
tmp.uint16v = (unsigned short)src->value.floatv;
break;
case CASE(NC_FLOAT,NC_UINT):
tmp.uint32v = (unsigned int)src->value.floatv;
break;
case CASE(NC_FLOAT,NC_UINT64):
tmp.uint64v = (unsigned long long)src->value.floatv;
break;
case CASE(NC_FLOAT,NC_SHORT):
tmp.int16v = (short)src->value.floatv;
break;
case CASE(NC_FLOAT,NC_INT):
tmp.int32v = (int)src->value.floatv;
break;
case CASE(NC_FLOAT,NC_INT64):
tmp.int64v = (long long)src->value.floatv;
break;
case CASE(NC_FLOAT,NC_FLOAT):
tmp.floatv = src->value.floatv;
2010-06-03 21:24:43 +08:00
break;
case CASE(NC_FLOAT,NC_DOUBLE):
tmp.doublev = (isnan(src->value.floatv)?NAN:(double)src->value.floatv);
2010-06-03 21:24:43 +08:00
break;
case CASE(NC_DOUBLE,NC_BYTE):
tmp.uint8v = (unsigned char)src->value.doublev;
break;
case CASE(NC_DOUBLE,NC_UBYTE):
tmp.uint8v = (unsigned char)src->value.doublev;
break;
case CASE(NC_DOUBLE,NC_USHORT):
tmp.uint16v = (unsigned short)src->value.doublev;
break;
case CASE(NC_DOUBLE,NC_UINT):
tmp.uint32v = (unsigned int)src->value.doublev;
break;
case CASE(NC_DOUBLE,NC_UINT64):
tmp.uint64v = (unsigned long long)src->value.doublev;
break;
case CASE(NC_DOUBLE,NC_SHORT):
tmp.int16v = (short)src->value.doublev;
break;
case CASE(NC_DOUBLE,NC_INT):
tmp.int32v = (int)src->value.doublev;
break;
case CASE(NC_DOUBLE,NC_INT64):
tmp.int64v = (long long)src->value.doublev;
break;
case CASE(NC_DOUBLE,NC_FLOAT):
tmp.floatv = (isnan(src->value.doublev)?NANF:(float)src->value.doublev);
2010-06-03 21:24:43 +08:00
break;
case CASE(NC_DOUBLE,NC_DOUBLE):
tmp.doublev = (double)src->value.doublev;
break;
/* Conversion of a string to e.g. an integer should be what?*/
#ifdef _MSC_VER
case CASE(NC_STRING,NC_BYTE):
sscanf(src->value.stringv.stringv,"%d",&byteval); tmp.int8v = (char)byteval; break;
case CASE(NC_STRING,NC_UBYTE):
sscanf(src->value.stringv.stringv,"%d",&byteval); tmp.uint8v = (unsigned char)byteval; break;
#else
2010-06-03 21:24:43 +08:00
case CASE(NC_STRING,NC_BYTE):
sscanf(src->value.stringv.stringv,"%hhd",&tmp.int8v); break;
case CASE(NC_STRING,NC_UBYTE):
sscanf(src->value.stringv.stringv,"%hhu",&tmp.uint8v); break;
#endif
2010-06-03 21:24:43 +08:00
case CASE(NC_STRING,NC_USHORT):
sscanf(src->value.stringv.stringv,"%hu",&tmp.uint16v); break;
case CASE(NC_STRING,NC_UINT):
sscanf(src->value.stringv.stringv,"%u",&tmp.uint32v); break;
case CASE(NC_STRING,NC_UINT64):
sscanf(src->value.stringv.stringv,"%llu",&tmp.uint64v); break;
case CASE(NC_STRING,NC_SHORT):
sscanf(src->value.stringv.stringv,"%hd",&tmp.int16v); break;
case CASE(NC_STRING,NC_INT):
sscanf(src->value.stringv.stringv,"%d",&tmp.int32v); break;
case CASE(NC_STRING,NC_INT64):
sscanf(src->value.stringv.stringv,"%lld",&tmp.int64v); break;
case CASE(NC_STRING,NC_FLOAT):
sscanf(src->value.stringv.stringv,"%g",&tmp.floatv); break;
case CASE(NC_STRING,NC_DOUBLE):
sscanf(src->value.stringv.stringv,"%lg",&tmp.doublev); break;
case CASE(NC_STRING,NC_CHAR):
tmp.charv = src->value.stringv.stringv[0];
break;
case CASE(NC_STRING,NC_STRING):
2013-09-24 04:19:40 +08:00
/* Need to watch out for embedded NULs */
2010-06-03 21:24:43 +08:00
tmp.stringv.len = src->value.stringv.len;
tmp.stringv.stringv = (char*)ecalloc((size_t)src->value.stringv.len+1);
2013-09-24 04:19:40 +08:00
memcpy((void*)tmp.stringv.stringv,
(void*)src->value.stringv.stringv,
(size_t)tmp.stringv.len);
tmp.stringv.stringv[tmp.stringv.len] = '\0';
2010-06-03 21:24:43 +08:00
break;
/* What is the proper conversion for T->STRING?*/
case CASE(NC_CHAR,NC_STRING):
snprintf(stmp, sizeof(stmp),"%c",src->value.charv);
2010-06-03 21:24:43 +08:00
tmp.stringv.len = nulllen(stmp);
tmp.stringv.stringv = nulldup(stmp);
break;
case CASE(NC_BYTE,NC_STRING):
snprintf(stmp, sizeof(stmp),"%hhd",src->value.uint8v);
2010-06-03 21:24:43 +08:00
tmp.stringv.len = nulllen(stmp);
tmp.stringv.stringv = nulldup(stmp);
break;
case CASE(NC_UBYTE,NC_STRING):
snprintf(stmp, sizeof(stmp),"%hhu",src->value.uint8v);
2010-06-03 21:24:43 +08:00
tmp.stringv.len = nulllen(stmp);
tmp.stringv.stringv = nulldup(stmp);
break;
case CASE(NC_USHORT,NC_STRING):
snprintf(stmp, sizeof(stmp),"%hu",src->value.uint16v);
2010-06-03 21:24:43 +08:00
tmp.stringv.len = nulllen(stmp);
tmp.stringv.stringv = nulldup(stmp);
break;
case CASE(NC_UINT,NC_STRING):
snprintf(stmp, sizeof(stmp),"%u",src->value.uint32v);
2010-06-03 21:24:43 +08:00
tmp.stringv.len = nulllen(stmp);
tmp.stringv.stringv = nulldup(stmp);
break;
case CASE(NC_UINT64,NC_STRING):
snprintf(stmp, sizeof(stmp),"%llu",src->value.uint64v);
2010-06-03 21:24:43 +08:00
tmp.stringv.len = nulllen(stmp);
tmp.stringv.stringv = nulldup(stmp);
break;
case CASE(NC_SHORT,NC_STRING):
snprintf(stmp, sizeof(stmp),"%hd",src->value.int16v);
2010-06-03 21:24:43 +08:00
tmp.stringv.len = nulllen(stmp);
tmp.stringv.stringv = nulldup(stmp);
break;
case CASE(NC_INT,NC_STRING):
snprintf(stmp, sizeof(stmp),"%d",src->value.int32v);
2010-06-03 21:24:43 +08:00
tmp.stringv.len = nulllen(stmp);
tmp.stringv.stringv = nulldup(stmp);
break;
case CASE(NC_INT64,NC_STRING):
snprintf(stmp, sizeof(stmp),"%lld",src->value.int64v);
2010-06-03 21:24:43 +08:00
tmp.stringv.len = nulllen(stmp);
tmp.stringv.stringv = nulldup(stmp);
break;
case CASE(NC_FLOAT,NC_STRING):
snprintf(stmp, sizeof(stmp),"%.8g",src->value.floatv);
2010-06-03 21:24:43 +08:00
tmp.stringv.len = nulllen(stmp);
tmp.stringv.stringv = nulldup(stmp);
break;
case CASE(NC_DOUBLE,NC_STRING):
snprintf(stmp, sizeof(stmp),"%.8g",src->value.doublev);
2010-06-03 21:24:43 +08:00
tmp.stringv.len = nulllen(stmp);
tmp.stringv.stringv = nulldup(stmp);
break;
case CASE(NC_OPAQUE,NC_CHAR):
if(bytes)
2010-06-03 21:24:43 +08:00
tmp.charv = *(char*)bytes;
break;
2010-06-03 21:24:43 +08:00
case CASE(NC_OPAQUE,NC_BYTE):
2017-11-09 21:24:18 +08:00
if(bytes)
tmp.uint8v = *(unsigned char*)bytes;
break;
2010-06-03 21:24:43 +08:00
case CASE(NC_OPAQUE,NC_UBYTE):
if(bytes)
2010-06-03 21:24:43 +08:00
tmp.uint8v = *(unsigned char*)bytes;
break;
2010-06-03 21:24:43 +08:00
case CASE(NC_OPAQUE,NC_USHORT):
if(bytes)
2010-06-03 21:24:43 +08:00
tmp.uint16v = *(unsigned short*)bytes;
break;
2010-06-03 21:24:43 +08:00
case CASE(NC_OPAQUE,NC_UINT):
if(bytes)
2010-06-03 21:24:43 +08:00
tmp.uint32v = *(unsigned int*)bytes;
break;
2010-06-03 21:24:43 +08:00
case CASE(NC_OPAQUE,NC_UINT64):
if(bytes)
2010-06-03 21:24:43 +08:00
tmp.uint64v = *(unsigned long long*)bytes;
break;
2010-06-03 21:24:43 +08:00
case CASE(NC_OPAQUE,NC_SHORT):
if(bytes)
2010-06-03 21:24:43 +08:00
tmp.int16v = *(short*)bytes;
break;
2010-06-03 21:24:43 +08:00
case CASE(NC_OPAQUE,NC_INT):
if(bytes)
2010-06-03 21:24:43 +08:00
tmp.int32v = *(int*)bytes;
break;
2010-06-03 21:24:43 +08:00
case CASE(NC_OPAQUE,NC_INT64):
if(bytes)
2010-06-03 21:24:43 +08:00
tmp.int64v = *(long long*)bytes;
break;
2010-06-03 21:24:43 +08:00
case CASE(NC_OPAQUE,NC_FLOAT):
if(bytes)
2010-06-03 21:24:43 +08:00
tmp.floatv = *(float*)bytes;
break;
2010-06-03 21:24:43 +08:00
case CASE(NC_OPAQUE,NC_DOUBLE):
if(bytes)
2010-06-03 21:24:43 +08:00
tmp.doublev = *(double*)bytes;
break;
2010-06-03 21:24:43 +08:00
case CASE(NC_OPAQUE,NC_OPAQUE):
tmp.opaquev.stringv = (char*)ecalloc((size_t)src->value.opaquev.len+1);
memcpy(tmp.opaquev.stringv,src->value.opaquev.stringv, (size_t)src->value.opaquev.len);
2010-06-03 21:24:43 +08:00
tmp.opaquev.len = src->value.opaquev.len;
2012-05-06 06:31:24 +08:00
tmp.opaquev.stringv[tmp.opaquev.len] = '\0';
2010-06-03 21:24:43 +08:00
break;
case CASE(NC_NIL,NC_NIL):
break; /* probably will never happen */
case CASE(NC_NIL,NC_STRING):
tmp.stringv.len = 0;
tmp.stringv.stringv = NULL;
break;
2010-06-03 21:24:43 +08:00
/* We are missing all CASE(X,NC_ECONST) cases*/
default:
semerror(lineno,"transform: illegal conversion: %s/%d -> %s/%d",
nctypename(src->nctype),src->nctype,
nctypename(dst->nctype),dst->nctype);
break;;
}
if(bytes != NULL) efree(bytes); /* cleanup*/
/* overwrite minimum necessary parts*/
dst->value = tmp;
}
#ifdef IGNORE
2010-06-03 21:24:43 +08:00
/* Force an Opaque or string to conform to a given length*/
void
setprimlength(NCConstant* prim, unsigned long len)
2010-06-03 21:24:43 +08:00
{
ASSERT(isprimplus(prim->nctype));
if(prim->nctype == NC_STRING) {
if(prim->value.stringv.len == len) {
/* do nothing*/
} else if(prim->value.stringv.len > len) { /* truncate*/
prim->value.stringv.stringv[len] = '\0';
prim->value.stringv.len = len;
} else {/* prim->value.stringv.len > srcov->len*/
char* s;
2017-10-31 05:52:08 +08:00
s = (char*)ecalloc(len+1);
2010-06-03 21:24:43 +08:00
memset(s,NC_FILL_CHAR,len);
s[len] = '\0';
memcpy(s,prim->value.stringv.stringv,prim->value.stringv.len);
efree(prim->value.stringv.stringv);
prim->value.stringv.stringv = s;
prim->value.stringv.len = len;
}
} else if(prim->nctype == NC_OPAQUE) {
/* Note that expansion/contraction is in terms of whole
bytes = 2 nibbles */
ASSERT((len % 2) == 0);
if(prim->value.opaquev.len == len) {
2010-06-03 21:24:43 +08:00
/* do nothing*/
} else if(prim->value.opaquev.len > len) { /* truncate*/
prim->value.opaquev.stringv[len] = '\0';
prim->value.opaquev.len = len;
} else {/* prim->value.opaquev.len < len => expand*/
char* s;
2017-10-31 05:52:08 +08:00
s = (char*)ecalloc(len+1);
2010-06-03 21:24:43 +08:00
memset(s,'0',len);
memcpy(s,prim->value.opaquev.stringv,prim->value.opaquev.len);
s[len] = '\0';
efree(prim->value.opaquev.stringv);
prim->value.opaquev.stringv=s;
prim->value.opaquev.len = len;
}
}
}
#endif
2010-06-03 21:24:43 +08:00
Datalist*
convertstringtochars(NCConstant* str)
2010-06-03 21:24:43 +08:00
{
int i;
Datalist* dl;
int slen;
char* s;
slen = str->value.stringv.len;
dl = builddatalist(slen);
s = str->value.stringv.stringv;
for(i=0;i<slen;i++) {
NCConstant con;
2010-06-03 21:24:43 +08:00
con.nctype = NC_CHAR;
con.lineno = str->lineno;
con.value.charv = s[i];
con.filled = 0;
2010-06-03 21:24:43 +08:00
dlappend(dl,&con);
}
return dl;
}
unsigned int
convertFilterID(const char* id)
{
unsigned int nid = 0;
int ok = 0;
/* for now, must be an integer */
ok = sscanf(id,"%u",&nid);
if(ok == 1)
return nid;
return 0; /* Not a recognizable id */
}