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.
|
|
|
|
*********************************************************************/
|
|
|
|
|
|
|
|
#include "includes.h"
|
2014-09-19 08:26:06 +08:00
|
|
|
#include "odom.h"
|
2010-06-03 21:24:43 +08:00
|
|
|
|
|
|
|
/******************************************************/
|
|
|
|
/* Code for generating char variables etc; mostly
|
|
|
|
language independent */
|
|
|
|
/******************************************************/
|
|
|
|
|
2014-09-19 08:26:06 +08:00
|
|
|
/*Forward*/
|
2013-07-17 04:22:48 +08:00
|
|
|
static size_t gen_charconstant(NCConstant*, Bytebuffer*, int fillchar);
|
2012-02-14 08:25:32 +08:00
|
|
|
static int getfillchar(Datalist* fillsrc);
|
2014-09-19 08:26:06 +08:00
|
|
|
static void gen_leafchararray(Dimset*,int,Datalist*,Bytebuffer*, int);
|
|
|
|
static NCConstant* makeconst(int lineno, int len, char* str);
|
2012-02-14 08:25:32 +08:00
|
|
|
|
2010-06-03 21:24:43 +08:00
|
|
|
/*
|
|
|
|
Matching strings to char variables, attributes, and vlen
|
|
|
|
constants is challenging because it is desirable to mimic
|
2014-08-18 02:03:23 +08:00
|
|
|
the original ncgen(3). The "algorithms" used there have no
|
2010-06-03 21:24:43 +08:00
|
|
|
simple characterization (such as "abc" == {'a','b','c'}).
|
|
|
|
So, this rather ugly code is kept in this file
|
2014-08-18 02:03:23 +08:00
|
|
|
and a variety of heuristics are used to mimic ncgen3.
|
2010-06-03 21:24:43 +08:00
|
|
|
|
2012-02-14 08:25:32 +08:00
|
|
|
The core algorithm is as follows.
|
2014-09-19 08:26:06 +08:00
|
|
|
1. Assume we have a set of dimensions D1..Dn.
|
|
|
|
Any of the Di may be unlimited,
|
|
|
|
but it is assumed that the sizes of the Di are all known.
|
2012-02-14 08:25:32 +08:00
|
|
|
2. Given a sequence of string or character constants
|
|
|
|
C1..Cm, our goal is to construct a single string
|
|
|
|
whose length is the cross product of D1 thru Dn.
|
|
|
|
3. For purposes of this algorithm, character constants
|
|
|
|
are treated as strings of size 1.
|
|
|
|
4. Construct Dx = cross product of D1 thru D(n-1).
|
|
|
|
5. For each constant Ci, add fill characters, if necessary,
|
|
|
|
so that its length is a multiple of Dn.
|
|
|
|
6. Concatenate the modified C1..Cm to produce string S.
|
|
|
|
7. Add fill characters to S to make its size be a multiple of
|
|
|
|
Dn.
|
|
|
|
8. If S is longer than the Dx * Dn, then truncate
|
|
|
|
and generate a warning.
|
|
|
|
|
2014-09-19 08:26:06 +08:00
|
|
|
Two special cases:
|
2012-02-14 08:25:32 +08:00
|
|
|
1. character vlen: char(*) vlen_t.
|
|
|
|
For this case, we simply concat all the elements.
|
|
|
|
2. character attribute.
|
|
|
|
For this case, we simply concat all the elements.
|
|
|
|
*/
|
2010-06-03 21:24:43 +08:00
|
|
|
|
|
|
|
void
|
2014-09-19 08:26:06 +08:00
|
|
|
gen_chararray(Dimset* dimset, int dimindex, Datalist* data, Bytebuffer* charbuf, Datalist* fillsrc)
|
2010-06-03 21:24:43 +08:00
|
|
|
{
|
2012-02-14 08:25:32 +08:00
|
|
|
int fillchar = getfillchar(fillsrc);
|
2014-09-19 08:26:06 +08:00
|
|
|
int rank = rankfor(dimset);
|
|
|
|
int firstunlim = findunlimited(dimset,0);
|
|
|
|
int nunlim = countunlimited(dimset);
|
|
|
|
int nc3unlim = (nunlim <= 1 && (firstunlim == 0 || firstunlim == rank)); /* netcdf-3 case of at most 1 unlim in 0th dimension */
|
2010-06-03 21:24:43 +08:00
|
|
|
|
2014-09-19 08:26:06 +08:00
|
|
|
/* Case: netcdf3 case */
|
|
|
|
if(nc3unlim) {
|
|
|
|
gen_leafchararray(dimset,0,data,charbuf,fillchar);
|
|
|
|
return;
|
2012-02-14 08:25:32 +08:00
|
|
|
}
|
2010-07-30 04:37:05 +08:00
|
|
|
|
2014-09-19 08:26:06 +08:00
|
|
|
/* else generate should have done all the hard work */
|
|
|
|
gen_leafchararray(dimset,dimindex,data,charbuf,fillchar);
|
2010-06-03 21:24:43 +08:00
|
|
|
}
|
|
|
|
|
2014-09-19 08:26:06 +08:00
|
|
|
#if 0
|
2012-02-14 08:25:32 +08:00
|
|
|
/* Recursive helper */
|
2010-06-03 21:24:43 +08:00
|
|
|
static void
|
2014-09-19 08:26:06 +08:00
|
|
|
gen_chararrayr(Dimset* dimset, int dimindex,
|
2012-02-14 08:25:32 +08:00
|
|
|
Bytebuffer* databuf, Datalist* data, int fillchar,
|
|
|
|
int unitsize, int expectedsize)
|
2010-06-03 21:24:43 +08:00
|
|
|
{
|
|
|
|
int i;
|
2014-09-19 08:26:06 +08:00
|
|
|
size_t dimsize = declsizefor(dimset,dimindex);
|
|
|
|
int rank = dimset->ndims;
|
|
|
|
int firstunlim = findunlimited(dimset,0);
|
|
|
|
int lastunlimited = findlastunlimited(dimset);
|
|
|
|
int nextunlimited = findunlimited(dimset,dimindex+1);
|
|
|
|
int islastgroup = (lastunlimited == rank || dimindex >= lastunlimited || dimindex == rank-1);
|
|
|
|
Odometer* subodom = NULL;
|
|
|
|
|
|
|
|
ASSERT(rank > 0);
|
|
|
|
ASSERT((islastgroup));
|
|
|
|
|
|
|
|
/* we should be at a list of simple constants */
|
|
|
|
for(i=0;i<data->length;i++) {
|
|
|
|
NCConstant* c = datalistith(data,i);
|
|
|
|
ASSERT(!islistconst(c));
|
|
|
|
if(isstringable(c->nctype)) {
|
|
|
|
int j;
|
|
|
|
size_t constsize;
|
|
|
|
constsize = gen_charconstant(c,databuf,fillchar);
|
|
|
|
if(constsize % unitsize > 0) {
|
|
|
|
size_t padsize = unitsize - (constsize % unitsize);
|
|
|
|
for(j=0;j<padsize;j++) bbAppend(databuf,fillchar);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
semwarn(constline(c),
|
|
|
|
"Encountered non-string and non-char constant in datalist; ignored");
|
|
|
|
}
|
|
|
|
}/* for */
|
2012-02-14 08:25:32 +08:00
|
|
|
|
|
|
|
/* If |databuf| > expectedsize, complain: exception is zero length */
|
|
|
|
if(bbLength(databuf) == 0 && expectedsize == 1) {
|
2014-09-19 08:26:06 +08:00
|
|
|
/* this is okay */
|
2012-02-14 08:25:32 +08:00
|
|
|
} else if(bbLength(databuf) > expectedsize) {
|
2014-09-19 08:26:06 +08:00
|
|
|
semwarn(data->data[0].lineno,"character data list too long; expected %d character constant, found %d: ",expectedsize,bbLength(databuf));
|
2012-02-14 08:25:32 +08:00
|
|
|
} else {
|
2014-09-19 08:26:06 +08:00
|
|
|
size_t bufsize = bbLength(databuf);
|
|
|
|
/* Pad to size dimproduct size */
|
|
|
|
if(bufsize % expectedsize > 0) {
|
|
|
|
size_t padsize = expectedsize - (bufsize % expectedsize);
|
2012-02-14 08:25:32 +08:00
|
|
|
for(i=0;i<padsize;i++) bbAppend(databuf,fillchar);
|
2014-09-19 08:26:06 +08:00
|
|
|
}
|
2010-06-03 21:24:43 +08:00
|
|
|
}
|
|
|
|
}
|
2014-09-19 08:26:06 +08:00
|
|
|
#endif
|
2010-06-03 21:24:43 +08:00
|
|
|
|
|
|
|
void
|
2012-02-14 08:25:32 +08:00
|
|
|
gen_charattr(Datalist* data, Bytebuffer* databuf)
|
2010-06-03 21:24:43 +08:00
|
|
|
{
|
2018-11-16 01:00:38 +08:00
|
|
|
gen_charseq(data,databuf);
|
2010-06-03 21:24:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2018-11-16 01:00:38 +08:00
|
|
|
gen_charseq(Datalist* data, Bytebuffer* databuf)
|
2010-06-03 21:24:43 +08:00
|
|
|
{
|
2012-02-14 08:25:32 +08:00
|
|
|
int i;
|
2013-07-17 04:22:48 +08:00
|
|
|
NCConstant* c;
|
2010-06-03 21:24:43 +08:00
|
|
|
|
2012-02-14 08:25:32 +08:00
|
|
|
ASSERT(bbLength(databuf) == 0);
|
2010-06-03 21:24:43 +08:00
|
|
|
|
2012-02-14 08:25:32 +08:00
|
|
|
for(i=0;i<data->length;i++) {
|
2014-09-19 08:26:06 +08:00
|
|
|
c = datalistith(data,i);
|
|
|
|
if(isstringable(c->nctype)) {
|
|
|
|
(void)gen_charconstant(c,databuf,NC_FILL_CHAR);
|
|
|
|
} else {
|
|
|
|
semerror(constline(c),
|
|
|
|
"Encountered non-string and non-char constant in datalist");
|
|
|
|
return;
|
|
|
|
}
|
2010-06-03 21:24:43 +08:00
|
|
|
}
|
2010-07-30 04:37:05 +08:00
|
|
|
}
|
|
|
|
|
2012-02-14 08:25:32 +08:00
|
|
|
static size_t
|
2013-07-17 04:22:48 +08:00
|
|
|
gen_charconstant(NCConstant* con, Bytebuffer* databuf, int fillchar)
|
2010-06-03 21:24:43 +08:00
|
|
|
{
|
2012-02-14 08:25:32 +08:00
|
|
|
/* Following cases should be consistent with isstringable */
|
|
|
|
size_t constsize = 1;
|
|
|
|
switch (con->nctype) {
|
|
|
|
case NC_CHAR:
|
|
|
|
bbAppend(databuf,con->value.charv);
|
|
|
|
break;
|
|
|
|
case NC_BYTE:
|
|
|
|
bbAppend(databuf,con->value.int8v);
|
|
|
|
break;
|
|
|
|
case NC_UBYTE:
|
|
|
|
bbAppend(databuf,con->value.uint8v);
|
|
|
|
break;
|
|
|
|
case NC_STRING:
|
2014-09-19 08:26:06 +08:00
|
|
|
constsize = con->value.stringv.len;
|
2018-11-16 01:00:38 +08:00
|
|
|
if(constsize > 0)
|
|
|
|
bbAppendn(databuf,con->value.stringv.stringv,
|
2013-09-24 04:19:40 +08:00
|
|
|
con->value.stringv.len);
|
2012-02-14 08:25:32 +08:00
|
|
|
bbNull(databuf);
|
|
|
|
break;
|
|
|
|
case NC_FILL:
|
2010-07-30 04:37:05 +08:00
|
|
|
bbAppend(databuf,fillchar);
|
2012-02-14 08:25:32 +08:00
|
|
|
break;
|
|
|
|
default:
|
2014-09-19 08:26:06 +08:00
|
|
|
PANIC("unexpected constant type");
|
2012-02-14 08:25:32 +08:00
|
|
|
}
|
|
|
|
return constsize;
|
2010-06-03 21:24:43 +08:00
|
|
|
}
|
2010-07-30 04:37:05 +08:00
|
|
|
|
2012-02-14 08:25:32 +08:00
|
|
|
static int
|
2010-07-30 04:37:05 +08:00
|
|
|
getfillchar(Datalist* fillsrc)
|
|
|
|
{
|
|
|
|
/* Determine the fill char */
|
|
|
|
int fillchar = 0;
|
|
|
|
if(fillsrc != NULL && fillsrc->length > 0) {
|
2018-11-16 01:00:38 +08:00
|
|
|
NCConstant* ccon = fillsrc->data[0];
|
2014-09-19 08:26:06 +08:00
|
|
|
if(ccon->nctype == NC_CHAR) {
|
|
|
|
fillchar = ccon->value.charv;
|
|
|
|
} else if(ccon->nctype == NC_STRING) {
|
|
|
|
if(ccon->value.stringv.len > 0) {
|
|
|
|
fillchar = ccon->value.stringv.stringv[0];
|
|
|
|
}
|
|
|
|
}
|
2010-07-30 04:37:05 +08:00
|
|
|
}
|
|
|
|
if(fillchar == 0) fillchar = NC_FILL_CHAR; /* default */
|
|
|
|
return fillchar;
|
|
|
|
}
|
2012-03-15 07:26:48 +08:00
|
|
|
|
2014-09-19 08:26:06 +08:00
|
|
|
/* I think there is a flaw in the ncgen manual
|
|
|
|
about handling something like this:
|
|
|
|
dimensions:
|
|
|
|
n = 8 ;
|
|
|
|
variables:
|
|
|
|
char cdata2(n) ;
|
|
|
|
data:
|
|
|
|
cdata2 = '\000','\001','\002','\177','\200','\201','\376','\377';
|
|
|
|
The rules would say that each of the 8 char constants must
|
|
|
|
be padded to length 8. I think this is only true if the dimension
|
|
|
|
is unlimited, and even then, I am not sure.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static void
|
|
|
|
gen_leafchararray(Dimset* dimset, int dimindex, Datalist* data,
|
|
|
|
Bytebuffer* charbuf, int fillchar)
|
2012-03-15 07:26:48 +08:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
size_t expectedsize,xproduct,unitsize;
|
2014-09-19 08:26:06 +08:00
|
|
|
int rank = rankfor(dimset);
|
2012-03-15 07:26:48 +08:00
|
|
|
|
2014-09-19 08:26:06 +08:00
|
|
|
ASSERT(bbLength(charbuf) == 0);
|
|
|
|
ASSERT((findlastunlimited(dimset) == rank
|
|
|
|
|| findlastunlimited(dimset) == dimindex));
|
2012-03-15 07:26:48 +08:00
|
|
|
|
2014-09-19 08:26:06 +08:00
|
|
|
/*
|
|
|
|
There are a number of special cases that must be
|
|
|
|
considered, mostly driven by the need to keep consistent
|
|
|
|
with ncgen3. These cases are driven by the number of
|
|
|
|
dimensions, which dimensions are unlimited (if any), etc.
|
2012-03-15 07:26:48 +08:00
|
|
|
|
2014-09-19 08:26:06 +08:00
|
|
|
The general rule is based on the size of the last
|
|
|
|
dimension, we compute the required size (after padding)
|
|
|
|
of each string constant. Expected size is then the size
|
|
|
|
of concat of the string constants after padding.
|
2012-03-15 07:26:48 +08:00
|
|
|
|
2015-08-20 17:42:05 +08:00
|
|
|
There is another special case used for back compatibility with ncgen3.
|
2014-09-19 08:26:06 +08:00
|
|
|
In the datalist, all sequences of character constants (i.e. 'X')
|
|
|
|
are concatenated into a single string; the result, however is not
|
|
|
|
concatenated with any trailing or leading string (with double quotes).
|
2012-03-15 07:26:48 +08:00
|
|
|
*/
|
2014-09-19 08:26:06 +08:00
|
|
|
|
2018-11-16 01:00:38 +08:00
|
|
|
/* Rebuild the datalist to merge '0x' constants;
|
|
|
|
WARNING: this is tricky.
|
|
|
|
*/
|
2014-09-19 08:26:06 +08:00
|
|
|
{
|
|
|
|
int i,cccount = 0;
|
|
|
|
/* Do initial walk */
|
|
|
|
for(i=0;i<datalistlen(data);i++) {
|
|
|
|
NCConstant* con = datalistith(data,i);
|
|
|
|
if(consttype(con) == NC_CHAR || consttype(con) == NC_BYTE) {
|
|
|
|
cccount++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(cccount > 1) {
|
2018-11-16 01:00:38 +08:00
|
|
|
Bytebuffer* accum = bbNew();
|
|
|
|
int len = 0; /* >0 implies doing accum */
|
2014-09-19 08:26:06 +08:00
|
|
|
Datalist* newlist = builddatalist(datalistlen(data));
|
|
|
|
int lineno = 0;
|
|
|
|
NCConstant* con;
|
2018-11-16 01:00:38 +08:00
|
|
|
/* We are going to construct a single string constant for each
|
|
|
|
contiguous sequence of single char values.
|
|
|
|
Assume that the constants are all primitive types */
|
2014-09-19 08:26:06 +08:00
|
|
|
for(i=0;i<datalistlen(data);i++) {
|
|
|
|
con = datalistith(data,i);
|
|
|
|
if(consttype(con) == NC_CHAR || consttype(con) == NC_BYTE) {
|
2018-11-16 01:00:38 +08:00
|
|
|
if(len == 0) { /* Start an accumulation */
|
2014-09-19 08:26:06 +08:00
|
|
|
lineno = constline(con);
|
2018-11-16 01:00:38 +08:00
|
|
|
bbClear(accum);
|
|
|
|
}
|
|
|
|
bbAppend(accum,con->value.charv);
|
2014-09-19 08:26:06 +08:00
|
|
|
len++;
|
2018-11-16 01:00:38 +08:00
|
|
|
/* Discard this constant */
|
|
|
|
reclaimconstant(con);
|
2014-09-19 08:26:06 +08:00
|
|
|
} else {
|
2018-11-16 01:00:38 +08:00
|
|
|
if(len > 0) { /* End the accumulation */
|
|
|
|
bbNull(accum);
|
|
|
|
con = makeconst(lineno,len,bbContents(accum));
|
2014-09-19 08:26:06 +08:00
|
|
|
len = 0;
|
|
|
|
lineno = 0;
|
2018-11-16 01:00:38 +08:00
|
|
|
dlappend(newlist,con);
|
|
|
|
} else
|
|
|
|
dlappend(newlist,con);
|
2014-09-19 08:26:06 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/* deal with any unclosed strings */
|
|
|
|
if(len > 0) {
|
2018-11-16 01:00:38 +08:00
|
|
|
con = makeconst(lineno,len,bbContents(accum));
|
2014-09-19 08:26:06 +08:00
|
|
|
len = 0;
|
|
|
|
lineno = 0;
|
|
|
|
dlappend(newlist,con);
|
|
|
|
}
|
2018-11-16 01:00:38 +08:00
|
|
|
bbFree(accum);
|
|
|
|
/* Move the newlist sequence of constants into the old list */
|
|
|
|
efree(data->data);
|
|
|
|
data->data = newlist->data;
|
|
|
|
data->length = newlist->length;
|
|
|
|
data->alloc = newlist->alloc;
|
|
|
|
efree(newlist);
|
2014-09-19 08:26:06 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-20 17:42:05 +08:00
|
|
|
/* Compute crossproduct up to (but not including) the last dimension */
|
2014-09-19 08:26:06 +08:00
|
|
|
xproduct = crossproduct(dimset,dimindex,rank-1);
|
|
|
|
|
|
|
|
/* Start casing it out */
|
|
|
|
if(rank == 0) {
|
|
|
|
unitsize = 1;
|
2012-03-15 07:26:48 +08:00
|
|
|
expectedsize = (xproduct * unitsize);
|
2014-09-19 08:26:06 +08:00
|
|
|
} else if(rank == 1) {
|
|
|
|
unitsize = 1;
|
|
|
|
expectedsize = (xproduct * declsizefor(dimset,rank-1));
|
|
|
|
} else if(isunlimited(dimset,rank-1)) {/* last dimension is unlimited */
|
2012-03-15 07:26:48 +08:00
|
|
|
unitsize = 1;
|
2014-09-19 08:26:06 +08:00
|
|
|
expectedsize = (xproduct*declsizefor(dimset,rank-1));
|
|
|
|
} else { /* rank > 0 && last dim is not unlimited */
|
|
|
|
unitsize = declsizefor(dimset,rank-1);
|
2012-03-15 07:26:48 +08:00
|
|
|
expectedsize = (xproduct * unitsize);
|
|
|
|
}
|
|
|
|
|
|
|
|
for(i=0;i<data->length;i++) {
|
2014-09-19 08:26:06 +08:00
|
|
|
NCConstant* c = datalistith(data,i);
|
|
|
|
ASSERT(!islistconst(c));
|
|
|
|
if(isstringable(c->nctype)) {
|
|
|
|
int j;
|
|
|
|
size_t constsize;
|
|
|
|
constsize = gen_charconstant(c,charbuf,fillchar);
|
|
|
|
if(constsize == 0 || constsize % unitsize > 0) {
|
|
|
|
size_t padsize = unitsize - (constsize % unitsize);
|
|
|
|
for(j=0;j<padsize;j++) bbAppend(charbuf,fillchar);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
semwarn(constline(c),"Encountered non-string and non-char constant in datalist; ignored");
|
|
|
|
}
|
2012-03-15 07:26:48 +08:00
|
|
|
}
|
|
|
|
/* If |databuf| > expectedsize, complain: exception is zero length */
|
2014-09-19 08:26:06 +08:00
|
|
|
if(bbLength(charbuf) == 0 && expectedsize == 1) {
|
|
|
|
/* this is okay */
|
|
|
|
} else if(bbLength(charbuf) > expectedsize) {
|
2018-11-16 01:00:38 +08:00
|
|
|
semwarn(data->data[0]->lineno,"character data list too long; expected %d character constant, found %d: ",expectedsize,bbLength(charbuf));
|
2012-03-15 07:26:48 +08:00
|
|
|
} else {
|
2014-09-19 08:26:06 +08:00
|
|
|
size_t bufsize = bbLength(charbuf);
|
|
|
|
/* Pad to size dimproduct size */
|
|
|
|
if(bufsize % expectedsize > 0) {
|
|
|
|
size_t padsize = expectedsize - (bufsize % expectedsize);
|
|
|
|
for(i=0;i<padsize;i++) bbAppend(charbuf,fillchar);
|
|
|
|
}
|
2012-03-15 07:26:48 +08:00
|
|
|
}
|
|
|
|
}
|
2014-09-19 08:26:06 +08:00
|
|
|
|
|
|
|
/* Create a new string constant */
|
|
|
|
static NCConstant*
|
|
|
|
makeconst(int lineno, int len, char* str)
|
|
|
|
{
|
2018-11-16 01:00:38 +08:00
|
|
|
NCConstant* con = nullconst();
|
2014-09-19 08:26:06 +08:00
|
|
|
con->nctype = NC_STRING;
|
|
|
|
con->lineno = lineno;
|
|
|
|
con->filled = 0;
|
|
|
|
con->value.stringv.len = len;
|
|
|
|
/* We cannot use strdup because str might have embedded nuls */
|
2017-10-31 05:52:08 +08:00
|
|
|
con->value.stringv.stringv = (char*)ecalloc(len+1);
|
2014-09-19 08:26:06 +08:00
|
|
|
memcpy((void*)con->value.stringv.stringv,(void*)str,len);
|
|
|
|
con->value.stringv.stringv[len] = '\0';
|
|
|
|
return con;
|
|
|
|
}
|