1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394
|
/*********************************************************************
* Copyright 2009, UCAR/Unidata
* See netcdf/COPYRIGHT file for copying and redistribution conditions.
*********************************************************************/
/* $Id: cdata.c,v 1.5 2010/05/24 19:59:56 dmh Exp $ */
/* $Header: /upc/share/CVS/netcdf-3/ncgen/cdata.c,v 1.5 2010/05/24 19:59:56 dmh Exp $ */
#include "includes.h"
#ifdef ENABLE_C
#include <math.h>
/**************************************************/
/* Code for generating C language data lists*/
/**************************************************/
/* For datalist constant rules: see the rules on the man page */
/* Forward*/
static void cdata_primdata(Symbol*, Datasrc*, Bytebuffer*, Datalist*);
static void cdata_fieldarray(Symbol*, Datasrc*, Odometer*, int, Bytebuffer*, Datalist* fillsrc);
/* Specialty wrappers for cdata_data */
void
cdata_attrdata(Symbol* asym, Bytebuffer* codebuf)
{
Datasrc* src;
int typecode = asym->typ.basetype->typ.typecode;
if(asym->data == NULL) return;
if(typecode == NC_CHAR) {
gen_charattr(asym,codebuf);
} else {
src = datalist2src(asym->data);
while(srcmore(src)) {
bbAppend(codebuf,' ');
cdata_basetype(asym->typ.basetype,src,codebuf,NULL);
}
}
}
void
cdata_array(Symbol* vsym,
Bytebuffer* codebuf,
Datasrc* src,
Odometer* odom,
int index,
Datalist* fillsrc)
{
int i;
int rank = odom->rank;
int pushed = 0;
size_t count;
Symbol* basetype = vsym->typ.basetype;
int lastdim = (index == (rank - 1)); /* last dimension*/
int firstdim = (index == 0); /* first dimension*/
int isunlimited = (odom->declsize[index] == 0);
ASSERT(index >= 0 && index < rank);
count = odom->count[index];
if(!firstdim && isunlimited && issublist(src)) {
srcpush(src);
pushed = 1;
}
if(lastdim) {
for(i=0;i<count;i++) {
cdata_basetype(basetype,src,codebuf,fillsrc);
}
} else {
/* now walk count elements and generate recursively */
for(i=0;i<count;i++) {
cdata_array(vsym,codebuf,src,odom,index+1,fillsrc);
}
}
if(isunlimited && pushed) srcpop(src);
return;
}
/* Generate an instance of the basetype using datasrc */
void
cdata_basetype(Symbol* tsym, Datasrc* datasrc, Bytebuffer* codebuf, Datalist* fillsrc)
{
int usecmpd;
switch (tsym->subclass) {
case NC_ENUM:
case NC_OPAQUE:
case NC_PRIM:
if(issublist(datasrc)) {
semerror(srcline(datasrc),"Expected primitive found {..}");
}
bbAppend(codebuf,' ');
cdata_primdata(tsym,datasrc,codebuf,fillsrc);
break;
case NC_COMPOUND: {
int i;
Constant* con;
if(!isfillvalue(datasrc) && !issublist(datasrc)) {/* fail on no compound*/
semerror(srcline(datasrc),"Compound data must be enclosed in {..}");
}
con = srcnext(datasrc);
if(con == NULL || con->nctype == NC_FILLVALUE) {
Datalist* filler = getfiller(tsym,fillsrc);
ASSERT(filler->length == 1);
con = &filler->data[0];
if(con->nctype != NC_COMPOUND) {
semerror(con->lineno,"Compound data fill value is not enclosed in {..}");
}
}
srcpushlist(datasrc,con->value.compoundv); /* enter the sublist*/
bbAppend(codebuf,'{');
for(i=0;i<listlength(tsym->subnodes);i++) {
Symbol* field = (Symbol*)listget(tsym->subnodes,i);
bbAppend(codebuf,' ');
cdata_basetype(field,datasrc,codebuf,NULL);
}
bbAppend(codebuf,'}');
srcpop(datasrc);
} break;
case NC_VLEN: {
Constant* con;
if(!isfillvalue(datasrc) && !issublist(datasrc)) {/* fail on no compound*/
semerror(srcline(datasrc),"Vlen data must be enclosed in {..}");
}
con = srcnext(datasrc);
if(con == NULL || con->nctype == NC_FILLVALUE) {
Datalist* filler = getfiller(tsym,fillsrc);
ASSERT(filler->length == 1);
con = &filler->data[0];
if(con->nctype != NC_COMPOUND) {
semerror(con->lineno,"Vlen data fill value is not enclosed in {..}");
}
}
/* generate the nc_vlen_t instance*/
bbprintf0(stmt,"{%u (void*)vlen_%u}",
con->value.compoundv->vlen.count,
con->value.compoundv->vlen.uid);
bbCatbuf(codebuf,stmt);
} break;
case NC_FIELD:
/* enclose in braces if and only if field is an array */
usecmpd = (issublist(datasrc) && tsym->typ.dimset.ndims > 0);
if(usecmpd) srcpush(datasrc);
if(tsym->typ.dimset.ndims > 0) {
Odometer* fullodom = newodometer(&tsym->typ.dimset,NULL,NULL);
cdata_fieldarray(tsym->typ.basetype,datasrc,fullodom,0,codebuf,fillsrc);
odometerfree(fullodom);
} else {
cdata_basetype(tsym->typ.basetype,datasrc,codebuf,NULL);
}
if(usecmpd) srcpop(datasrc);
break;
default: PANIC1("cdata_basetype: unexpected subclass %d",tsym->subclass);
}
}
/* Used only for structure field arrays*/
static void
cdata_fieldarray(Symbol* basetype, Datasrc* src, Odometer* odom, int index,
Bytebuffer* codebuf, Datalist* fillsrc)
{
int i;
int rank = odom->rank;
unsigned int size = odom->declsize[index];
int lastdim = (index == (rank - 1)); /* last dimension*/
int chartype = (basetype->typ.typecode == NC_CHAR);
if(chartype) {
/* Collect the char field in a separate buffer */
Bytebuffer* fieldbuf = bbNew();
gen_charfield(src,odom,fieldbuf);
/* Add to the existing data buf as a single constant */
cquotestring(fieldbuf);
bbCat(codebuf," ");
bbCatbuf(codebuf,fieldbuf);
bbFree(fieldbuf);
} else {
ASSERT(size != 0);
for(i=0;i<size;i++) {
if(lastdim) {
bbAppend(codebuf,' ');
cdata_basetype(basetype,src,codebuf,NULL);
} else { /* !lastdim*/
cdata_fieldarray(basetype,src,odom,index+1,codebuf,fillsrc);
}
}
}
}
static void
cdata_primdata(Symbol* basetype, Datasrc* src, Bytebuffer* codebuf, Datalist* fillsrc)
{
Constant* prim;
Constant target;
prim = srcnext(src);
if(prim == NULL) prim = &fillconstant;
ASSERT(prim->nctype != NC_COMPOUND);
if(prim->nctype == NC_FILLVALUE) {
Datalist* filler = getfiller(basetype,fillsrc);
ASSERT(filler->length == 1);
srcpushlist(src,filler);
bbAppend(codebuf,' ');
cdata_primdata(basetype,src,codebuf,NULL);
srcpop(src);
goto done;
}
target.nctype = basetype->typ.typecode;
if(target.nctype != NC_ECONST) {
convert1(prim,&target);
}
switch (target.nctype) {
case NC_ECONST:
if(basetype->subclass != NC_ENUM) {
semerror(prim->lineno,"Conversion to enum not supported (yet)");
} else {
Datalist* econ = builddatalist(1);
Symbol* enumv = prim->value.enumv;
srcpushlist(src,econ);
dlappend(econ,&enumv->typ.econst);
cdata_primdata(enumv->typ.basetype,src,codebuf,fillsrc);
srcpop(src);
} break;
case NC_OPAQUE: {
setprimlength(&target,basetype->typ.size*2);
} break;
default: break;
}
bbCat(codebuf,cdata_const(&target));
done:
return;
}
/*
This walk of the data lists collects
vlen sublists and constructs separate C constants
for each of them. The "id" of each list is then
recorded in the containing datalist.
*/
void
cdata_vlenconstants(List* vlenconstants, Bytebuffer* codebuf)
{
int i,nvlen;
Datasrc* vlensrc;
Bytebuffer* tmp = bbNew();
nvlen = listlength(vlenconstants);
for(i=0;i<nvlen;i++) {
Constant* cmpd = (Constant*)listget(vlenconstants,i);
int chartype;
Symbol* tsym = cmpd->value.compoundv->vlen.schema;
ASSERT(tsym != NULL);
chartype = (tsym->typ.basetype->typ.typecode == NC_CHAR);
bbprintf0(tmp,"static const %s vlen_%u[] = ",
ctypename(tsym->typ.basetype),
cmpd->value.compoundv->vlen.uid);
bbCatbuf(codebuf,tmp);
vlensrc = datalist2src(cmpd->value.compoundv);
bbAppend(codebuf,'{');
if(chartype) {
/* Collect the char vlen in a separate buffer */
Bytebuffer* vlenbuf = bbNew();
gen_charvlen(vlensrc,vlenbuf);
/* Add to the existing data buf as a single constant */
cquotestring(vlenbuf);
bbCatbuf(codebuf,vlenbuf);
bbFree(vlenbuf);
} else {
size_t count = 0;
while(srcmore(vlensrc)) {
if(count > 0) bbCat(codebuf,", ");
cdata_basetype(tsym->typ.basetype,vlensrc,codebuf,NULL);
count++;
}
ASSERT(count == cmpd->value.compoundv->vlen.count);
}
bbCat(codebuf,"} ;\n");
}
bbFree(tmp);
}
/* Result is a pool string or a constant => do not free*/
char*
cdata_const(Constant* ci)
{
Bytebuffer* codetmp = bbNew();
char* result;
switch (ci->nctype) {
case NC_CHAR:
{
char tmp[64];
tmp[0] = '\0';
escapifychar(ci->value.charv,tmp,'\'');
bbCat(codetmp,"'");
bbCat(codetmp,tmp);
bbCat(codetmp,"'");
}
break;
case NC_BYTE:
bbprintf(codetmp,"%hhd",ci->value.int8v);
break;
case NC_SHORT:
bbprintf(codetmp,"%hd",ci->value.int16v);
break;
case NC_INT:
bbprintf(codetmp,"%d",ci->value.int32v);
break;
case NC_FLOAT:
/* Special case for nanf */
if(isnan(ci->value.floatv))
bbprintf(codetmp,"nanf");
else
bbprintf(codetmp,"%f",ci->value.floatv);
break;
case NC_DOUBLE:
/* Special case for nanf */
if(isnan(ci->value.doublev))
bbprintf(codetmp,"nan");
else
bbprintf(codetmp,"%lf",ci->value.doublev);
break;
case NC_UBYTE:
bbprintf(codetmp,"%hhu",ci->value.uint8v);
break;
case NC_USHORT:
bbprintf(codetmp,"%hu",ci->value.uint16v);
break;
case NC_UINT:
bbprintf(codetmp,"%uU",ci->value.uint32v);
break;
case NC_INT64:
bbprintf(codetmp,"%lldLL",ci->value.int64v);
break;
case NC_UINT64:
bbprintf(codetmp,"%lluLLU",ci->value.uint64v);
break;
case NC_ECONST:
bbprintf(codetmp,"%s",cname(ci->value.enumv));
break;
case NC_STRING:
{ /* handle separately */
char* escaped = escapify(ci->value.stringv.stringv,
'"',ci->value.stringv.len);
result = poolalloc(1+2+strlen(escaped));
strcpy(result,"\"");
strcat(result,escaped);
strcat(result,"\"");
goto done;
}
break;
case NC_OPAQUE: {
char* p;
int bslen;
bslen=(4*ci->value.opaquev.len);
result = poolalloc(bslen+2+1);
strcpy(result,"\"");
p = ci->value.opaquev.stringv;
while(*p) {
strcat(result,"\\x");
strncat(result,p,2);
p += 2;
}
strcat(result,"\"");
goto done;
} break;
default: PANIC1("ncstype: bad type code: %d",ci->nctype);
}
result = pooldup(bbContents(codetmp)); /*except for NC_STRING and NC_OPAQUE*/
bbFree(codetmp);
done:
return result;
}
#endif /*ENABLE_C*/
|