2010-06-03 21:24:43 +08:00
|
|
|
/*********************************************************************
|
|
|
|
* Copyright 1993, UCAR/Unidata
|
|
|
|
* See netcdf/COPYRIGHT file for copying and redistribution conditions.
|
|
|
|
* $Header: /upc/share/CVS/netcdf-3/ncgen/genc.c,v 1.6 2010/05/17 23:26:44 dmh Exp $
|
|
|
|
*********************************************************************/
|
|
|
|
|
|
|
|
#include "includes.h"
|
2013-03-01 01:51:09 +08:00
|
|
|
#include "nc_iter.h"
|
2010-06-03 21:24:43 +08:00
|
|
|
#include <ctype.h> /* for isprint() */
|
|
|
|
|
|
|
|
#ifdef ENABLE_C
|
|
|
|
|
|
|
|
#undef TRACE
|
|
|
|
|
|
|
|
/* Forward */
|
|
|
|
static const char* groupncid(Symbol*);
|
|
|
|
static const char* typencid(Symbol*);
|
|
|
|
static const char* varncid(Symbol*);
|
|
|
|
static const char* dimncid(Symbol*);
|
|
|
|
|
|
|
|
#ifdef USE_NETCDF4
|
|
|
|
static void definectype(Symbol*);
|
|
|
|
static void genc_deftype(Symbol*);
|
|
|
|
static void genc_definespecialattributes(Symbol* vsym);
|
2014-03-09 11:41:30 +08:00
|
|
|
static void genc_defineglobalspecials(void);
|
2010-06-03 21:24:43 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
static void genc_defineattr(Symbol* asym);
|
|
|
|
static void genc_definevardata(Symbol*);
|
2012-02-14 08:25:32 +08:00
|
|
|
static void genc_write(Generator*,Symbol* sym, Bytebuffer* code,
|
|
|
|
int rank, size_t* start, size_t* count);
|
|
|
|
static void genc_writevar(Generator*,Symbol*,Bytebuffer*,int,size_t*,size_t*);
|
|
|
|
static void genc_writeattr(Generator*,Symbol*,Bytebuffer*,int,size_t*,size_t*);
|
2010-06-03 21:24:43 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Generate C code for creating netCDF from in-memory structure.
|
|
|
|
*/
|
|
|
|
void
|
2018-11-16 01:00:38 +08:00
|
|
|
genc_netcdf(void)
|
2010-06-03 21:24:43 +08:00
|
|
|
{
|
|
|
|
int idim, ivar, iatt, maxdims;
|
2012-01-30 02:56:29 +08:00
|
|
|
int ndims, nvars, natts, ngatts;
|
2010-06-03 21:24:43 +08:00
|
|
|
char* cmode_string;
|
2018-11-16 01:00:38 +08:00
|
|
|
const char *filename = rootgroup->file.filename;
|
2010-06-03 21:24:43 +08:00
|
|
|
|
|
|
|
#ifdef USE_NETCDF4
|
2012-01-30 02:56:29 +08:00
|
|
|
int igrp,ityp, ngrps, ntyps;
|
2010-06-03 21:24:43 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
ndims = listlength(dimdefs);
|
|
|
|
nvars = listlength(vardefs);
|
|
|
|
natts = listlength(attdefs);
|
|
|
|
ngatts = listlength(gattdefs);
|
2012-01-30 02:56:29 +08:00
|
|
|
#ifdef USE_NETCDF4
|
2010-06-03 21:24:43 +08:00
|
|
|
ngrps = listlength(grpdefs);
|
|
|
|
ntyps = listlength(typdefs);
|
2012-01-30 02:56:29 +08:00
|
|
|
#endif /*USE_NETCDF4*/
|
2010-06-03 21:24:43 +08:00
|
|
|
|
|
|
|
/* wrap in main program */
|
|
|
|
codeline("#include <stdio.h>");
|
|
|
|
codeline("#include <stdlib.h>");
|
|
|
|
codeline("#include <netcdf.h>");
|
|
|
|
codeline("");
|
|
|
|
codeflush();
|
|
|
|
|
2010-11-10 06:53:03 +08:00
|
|
|
if(specialconstants) {
|
|
|
|
/* If the input referenced e.g. nan, inf, etc;
|
|
|
|
then provide definitions for them */
|
|
|
|
codeline("");
|
|
|
|
codeline("#define nanf (0.0f/0.0f)");
|
|
|
|
codeline("#define nan (0.0/0.0)");
|
|
|
|
codeline("#define inff (1.0f/0.0f)");
|
|
|
|
codeline("#define inf (1.0/0.0)");
|
|
|
|
codeline("#define infinityf inff");
|
|
|
|
codeline("#define infinity inf");
|
|
|
|
codeline("");
|
|
|
|
codeflush();
|
|
|
|
}
|
|
|
|
codeline("");
|
|
|
|
codeflush();
|
|
|
|
|
2010-06-03 21:24:43 +08:00
|
|
|
#ifdef USE_NETCDF4
|
|
|
|
|
|
|
|
/* Construct C type definitions*/
|
|
|
|
if (ntyps > 0) {
|
|
|
|
for(ityp = 0; ityp < ntyps; ityp++) {
|
|
|
|
Symbol* tsym = (Symbol*)listget(typdefs,ityp);
|
|
|
|
definectype(tsym);
|
|
|
|
}
|
|
|
|
codeline("");
|
|
|
|
}
|
|
|
|
codeflush();
|
|
|
|
|
|
|
|
/* Construct the chunking constants*/
|
|
|
|
if(!usingclassic) {
|
|
|
|
for(ivar=0;ivar<nvars;ivar++) {
|
|
|
|
Bytebuffer* tmp = bbNew();
|
|
|
|
Symbol* var = (Symbol*)listget(vardefs,ivar);
|
2018-11-16 01:00:38 +08:00
|
|
|
Specialdata* special = var->var.special;
|
2010-06-03 21:24:43 +08:00
|
|
|
if(special->flags & _CHUNKSIZES_FLAG) {
|
|
|
|
int i;
|
|
|
|
size_t* chunks = special->_ChunkSizes;
|
|
|
|
if(special->nchunks == 0 || chunks == NULL) continue;
|
|
|
|
bbClear(tmp);
|
|
|
|
for(i=0;i<special->nchunks;i++) {
|
|
|
|
bbprintf(tmp,"%s%ld",
|
|
|
|
(i == 0?"":", "),special->_ChunkSizes[i]);
|
|
|
|
}
|
|
|
|
bbprintf0(stmt,"static size_t %s_chunksizes[%d] = {",
|
|
|
|
cname(var),special->nchunks);
|
|
|
|
codedump(stmt);
|
|
|
|
codedump(tmp);
|
|
|
|
codeline("} ;");
|
|
|
|
}
|
2017-05-15 08:10:02 +08:00
|
|
|
if(special->flags & _FILTER_FLAG) {
|
2017-04-28 03:01:59 +08:00
|
|
|
int i;
|
|
|
|
unsigned int* params = special->_FilterParams;
|
|
|
|
if(special->nparams == 0 || params == NULL) continue;
|
|
|
|
bbClear(tmp);
|
|
|
|
for(i=0;i<special->nparams;i++) {
|
|
|
|
bbprintf(tmp,"%s%luU",
|
|
|
|
(i == 0?"":", "),params[i]);
|
|
|
|
}
|
|
|
|
bbprintf0(stmt,"static unsigned int %s_filterparams[%d] = {",
|
|
|
|
cname(var),special->nparams);
|
|
|
|
codedump(stmt);
|
|
|
|
codedump(tmp);
|
|
|
|
codeline("} ;");
|
|
|
|
}
|
2013-07-20 05:32:27 +08:00
|
|
|
bbFree(tmp);
|
2010-06-03 21:24:43 +08:00
|
|
|
}
|
|
|
|
codeline("");
|
|
|
|
}
|
|
|
|
#endif /*USE_NETCDF4*/
|
|
|
|
|
|
|
|
/* Now construct the main procedures*/
|
|
|
|
codeline("void");
|
|
|
|
codeline("check_err(const int stat, const int line, const char *file) {");
|
|
|
|
codelined(1,"if (stat != NC_NOERR) {");
|
|
|
|
codelined(2,"(void)fprintf(stderr,\"line %d of %s: %s\\n\", line, file, nc_strerror(stat));");
|
|
|
|
codelined(2,"fflush(stderr);");
|
|
|
|
codelined(2,"exit(1);");
|
|
|
|
codelined(1,"}");
|
|
|
|
codeline("}");
|
|
|
|
codeline("");
|
|
|
|
codeline("int");
|
|
|
|
bbprintf0(stmt,"%s() {/* create %s */\n", mainname, filename);
|
|
|
|
codedump(stmt);
|
|
|
|
/* create necessary declarations */
|
2015-11-12 06:36:02 +08:00
|
|
|
codeline("");
|
2010-06-03 21:24:43 +08:00
|
|
|
codelined(1,"int stat; /* return status */");
|
|
|
|
codelined(1,"int ncid; /* netCDF id */");
|
|
|
|
codeflush();
|
|
|
|
|
|
|
|
#ifdef USE_NETCDF4
|
|
|
|
/* Define variables to hold group ids*/
|
|
|
|
if(!usingclassic) {
|
|
|
|
codeline("");
|
|
|
|
codelined(1,"/* group ids */");
|
|
|
|
}
|
2015-11-12 06:36:02 +08:00
|
|
|
if(!usingclassic && ngrps > 0) {
|
2010-06-03 21:24:43 +08:00
|
|
|
for(igrp = 0; igrp < ngrps; igrp++) {
|
|
|
|
Symbol* gsym = (Symbol*)listget(grpdefs,igrp);
|
|
|
|
bbprintf0(stmt,"%sint %s;\n",indented(1),groupncid(gsym));
|
|
|
|
codedump(stmt);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* define variables to hold type ids*/
|
|
|
|
if(!usingclassic && ntyps > 0) {
|
|
|
|
codeline("");
|
|
|
|
codelined(1,"/* type ids */");
|
|
|
|
for(ityp = 0; ityp < ntyps; ityp++) {
|
|
|
|
Symbol* tsym = (Symbol*)listget(typdefs,ityp);
|
|
|
|
bbprintf0(stmt,"%sint %s;\n",indented(1), typencid(tsym));
|
|
|
|
codedump(stmt);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
codeflush();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (ndims > 0) {
|
|
|
|
codeline("");
|
|
|
|
codelined(1,"/* dimension ids */");
|
|
|
|
for(idim = 0; idim < ndims; idim++) {
|
|
|
|
Symbol* dsym = (Symbol*)listget(dimdefs,idim);
|
|
|
|
bbprintf0(stmt,"%sint %s;\n", indented(1), dimncid(dsym));
|
|
|
|
codedump(stmt);
|
|
|
|
}
|
|
|
|
|
|
|
|
codeline("");
|
|
|
|
codelined(1,"/* dimension lengths */");
|
|
|
|
for(idim = 0; idim < ndims; idim++) {
|
|
|
|
Symbol* dsym = (Symbol*)listget(dimdefs,idim);
|
2012-02-14 08:25:32 +08:00
|
|
|
if(dsym->dim.isunlimited) {
|
2010-06-03 21:24:43 +08:00
|
|
|
bbprintf0(stmt,"%ssize_t %s_len = NC_UNLIMITED;\n",
|
|
|
|
indented(1),cname(dsym));
|
|
|
|
} else {
|
|
|
|
bbprintf0(stmt,"%ssize_t %s_len = %lu;\n",
|
|
|
|
indented(1),
|
|
|
|
cname(dsym),
|
|
|
|
(unsigned long) dsym->dim.declsize);
|
|
|
|
}
|
|
|
|
codedump(stmt);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
codeflush();
|
|
|
|
|
|
|
|
maxdims = 0; /* most dimensions of any variable */
|
|
|
|
for(ivar = 0; ivar < nvars; ivar++) {
|
|
|
|
Symbol* vsym = (Symbol*)listget(vardefs,ivar);
|
|
|
|
if(vsym->typ.dimset.ndims > maxdims)
|
|
|
|
maxdims = vsym->typ.dimset.ndims;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nvars > 0) {
|
|
|
|
codeline("");
|
|
|
|
codelined(1,"/* variable ids */");
|
|
|
|
for(ivar = 0; ivar < nvars; ivar++) {
|
|
|
|
Symbol* vsym = (Symbol*)listget(vardefs,ivar);
|
|
|
|
bbprintf0(stmt," int %s;\n", varncid(vsym));
|
|
|
|
codedump(stmt);
|
|
|
|
}
|
|
|
|
|
|
|
|
codeline("");
|
|
|
|
codelined(1,"/* rank (number of dimensions) for each variable */");
|
|
|
|
for(ivar = 0; ivar < nvars; ivar++) {
|
|
|
|
Symbol* vsym = (Symbol*)listget(vardefs,ivar);
|
|
|
|
bbprintf0(stmt,"# define RANK_%s %d\n", cname(vsym),
|
|
|
|
vsym->typ.dimset.ndims);
|
|
|
|
codedump(stmt);
|
|
|
|
}
|
|
|
|
if (maxdims > 0) { /* we have dimensioned variables */
|
|
|
|
codeline("");
|
|
|
|
codelined(1,"/* variable shapes */");
|
|
|
|
for(ivar = 0; ivar < nvars; ivar++) {
|
|
|
|
Symbol* vsym = (Symbol*)listget(vardefs,ivar);
|
|
|
|
if (vsym->typ.dimset.ndims > 0) {
|
|
|
|
bbprintf0(stmt," int %s_dims[RANK_%s];\n",
|
|
|
|
cname(vsym), cname(vsym));
|
|
|
|
codedump(stmt);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
codeflush();
|
|
|
|
|
2016-09-02 12:06:07 +08:00
|
|
|
/* Set log level */
|
|
|
|
if(ncloglevel >= 0) {
|
|
|
|
codeline("");
|
|
|
|
bbprintf0(stmt," nc_set_log_level(%d); /* set log level */",ncloglevel);
|
|
|
|
codedump(stmt);
|
|
|
|
codeline("");
|
|
|
|
}
|
|
|
|
|
2010-06-03 21:24:43 +08:00
|
|
|
/* create netCDF file, uses NC_CLOBBER mode */
|
|
|
|
codeline("");
|
|
|
|
codelined(1,"/* enter define mode */");
|
|
|
|
|
|
|
|
if (!cmode_modifier) {
|
|
|
|
cmode_string = "NC_CLOBBER";
|
|
|
|
} else if (cmode_modifier & NC_64BIT_OFFSET) {
|
|
|
|
cmode_string = "NC_CLOBBER|NC_64BIT_OFFSET";
|
|
|
|
#ifdef USE_NETCDF4
|
|
|
|
} else if (cmode_modifier & NC_CLASSIC_MODEL) {
|
|
|
|
cmode_string = "NC_CLOBBER|NC_NETCDF4|NC_CLASSIC_MODEL";
|
|
|
|
} else if (cmode_modifier & NC_NETCDF4) {
|
2015-11-12 06:36:02 +08:00
|
|
|
cmode_string = "NC_CLOBBER|NC_NETCDF4";
|
2010-06-03 21:24:43 +08:00
|
|
|
#endif
|
|
|
|
} else {
|
|
|
|
derror("unknown cmode modifier");
|
|
|
|
cmode_string = "NC_CLOBBER";
|
|
|
|
}
|
|
|
|
bbprintf0(stmt," stat = nc_create(\"%s\", %s, &ncid);\n",
|
|
|
|
filename,cmode_string);
|
|
|
|
codedump(stmt);
|
|
|
|
codelined(1,"check_err(stat,__LINE__,__FILE__);");
|
|
|
|
codeflush();
|
2015-11-12 06:36:02 +08:00
|
|
|
|
2014-03-09 11:41:30 +08:00
|
|
|
#ifdef USE_NETCDF4
|
|
|
|
genc_defineglobalspecials();
|
|
|
|
#endif /*USE_NETCDF4*/
|
|
|
|
|
2010-06-03 21:24:43 +08:00
|
|
|
#ifdef USE_NETCDF4
|
|
|
|
/* Define the group structure */
|
|
|
|
/* ncid created above is also root group*/
|
|
|
|
if(!usingclassic) {
|
|
|
|
bbprintf0(stmt," %s = ncid;\n",groupncid(rootgroup));
|
|
|
|
codedump(stmt);
|
|
|
|
/* walking grpdefs list will do a preorder walk of all defined groups*/
|
|
|
|
for(igrp=0;igrp<listlength(grpdefs);igrp++) {
|
|
|
|
Symbol* gsym = (Symbol*)listget(grpdefs,igrp);
|
|
|
|
if(gsym == rootgroup) continue; /* ignore root*/
|
|
|
|
if(gsym->container == NULL)
|
|
|
|
PANIC("null container");
|
|
|
|
bbprintf0(stmt,
|
|
|
|
" stat = nc_def_grp(%s, \"%s\", &%s);\n",
|
|
|
|
groupncid(gsym->container),
|
|
|
|
gsym->name, groupncid(gsym));
|
|
|
|
codedump(stmt);
|
|
|
|
codelined(1,"check_err(stat,__LINE__,__FILE__);");
|
|
|
|
}
|
|
|
|
codeflush();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef USE_NETCDF4
|
|
|
|
/* Construct code to define types*/
|
|
|
|
if(ntyps > 0) {
|
|
|
|
codeline("");
|
|
|
|
for(ityp = 0; ityp < ntyps; ityp++) {
|
|
|
|
Symbol* tsym = (Symbol*)listget(typdefs,ityp);
|
|
|
|
if(tsym->subclass == NC_PRIM
|
|
|
|
|| tsym->subclass == NC_ARRAY) continue; /* no need to do these*/
|
|
|
|
genc_deftype(tsym);
|
|
|
|
codeline("");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
codeflush();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* define dimensions from info in dims array */
|
|
|
|
if (ndims > 0) {
|
|
|
|
codeline("");
|
|
|
|
codelined(1,"/* define dimensions */");
|
|
|
|
for(idim = 0; idim < ndims; idim++) {
|
|
|
|
Symbol* dsym = (Symbol*)listget(dimdefs,idim);
|
2012-02-14 08:25:32 +08:00
|
|
|
{
|
|
|
|
bbprintf0(stmt,
|
|
|
|
" stat = nc_def_dim(%s, \"%s\", %s_len, &%s);\n",
|
|
|
|
groupncid(dsym->container),
|
|
|
|
escapifyname(dsym->name),
|
|
|
|
cname(dsym),
|
|
|
|
dimncid(dsym));
|
|
|
|
}
|
2010-06-03 21:24:43 +08:00
|
|
|
codedump(stmt);
|
|
|
|
codelined(1,"check_err(stat,__LINE__,__FILE__);");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
codeflush();
|
|
|
|
|
|
|
|
/* define variables from info in vars array */
|
|
|
|
if (nvars > 0) {
|
|
|
|
codeline("");
|
|
|
|
codelined(1,"/* define variables */");
|
|
|
|
for(ivar = 0; ivar < nvars; ivar++) {
|
|
|
|
Symbol* vsym = (Symbol*)listget(vardefs,ivar);
|
|
|
|
Symbol* basetype = vsym->typ.basetype;
|
|
|
|
Dimset* dimset = &vsym->typ.dimset;
|
|
|
|
codeline("");
|
|
|
|
if(dimset->ndims > 0) {
|
|
|
|
for(idim = 0; idim < dimset->ndims; idim++) {
|
|
|
|
Symbol* dsym = dimset->dimsyms[idim];
|
|
|
|
bbprintf0(stmt,
|
|
|
|
" %s_dims[%d] = %s;\n",
|
|
|
|
cname(vsym),
|
|
|
|
idim,
|
|
|
|
dimncid(dsym));
|
|
|
|
codedump(stmt);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
bbprintf0(stmt,
|
|
|
|
" stat = nc_def_var(%s, \"%s\", %s, RANK_%s, %s, &%s);\n",
|
|
|
|
groupncid(vsym->container),
|
|
|
|
escapifyname(vsym->name),
|
|
|
|
typencid(basetype),
|
|
|
|
cname(vsym),
|
|
|
|
(dimset->ndims == 0?"0":poolcat(cname(vsym),"_dims")),
|
|
|
|
varncid(vsym));
|
|
|
|
codedump(stmt);
|
|
|
|
codelined(1,"check_err(stat,__LINE__,__FILE__);");
|
|
|
|
#ifdef USE_NETCDF4
|
|
|
|
genc_definespecialattributes(vsym);
|
|
|
|
#endif /*USE_NETCDF4*/
|
|
|
|
}
|
|
|
|
}
|
|
|
|
codeflush();
|
2015-11-12 06:36:02 +08:00
|
|
|
|
2010-06-03 21:24:43 +08:00
|
|
|
/* Define the global attributes*/
|
|
|
|
if(ngatts > 0) {
|
|
|
|
codeline("");
|
|
|
|
codelined(1,"/* assign global attributes */");
|
|
|
|
for(iatt = 0; iatt < ngatts; iatt++) {
|
|
|
|
Symbol* gasym = (Symbol*)listget(gattdefs,iatt);
|
2015-11-12 06:36:02 +08:00
|
|
|
genc_defineattr(gasym);
|
2010-06-03 21:24:43 +08:00
|
|
|
}
|
|
|
|
codeline("");
|
|
|
|
}
|
|
|
|
codeflush();
|
2015-11-12 06:36:02 +08:00
|
|
|
|
2010-06-03 21:24:43 +08:00
|
|
|
/* Define the variable specific attributes*/
|
|
|
|
if(natts > 0) {
|
|
|
|
codeline("");
|
|
|
|
codelined(1,"/* assign per-variable attributes */");
|
|
|
|
for(iatt = 0; iatt < natts; iatt++) {
|
|
|
|
Symbol* asym = (Symbol*)listget(attdefs,iatt);
|
|
|
|
genc_defineattr(asym);
|
|
|
|
}
|
|
|
|
codeline("");
|
|
|
|
}
|
|
|
|
codeflush();
|
|
|
|
|
|
|
|
if (nofill_flag) {
|
|
|
|
codelined(1,"/* don't initialize variables with fill values */");
|
|
|
|
bbindent(stmt,1);
|
|
|
|
bbprintf0(stmt,"stat = nc_set_fill(%s, NC_NOFILL, 0);",groupncid(rootgroup));
|
|
|
|
codelined(1,"check_err(stat,__LINE__,__FILE__);");
|
|
|
|
}
|
|
|
|
|
|
|
|
codeline("");
|
|
|
|
codelined(1,"/* leave define mode */");
|
|
|
|
bbprintf0(stmt," stat = nc_enddef (%s);\n",groupncid(rootgroup));
|
|
|
|
codedump(stmt);
|
|
|
|
codelined(1,"check_err(stat,__LINE__,__FILE__);");
|
|
|
|
codeflush();
|
|
|
|
|
2012-03-08 07:38:51 +08:00
|
|
|
if(!header_only) {
|
|
|
|
/* Load values into those variables with defined data */
|
|
|
|
if(nvars > 0) {
|
|
|
|
codeline("");
|
|
|
|
codelined(1,"/* assign variable data */");
|
|
|
|
for(ivar = 0; ivar < nvars; ivar++) {
|
|
|
|
Symbol* vsym = (Symbol*)listget(vardefs,ivar);
|
|
|
|
if(vsym->data != NULL) genc_definevardata(vsym);
|
|
|
|
}
|
|
|
|
codeline("");
|
|
|
|
}
|
|
|
|
codeflush();
|
2010-06-03 21:24:43 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef USE_NETCDF4
|
2014-03-09 11:41:30 +08:00
|
|
|
|
|
|
|
static void
|
|
|
|
genc_defineglobalspecials(void)
|
|
|
|
{
|
|
|
|
const char* format = NULL;
|
|
|
|
if(usingclassic) return;
|
|
|
|
if(!/*Main.*/format_attribute) return;
|
|
|
|
/* Watch out, this is a global Attribute */
|
2016-05-04 11:17:06 +08:00
|
|
|
format = kind_string(globalspecials._Format);
|
2014-03-09 11:41:30 +08:00
|
|
|
bbprintf0(stmt,"%sstat = nc_put_att_text(ncid, NC_GLOBAL, \"_Format\", 1, \"%s\");\n",
|
|
|
|
indented(1),
|
|
|
|
format);
|
|
|
|
codedump(stmt);
|
|
|
|
codelined(1,"check_err(stat,__LINE__,__FILE__);");
|
|
|
|
}
|
|
|
|
|
2010-06-03 21:24:43 +08:00
|
|
|
static void
|
|
|
|
genc_definespecialattributes(Symbol* vsym)
|
|
|
|
{
|
2018-11-16 01:00:38 +08:00
|
|
|
Specialdata* special = vsym->var.special;
|
2010-06-03 21:24:43 +08:00
|
|
|
if(usingclassic) return;
|
|
|
|
if(special->flags & _STORAGE_FLAG) {
|
|
|
|
int storage = special->_Storage;
|
|
|
|
size_t* chunks = special->_ChunkSizes;
|
|
|
|
bbprintf0(stmt,
|
|
|
|
" stat = nc_def_var_chunking(%s, %s, %s, ",
|
|
|
|
groupncid(vsym->container),
|
|
|
|
varncid(vsym),
|
|
|
|
(storage == NC_CONTIGUOUS?"NC_CONTIGUOUS":"NC_CHUNKED"));
|
|
|
|
codedump(stmt);
|
|
|
|
if(special->nchunks == 0 || chunks == NULL)
|
|
|
|
codepartial("NULL");
|
|
|
|
else {
|
|
|
|
bbprintf0(stmt,"%s_chunksizes",cname(vsym));
|
2015-11-12 06:36:02 +08:00
|
|
|
codedump(stmt);
|
2010-06-03 21:24:43 +08:00
|
|
|
}
|
|
|
|
codeline(");");
|
|
|
|
codelined(1,"check_err(stat,__LINE__,__FILE__);");
|
2015-11-12 06:36:02 +08:00
|
|
|
}
|
2010-06-03 21:24:43 +08:00
|
|
|
if(special->flags & _FLETCHER32_FLAG) {
|
|
|
|
bbprintf0(stmt,
|
|
|
|
" stat = nc_def_var_fletcher32(%s, %s, %d);\n",
|
|
|
|
groupncid(vsym->container),
|
|
|
|
varncid(vsym),
|
|
|
|
special->_Fletcher32);
|
|
|
|
codedump(stmt);
|
|
|
|
codelined(1,"check_err(stat,__LINE__,__FILE__);");
|
|
|
|
}
|
|
|
|
if(special->flags & (_DEFLATE_FLAG | _SHUFFLE_FLAG)) {
|
|
|
|
bbprintf0(stmt,
|
|
|
|
" stat = nc_def_var_deflate(%s, %s, %s, %d, %d);\n",
|
|
|
|
groupncid(vsym->container),
|
|
|
|
varncid(vsym),
|
|
|
|
(special->_Shuffle == 1?"NC_SHUFFLE":"NC_NOSHUFFLE"),
|
|
|
|
(special->_DeflateLevel >= 0?1:0),
|
|
|
|
(special->_DeflateLevel >= 0?special->_DeflateLevel:0));
|
|
|
|
codedump(stmt);
|
|
|
|
codelined(1,"check_err(stat,__LINE__,__FILE__);");
|
2015-11-12 06:36:02 +08:00
|
|
|
}
|
2010-06-03 21:24:43 +08:00
|
|
|
if(special->flags & _ENDIAN_FLAG) {
|
|
|
|
bbprintf0(stmt,
|
|
|
|
" stat = nc_def_var_endian(%s, %s, %s);\n",
|
|
|
|
groupncid(vsym->container),
|
|
|
|
varncid(vsym),
|
|
|
|
(special->_Endianness == NC_ENDIAN_LITTLE?"NC_ENDIAN_LITTLE"
|
|
|
|
:"NC_ENDIAN_BIG")
|
|
|
|
);
|
|
|
|
codedump(stmt);
|
|
|
|
codelined(1,"check_err(stat,__LINE__,__FILE__);");
|
2015-11-12 06:36:02 +08:00
|
|
|
}
|
2010-06-03 21:24:43 +08:00
|
|
|
if(special->flags & _NOFILL_FLAG) {
|
|
|
|
bbprintf0(stmt,
|
|
|
|
" stat = nc_def_var_fill(%s, %s, %s, NULL);\n",
|
|
|
|
groupncid(vsym->container),
|
|
|
|
varncid(vsym),
|
|
|
|
(special->_Fill?"NC_FILL":"NC_NOFILL")
|
|
|
|
);
|
|
|
|
codedump(stmt);
|
|
|
|
codelined(1,"check_err(stat,__LINE__,__FILE__);");
|
2015-11-12 06:36:02 +08:00
|
|
|
}
|
2017-05-15 08:10:02 +08:00
|
|
|
if(special->flags & _FILTER_FLAG) {
|
2017-04-28 03:01:59 +08:00
|
|
|
/* Special check for alternate way to specify _Deflate */
|
|
|
|
if(special->_FilterID == ZIP_ID) {
|
|
|
|
unsigned int level;
|
|
|
|
if(special->nparams == 0 || special->_FilterParams == NULL)
|
|
|
|
level = 9; /* default */
|
|
|
|
else
|
|
|
|
level = special->_FilterParams[0];
|
2018-02-01 01:04:00 +08:00
|
|
|
if(level > 9)
|
2017-04-28 03:01:59 +08:00
|
|
|
derror("Illegal deflate level");
|
|
|
|
else {
|
|
|
|
bbprintf0(stmt,
|
|
|
|
" stat = nc_def_var_deflate(%s, %s, %s, %d, %d);\n",
|
|
|
|
groupncid(vsym->container),
|
|
|
|
varncid(vsym),
|
|
|
|
(special->_Shuffle == 1?"NC_SHUFFLE":"NC_NOSHUFFLE"),
|
2018-05-07 22:34:41 +08:00
|
|
|
1,
|
2017-04-28 03:01:59 +08:00
|
|
|
level);
|
|
|
|
codedump(stmt);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
bbprintf0(stmt,
|
|
|
|
" stat = nc_def_var_filter(%s, %s, %u, %lu, ",
|
|
|
|
groupncid(vsym->container),
|
|
|
|
varncid(vsym),
|
|
|
|
special->_FilterID,
|
|
|
|
special->nparams
|
|
|
|
);
|
|
|
|
codedump(stmt);
|
|
|
|
if(special->nparams == 0 || special->_FilterParams == NULL)
|
|
|
|
codepartial("NULL");
|
|
|
|
else {
|
|
|
|
bbprintf0(stmt,"%s_filterparams",cname(vsym));
|
|
|
|
codedump(stmt);
|
|
|
|
}
|
|
|
|
codeline(");");
|
|
|
|
}
|
|
|
|
codelined(1,"check_err(stat,__LINE__,__FILE__);");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-06-03 21:24:43 +08:00
|
|
|
}
|
|
|
|
#endif /*USE_NETCDF4*/
|
|
|
|
|
|
|
|
void
|
2018-11-16 01:00:38 +08:00
|
|
|
genc_close(void)
|
2010-06-03 21:24:43 +08:00
|
|
|
{
|
|
|
|
bbprintf0(stmt,"%sstat = nc_close(%s);\n",indented(1),groupncid(rootgroup));
|
|
|
|
codedump(stmt);
|
|
|
|
codelined(1,"check_err(stat,__LINE__,__FILE__);");
|
|
|
|
#ifndef vms
|
|
|
|
codelined(1,"return 0;");
|
|
|
|
#else
|
|
|
|
codelined(1,"return 1;");
|
|
|
|
#endif
|
|
|
|
codeline("}");
|
|
|
|
codeflush();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Output a C statement
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* return C name for netCDF type, given type code */
|
|
|
|
const char *
|
|
|
|
nctype(nc_type type)
|
|
|
|
{
|
|
|
|
switch (type) {
|
|
|
|
case NC_CHAR: return "NC_CHAR";
|
|
|
|
case NC_BYTE: return "NC_BYTE";
|
|
|
|
case NC_SHORT: return "NC_SHORT";
|
|
|
|
case NC_INT: return "NC_INT";
|
|
|
|
case NC_FLOAT: return "NC_FLOAT";
|
|
|
|
case NC_DOUBLE: return "NC_DOUBLE";
|
|
|
|
case NC_UBYTE: return "NC_UBYTE";
|
|
|
|
case NC_USHORT: return "NC_USHORT";
|
|
|
|
case NC_UINT: return "NC_UINT";
|
|
|
|
case NC_INT64: return "NC_INT64";
|
|
|
|
case NC_UINT64: return "NC_UINT64";
|
|
|
|
case NC_STRING: return "NC_STRING";
|
|
|
|
default: PANIC("nctype: bad type code");
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Return C type name for netCDF type, given type code.
|
|
|
|
*/
|
2015-11-12 06:36:02 +08:00
|
|
|
const char*
|
2010-06-03 21:24:43 +08:00
|
|
|
ncctype(nc_type type)
|
|
|
|
{
|
|
|
|
switch (type) {
|
|
|
|
case NC_CHAR:
|
|
|
|
return "char";
|
|
|
|
case NC_BYTE:
|
|
|
|
return "signed char";
|
|
|
|
case NC_SHORT:
|
|
|
|
return "short";
|
|
|
|
case NC_INT:
|
|
|
|
return "int";
|
|
|
|
case NC_FLOAT:
|
|
|
|
return "float";
|
|
|
|
case NC_DOUBLE:
|
|
|
|
return "double";
|
|
|
|
case NC_UBYTE:
|
|
|
|
return "unsigned char";
|
|
|
|
case NC_USHORT:
|
|
|
|
return "unsigned short";
|
|
|
|
case NC_UINT:
|
|
|
|
return "unsigned int";
|
|
|
|
case NC_INT64:
|
|
|
|
return "signed long long";
|
|
|
|
case NC_UINT64:
|
|
|
|
return "unsigned long long";
|
|
|
|
case NC_STRING:
|
|
|
|
return "char*";
|
|
|
|
default:
|
|
|
|
PANIC1("ncctype: bad type code:%d",type);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Return C type name for netCDF type suffix, given type code.
|
|
|
|
*/
|
2015-11-12 06:36:02 +08:00
|
|
|
const char*
|
2010-06-03 21:24:43 +08:00
|
|
|
ncstype(nc_type nctype)
|
|
|
|
{
|
|
|
|
switch (nctype) {
|
|
|
|
case NC_CHAR:
|
|
|
|
return "text";
|
|
|
|
case NC_BYTE:
|
|
|
|
return "schar";
|
|
|
|
case NC_SHORT:
|
|
|
|
return "short";
|
|
|
|
case NC_INT:
|
|
|
|
return "int";
|
|
|
|
case NC_FLOAT:
|
|
|
|
return "float";
|
|
|
|
case NC_DOUBLE:
|
|
|
|
return "double";
|
|
|
|
case NC_UBYTE:
|
|
|
|
return "ubyte";
|
|
|
|
case NC_USHORT:
|
|
|
|
return "ushort";
|
|
|
|
case NC_UINT:
|
|
|
|
return "uint";
|
|
|
|
case NC_INT64:
|
|
|
|
return "longlong";
|
|
|
|
case NC_UINT64:
|
|
|
|
return "ulonglong";
|
|
|
|
case NC_STRING:
|
|
|
|
return "string";
|
|
|
|
default:
|
|
|
|
derror("ncstype: bad type code: %d",nctype);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Return the group name for the specified group*/
|
|
|
|
static const char*
|
|
|
|
groupncid(Symbol* sym)
|
|
|
|
{
|
|
|
|
#ifdef USE_NETCDF4
|
|
|
|
if(usingclassic) {
|
|
|
|
return "ncid";
|
|
|
|
} else {
|
|
|
|
char* grptmp;
|
|
|
|
const char* tmp1;
|
|
|
|
if(sym == NULL) return groupncid(rootgroup);
|
|
|
|
ASSERT(sym->objectclass == NC_GRP);
|
|
|
|
tmp1 = cname(sym);
|
|
|
|
grptmp = poolalloc(strlen(tmp1)+strlen("_grp")+1);
|
|
|
|
strcpy(grptmp,tmp1);
|
|
|
|
strcat(grptmp,"_grp");
|
|
|
|
return grptmp;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
return "ncid";
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Compute the C name for a given type's id*/
|
|
|
|
/* Watch out: the result is a static*/
|
|
|
|
static const char*
|
|
|
|
typencid(Symbol* tsym)
|
|
|
|
{
|
|
|
|
char* typtmp;
|
|
|
|
const char* tmp1;
|
|
|
|
if(tsym->subclass == NC_PRIM)
|
|
|
|
return nctype(tsym->typ.typecode);
|
|
|
|
tmp1 = ctypename(tsym);
|
|
|
|
typtmp = poolalloc(strlen(tmp1)+strlen("_typ")+1);
|
|
|
|
strcpy(typtmp,tmp1);
|
|
|
|
strcat(typtmp,"_typ");
|
|
|
|
return typtmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Compute the C name for a given var's id*/
|
|
|
|
/* Watch out: the result is a static*/
|
|
|
|
static const char*
|
|
|
|
varncid(Symbol* vsym)
|
|
|
|
{
|
|
|
|
const char* tmp1;
|
|
|
|
char* vartmp;
|
|
|
|
tmp1 = cname(vsym);
|
|
|
|
vartmp = poolalloc(strlen(tmp1)+strlen("_id")+1);
|
|
|
|
strcpy(vartmp,tmp1);
|
|
|
|
strcat(vartmp,"_id");
|
|
|
|
return vartmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Compute the C name for a given dim's id*/
|
2013-09-21 10:31:21 +08:00
|
|
|
/* Watch out: the result is pooled*/
|
2010-06-03 21:24:43 +08:00
|
|
|
static const char*
|
|
|
|
dimncid(Symbol* dsym)
|
|
|
|
{
|
|
|
|
const char* tmp1;
|
|
|
|
char* dimtmp;
|
|
|
|
tmp1 = cname(dsym);
|
2013-09-22 06:19:06 +08:00
|
|
|
dimtmp = poolalloc(strlen(tmp1)+strlen("_dim")+1);
|
2010-06-03 21:24:43 +08:00
|
|
|
strcpy(dimtmp,tmp1);
|
2013-09-22 06:19:06 +08:00
|
|
|
strcat(dimtmp,"_dim");
|
2010-06-03 21:24:43 +08:00
|
|
|
return dimtmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Compute the C name for a given type*/
|
|
|
|
const char*
|
|
|
|
ctypename(Symbol* tsym)
|
|
|
|
{
|
|
|
|
const char* name;
|
|
|
|
ASSERT(tsym->objectclass == NC_TYPE);
|
|
|
|
if(tsym->subclass == NC_PRIM)
|
|
|
|
name = ncctype(tsym->typ.typecode);
|
|
|
|
else
|
|
|
|
name = cname(tsym);
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef USE_NETCDF4
|
|
|
|
static void
|
|
|
|
definectype(Symbol* tsym)
|
|
|
|
{
|
|
|
|
int i,j;
|
|
|
|
|
|
|
|
ASSERT(tsym->objectclass == NC_TYPE);
|
|
|
|
switch (tsym->subclass) {
|
|
|
|
case NC_PRIM: break; /* these are already taken care of*/
|
|
|
|
case NC_OPAQUE:
|
|
|
|
bbprintf0(stmt,"typedef unsigned char %s[%lu];\n",
|
|
|
|
cname(tsym), tsym->typ.size);
|
|
|
|
codedump(stmt);
|
|
|
|
break;
|
|
|
|
case NC_ENUM:
|
|
|
|
for(i=0;i<listlength(tsym->subnodes);i++) {
|
|
|
|
Symbol* econst = (Symbol*)listget(tsym->subnodes,i);
|
2012-02-14 08:25:32 +08:00
|
|
|
Bytebuffer* econststring = bbNew();
|
2010-06-03 21:24:43 +08:00
|
|
|
ASSERT(econst->subclass == NC_ECONST);
|
2018-11-16 01:00:38 +08:00
|
|
|
c_generator->constant(c_generator,tsym,econst->typ.econst,econststring);
|
2012-02-14 08:25:32 +08:00
|
|
|
bbNull(econststring);
|
2013-09-21 10:31:21 +08:00
|
|
|
/* Enum constants must be converted to a fully qualified name */
|
2010-06-03 21:24:43 +08:00
|
|
|
bbprintf0(stmt,"#define %s ((%s)%s)\n",
|
|
|
|
cname(econst),
|
|
|
|
ctypename(econst->typ.basetype),
|
2012-02-14 08:25:32 +08:00
|
|
|
bbContents(econststring));
|
|
|
|
bbFree(econststring);
|
2010-06-03 21:24:43 +08:00
|
|
|
codedump(stmt);
|
|
|
|
}
|
|
|
|
bbprintf0(stmt,"typedef %s %s;\n",
|
|
|
|
ctypename(tsym->typ.basetype), cname(tsym));
|
|
|
|
codedump(stmt);
|
|
|
|
break;
|
|
|
|
case NC_VLEN:
|
|
|
|
bbprintf0(stmt,"typedef nc_vlen_t %s;\n",
|
|
|
|
ctypename(tsym));
|
|
|
|
codedump(stmt);
|
|
|
|
break;
|
|
|
|
case NC_COMPOUND:
|
|
|
|
bbprintf0(stmt,"typedef struct %s {\n",cname(tsym));
|
|
|
|
codedump(stmt);
|
|
|
|
for(i=0;i<listlength(tsym->subnodes);i++) {
|
|
|
|
Symbol* efield = (Symbol*)listget(tsym->subnodes,i);
|
|
|
|
ASSERT(efield->subclass == NC_FIELD);
|
|
|
|
bbprintf0(stmt,"%s%s %s",
|
|
|
|
indented(1),ctypename(efield->typ.basetype),cname(efield));
|
|
|
|
codedump(stmt);
|
|
|
|
/* compute any dimension specification*/
|
|
|
|
if(efield->typ.dimset.ndims > 0) {
|
|
|
|
Bytebuffer* dimbuf = bbNew();
|
|
|
|
for(j=0;j<efield->typ.dimset.ndims;j++) {
|
|
|
|
Symbol* dim;
|
|
|
|
char tmp[64];
|
|
|
|
bbCat(dimbuf,"[");
|
|
|
|
dim = efield->typ.dimset.dimsyms[j];
|
|
|
|
ASSERT(dim->dim.isconstant);
|
|
|
|
snprintf(tmp,sizeof(tmp),"%u",
|
|
|
|
(unsigned int)dim->dim.declsize);
|
|
|
|
bbCat(dimbuf,tmp);
|
|
|
|
bbCat(dimbuf,"]");
|
|
|
|
}
|
|
|
|
codedump(dimbuf);
|
|
|
|
bbFree(dimbuf);
|
|
|
|
}
|
|
|
|
codeline(";");
|
|
|
|
}
|
|
|
|
bbprintf0(stmt,"} %s;\n", ctypename(tsym));
|
|
|
|
codedump(stmt);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case NC_ARRAY:
|
|
|
|
/* ignore: this will be handled by def_var*/
|
|
|
|
break;
|
|
|
|
|
|
|
|
default: panic("definectype: unexpected type subclass: %d",tsym->subclass);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Generate the C code for defining a given type
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
genc_deftype(Symbol* tsym)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
ASSERT(tsym->objectclass == NC_TYPE);
|
|
|
|
switch (tsym->subclass) {
|
|
|
|
case NC_PRIM: break; /* these are already taken care of*/
|
|
|
|
case NC_OPAQUE:
|
|
|
|
bbprintf0(stmt,"%sstat = nc_def_opaque(%s, %lu, \"%s\", &%s);\n",
|
|
|
|
indented(1),
|
|
|
|
groupncid(tsym->container),
|
|
|
|
tsym->typ.size,
|
|
|
|
tsym->name,
|
|
|
|
typencid(tsym));
|
|
|
|
codedump(stmt);
|
|
|
|
codelined(1,"check_err(stat,__LINE__,__FILE__);");
|
|
|
|
break;
|
|
|
|
case NC_ENUM:
|
|
|
|
codelined(1,"{");
|
|
|
|
bbprintf0(stmt,"%s%s econst;\n",
|
|
|
|
indented(1),
|
|
|
|
ncctype(tsym->typ.basetype->typ.typecode));
|
|
|
|
codedump(stmt);
|
|
|
|
bbprintf0(stmt,"%sstat = nc_def_enum(%s, %s, \"%s\", &%s);\n",
|
|
|
|
indented(1),
|
|
|
|
groupncid(tsym->container),
|
|
|
|
nctype(tsym->typ.basetype->typ.typecode),
|
|
|
|
tsym->name,
|
|
|
|
typencid(tsym));
|
|
|
|
codedump(stmt);
|
|
|
|
codelined(1,"check_err(stat,__LINE__,__FILE__);");
|
|
|
|
for(i=0;i<listlength(tsym->subnodes);i++) {
|
|
|
|
Symbol* econst = (Symbol*)listget(tsym->subnodes,i);
|
2012-02-14 08:25:32 +08:00
|
|
|
Bytebuffer* econststring = bbNew();
|
2010-06-03 21:24:43 +08:00
|
|
|
ASSERT(econst->subclass == NC_ECONST);
|
2018-11-16 01:00:38 +08:00
|
|
|
c_generator->constant(c_generator,tsym,econst->typ.econst,econststring);
|
2012-02-14 08:25:32 +08:00
|
|
|
bbNull(econststring);
|
2010-06-03 21:24:43 +08:00
|
|
|
bbprintf0(stmt,"%seconst = %s;\n",
|
2012-02-14 08:25:32 +08:00
|
|
|
indented(1),bbContents(econststring));
|
|
|
|
bbFree(econststring);
|
2010-06-03 21:24:43 +08:00
|
|
|
codedump(stmt);
|
|
|
|
bbprintf0(stmt,"%sstat = nc_insert_enum(%s, %s, \"%s\", &econst);\n",
|
|
|
|
indented(1),
|
|
|
|
groupncid(tsym->container),
|
|
|
|
typencid(tsym),
|
|
|
|
escapifyname(econst->name));
|
|
|
|
codedump(stmt);
|
|
|
|
}
|
|
|
|
codelined(1,"}");
|
|
|
|
break;
|
|
|
|
case NC_VLEN:
|
|
|
|
bbprintf0(stmt,"%sstat = nc_def_vlen(%s, \"%s\", %s, &%s);",
|
|
|
|
indented(1),
|
|
|
|
groupncid(tsym->container),
|
|
|
|
escapifyname(tsym->name),
|
|
|
|
typencid(tsym->typ.basetype),
|
|
|
|
typencid(tsym));
|
|
|
|
codedump(stmt);
|
|
|
|
codelined(1,"check_err(stat,__LINE__,__FILE__);");
|
|
|
|
break;
|
|
|
|
case NC_COMPOUND:
|
|
|
|
bbprintf0(stmt,"%sstat = nc_def_compound(%s, sizeof(%s), \"%s\", &%s);",
|
|
|
|
indented(1),
|
|
|
|
groupncid(tsym->container),
|
|
|
|
ctypename(tsym),
|
|
|
|
escapifyname(tsym->name),
|
|
|
|
typencid(tsym));
|
|
|
|
codedump(stmt);
|
|
|
|
codelined(1,"check_err(stat,__LINE__,__FILE__);");
|
|
|
|
/* Generate the field dimension constants*/
|
|
|
|
codelined(1,"{");
|
|
|
|
for(i=0;i<listlength(tsym->subnodes);i++) {
|
|
|
|
int j;
|
|
|
|
Symbol* efield = (Symbol*)listget(tsym->subnodes,i);
|
|
|
|
ASSERT(efield->subclass == NC_FIELD);
|
2015-11-12 06:36:02 +08:00
|
|
|
if(efield->typ.dimset.ndims == 0) continue;
|
2010-06-03 21:24:43 +08:00
|
|
|
bbprintf0(stmt,"%sstatic int %s_dims[%d] = {\n",
|
|
|
|
indented(1),
|
|
|
|
cname(efield),efield->typ.dimset.ndims);
|
|
|
|
for(j=0;j<efield->typ.dimset.ndims;j++) {
|
|
|
|
char tmp[256];
|
|
|
|
Symbol* e = efield->typ.dimset.dimsyms[j];
|
|
|
|
ASSERT(e->dim.isconstant);
|
|
|
|
snprintf(tmp,sizeof(tmp),"%u",(unsigned int)e->dim.declsize);
|
|
|
|
bbCat(stmt,(j==0?"":", "));
|
|
|
|
bbCat(stmt,tmp);
|
|
|
|
}
|
|
|
|
bbCat(stmt,"};");
|
|
|
|
codedump(stmt);
|
|
|
|
}
|
|
|
|
for(i=0;i<listlength(tsym->subnodes);i++) {
|
|
|
|
Symbol* efield = (Symbol*)listget(tsym->subnodes,i);
|
|
|
|
char tmp[1024];
|
|
|
|
ASSERT(efield->subclass == NC_FIELD);
|
|
|
|
#ifdef TESTALIGNMENT
|
|
|
|
snprintf(tmp,sizeof(tmp),"%lu",efield->typ.offset);
|
|
|
|
#else
|
|
|
|
snprintf(tmp,sizeof(tmp),"NC_COMPOUND_OFFSET(%s,%s)",
|
|
|
|
ctypename(tsym), cname(efield));
|
|
|
|
#endif
|
2015-11-12 06:36:02 +08:00
|
|
|
if(efield->typ.dimset.ndims > 0){
|
2010-06-03 21:24:43 +08:00
|
|
|
bbprintf0(stmt,"%sstat = nc_insert_array_compound(%s, %s, \"%s\", %s, %s, %d, %s_dims);",
|
|
|
|
indented(1),
|
|
|
|
groupncid(tsym->container),
|
|
|
|
typencid(tsym),
|
|
|
|
escapifyname(efield->name),
|
|
|
|
tmp,
|
|
|
|
typencid(efield->typ.basetype),
|
|
|
|
efield->typ.dimset.ndims,
|
|
|
|
cname(efield));
|
|
|
|
} else {
|
|
|
|
bbprintf0(stmt,"%sstat = nc_insert_compound(%s, %s, \"%s\", %s, %s);",
|
|
|
|
indented(1),
|
|
|
|
groupncid(tsym->container),
|
|
|
|
typencid(tsym),
|
|
|
|
escapifyname(efield->name),
|
|
|
|
tmp,
|
|
|
|
typencid(efield->typ.basetype));
|
|
|
|
}
|
|
|
|
codedump(stmt);
|
|
|
|
codelined(1,"check_err(stat,__LINE__,__FILE__);");
|
|
|
|
}
|
|
|
|
codelined(1,"}");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case NC_ARRAY:
|
|
|
|
/* ignore: this will be handled by def_var*/
|
|
|
|
break;
|
|
|
|
|
|
|
|
default: panic("genc_deftype: unexpected type subclass: %d",tsym->subclass);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /*USE_NETCDF4*/
|
|
|
|
|
|
|
|
static void
|
|
|
|
genc_defineattr(Symbol* asym)
|
|
|
|
{
|
2012-02-14 08:25:32 +08:00
|
|
|
/* we need to capture vlen strings for dumping */
|
|
|
|
Bytebuffer* save = bbNew(); /* capture so we can dump
|
|
|
|
vlens first */
|
|
|
|
List* oldstate = NULL;
|
|
|
|
generator_getstate(c_generator,(void*)&oldstate);
|
|
|
|
listfree(oldstate);
|
|
|
|
generator_reset(c_generator,(void*)listnew());
|
|
|
|
generate_attrdata(asym,c_generator,(Writer)genc_write,save);
|
|
|
|
bbFree(save);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
genc_definevardata(Symbol* vsym)
|
|
|
|
{
|
|
|
|
Bytebuffer* save; /* capture so we can dump vlens first */
|
|
|
|
List* oldstate = NULL;
|
|
|
|
if(vsym->data == NULL) return;
|
|
|
|
save = bbNew();
|
|
|
|
generator_getstate(c_generator,(void*)&oldstate);
|
|
|
|
listfree(oldstate);
|
|
|
|
generator_reset(c_generator,(void*)listnew());
|
|
|
|
generate_vardata(vsym,c_generator,(Writer)genc_write,save);
|
|
|
|
bbFree(save);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
genc_write(Generator* generator, Symbol* sym, Bytebuffer* code,
|
|
|
|
int rank, size_t* start, size_t* count)
|
|
|
|
{
|
|
|
|
if(sym->objectclass == NC_ATT)
|
|
|
|
genc_writeattr(generator,sym,code,rank,start,count);
|
|
|
|
else if(sym->objectclass == NC_VAR)
|
|
|
|
genc_writevar(generator,sym,code,rank,start,count);
|
|
|
|
else
|
|
|
|
PANIC("illegal symbol for genc_write");
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
genc_writevar(Generator* generator, Symbol* vsym, Bytebuffer* code,
|
|
|
|
int rank, size_t* start, size_t* count)
|
|
|
|
{
|
|
|
|
Symbol* basetype = vsym->typ.basetype;
|
2010-06-03 21:24:43 +08:00
|
|
|
nc_type typecode = basetype->typ.typecode;
|
2012-02-14 08:25:32 +08:00
|
|
|
List* vlendecls;
|
|
|
|
|
|
|
|
/* define a block to avoid name clashes*/
|
|
|
|
codeline("");
|
|
|
|
codelined(1,"{");
|
2010-06-03 21:24:43 +08:00
|
|
|
|
2012-02-14 08:25:32 +08:00
|
|
|
/* Dump any vlen decls first */
|
|
|
|
generator_getstate(generator,(void**)&vlendecls);
|
|
|
|
if(vlendecls != NULL && listlength(vlendecls) > 0) {
|
|
|
|
int i;
|
|
|
|
for(i=0;i<listlength(vlendecls);i++) {
|
|
|
|
Bytebuffer* decl = (Bytebuffer*)listget(vlendecls,i);
|
|
|
|
codelined(1,bbContents(decl));
|
|
|
|
bbFree(decl);
|
|
|
|
}
|
|
|
|
listfree(vlendecls);
|
|
|
|
generator_reset(generator,NULL);
|
2015-11-12 06:36:02 +08:00
|
|
|
}
|
2010-06-03 21:24:43 +08:00
|
|
|
|
2012-02-14 08:25:32 +08:00
|
|
|
if(rank == 0) {
|
2013-07-11 04:00:48 +08:00
|
|
|
codelined(1,"size_t count = 0;");
|
2012-02-14 08:25:32 +08:00
|
|
|
/* We make the data be an array so we do not need to
|
|
|
|
ampersand it later => we need an outer pair of braces
|
|
|
|
*/
|
2013-07-11 04:00:48 +08:00
|
|
|
commify(code); /* insert commas at proper places */
|
|
|
|
bbprintf0(stmt,"%sstatic %s %s_data[1] = {%s};\n",
|
2012-02-14 08:25:32 +08:00
|
|
|
indented(1),
|
|
|
|
ctypename(basetype),
|
|
|
|
cname(vsym),
|
|
|
|
bbContents(code));
|
|
|
|
codedump(stmt);
|
2013-07-11 04:00:48 +08:00
|
|
|
bbprintf0(stmt,"%sstat = nc_put_var1(%s, %s, &count, %s_data);\n",
|
2012-02-14 08:25:32 +08:00
|
|
|
indented(1),
|
|
|
|
groupncid(vsym->container),
|
|
|
|
varncid(vsym),
|
|
|
|
cname(vsym));
|
|
|
|
codedump(stmt);
|
|
|
|
codelined(1,"check_err(stat,__LINE__,__FILE__);");
|
|
|
|
codeflush();
|
|
|
|
} else { /* rank > 0 */
|
|
|
|
int i;
|
|
|
|
size_t length = 0;
|
|
|
|
if(typecode == NC_CHAR) {
|
|
|
|
length = bbLength(code);
|
|
|
|
/* generate data constant */
|
|
|
|
bbprintf(stmt,"%schar* %s_data = ",
|
|
|
|
indented(1),
|
|
|
|
cname(vsym),
|
|
|
|
(unsigned long)length);
|
|
|
|
codedump(stmt);
|
|
|
|
cquotestring(code,'"');
|
|
|
|
codedump(code);
|
|
|
|
codeline(" ;");
|
|
|
|
} else {
|
2015-11-12 06:36:02 +08:00
|
|
|
/* Compute total size */
|
2012-02-14 08:25:32 +08:00
|
|
|
length = 1;
|
|
|
|
for(i=0;i<rank;i++) length *= count[i];
|
|
|
|
/* generate data constant */
|
|
|
|
commify(code); /* insert commas at proper places */
|
|
|
|
bbprintf(stmt,"%s%s %s_data[%lu] = ",
|
|
|
|
indented(1),
|
|
|
|
ctypename(basetype),
|
|
|
|
cname(vsym),
|
|
|
|
(unsigned long)length);
|
|
|
|
codedump(stmt);
|
|
|
|
/* C requires an outer set of braces on datalist constants */
|
|
|
|
codepartial("{");
|
|
|
|
codedump(code);
|
|
|
|
codeline("} ;");
|
|
|
|
}
|
2015-11-12 06:36:02 +08:00
|
|
|
|
2012-02-14 08:25:32 +08:00
|
|
|
/* generate constants for startset, countset*/
|
2015-11-12 06:36:02 +08:00
|
|
|
bbprintf0(stmt,"%ssize_t %s_startset[%u] = {",
|
2012-02-14 08:25:32 +08:00
|
|
|
indented(1),
|
|
|
|
cname(vsym),
|
|
|
|
rank);
|
|
|
|
for(i=0;i<rank;i++) {
|
|
|
|
bbprintf(stmt,"%s%lu",(i>0?", ":""),start[i]);
|
|
|
|
}
|
|
|
|
codedump(stmt);
|
|
|
|
codeline("} ;");
|
2015-11-12 06:36:02 +08:00
|
|
|
|
|
|
|
bbprintf0(stmt,"%ssize_t %s_countset[%u] = {",
|
2012-02-14 08:25:32 +08:00
|
|
|
indented(1),
|
|
|
|
cname(vsym),
|
|
|
|
rank);
|
|
|
|
for(i=0;i<rank;i++) {
|
|
|
|
bbprintf(stmt,"%s%lu",(i>0?", ":""),count[i]);
|
|
|
|
}
|
|
|
|
codedump(stmt);
|
|
|
|
codeline("};");
|
2015-11-12 06:36:02 +08:00
|
|
|
|
2012-02-14 08:25:32 +08:00
|
|
|
bbprintf0(stmt,"%sstat = nc_put_vara(%s, %s, %s_startset, %s_countset, %s_data);\n",
|
|
|
|
indented(1),
|
|
|
|
groupncid(vsym->container), varncid(vsym),
|
|
|
|
cname(vsym),
|
|
|
|
cname(vsym),
|
|
|
|
cname(vsym));
|
|
|
|
codedump(stmt);
|
|
|
|
codelined(1,"check_err(stat,__LINE__,__FILE__);");
|
2015-11-12 06:36:02 +08:00
|
|
|
|
2012-02-14 08:25:32 +08:00
|
|
|
}
|
|
|
|
/* end defined block*/
|
|
|
|
codelined(1,"}\n");
|
|
|
|
codeflush();
|
|
|
|
}
|
2015-11-12 06:36:02 +08:00
|
|
|
|
2012-02-14 08:25:32 +08:00
|
|
|
static void
|
|
|
|
genc_writeattr(Generator* generator, Symbol* asym, Bytebuffer* code,
|
|
|
|
int rank, size_t* start, size_t* count)
|
|
|
|
{
|
|
|
|
Symbol* basetype = asym->typ.basetype;
|
|
|
|
int typecode = basetype->typ.typecode;
|
|
|
|
size_t len = asym->data->length; /* default assumption */
|
2010-06-03 21:24:43 +08:00
|
|
|
|
2012-02-14 08:25:32 +08:00
|
|
|
/* define a block to avoid name clashes*/
|
|
|
|
codeline("");
|
|
|
|
codelined(1,"{");
|
2010-06-03 21:24:43 +08:00
|
|
|
|
|
|
|
/* Handle NC_CHAR specially */
|
|
|
|
if(typecode == NC_CHAR) {
|
2012-02-14 08:25:32 +08:00
|
|
|
len = bbLength(code); /* presumably before quoting */
|
2011-11-14 12:20:19 +08:00
|
|
|
/* Revise length if length == 0 */
|
|
|
|
if(len == 0) len++;
|
2012-02-14 08:25:32 +08:00
|
|
|
cquotestring(code,'"');
|
2010-06-03 21:24:43 +08:00
|
|
|
} else {
|
|
|
|
/* All other cases */
|
2013-07-11 04:00:48 +08:00
|
|
|
/* Dump any vlen decls first */
|
|
|
|
List* vlendecls;
|
|
|
|
generator_getstate(generator,(void**)&vlendecls);
|
|
|
|
if(vlendecls != NULL && listlength(vlendecls) > 0) {
|
|
|
|
int i;
|
|
|
|
for(i=0;i<listlength(vlendecls);i++) {
|
|
|
|
Bytebuffer* decl = (Bytebuffer*)listget(vlendecls,i);
|
|
|
|
codelined(1,bbContents(decl));
|
|
|
|
bbFree(decl);
|
|
|
|
}
|
|
|
|
listfree(vlendecls);
|
|
|
|
generator_reset(generator,NULL);
|
2015-11-12 06:36:02 +08:00
|
|
|
}
|
2010-06-03 21:24:43 +08:00
|
|
|
commify(code);
|
2012-02-14 08:25:32 +08:00
|
|
|
bbprintf0(stmt,"%sstatic const %s %s_att[%ld] = ",
|
2013-07-11 04:00:48 +08:00
|
|
|
indented(1),
|
|
|
|
ctypename(basetype),
|
|
|
|
cname(asym),
|
|
|
|
asym->data->length
|
|
|
|
);
|
2010-06-03 21:24:43 +08:00
|
|
|
codedump(stmt);
|
|
|
|
codepartial("{");
|
|
|
|
codedump(code);
|
|
|
|
codepartial("}");
|
|
|
|
codeline(" ;");
|
|
|
|
bbClear(code);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Use the specialized put_att_XX routines if possible*/
|
|
|
|
switch (basetype->typ.typecode) {
|
|
|
|
case NC_BYTE:
|
|
|
|
case NC_SHORT:
|
|
|
|
case NC_INT:
|
|
|
|
case NC_FLOAT:
|
|
|
|
case NC_DOUBLE:
|
|
|
|
bbprintf0(stmt,"%sstat = nc_put_att_%s(%s, %s, \"%s\", %s, %lu, %s_att);\n",
|
|
|
|
indented(1),
|
|
|
|
ncstype(basetype->typ.typecode),
|
|
|
|
groupncid(asym->container),
|
|
|
|
(asym->att.var == NULL?"NC_GLOBAL"
|
|
|
|
:varncid(asym->att.var)),
|
|
|
|
escapifyname(asym->name),
|
2015-11-12 06:36:02 +08:00
|
|
|
typencid(basetype),
|
2010-06-03 21:24:43 +08:00
|
|
|
len,
|
|
|
|
cname(asym));
|
|
|
|
codedump(stmt);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case NC_CHAR:
|
|
|
|
/* Include the string constant in-line */
|
|
|
|
bbprintf0(stmt,"%sstat = nc_put_att_%s(%s, %s, \"%s\", %lu, %s);\n",
|
|
|
|
indented(1),
|
|
|
|
ncstype(basetype->typ.typecode),
|
|
|
|
groupncid(asym->container),
|
|
|
|
(asym->att.var == NULL?"NC_GLOBAL"
|
|
|
|
:varncid(asym->att.var)),
|
|
|
|
escapifyname(asym->name),
|
|
|
|
len,
|
|
|
|
bbContents(code));
|
|
|
|
codedump(stmt);
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* !usingclassic only (except NC_STRING) */
|
|
|
|
case NC_UBYTE:
|
|
|
|
case NC_USHORT:
|
|
|
|
case NC_UINT:
|
|
|
|
case NC_INT64:
|
|
|
|
case NC_UINT64:
|
2015-08-16 06:26:35 +08:00
|
|
|
if(usingclassic && k_flag <= 2) {
|
2010-06-03 21:24:43 +08:00
|
|
|
verror("Non-classic type: %s",nctypename(basetype->typ.typecode));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
bbprintf0(stmt,"%sstat = nc_put_att_%s(%s, %s, \"%s\", %s, %lu, %s_att);",
|
|
|
|
indented(1),
|
|
|
|
ncstype(basetype->typ.typecode),
|
|
|
|
groupncid(asym->container),
|
|
|
|
(asym->att.var == NULL?"NC_GLOBAL"
|
|
|
|
:varncid(asym->att.var)),
|
|
|
|
escapifyname(asym->name),
|
2015-11-12 06:36:02 +08:00
|
|
|
typencid(basetype),
|
2010-06-03 21:24:43 +08:00
|
|
|
len,
|
|
|
|
cname(asym));
|
|
|
|
codedump(stmt);
|
|
|
|
break;
|
|
|
|
|
2015-08-16 06:26:35 +08:00
|
|
|
#ifdef USE_NETCDF4
|
2010-06-03 21:24:43 +08:00
|
|
|
case NC_STRING:
|
|
|
|
if(usingclassic) {
|
|
|
|
verror("Non-classic type: %s",nctypename(basetype->typ.typecode));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
bbprintf0(stmt,"%sstat = nc_put_att_%s(%s, %s, \"%s\", %lu, %s_att);",
|
|
|
|
indented(1),
|
|
|
|
ncstype(basetype->typ.typecode),
|
|
|
|
groupncid(asym->container),
|
|
|
|
(asym->att.var == NULL?"NC_GLOBAL"
|
|
|
|
:varncid(asym->att.var)),
|
|
|
|
escapifyname(asym->name),
|
|
|
|
len,
|
|
|
|
cname(asym));
|
|
|
|
codedump(stmt);
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
default: /* User defined type */
|
|
|
|
#ifndef USE_NETCDF4
|
|
|
|
verror("Non-classic type: %s",nctypename(basetype->typ.typecode));
|
2015-08-16 06:26:35 +08:00
|
|
|
#else /* !USE_NETCDF4 */
|
2010-06-03 21:24:43 +08:00
|
|
|
if(usingclassic && !isclassicprim(basetype->typ.typecode)) {
|
|
|
|
verror("Non-classic type: %s",nctypename(basetype->typ.typecode));
|
|
|
|
}
|
2012-02-14 08:25:32 +08:00
|
|
|
bbprintf0(stmt,"%sstat = nc_put_att(%s, %s, \"%s\", %s, %lu, %s_att);\n",
|
2010-06-03 21:24:43 +08:00
|
|
|
indented(1),
|
|
|
|
groupncid(asym->container),
|
|
|
|
(asym->att.var == NULL?"NC_GLOBAL"
|
|
|
|
:varncid(asym->att.var)),
|
|
|
|
escapifyname(asym->name),
|
|
|
|
typencid(basetype),
|
|
|
|
len,
|
|
|
|
cname(asym));
|
|
|
|
codedump(stmt);
|
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
codelined(1,"check_err(stat,__LINE__,__FILE__);");
|
|
|
|
codelined(1,"}");
|
|
|
|
}
|
2013-09-21 10:31:21 +08:00
|
|
|
|
|
|
|
|
|
|
|
/* Compute the C name for a given symbol;
|
|
|
|
modified to use the fqn
|
|
|
|
*/
|
|
|
|
const char*
|
|
|
|
cname(Symbol* sym)
|
|
|
|
{
|
|
|
|
char* name;
|
|
|
|
|
|
|
|
assert (sym->fqn != NULL && sym->name != NULL);
|
|
|
|
/* Convert the fqn as its C name. */
|
|
|
|
if(sym->grp.is_root)
|
|
|
|
name = codify(sym->name);
|
|
|
|
else
|
2015-11-12 06:36:02 +08:00
|
|
|
name = codify(sym->fqn);
|
2013-09-21 10:31:21 +08:00
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
2010-06-03 21:24:43 +08:00
|
|
|
#endif /*ENABLE_C*/
|