2012-02-14 08:25:32 +08:00
|
|
|
/*********************************************************************
|
|
|
|
* Copyright 2009, UCAR/Unidata
|
|
|
|
* See netcdf/COPYRIGHT file for copying and redistribution conditions.
|
|
|
|
*********************************************************************/
|
|
|
|
|
|
|
|
#include "includes.h"
|
2013-03-01 01:51:09 +08:00
|
|
|
#include "nc_iter.h"
|
2012-02-14 08:25:32 +08:00
|
|
|
#include "odom.h"
|
2017-03-09 08:01:10 +08:00
|
|
|
#include "ncoffsets.h"
|
2018-11-16 01:00:38 +08:00
|
|
|
#include "netcdf_aux.h"
|
2012-02-14 08:25:32 +08:00
|
|
|
|
|
|
|
/**************************************************/
|
|
|
|
/* Code for generating data lists*/
|
|
|
|
/**************************************************/
|
|
|
|
/* For datalist constant rules: see the rules on the man page */
|
|
|
|
|
|
|
|
/* Forward*/
|
|
|
|
static void generate_array(Symbol*,Bytebuffer*,Datalist*,Generator*,Writer);
|
|
|
|
static void generate_arrayr(Symbol*,Bytebuffer*,Datalist*,Odometer*,int,Datalist*,Generator*);
|
2013-07-17 04:22:48 +08:00
|
|
|
static void generate_primdata(Symbol*, NCConstant*, Bytebuffer*, Datalist* fillsrc, Generator*);
|
|
|
|
static void generate_fieldarray(Symbol*, NCConstant*, Dimset*, Bytebuffer*, Datalist* fillsrc, Generator*);
|
2012-02-14 08:25:32 +08:00
|
|
|
|
|
|
|
/* Mnemonics */
|
|
|
|
#define VLENLIST1
|
|
|
|
#define FIELDARRAY 1
|
|
|
|
|
|
|
|
/**************************************************/
|
2014-09-19 08:26:06 +08:00
|
|
|
|
2012-02-14 08:25:32 +08:00
|
|
|
|
|
|
|
int
|
|
|
|
generator_getstate(Generator* generator ,void** statep)
|
|
|
|
{
|
2016-11-18 06:29:32 +08:00
|
|
|
if(statep) *statep = (void*)generator->globalstate;
|
2012-02-14 08:25:32 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int generator_reset(Generator* generator, void* state)
|
|
|
|
{
|
2016-11-18 06:29:32 +08:00
|
|
|
generator->globalstate = state;
|
2012-02-14 08:25:32 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2012-02-18 02:51:02 +08:00
|
|
|
#ifdef IGNORe
|
2012-02-18 02:50:25 +08:00
|
|
|
static void
|
|
|
|
checkodom(Odometer* odom)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for(i=0;i<odom->rank;i++) {
|
|
|
|
ASSERT(odom->index[i] == odom->start[i]+odom->count[i]);
|
|
|
|
}
|
|
|
|
}
|
2012-02-18 02:51:02 +08:00
|
|
|
#endif
|
2012-02-18 02:50:25 +08:00
|
|
|
|
2012-02-14 08:25:32 +08:00
|
|
|
/**************************************************/
|
|
|
|
|
|
|
|
void
|
|
|
|
generate_attrdata(Symbol* asym, Generator* generator, Writer writer, Bytebuffer* codebuf)
|
|
|
|
{
|
|
|
|
Symbol* basetype = asym->typ.basetype;
|
|
|
|
nc_type typecode = basetype->typ.typecode;
|
|
|
|
|
2013-07-11 04:00:48 +08:00
|
|
|
if(typecode == NC_CHAR) {
|
2012-02-14 08:25:32 +08:00
|
|
|
gen_charattr(asym->data,codebuf);
|
|
|
|
} else {
|
|
|
|
int uid;
|
|
|
|
size_t count;
|
2016-11-18 06:29:32 +08:00
|
|
|
generator->listbegin(generator,asym,NULL,LISTATTR,asym->data->length,codebuf,&uid);
|
2012-02-14 08:25:32 +08:00
|
|
|
for(count=0;count<asym->data->length;count++) {
|
2014-09-19 08:26:06 +08:00
|
|
|
NCConstant* con = datalistith(asym->data,count);
|
2016-11-18 06:29:32 +08:00
|
|
|
generator->list(generator,asym,NULL,LISTATTR,uid,count,codebuf);
|
2012-02-14 08:25:32 +08:00
|
|
|
generate_basetype(asym->typ.basetype,con,codebuf,NULL,generator);
|
|
|
|
}
|
2016-11-18 06:29:32 +08:00
|
|
|
generator->listend(generator,asym,NULL,LISTATTR,uid,count,codebuf);
|
2012-02-14 08:25:32 +08:00
|
|
|
}
|
|
|
|
writer(generator,asym,codebuf,0,NULL,NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
generate_vardata(Symbol* vsym, Generator* generator, Writer writer, Bytebuffer* code)
|
|
|
|
{
|
|
|
|
Dimset* dimset = &vsym->typ.dimset;
|
|
|
|
int rank = dimset->ndims;
|
|
|
|
Symbol* basetype = vsym->typ.basetype;
|
2012-05-05 03:22:30 +08:00
|
|
|
Datalist* filler = getfiller(vsym);
|
2012-02-14 08:25:32 +08:00
|
|
|
|
|
|
|
if(vsym->data == NULL) return;
|
|
|
|
|
|
|
|
/* give the buffer a running start to be large enough*/
|
2014-08-16 05:42:13 +08:00
|
|
|
if(!bbSetalloc(code, nciterbuffersize))
|
|
|
|
return;
|
2012-02-14 08:25:32 +08:00
|
|
|
|
|
|
|
if(rank == 0) {/*scalar case*/
|
2013-09-22 06:19:06 +08:00
|
|
|
NCConstant* c0 = datalistith(vsym->data,0);
|
2012-05-05 03:22:30 +08:00
|
|
|
generate_basetype(basetype,c0,code,filler,generator);
|
2012-02-14 08:25:32 +08:00
|
|
|
writer(generator,vsym,code,0,NULL,NULL);
|
|
|
|
} else {/*rank > 0*/
|
2015-08-16 06:26:35 +08:00
|
|
|
#if 0
|
2014-09-19 08:26:06 +08:00
|
|
|
/* First, create an odometer using all of the dimensions */
|
|
|
|
odom = newodometer(dimset,NULL,NULL);
|
|
|
|
start = odometerstartvector(odom);
|
|
|
|
count = odometercountvector(odom);
|
2015-08-16 06:26:35 +08:00
|
|
|
#endif
|
2012-05-05 03:22:30 +08:00
|
|
|
generate_array(vsym,code,filler,generator,writer);
|
2012-02-14 08:25:32 +08:00
|
|
|
}
|
|
|
|
}
|
2014-08-16 05:42:13 +08:00
|
|
|
|
2014-09-19 08:26:06 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
The basic idea is to split the set of dimensions into
|
|
|
|
groups and iterate over each group by recursion.
|
|
|
|
|
|
|
|
A group is defined as the range of indices starting at an
|
|
|
|
unlimited dimension upto (but not including) the next
|
|
|
|
unlimited.
|
|
|
|
|
|
|
|
The first group starts at index 0, even if dimension 0 is not
|
|
|
|
unlimited. The last group is everything from the last
|
|
|
|
unlimited dimension thru the last dimension (index rank-1).
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2012-02-14 08:25:32 +08:00
|
|
|
static void
|
|
|
|
generate_array(Symbol* vsym,
|
|
|
|
Bytebuffer* code,
|
|
|
|
Datalist* filler,
|
|
|
|
Generator* generator,
|
|
|
|
Writer writer
|
|
|
|
)
|
|
|
|
{
|
|
|
|
Dimset* dimset = &vsym->typ.dimset;
|
|
|
|
int rank = dimset->ndims;
|
|
|
|
Symbol* basetype = vsym->typ.basetype;
|
|
|
|
nc_type typecode = basetype->typ.typecode;
|
|
|
|
nciter_t iter;
|
2014-09-19 08:26:06 +08:00
|
|
|
int firstunlim = findunlimited(dimset,1);
|
|
|
|
int nunlim = countunlimited(dimset);
|
|
|
|
int isnc3unlim = (nunlim <= 1 && (firstunlim == 0 || firstunlim == rank)); /* netcdf-3 case of at most 1 unlim in 0th dimension */
|
2012-02-14 08:25:32 +08:00
|
|
|
|
|
|
|
ASSERT(rank > 0);
|
|
|
|
|
2014-09-19 08:26:06 +08:00
|
|
|
if(isnc3unlim) {
|
|
|
|
/* Handle NC_CHAR case separately */
|
|
|
|
if(typecode == NC_CHAR) {
|
2018-11-16 01:00:38 +08:00
|
|
|
Odometer* odom = newodometer(dimset,NULL,NULL);
|
2014-09-19 08:26:06 +08:00
|
|
|
Bytebuffer* charbuf = bbNew();
|
|
|
|
gen_chararray(dimset,0,vsym->data,charbuf,filler);
|
2016-11-18 06:29:32 +08:00
|
|
|
generator->charconstant(generator,vsym,code,charbuf);
|
2014-09-19 08:26:06 +08:00
|
|
|
/* Create an odometer to get the dimension info */
|
|
|
|
writer(generator,vsym,code,odom->rank,odom->start,odom->count);
|
2015-08-16 06:26:35 +08:00
|
|
|
#if 0
|
|
|
|
writer(generator,vsym,code,odom->rank,0,bbLength(charbuf));
|
|
|
|
#endif
|
2014-09-19 08:26:06 +08:00
|
|
|
bbFree(charbuf);
|
2018-11-16 01:00:38 +08:00
|
|
|
odometerfree(odom);
|
2014-09-19 08:26:06 +08:00
|
|
|
} else { /* typecode != NC_CHAR */
|
2018-11-16 01:00:38 +08:00
|
|
|
Odometer* odom = newodometer(dimset,NULL,NULL);
|
2014-09-19 08:26:06 +08:00
|
|
|
/* Case: dim 1..rank-1 are not unlimited, dim 0 might be */
|
|
|
|
size_t offset = 0; /* where are we in the data list */
|
|
|
|
size_t nelems = 0; /* # of data list items to generate */
|
|
|
|
/* Create an iterator and odometer and just walk the datalist */
|
|
|
|
nc_get_iter(vsym,nciterbuffersize,&iter);
|
|
|
|
for(;;offset+=nelems) {
|
|
|
|
int i,uid;
|
|
|
|
nelems=nc_next_iter(&iter,odometerstartvector(odom),odometercountvector(odom));
|
|
|
|
if(nelems == 0)
|
2012-02-18 02:50:25 +08:00
|
|
|
break;
|
2014-09-19 08:26:06 +08:00
|
|
|
bbClear(code);
|
2016-11-18 06:29:32 +08:00
|
|
|
generator->listbegin(generator,vsym,NULL,LISTDATA,vsym->data->length,code,&uid);
|
2014-09-19 08:26:06 +08:00
|
|
|
for(i=0;i<nelems;i++) {
|
|
|
|
NCConstant* con = datalistith(vsym->data,i+offset);
|
2016-11-18 06:29:32 +08:00
|
|
|
generator->list(generator,vsym,NULL,LISTDATA,uid,i,code);
|
2012-02-18 02:50:25 +08:00
|
|
|
generate_basetype(basetype,con,code,filler,generator);
|
2014-09-19 08:26:06 +08:00
|
|
|
}
|
2016-11-18 06:29:32 +08:00
|
|
|
generator->listend(generator,vsym,NULL,LISTDATA,uid,i,code);
|
2014-09-19 08:26:06 +08:00
|
|
|
writer(generator,vsym,code,rank,odom->start,odom->count);
|
|
|
|
}
|
2018-11-16 01:00:38 +08:00
|
|
|
odometerfree(odom);
|
2012-02-14 08:25:32 +08:00
|
|
|
}
|
2014-09-19 08:26:06 +08:00
|
|
|
} else { /* Hard case: multiple unlimited dimensions or unlim in dim > 0*/
|
2018-11-16 01:00:38 +08:00
|
|
|
Odometer* odom = newodometer(dimset,NULL,NULL);
|
2012-02-14 08:25:32 +08:00
|
|
|
/* Setup iterator and odometer */
|
2014-09-19 08:26:06 +08:00
|
|
|
nc_get_iter(vsym,NC_MAX_UINT,&iter); /* effectively infinite */
|
|
|
|
for(;;) {/* iterate in nelem chunks */
|
|
|
|
/* get nelems count and modify odometer */
|
|
|
|
size_t nelems=nc_next_iter(&iter,odom->start,odom->count);
|
|
|
|
if(nelems == 0) break;
|
2012-02-14 08:25:32 +08:00
|
|
|
generate_arrayr(vsym,code,vsym->data,
|
2014-09-19 08:26:06 +08:00
|
|
|
odom,
|
2012-02-14 08:25:32 +08:00
|
|
|
/*dim index=*/0,
|
2014-09-19 08:26:06 +08:00
|
|
|
filler,generator
|
|
|
|
);
|
2012-02-14 08:25:32 +08:00
|
|
|
writer(generator,vsym,code,odom->rank,odom->start,odom->count);
|
|
|
|
}
|
2014-09-19 08:26:06 +08:00
|
|
|
odometerfree(odom);
|
2018-11-16 01:00:38 +08:00
|
|
|
}
|
2012-02-14 08:25:32 +08:00
|
|
|
}
|
|
|
|
|
2014-09-19 08:26:06 +08:00
|
|
|
/**
|
|
|
|
The basic idea is to split the set of dimensions into groups
|
|
|
|
and iterate over each group. A group is defined as the
|
|
|
|
range of indices starting at an unlimited dimension upto
|
|
|
|
(but not including) the next unlimited. The first group
|
|
|
|
starts at index 0, even if dimension 0 is not unlimited.
|
|
|
|
The last group is everything from the last unlimited
|
|
|
|
dimension thru the last dimension (index rank-1).
|
|
|
|
*/
|
2012-02-14 08:25:32 +08:00
|
|
|
static void
|
|
|
|
generate_arrayr(Symbol* vsym,
|
|
|
|
Bytebuffer* code,
|
|
|
|
Datalist* list,
|
2014-09-19 08:26:06 +08:00
|
|
|
Odometer* odom,
|
2012-02-14 08:25:32 +08:00
|
|
|
int dimindex,
|
|
|
|
Datalist* filler,
|
|
|
|
Generator* generator
|
|
|
|
)
|
|
|
|
{
|
2014-09-19 08:26:06 +08:00
|
|
|
int uid,i;
|
2012-02-14 08:25:32 +08:00
|
|
|
Symbol* basetype = vsym->typ.basetype;
|
|
|
|
Dimset* dimset = &vsym->typ.dimset;
|
|
|
|
int rank = dimset->ndims;
|
2014-09-19 08:26:06 +08:00
|
|
|
int lastunlimited = findlastunlimited(dimset);
|
|
|
|
int nextunlimited = findunlimited(dimset,dimindex+1);
|
2012-03-15 07:26:48 +08:00
|
|
|
int typecode = basetype->typ.typecode;
|
2014-09-19 08:26:06 +08:00
|
|
|
int islastgroup = (lastunlimited == rank || dimindex >= lastunlimited || dimindex == rank-1);
|
|
|
|
Odometer* subodom = NULL;
|
2012-02-14 08:25:32 +08:00
|
|
|
|
|
|
|
ASSERT(rank > 0);
|
2014-09-19 08:26:06 +08:00
|
|
|
ASSERT((dimindex >= 0 && dimindex < rank));
|
|
|
|
|
|
|
|
if(islastgroup) {
|
|
|
|
/* Handle NC_CHAR case separately */
|
|
|
|
if(typecode == NC_CHAR) {
|
|
|
|
Bytebuffer* charbuf = bbNew();
|
|
|
|
gen_chararray(dimset,dimindex,list,charbuf,filler);
|
2016-11-18 06:29:32 +08:00
|
|
|
generator->charconstant(generator,vsym,code,charbuf);
|
2014-09-19 08:26:06 +08:00
|
|
|
bbFree(charbuf);
|
|
|
|
} else {
|
|
|
|
/* build a special odometer to walk the last few dimensions */
|
|
|
|
subodom = newsubodometer(odom,dimset,dimindex,rank);
|
2016-11-18 06:29:32 +08:00
|
|
|
generator->listbegin(generator,vsym,NULL,LISTDATA,list->length,code,&uid);
|
2014-09-19 08:26:06 +08:00
|
|
|
for(i=0;odometermore(subodom);i++) {
|
|
|
|
size_t offset = odometeroffset(subodom);
|
|
|
|
NCConstant* con = datalistith(list,offset);
|
2016-11-18 06:29:32 +08:00
|
|
|
generator->list(generator,vsym,NULL,LISTDATA,uid,i,code);
|
2014-09-19 08:26:06 +08:00
|
|
|
generate_basetype(basetype,con,code,filler,generator);
|
|
|
|
odometerincr(subodom);
|
|
|
|
}
|
2016-11-18 06:29:32 +08:00
|
|
|
generator->listend(generator,vsym,NULL,LISTDATA,uid,i,code);
|
2014-09-19 08:26:06 +08:00
|
|
|
odometerfree(subodom); subodom = NULL;
|
|
|
|
}
|
|
|
|
} else {/* !islastgroup */
|
|
|
|
/* Our datalist must be a list of compounds representing
|
2012-02-14 08:25:32 +08:00
|
|
|
the next unlimited; so walk the subarray from this index
|
2014-09-19 08:26:06 +08:00
|
|
|
upto next unlimited.
|
2012-02-14 08:25:32 +08:00
|
|
|
*/
|
2014-09-19 08:26:06 +08:00
|
|
|
ASSERT((dimindex < nextunlimited));
|
|
|
|
ASSERT((isunlimited(dimset,nextunlimited)));
|
|
|
|
/* build a sub odometer */
|
|
|
|
subodom = newsubodometer(odom,dimset,dimindex,nextunlimited);
|
|
|
|
for(i=0;odometermore(subodom);i++) {
|
|
|
|
size_t offset = odometeroffset(subodom);
|
|
|
|
NCConstant* con = datalistith(list,offset);
|
|
|
|
if(con == NULL || con->nctype == NC_FILL) {
|
|
|
|
if(filler == NULL)
|
|
|
|
filler = getfiller(vsym);
|
|
|
|
generate_arrayr(vsym,code,filler,odom,nextunlimited,NULL,generator);
|
|
|
|
|
|
|
|
} else if(islistconst(con)) {
|
|
|
|
Datalist* sublist = compoundfor(con);
|
|
|
|
generate_arrayr(vsym,code,sublist,odom,nextunlimited,filler,generator);
|
|
|
|
} else {
|
|
|
|
semerror(constline(con),"Expected {...} representing unlimited list");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
odometerincr(subodom);
|
2012-02-14 08:25:32 +08:00
|
|
|
}
|
2014-09-19 08:26:06 +08:00
|
|
|
odometerfree(subodom); subodom = NULL;
|
2012-02-14 08:25:32 +08:00
|
|
|
}
|
2014-09-19 08:26:06 +08:00
|
|
|
if(subodom != NULL)
|
|
|
|
odometerfree(subodom);
|
2012-02-14 08:25:32 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-11-16 01:00:38 +08:00
|
|
|
/* Generate an instance of the basetype using the value of con*/
|
2012-02-14 08:25:32 +08:00
|
|
|
void
|
2013-07-17 04:22:48 +08:00
|
|
|
generate_basetype(Symbol* tsym, NCConstant* con, Bytebuffer* codebuf, Datalist* filler, Generator* generator)
|
2012-02-14 08:25:32 +08:00
|
|
|
{
|
|
|
|
Datalist* data;
|
2016-11-18 06:29:32 +08:00
|
|
|
int offsetbase = 0;
|
2012-02-14 08:25:32 +08:00
|
|
|
|
|
|
|
switch (tsym->subclass) {
|
|
|
|
|
|
|
|
case NC_ENUM:
|
|
|
|
case NC_OPAQUE:
|
|
|
|
case NC_PRIM:
|
2014-09-19 08:26:06 +08:00
|
|
|
if(islistconst(con)) {
|
|
|
|
semerror(constline(con),"Expected primitive found {..}");
|
|
|
|
}
|
|
|
|
generate_primdata(tsym,con,codebuf,filler,generator);
|
|
|
|
break;
|
2012-02-14 08:25:32 +08:00
|
|
|
|
|
|
|
case NC_COMPOUND: {
|
2014-09-19 08:26:06 +08:00
|
|
|
int i,uid, nfields, dllen;
|
|
|
|
if(con == NULL || isfillconst(con)) {
|
|
|
|
Datalist* fill = (filler==NULL?getfiller(tsym):filler);
|
2018-11-16 01:00:38 +08:00
|
|
|
ASSERT(fill->length == 1);
|
|
|
|
con = fill->data[0];
|
2014-10-03 01:28:07 +08:00
|
|
|
if(!islistconst(con)) {
|
|
|
|
if(con)
|
2014-09-19 08:26:06 +08:00
|
|
|
semerror(con->lineno,"Compound data fill value is not enclosed in {..}");
|
2014-10-03 01:28:07 +08:00
|
|
|
else
|
|
|
|
semerror(0,"Compound data fill value not enclosed in {..}, con is NULL.");
|
|
|
|
}
|
2014-09-19 08:26:06 +08:00
|
|
|
}
|
2015-06-19 04:37:31 +08:00
|
|
|
|
|
|
|
if(!con) { /* fail on null compound. */
|
|
|
|
semerror(constline(con),"NULL compound data.");
|
2015-06-19 04:41:09 +08:00
|
|
|
break;
|
2015-06-19 04:37:31 +08:00
|
|
|
}
|
|
|
|
|
2014-09-19 08:26:06 +08:00
|
|
|
if(!islistconst(con)) {/* fail on no compound*/
|
|
|
|
semerror(constline(con),"Compound data must be enclosed in {..}");
|
2012-02-14 08:25:32 +08:00
|
|
|
}
|
2015-06-19 04:37:31 +08:00
|
|
|
|
2014-09-19 08:26:06 +08:00
|
|
|
data = con->value.compoundv;
|
2013-06-27 02:55:30 +08:00
|
|
|
nfields = listlength(tsym->subnodes);
|
2014-09-19 08:26:06 +08:00
|
|
|
dllen = datalistlen(data);
|
|
|
|
if(dllen > nfields) {
|
2015-06-19 04:37:31 +08:00
|
|
|
semerror(con->lineno,"Datalist longer than the number of compound fields");
|
2014-09-19 08:26:06 +08:00
|
|
|
break;
|
|
|
|
}
|
2016-11-18 06:29:32 +08:00
|
|
|
generator->listbegin(generator,tsym,&offsetbase,LISTCOMPOUND,listlength(tsym->subnodes),codebuf,&uid);
|
2013-06-27 02:55:30 +08:00
|
|
|
for(i=0;i<nfields;i++) {
|
2012-02-14 08:25:32 +08:00
|
|
|
Symbol* field = (Symbol*)listget(tsym->subnodes,i);
|
2014-09-19 08:26:06 +08:00
|
|
|
con = datalistith(data,i);
|
2016-11-18 06:29:32 +08:00
|
|
|
generator->list(generator,field,&offsetbase,LISTCOMPOUND,uid,i,codebuf);
|
2012-02-14 08:25:32 +08:00
|
|
|
generate_basetype(field,con,codebuf,NULL,generator);
|
2014-09-19 08:26:06 +08:00
|
|
|
}
|
2016-11-18 06:29:32 +08:00
|
|
|
generator->listend(generator,tsym,&offsetbase,LISTCOMPOUND,uid,i,codebuf);
|
2014-09-19 08:26:06 +08:00
|
|
|
} break;
|
2012-02-14 08:25:32 +08:00
|
|
|
|
|
|
|
case NC_VLEN: {
|
2014-09-19 08:26:06 +08:00
|
|
|
Bytebuffer* vlenbuf;
|
2012-02-14 08:25:32 +08:00
|
|
|
int uid;
|
2014-09-19 08:26:06 +08:00
|
|
|
size_t count;
|
|
|
|
|
|
|
|
if(con == NULL || isfillconst(con)) {
|
|
|
|
Datalist* fill = (filler==NULL?getfiller(tsym):filler);
|
|
|
|
ASSERT(fill->length == 1);
|
2018-11-16 01:00:38 +08:00
|
|
|
con = fill->data[0];
|
2014-09-19 08:26:06 +08:00
|
|
|
if(con->nctype != NC_COMPOUND) {
|
|
|
|
semerror(con->lineno,"Vlen data fill value is not enclosed in {..}");
|
|
|
|
}
|
|
|
|
}
|
2012-02-14 08:25:32 +08:00
|
|
|
|
2014-09-19 08:26:06 +08:00
|
|
|
if(!islistconst(con)) {
|
|
|
|
semerror(constline(con),"Vlen data must be enclosed in {..}");
|
2012-02-14 08:25:32 +08:00
|
|
|
}
|
|
|
|
data = con->value.compoundv;
|
|
|
|
/* generate the nc_vlen_t instance*/
|
2014-09-19 08:26:06 +08:00
|
|
|
vlenbuf = bbNew();
|
|
|
|
if(tsym->typ.basetype->typ.typecode == NC_CHAR) {
|
2018-11-16 01:00:38 +08:00
|
|
|
gen_charseq(data,vlenbuf);
|
2016-11-18 06:29:32 +08:00
|
|
|
generator->vlenstring(generator,tsym,vlenbuf,&uid,&count);
|
2014-09-19 08:26:06 +08:00
|
|
|
} else {
|
2016-11-18 06:29:32 +08:00
|
|
|
generator->listbegin(generator,tsym,NULL,LISTVLEN,data->length,codebuf,&uid);
|
2012-02-14 08:25:32 +08:00
|
|
|
for(count=0;count<data->length;count++) {
|
2014-09-19 08:26:06 +08:00
|
|
|
NCConstant* con;
|
2016-11-18 06:29:32 +08:00
|
|
|
generator->list(generator,tsym,NULL,LISTVLEN,uid,count,vlenbuf);
|
2014-09-19 08:26:06 +08:00
|
|
|
con = datalistith(data,count);
|
2013-06-27 02:55:30 +08:00
|
|
|
generate_basetype(tsym->typ.basetype,con,vlenbuf,NULL,generator);
|
2014-09-19 08:26:06 +08:00
|
|
|
}
|
2016-11-18 06:29:32 +08:00
|
|
|
generator->listend(generator,tsym,NULL,LISTVLEN,uid,count,codebuf,(void*)vlenbuf);
|
2014-09-19 08:26:06 +08:00
|
|
|
}
|
2018-11-16 01:00:38 +08:00
|
|
|
generator->vlendecl(generator,tsym,codebuf,uid,count,vlenbuf); /* Will extract contents of vlenbuf */
|
2014-09-19 08:26:06 +08:00
|
|
|
bbFree(vlenbuf);
|
2012-02-14 08:25:32 +08:00
|
|
|
} break;
|
|
|
|
|
|
|
|
case NC_FIELD:
|
2014-09-19 08:26:06 +08:00
|
|
|
if(tsym->typ.dimset.ndims > 0) {
|
|
|
|
/* Verify that we have a sublist (or fill situation) */
|
|
|
|
if(con != NULL && !isfillconst(con) && !islistconst(con))
|
|
|
|
semerror(constline(con),"Dimensioned fields must be enclose in {...}");
|
2012-02-14 08:25:32 +08:00
|
|
|
generate_fieldarray(tsym->typ.basetype,con,&tsym->typ.dimset,codebuf,filler,generator);
|
2014-09-19 08:26:06 +08:00
|
|
|
} else {
|
|
|
|
generate_basetype(tsym->typ.basetype,con,codebuf,NULL,generator);
|
|
|
|
}
|
|
|
|
break;
|
2012-02-14 08:25:32 +08:00
|
|
|
|
|
|
|
default: PANIC1("generate_basetype: unexpected subclass %d",tsym->subclass);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Used only for structure field arrays*/
|
|
|
|
static void
|
2013-07-17 04:22:48 +08:00
|
|
|
generate_fieldarray(Symbol* basetype, NCConstant* con, Dimset* dimset,
|
2014-09-19 08:26:06 +08:00
|
|
|
Bytebuffer* codebuf, Datalist* filler, Generator* generator)
|
2012-02-14 08:25:32 +08:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int chartype = (basetype->typ.typecode == NC_CHAR);
|
|
|
|
Datalist* data;
|
2014-09-19 08:26:06 +08:00
|
|
|
int rank = rankfor(dimset);
|
2012-02-14 08:25:32 +08:00
|
|
|
|
|
|
|
ASSERT(dimset->ndims > 0);
|
|
|
|
|
|
|
|
if(con != NULL && !isfillconst(con))
|
|
|
|
data = con->value.compoundv;
|
|
|
|
else
|
2014-09-19 08:26:06 +08:00
|
|
|
data = NULL;
|
2012-02-14 08:25:32 +08:00
|
|
|
|
|
|
|
if(chartype) {
|
2014-09-19 08:26:06 +08:00
|
|
|
Bytebuffer* charbuf = bbNew();
|
|
|
|
gen_chararray(dimset,0,data,charbuf,filler);
|
2016-11-18 06:29:32 +08:00
|
|
|
generator->charconstant(generator,basetype,codebuf,charbuf);
|
2014-09-19 08:26:06 +08:00
|
|
|
bbFree(charbuf);
|
2012-02-14 08:25:32 +08:00
|
|
|
} else {
|
2014-09-19 08:26:06 +08:00
|
|
|
int uid;
|
|
|
|
size_t xproduct = crossproduct(dimset,0,rank); /* compute total number of elements */
|
2016-11-18 06:29:32 +08:00
|
|
|
generator->listbegin(generator,basetype,NULL,LISTFIELDARRAY,xproduct,codebuf,&uid);
|
2012-02-14 08:25:32 +08:00
|
|
|
for(i=0;i<xproduct;i++) {
|
2014-09-19 08:26:06 +08:00
|
|
|
con = (data == NULL ? NULL : datalistith(data,i));
|
2016-11-18 06:29:32 +08:00
|
|
|
generator->list(generator,basetype,NULL,LISTFIELDARRAY,uid,i,codebuf);
|
2012-02-14 08:25:32 +08:00
|
|
|
generate_basetype(basetype,con,codebuf,NULL,generator);
|
2014-09-19 08:26:06 +08:00
|
|
|
}
|
2016-11-18 06:29:32 +08:00
|
|
|
generator->listend(generator,basetype,NULL,LISTFIELDARRAY,uid,i,codebuf);
|
2012-02-14 08:25:32 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-05 03:22:30 +08:00
|
|
|
|
|
|
|
/* An opaque string value might not conform
|
|
|
|
to the size of the opaque to which it is being
|
|
|
|
assigned. Normalize it to match the required
|
|
|
|
opaque length (in bytes).
|
|
|
|
Note that the string is a sequence of nibbles (4 bits).
|
|
|
|
*/
|
|
|
|
static void
|
2013-07-17 04:22:48 +08:00
|
|
|
normalizeopaquelength(NCConstant* prim, unsigned long nbytes)
|
2012-05-05 03:22:30 +08:00
|
|
|
{
|
|
|
|
int nnibs = 2*nbytes;
|
|
|
|
ASSERT(prim->nctype==NC_OPAQUE);
|
2014-08-16 05:42:13 +08:00
|
|
|
if(prim->value.opaquev.len == nnibs) {
|
2012-05-05 03:22:30 +08:00
|
|
|
/* do nothing*/
|
|
|
|
} else if(prim->value.opaquev.len > nnibs) { /* truncate*/
|
2014-09-19 08:26:06 +08:00
|
|
|
prim->value.opaquev.stringv[nnibs] = '\0';
|
|
|
|
prim->value.opaquev.len = nnibs;
|
2012-05-05 03:22:30 +08:00
|
|
|
} else {/* prim->value.opaquev.len < nnibs => expand*/
|
|
|
|
char* s;
|
2017-10-31 05:52:08 +08:00
|
|
|
s = (char*)ecalloc(nnibs+1);
|
2014-09-19 08:26:06 +08:00
|
|
|
memset(s,'0',nnibs); /* Fill with '0' characters */
|
|
|
|
memcpy(s,prim->value.opaquev.stringv,prim->value.opaquev.len);
|
|
|
|
s[nnibs] = '\0';
|
|
|
|
efree(prim->value.opaquev.stringv);
|
|
|
|
prim->value.opaquev.stringv=s;
|
|
|
|
prim->value.opaquev.len = nnibs;
|
2012-05-05 03:22:30 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-14 08:25:32 +08:00
|
|
|
static void
|
2013-07-17 04:22:48 +08:00
|
|
|
generate_primdata(Symbol* basetype, NCConstant* prim, Bytebuffer* codebuf,
|
2014-09-19 08:26:06 +08:00
|
|
|
Datalist* filler, Generator* generator)
|
2012-02-14 08:25:32 +08:00
|
|
|
{
|
2018-11-16 01:00:38 +08:00
|
|
|
NCConstant* target;
|
2013-09-22 06:19:06 +08:00
|
|
|
int match;
|
2012-02-14 08:25:32 +08:00
|
|
|
|
|
|
|
if(prim == NULL || isfillconst(prim)) {
|
2014-09-19 08:26:06 +08:00
|
|
|
Datalist* fill = (filler==NULL?getfiller(basetype):filler);
|
|
|
|
ASSERT(fill->length == 1);
|
|
|
|
prim = datalistith(fill,0);
|
2012-02-14 08:25:32 +08:00
|
|
|
}
|
|
|
|
|
2014-09-19 08:26:06 +08:00
|
|
|
ASSERT((prim->nctype != NC_COMPOUND));
|
2012-02-14 08:25:32 +08:00
|
|
|
|
2013-09-22 06:19:06 +08:00
|
|
|
/* Verify that the constant is consistent with the type */
|
|
|
|
match = 1;
|
|
|
|
switch (prim->nctype) {
|
|
|
|
case NC_CHAR:
|
|
|
|
case NC_BYTE:
|
|
|
|
case NC_SHORT:
|
|
|
|
case NC_INT:
|
|
|
|
case NC_FLOAT:
|
|
|
|
case NC_DOUBLE:
|
|
|
|
case NC_UBYTE:
|
|
|
|
case NC_USHORT:
|
|
|
|
case NC_UINT:
|
|
|
|
case NC_INT64:
|
|
|
|
case NC_UINT64:
|
|
|
|
case NC_STRING:
|
2014-09-19 08:26:06 +08:00
|
|
|
match = (basetype->subclass == NC_PRIM ? 1 : 0);
|
|
|
|
break;
|
2013-09-22 06:19:06 +08:00
|
|
|
|
|
|
|
#ifdef USE_NETCDF4
|
|
|
|
case NC_NIL:
|
2014-09-19 08:26:06 +08:00
|
|
|
match = (basetype->subclass == NC_PRIM && basetype->typ.typecode == NC_STRING ? 1 : 0);
|
|
|
|
break;
|
2013-09-22 06:19:06 +08:00
|
|
|
|
|
|
|
case NC_OPAQUE:
|
2014-09-19 08:26:06 +08:00
|
|
|
/* OPAQUE is also consistent with numbers */
|
|
|
|
match = (basetype->subclass == NC_OPAQUE
|
|
|
|
|| basetype->subclass == NC_PRIM ? 1 : 0);
|
|
|
|
break;
|
2013-09-22 06:19:06 +08:00
|
|
|
case NC_ECONST:
|
2014-09-19 08:26:06 +08:00
|
|
|
match = (basetype->subclass == NC_ENUM ? 1 : 0);
|
|
|
|
if(match) {
|
|
|
|
/* Make sure this econst belongs to this enum */
|
|
|
|
Symbol* ec = prim->value.enumv;
|
|
|
|
Symbol* en = ec->container;
|
|
|
|
match = (en == basetype);
|
|
|
|
}
|
|
|
|
break;
|
2013-09-22 06:19:06 +08:00
|
|
|
#endif
|
|
|
|
default:
|
2014-09-19 08:26:06 +08:00
|
|
|
match = 0;
|
2013-09-22 06:19:06 +08:00
|
|
|
}
|
|
|
|
if(!match) {
|
|
|
|
semerror(constline(prim),"Data value is not consistent with the expected type: %s",
|
2014-09-19 08:26:06 +08:00
|
|
|
basetype->name);
|
2013-09-22 06:19:06 +08:00
|
|
|
}
|
|
|
|
|
2018-11-16 01:00:38 +08:00
|
|
|
target = nullconst();
|
|
|
|
target->nctype = basetype->typ.typecode;
|
2012-02-14 08:25:32 +08:00
|
|
|
|
2018-11-16 01:00:38 +08:00
|
|
|
if(target->nctype != NC_ECONST) {
|
|
|
|
convert1(prim,target);
|
2012-02-14 08:25:32 +08:00
|
|
|
}
|
|
|
|
|
2018-11-16 01:00:38 +08:00
|
|
|
switch (target->nctype) {
|
2012-02-14 08:25:32 +08:00
|
|
|
case NC_ECONST:
|
|
|
|
if(basetype->subclass != NC_ENUM) {
|
2014-09-19 08:26:06 +08:00
|
|
|
semerror(constline(prim),"Conversion to enum not supported (yet)");
|
|
|
|
} break;
|
2014-08-16 05:42:13 +08:00
|
|
|
case NC_OPAQUE:
|
2018-11-16 01:00:38 +08:00
|
|
|
normalizeopaquelength(target,basetype->typ.size);
|
2014-09-19 08:26:06 +08:00
|
|
|
break;
|
2012-02-14 08:25:32 +08:00
|
|
|
default:
|
2014-09-19 08:26:06 +08:00
|
|
|
break;
|
2012-02-14 08:25:32 +08:00
|
|
|
}
|
2018-11-16 01:00:38 +08:00
|
|
|
generator->constant(generator,basetype,target,codebuf);
|
|
|
|
reclaimconstant(target);
|
|
|
|
target = NULL;
|
2012-02-14 08:25:32 +08:00
|
|
|
return;
|
|
|
|
}
|