/********************************************************************* * Copyright 2018, UCAR/Unidata * See netcdf/COPYRIGHT file for copying and redistribution conditions. *********************************************************************/ #include "includes.h" #include /******************************************************/ /* Code for generating char variables etc; mostly language independent */ /******************************************************/ /*Forward*/ static size_t gen_charconstant(NCConstant*, Bytebuffer*, char fillchar); static char getfillchar(Datalist* fillsrc); static void gen_leafchararray(Dimset*,int,Datalist*,Bytebuffer*, char); static NCConstant* makeconst(int lineno, size_t len, char* str); static void rebuildsingletons(Datalist* data); /* Matching strings to char variables, attributes, and vlen constants is challenging because it is desirable to mimic the original ncgen(3). The "algorithms" used there have no simple characterization (such as "abc" == {'a','b','c'}). So, this rather ugly code is kept in this file and a variety of heuristics are used to mimic ncgen3. The core algorithm is as follows. 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. 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. Two special cases: 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. */ void gen_chararray(Dimset* dimset, int dimindex, Datalist* data, Bytebuffer* charbuf, Datalist* fillsrc) { char fillchar = getfillchar(fillsrc); 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 */ /* Case: netcdf3 case */ if(nc3unlim) { gen_leafchararray(dimset,0,data,charbuf,fillchar); return; } #if 0 if(dimindex < (rank - 1)) { size_t stop = datalistlen(data); size_t offset; for(offset=0;offset 0 && !isunlimited(dimset,rank-1)) {/*last dim is not unlimited */ unitsize = declsizefor(dimset,rank-1); expectedsize = (xproduct * unitsize); } else abort(); /* in case we forgot a case */ flat = flatten(data,rank); for(i=0;ilength;i++) { NCConstant* c = datalistith(flat,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 expectedsize, complain: exception is zero length */ if(bbLength(charbuf) == 0 && expectedsize == 1) { /* this is okay */ } else if(bbLength(charbuf) > expectedsize) { semwarn(flat->data[0]->lineno,"character data list too long; expected %d character constant, found %d: ",expectedsize,bbLength(charbuf)); } else { size_t bufsize = bbLength(charbuf); /* Pad to size dimproduct size */ if(bufsize % expectedsize > 0) { size_t padsize = expectedsize - (bufsize % expectedsize); for(i=0;ilength;i++) { 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; } } } static size_t gen_charconstant(NCConstant* con, Bytebuffer* databuf, char fillchar) { /* 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,(char)con->value.uint8v); break; case NC_STRING: constsize = con->value.stringv.len; if(constsize > 0) bbAppendn(databuf,con->value.stringv.stringv, con->value.stringv.len); bbNull(databuf); break; case NC_FILL: bbAppend(databuf,fillchar); break; default: PANIC("unexpected constant type"); } return constsize; } /* Create a new string constant */ static NCConstant* makeconst(int lineno, size_t len, char* str) { NCConstant* con = nullconst(); 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 */ con->value.stringv.stringv = (char*)ecalloc(len+1); memcpy((void*)con->value.stringv.stringv,(void*)str, len); con->value.stringv.stringv[len] = '\0'; return con; } static char getfillchar(Datalist* fillsrc) { /* Determine the fill char */ if(fillsrc != NULL && fillsrc->length > 0) { NCConstant* ccon = fillsrc->data[0]; if(ccon->nctype == NC_CHAR) { return ccon->value.charv; } else if(ccon->nctype == NC_STRING) { if(ccon->value.stringv.len > 0) { return ccon->value.stringv.stringv[0]; } } } return NC_FILL_CHAR; /* default */ } /* Rebuild the datalist to merge '0x' constants; WARNING: this is tricky. */ static void rebuildsingletons(Datalist* data) { int i,cccount = 0; /* Do initial walk */ for(i=0;i 1) { Bytebuffer* accum = bbNew(); size_t len = 0; /* >0 implies doing accum */ Datalist* newlist = builddatalist((int)datalistlen(data)); int lineno = 0; NCConstant* con; /* 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 */ for(i=0;ivalue.charv); len++; /* Discard this constant */ reclaimconstant(con); } else { if(len > 0) { /* End the accumulation */ bbNull(accum); con = makeconst(lineno,len,bbContents(accum)); len = 0; lineno = 0; dlappend(newlist,con); } else dlappend(newlist,con); } } /* deal with any unclosed strings */ if(len > 0) { con = makeconst(lineno,len,bbContents(accum)); len = 0; lineno = 0; dlappend(newlist,con); } 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); } } #if 0 /* Recursive helper */ static void gen_chararrayr(Dimset* dimset, int dimindex, Bytebuffer* databuf, Datalist* data, int fillchar, int unitsize, int expectedsize) { int i; 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); ASSERT(rank > 0); ASSERT((islastgroup)); /* we should be at a list of simple constants */ for(i=0;ilength;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 expectedsize, complain: exception is zero length */ if(bbLength(databuf) == 0 && expectedsize == 1) { /* this is okay */ } else if(bbLength(databuf) > expectedsize) { semwarn(data->data[0].lineno,"character data list too long; expected %lu character constant, found %lu: ",expectedsize,(unsigned long)bbLength(databuf)); } else { size_t bufsize = bbLength(databuf); /* Pad to size dimproduct size */ if(bufsize % expectedsize > 0) { size_t padsize = expectedsize - (bufsize % expectedsize); for(i=0;i