int main(int argc, const char** argv) { int i; char f[BUFSIZ]; /* intput file */ const char *o; /* output dir */ int Fst; float x0, y0, z0; float x, y, z; int ix, iy, iz; /* periodic image index */ i = 1; X = atof(argv[i++]); Y = atof(argv[i++]); Z = atof(argv[i++]); o = argv[i++]; ix = iy = iz = 0; Fst = 1; while (fline(/**/ f) != NULL) { if (Fst) { Fst = 0; read_fst(f); compute_cm(/**/ &x, &y, &z); } else { read_rst(f); compute_cm(/**/ &x, &y, &z); upd_img(x, y, z, x0, y0, z0, /*io*/ &ix, &iy, &iz); shift(ix, iy, iz); } write_file(o); x0 = x; y0 = y; z0 = z; } if (Fst) fprintf(stderr, "ply2pbc0: warning: no input files\n"); }
main() { register int i; offmouse(); physbase=sxbios(2); res = (int) sxbios(4); sxbios(5,-1L,-1L,0); for(i=0;i<500;i++) fhline(XRND,YRND,XRND,i&15,physbase,clip); for(i=0;i<500;i++) fvline(XRND,YRND,YRND,i&15,physbase,clip); for(i=0;i<500;i++) fcircle(XRND,YRND,RRND,i&15,physbase,clip); for(i=0;i<500;i++) fframe(XRND,YRND,XRND,YRND,i&15,physbase,clip); for(i=0;i<500;i++) fline(XRND,YRND,XRND,YRND,i&15,physbase,clip); for(i=0;i<500;i++) fbox(XRND,YRND,XRND,YRND,i&15,physbase,clip); for(i=0;i<500;i++) fpcircle(XRND,YRND,RRND,i&15,physbase,clip); for(i=0;i<7500;i++) fplot(XRND,YRND,i&15,physbase,clip); sxbios(5,-1L,-1L,res); onmouse(); }
/* make Fortran to put record */ static void gen_load_fortran( void *rec_start ) { char stmnt[FORT_MAX_STMNT]; struct vars *v = &vars[varnum]; if (!v->has_data) return; if (v->ndims == 0 || v->dims[0] != rec_dim) { sprintf(stmnt, "* store %s", v->name); fline(stmnt); } /* generate code to initialize variable with values found in CDL input */ if (v->type != NC_CHAR) { f_var_init(varnum, rec_start); } else { v->data_stmnt = fstrstr(rec_start, valnum); } if (v->ndims >0 && v->dims[0] == rec_dim) { return; } if (v->type != NC_CHAR) { sprintf(stmnt, "iret = nf_put_var_%s(ncid, %s_id, %s)", nfftype(v->type), v->lname, v->lname); } else { char *char_expr = fstrstr(rec_start, valnum); if(strlen("iret = nf_put_var_(ncid, _id, )") + strlen(nfftype(v->type)) + strlen(v->lname) + strlen(char_expr) > FORT_MAX_STMNT) { derror("FORTRAN statement to assign values to %s too long!", v->lname); exit(9); } sprintf(stmnt, "iret = nf_put_var_%s(ncid, %s_id, %s)", nfftype(v->type), v->lname, char_expr); free(char_expr); } fline(stmnt); fline("call check_err(iret)"); }
/* make Fortran to put record */ static void gen_load_fortran( void *rec_start ) { char stmnt[FORT_MAX_STMNT]; struct vars *v = &vars[varnum]; if (!v->has_data) return; if (v->ndims == 0 || v->dims[0] != rec_dim) { sprintf(stmnt, "* store %s", v->name); fline(stmnt); } /* generate code to initialize variable with values found in CDL input */ if (v->type != NC_CHAR) { f_var_init(varnum, (char*)rec_start); } else { v->data_stmnt = (char*) fstrstr((char*)rec_start, valnum); } if (v->ndims >0 && v->dims[0] == rec_dim) { return; } if (v->type != NC_CHAR) { sprintf(stmnt, "iret = nf_put_var_%s(ncid, %s_id, %s)", nfftype(v->type), v->lname, v->lname); } else { char *char_expr = (char*) fstrstr((char*)rec_start, valnum); sprintf(stmnt, "iret = nf_put_var_%s(ncid, %s_id, %s)", nfftype(v->type), v->lname, char_expr); free(char_expr); } fline(stmnt); fline("call check_err(iret)"); }
/* * Add to a partial Fortran statement, checking if it's too long. If it is too * long, output the first part of it as a single statement with continuation * characters and start a new (probably invalid) statement with the remainder. * This will cause a Fortran compiler error, but at least all the information * will be available. */ static void fstrcat( char *s, /* source string of stement being built */ const char *t, /* string to be appended to source */ size_t *slenp /* pointer to length of source string */ ) { *slenp += strlen(t); if (*slenp >= FORT_MAX_STMNT) { derror("FORTRAN statement too long: %s",s); fline(s); strcpy(s, t); *slenp = strlen(s); } else { strcat(s, t); } }
/* * Add to a partial Fortran statement, checking if it's too long. If it is too * long, output the first part of it as a single statement with continuation * characters and start a new (probably invalid) statement with the remainder. * This will cause a Fortran compiler error, but at least all the information * will be available. */ static void fstrcat( char *s, /* source string of stement being built */ const char *t, /* string to be appended to source */ size_t *slenp /* pointer to length of source string */ ) { *slenp += strlen(t); if (*slenp >= FORT_MAX_STMNT) { derror("FORTRAN statement too long: %s",s); fline(s); strncpy(s, t, FORT_MAX_STMNT); *slenp = strlen(s); } else { /* Suppress a coverity-related issue without actually ignoring it in the coverity dashboard. */ /* coverity[unsigned_compare] */ strncat(s, t, MAX(0,MIN(strlen(t),strlen(s)-(strlen(t))))); } }
static char *read_line(void) { FILE *f; char *line; int newline; re_read: if(file_stack_idx < 0) ICE("file stack idx = 0 on read()"); f = file_stack[file_stack_idx].file; line = fline(f, &newline); if(!line){ if(ferror(f)) die("read():"); fclose(f); if(file_stack_idx > 0){ free(dirname_pop()); preproc_pop(); goto re_read; }else{ if(!prev_newline){ CPP_WARN(WNEWLINE, "no newline at end-of-file"); } } return NULL; } prev_newline = newline; current_line++; return line; }
/* * Create Fortran data statement to initialize numeric variable with * values. */ static void f_var_init( int varnum, /* which variable */ void *rec_start /* start of data */ ) { char *val_string; char *charvalp; short *shortvalp; int *intvalp; float *floatvalp; double *doublevalp; unsigned char *ubytevalp; unsigned short *ushortvalp; unsigned int *uintvalp; long long *int64valp; unsigned long long *uint64valp; char stmnt[FORT_MAX_STMNT]; size_t stmnt_len; char s2[FORT_MAX_STMNT]; int ival; /* load variable with data values */ sprintf(stmnt, "data %s /",vars[varnum].lname); stmnt_len = strlen(stmnt); switch (vars[varnum].type) { case NC_BYTE: charvalp = (char *) rec_start; for (ival = 0; ival < var_len-1; ival++) { val_string = fstring(NC_BYTE,(void *)charvalp++,0); sprintf(s2, "%s, ", val_string); fstrcat(stmnt, s2, &stmnt_len); free(val_string); } val_string = fstring(NC_BYTE,(void *)charvalp++,0); fstrcat(stmnt, val_string, &stmnt_len); free(val_string); break; case NC_SHORT: shortvalp = (short *) rec_start; for (ival = 0; ival < var_len-1; ival++) { sprintf(s2, "%d, ", *shortvalp++); fstrcat(stmnt, s2, &stmnt_len); } sprintf(s2, "%d", *shortvalp); fstrcat(stmnt, s2, &stmnt_len); break; case NC_INT: intvalp = (int *) rec_start; for (ival = 0; ival < var_len-1; ival++) { sprintf(s2, "%ld, ", (long)*intvalp++); fstrcat(stmnt, s2, &stmnt_len); } sprintf(s2, "%ld", (long)*intvalp); fstrcat(stmnt, s2, &stmnt_len); break; case NC_FLOAT: floatvalp = (float *) rec_start; for (ival = 0; ival < var_len-1; ival++) { sprintf(s2, "%.8g, ", *floatvalp++); fstrcat(stmnt, s2, &stmnt_len); } sprintf(s2, "%.8g", *floatvalp); fstrcat(stmnt, s2, &stmnt_len); break; case NC_DOUBLE: doublevalp = (double *) rec_start; for (ival = 0; ival < var_len-1; ival++) { sprintf(s2, "%#.16g", *doublevalp++); tztrim(s2); expe2d(s2); /* change 'e' to 'd' in exponent */ fstrcat(s2, ", ", &stmnt_len); fstrcat(stmnt, s2, &stmnt_len); } sprintf(s2, "%#.16g", *doublevalp++); tztrim(s2); expe2d(s2); fstrcat(stmnt, s2, &stmnt_len); break; case NC_UBYTE: ubytevalp = (unsigned char *) rec_start; for (ival = 0; ival < var_len-1; ival++) { sprintf(s2, "%hhu, ", (unsigned char)*ubytevalp++); fstrcat(stmnt, s2, &stmnt_len); } sprintf(s2, "%hhu", (unsigned char)*ubytevalp); fstrcat(stmnt, s2, &stmnt_len); break; case NC_USHORT: ushortvalp = (unsigned short *) rec_start; for (ival = 0; ival < var_len-1; ival++) { sprintf(s2, "%hu, ", (unsigned short)*ushortvalp++); fstrcat(stmnt, s2, &stmnt_len); } sprintf(s2, "%hu", (unsigned short)*ushortvalp); fstrcat(stmnt, s2, &stmnt_len); break; case NC_UINT: uintvalp = (unsigned int *) rec_start; for (ival = 0; ival < var_len-1; ival++) { sprintf(s2, "%u, ", (unsigned int)*uintvalp++); fstrcat(stmnt, s2, &stmnt_len); } sprintf(s2, "%u", (unsigned int)*uintvalp); fstrcat(stmnt, s2, &stmnt_len); break; case NC_INT64: int64valp = (long long *) rec_start; for (ival = 0; ival < var_len-1; ival++) { sprintf(s2, "%lld, ", (long long)*int64valp++); fstrcat(stmnt, s2, &stmnt_len); } sprintf(s2, "%lld", (long long)*int64valp); fstrcat(stmnt, s2, &stmnt_len); break; case NC_UINT64: uint64valp = (unsigned long long *) rec_start; for (ival = 0; ival < var_len-1; ival++) { sprintf(s2, "%llu, ", (unsigned long long)*uint64valp++); fstrcat(stmnt, s2, &stmnt_len); } sprintf(s2, "%llu", (unsigned long long)*uint64valp); fstrcat(stmnt, s2, &stmnt_len); break; default: derror("fstrstr: bad type"); break; } fstrcat(stmnt, "/", &stmnt_len); /* For record variables, store data statement for later use; otherwise, just print it. */ if (vars[varnum].ndims > 0 && vars[varnum].dims[0] == rec_dim) { char *dup_stmnt = (char*) emalloc(strlen(stmnt)+1); strcpy(dup_stmnt, stmnt); /* ULTRIX missing strdup */ vars[varnum].data_stmnt = dup_stmnt; } else { fline(stmnt); } }
static void cl_fortran() { fline("call ncclos (ncid, iret)"); fline("end"); }
int main(int argc, char **argv) { argc--; argv++; int ret = 0; if (argc == 0) { usage: fprintf(stderr, "%s [-help] | <files> | [-o output]\n", argv[-1]); fprintf(stderr, " -o - output file [default is \"data.c\"]\n"); fprintf(stderr, " -help - this\n"); fprintf(stderr, "example:\n"); fprintf(stderr, " %s icon.png music.mp3 -o file.c\n", argv[-1]); return 1; } char outfile[FILENAME_MAX] = "data.c"; for (int i = 0; i < argc; i++) { if (!strcmp(argv[i], "-o")) { if (i + 1 < argc) { strcpy(outfile, argv[i + 1]); memmove(argv+i, argv+i+2, (argc-i) * sizeof(char*)); argc -= 2; /* Eliminate "-o <thing>" */ continue; } } if (!strcmp(argv[i], "-help")) goto usage; } FILE *out = fopen(outfile, "w"); if (!out) { fprintf(stderr, "failed to open `%s' for output\n", outfile); return 1; } fprintf(out, "/* File automatically generated by incbin */\n"); fprintf(out, "#ifdef __cplusplus\n"); fprintf(out, "extern \"C\" {\n"); fprintf(out, "#endif\n\n"); for (int i = 0; i < argc; i++) { FILE *fp = fopen(argv[i], "r"); if (!fp) { fprintf(stderr, "failed to open `%s' for reading\n", argv[i]); fclose(out); return 1; } char *line = NULL; size_t size = 0; while (fline(&line, &size, fp) != -1) { char *inc = strstr(line, "INCBIN"); if (!inc) continue; char *beg = strchr(inc, '('); if (!beg) continue; char *sep = strchr(beg, ','); if (!sep) continue; char *end = strchr(sep, ')'); if (!end) continue; char *name = beg + 1; char *file = sep + 1; while (isspace(*name)) name++; while (isspace(*file)) file++; sep--; while (isspace(*sep)) sep--; *++sep = '\0'; end--; while (isspace(*end)) end--; *++end = '\0'; fprintf(out, "/* INCBIN(%s, %s); */\n", name, file); fprintf(out, "unsigned char g%sData[] = {\n ", name); *--end = '\0'; file++; FILE *f = fopen(file, "rb"); if (!f) { fprintf(stderr, "failed to include data `%s'\n", file); goto end; } else { fseek(f, 0, SEEK_END); long size = ftell(f); fseek(f, 0, SEEK_SET); unsigned char *data = (unsigned char *)malloc(size); if (!data) { fprintf(stderr, "out of memory\n"); fclose(f); ret = 1; goto end; } if (fread(data, size, 1, f) != 1) { fprintf(stderr, "failed reading include data `%s'\n", file); free(data); fclose(f); ret = 1; goto end; } unsigned char count = 0; for (long i = 0; i < size; i++) { if (count == 12) { fprintf(out, "\n "); count = 0; } fprintf(out, i != size - 1 ? "0x%02X, " : "0x%02X", data[i]); count++; } free(data); fclose(f); } fprintf(out, "\n};\n"); fprintf(out, "unsigned char *g%sEnd = g%sData + sizeof(g%sData);\n", name, name, name); fprintf(out, "unsigned int g%sSize = sizeof(g%sData);\n", name, name); } end: free(line); fclose(fp); } if (ret == 0) { fprintf(out, "\n#ifdef __cplusplus\n"); fprintf(out, "}\n"); fprintf(out, "#endif\n"); fclose(out); return 0; } fclose(out); remove(outfile); return 1; }
char *splice_line(void) { static int n_nls; char *last; int join; if(n_nls){ n_nls--; return ustrdup(""); } last = NULL; join = 0; for(;;){ FILE *f; int len; char *line; re_read: if(file_stack_idx < 0) ICE("file stack idx = 0 on read()"); f = file_stack[file_stack_idx].file; line = fline(f); if(!line){ if(ferror(f)) die("read():"); fclose(f); if(file_stack_idx > 0){ free(dirname_pop()); preproc_pop(); goto re_read; } return NULL; } current_line++; if(join){ join = 0; last = urealloc(last, strlen(last) + strlen(line) + 1); strcpy(last + strlen(last), line); free(line); line = last; } len = strlen(line); if(len && line[len - 1] == '\\'){ line[len - 1] = '\0'; join = 1; last = line; n_nls++; }else{ return line; } } }
/* * Generate FORTRAN code for creating netCDF from in-memory structure. */ static void gen_fortran( const char *filename) { int idim, ivar, iatt, jatt, itype, maxdims; int vector_atts; char *val_string; char stmnt[FORT_MAX_STMNT]; char s2[NC_MAX_NAME + 10]; char *sp; /* Need how many netCDF types there are, because we create an array * for each type of attribute. */ int ntypes = 6; /* number of netCDF types, NC_BYTE, ... */ nc_type types[6]; /* at least ntypes */ size_t max_atts[NC_DOUBLE + 1]; types[0] = NC_BYTE; types[1] = NC_CHAR; types[2] = NC_SHORT; types[3] = NC_INT; types[4] = NC_FLOAT; types[5] = NC_DOUBLE; fline("program fgennc"); fline("include 'netcdf.inc'"); /* create necessary declarations */ fline("* error status return"); fline("integer iret"); fline("* netCDF id"); fline("integer ncid"); if (nofill_flag) { fline("* to save old fill mode before changing it temporarily"); fline("integer oldmode"); } if (ndims > 0) { fline("* dimension ids"); for (idim = 0; idim < ndims; idim++) { sprintf(stmnt, "integer %s_dim", dims[idim].lname); fline(stmnt); } fline("* dimension lengths"); for (idim = 0; idim < ndims; idim++) { sprintf(stmnt, "integer %s_len", dims[idim].lname); fline(stmnt); } for (idim = 0; idim < ndims; idim++) { if (dims[idim].size == NC_UNLIMITED) { sprintf(stmnt, "parameter (%s_len = NF_UNLIMITED)", dims[idim].lname); } else { sprintf(stmnt, "parameter (%s_len = %lu)", dims[idim].lname, (unsigned long) dims[idim].size); } fline(stmnt); } } maxdims = 0; /* most dimensions of any variable */ for (ivar = 0; ivar < nvars; ivar++) if (vars[ivar].ndims > maxdims) maxdims = vars[ivar].ndims; if (nvars > 0) { fline("* variable ids"); for (ivar = 0; ivar < nvars; ivar++) { sprintf(stmnt, "integer %s_id", vars[ivar].lname); fline(stmnt); } fline("* rank (number of dimensions) for each variable"); for (ivar = 0; ivar < nvars; ivar++) { sprintf(stmnt, "integer %s_rank", vars[ivar].lname); fline(stmnt); } for (ivar = 0; ivar < nvars; ivar++) { sprintf(stmnt, "parameter (%s_rank = %d)", vars[ivar].lname, vars[ivar].ndims); fline(stmnt); } fline("* variable shapes"); for (ivar = 0; ivar < nvars; ivar++) { if (vars[ivar].ndims > 0) { sprintf(stmnt, "integer %s_dims(%s_rank)", vars[ivar].lname, vars[ivar].lname); fline(stmnt); } } } /* declarations for variables to be initialized */ if (nvars > 0) { /* we have variables */ fline("* data variables"); for (ivar = 0; ivar < nvars; ivar++) { struct vars *v = &vars[ivar]; /* Generate declarations here for non-record data variables only. Record variables are declared in separate subroutine later, when we know how big they are. */ if (v->ndims > 0 && v->dims[0] == rec_dim) { continue; } /* Make declarations for non-text variables only; for text variables, just include string in nf_put_var call */ if (v->type == NC_CHAR) { continue; } if (v->ndims == 0) { /* scalar */ sprintf(stmnt, "%s %s", ncftype(v->type), v->lname); } else { sprintf(stmnt, "%s %s(", ncftype(v->type), v->lname); /* reverse dimensions for FORTRAN */ for (idim = v->ndims-1; idim >= 0; idim--) { sprintf(s2, "%s_len, ", dims[v->dims[idim]].lname); strcat(stmnt, s2); } sp = strrchr(stmnt, ','); if(sp != NULL) { *sp = '\0'; } strcat(stmnt, ")"); } fline(stmnt); } } /* determine what attribute vectors needed */ for (itype = 0; itype < ntypes; itype++) max_atts[(int)types[itype]] = 0; vector_atts = 0; for (iatt = 0; iatt < natts; iatt++) { if (atts[iatt].len > max_atts[(int) atts[iatt].type]) { max_atts[(int)atts[iatt].type] = atts[iatt].len; vector_atts = 1; } } if (vector_atts) { fline("* attribute vectors"); for (itype = 0; itype < ntypes; itype++) { if (types[itype] != NC_CHAR && max_atts[(int)types[itype]] > 0) { sprintf(stmnt, "%s %sval(%lu)", ncftype(types[itype]), nfstype(types[itype]), (unsigned long) max_atts[(int)types[itype]]); fline(stmnt); } } } /* create netCDF file, uses NC_CLOBBER mode */ fline("* enter define mode"); if (!cmode_modifier) { sprintf(stmnt, "iret = nf_create(\'%s\', NF_CLOBBER, ncid)", filename); } else if (cmode_modifier & NC_64BIT_OFFSET) { sprintf(stmnt, "iret = nf_create(\'%s\', OR(NF_CLOBBER,NF_64BIT_OFFSET), ncid)", filename); #ifdef USE_NETCDF4 } else if (cmode_modifier & NC_CLASSIC_MODEL) { sprintf(stmnt, "iret = nf_create(\'%s\', OR(NF_CLOBBER,NC_NETCDF4,NC_CLASSIC_MODEL), ncid)", filename); } else if (cmode_modifier & NC_NETCDF4) { sprintf(stmnt, "iret = nf_create(\'%s\', OR(NF_CLOBBER,NF_NETCDF4), ncid)", filename); #endif } else { derror("unknown cmode modifier"); } fline(stmnt); fline("call check_err(iret)"); /* define dimensions from info in dims array */ if (ndims > 0) fline("* define dimensions"); for (idim = 0; idim < ndims; idim++) { if (dims[idim].size == NC_UNLIMITED) sprintf(stmnt, "iret = nf_def_dim(ncid, \'%s\', NF_UNLIMITED, %s_dim)", dims[idim].name, dims[idim].lname); else sprintf(stmnt, "iret = nf_def_dim(ncid, \'%s\', %lu, %s_dim)", dims[idim].name, (unsigned long) dims[idim].size, dims[idim].lname); fline(stmnt); fline("call check_err(iret)"); } /* define variables from info in vars array */ if (nvars > 0) { fline("* define variables"); for (ivar = 0; ivar < nvars; ivar++) { for (idim = 0; idim < vars[ivar].ndims; idim++) { sprintf(stmnt, "%s_dims(%d) = %s_dim", vars[ivar].lname, vars[ivar].ndims - idim, /* reverse dimensions */ dims[vars[ivar].dims[idim]].lname); fline(stmnt); } if (vars[ivar].ndims > 0) { /* a dimensioned variable */ sprintf(stmnt, "iret = nf_def_var(ncid, \'%s\', %s, %s_rank, %s_dims, %s_id)", vars[ivar].name, ftypename(vars[ivar].type), vars[ivar].lname, vars[ivar].lname, vars[ivar].lname); } else { /* a scalar */ sprintf(stmnt, "iret = nf_def_var(ncid, \'%s\', %s, %s_rank, 0, %s_id)", vars[ivar].name, ftypename(vars[ivar].type), vars[ivar].lname, vars[ivar].lname); } fline(stmnt); fline("call check_err(iret)"); } } /* define attributes from info in atts array */ if (natts > 0) { fline("* assign attributes"); for (iatt = 0; iatt < natts; iatt++) { if (atts[iatt].type == NC_CHAR) { /* string */ val_string = fstrstr((char *) atts[iatt].val, atts[iatt].len); sprintf(stmnt, "iret = nf_put_att_text(ncid, %s%s, \'%s\', %lu, %s)", atts[iatt].var == -1 ? "NF_GLOBAL" : vars[atts[iatt].var].lname, atts[iatt].var == -1 ? "" : "_id", atts[iatt].name, (unsigned long) atts[iatt].len, val_string); fline(stmnt); fline("call check_err(iret)"); free(val_string); } else { for (jatt = 0; jatt < atts[iatt].len ; jatt++) { val_string = fstring(atts[iatt].type,atts[iatt].val,jatt); sprintf(stmnt, "%sval(%d) = %s", nfstype(atts[iatt].type), jatt+1, val_string); fline(stmnt); free (val_string); } sprintf(stmnt, "iret = nf_put_att_%s(ncid, %s%s, \'%s\', %s, %lu, %sval)", nfftype(atts[iatt].type), atts[iatt].var == -1 ? "NCGLOBAL" : vars[atts[iatt].var].lname, atts[iatt].var == -1 ? "" : "_id", atts[iatt].name, ftypename(atts[iatt].type), (unsigned long) atts[iatt].len, nfstype(atts[iatt].type)); fline(stmnt); fline("call check_err(iret)"); } } } if (nofill_flag) { fline("* don't initialize variables with fill values"); fline("iret = nf_set_fill(ncid, NF_NOFILL, oldmode)"); fline("call check_err(iret)"); } fline("* leave define mode"); fline("iret = nf_enddef(ncid)"); fline("call check_err(iret)"); }
/* Generate Fortran for cleaning up and closing file */ static void cl_fortran(void) { int ivar; int idim; char stmnt[FORT_MAX_STMNT]; char s2[FORT_MAX_STMNT]; char*sp; int have_rec_var = 0; /* do we have any record variables? */ for (ivar = 0; ivar < nvars; ivar++) { struct vars *v = &vars[ivar]; if (v->ndims > 0 && v->dims[0] == rec_dim) { have_rec_var = 1; break; } } if (have_rec_var) { fline(" "); fline("* Write record variables"); sprintf(stmnt, "call writerecs(ncid,"); /* generate parameter list for subroutine to write record vars */ for (ivar = 0; ivar < nvars; ivar++) { struct vars *v = &vars[ivar]; /* if a record variable, include id in parameter list */ if (v->ndims > 0 && v->dims[0] == rec_dim) { sprintf(s2, "%s_id,", v->lname); strcat(stmnt, s2); } } sp = strrchr(stmnt, ','); if(sp != NULL) { *sp = '\0'; } strcat(stmnt, ")"); fline(stmnt); } fline(" "); fline("iret = nf_close(ncid)"); fline("call check_err(iret)"); fline("end"); fline(" "); if (have_rec_var) { sprintf(stmnt, "subroutine writerecs(ncid,"); for (ivar = 0; ivar < nvars; ivar++) { struct vars *v = &vars[ivar]; if (v->ndims > 0 && v->dims[0] == rec_dim) { sprintf(s2, "%s_id,", v->lname); strcat(stmnt, s2); } } sp = strrchr(stmnt, ','); if(sp != NULL) { *sp = '\0'; } strcat(stmnt, ")"); fline(stmnt); fline(" "); fline("* netCDF id"); fline("integer ncid"); fline("* variable ids"); for (ivar = 0; ivar < nvars; ivar++) { struct vars *v = &vars[ivar]; if (v->ndims > 0 && v->dims[0] == rec_dim) { sprintf(stmnt, "integer %s_id", v->lname); fline(stmnt); } } fline(" "); fline("include 'netcdf.inc'"); /* create necessary declarations */ fline("* error status return"); fline("integer iret"); /* generate integer/parameter declarations for all dimensions used in record variables, except record dimension. */ fline(" "); fline("* netCDF dimension sizes for dimensions used with record variables"); for (idim = 0; idim < ndims; idim++) { /* if used in a record variable and not record dimension */ if (used_in_rec_var(idim) && dims[idim].size != NC_UNLIMITED) { sprintf(stmnt, "integer %s_len", dims[idim].lname); fline(stmnt); sprintf(stmnt, "parameter (%s_len = %lu)", dims[idim].lname, (unsigned long) dims[idim].size); fline(stmnt); } } fline(" "); fline("* rank (number of dimensions) for each variable"); for (ivar = 0; ivar < nvars; ivar++) { struct vars *v = &vars[ivar]; if (v->ndims > 0 && v->dims[0] == rec_dim) { sprintf(stmnt, "integer %s_rank", v->lname); fline(stmnt); } } for (ivar = 0; ivar < nvars; ivar++) { struct vars *v = &vars[ivar]; if (v->ndims > 0 && v->dims[0] == rec_dim) { sprintf(stmnt, "parameter (%s_rank = %d)", v->lname, v->ndims); fline(stmnt); } } fline("* starts and counts for array sections of record variables"); for (ivar = 0; ivar < nvars; ivar++) { struct vars *v = &vars[ivar]; if (v->ndims > 0 && v->dims[0] == rec_dim) { sprintf(stmnt, "integer %s_start(%s_rank), %s_count(%s_rank)", v->lname, v->lname, v->lname, v->lname); fline(stmnt); } } fline(" "); fline("* data variables"); for (ivar = 0; ivar < nvars; ivar++) { struct vars *v = &vars[ivar]; if (v->ndims > 0 && v->dims[0] == rec_dim) { char *sp; fline(" "); sprintf(stmnt, "integer %s_nr", v->lname); fline(stmnt); if (v->nrecs > 0) { sprintf(stmnt, "parameter (%s_nr = %lu)", v->lname, (unsigned long) v->nrecs); } else { sprintf(stmnt, "parameter (%s_nr = 1)", v->lname); } fline(stmnt); if (v->type != NC_CHAR) { sprintf(stmnt, "%s %s(", ncftype(v->type), v->lname); /* reverse dimensions for FORTRAN */ for (idim = v->ndims-1; idim >= 0; idim--) { if(v->dims[idim] == rec_dim) { sprintf(s2, "%s_nr, ", v->lname); } else { sprintf(s2, "%s_len, ", dims[v->dims[idim]].lname); } strcat(stmnt, s2); } sp = strrchr(stmnt, ','); if(sp != NULL) { *sp = '\0'; } strcat(stmnt, ")"); fline(stmnt); } } } fline(" "); /* Emit DATA statements after declarations, because f2c on Linux can't handle interspersing them */ for (ivar = 0; ivar < nvars; ivar++) { struct vars *v = &vars[ivar]; if (v->ndims > 0 && v->dims[0] == rec_dim && v->type != NC_CHAR) { if (v->has_data) { fline(v->data_stmnt); } else { /* generate data statement for FILL record */ size_t rec_len = 1; for (idim = 1; idim < v->ndims; idim++) { rec_len *= dims[v->dims[idim]].size; } sprintf(stmnt,"data %s /%lu * %s/", v->lname, (unsigned long) rec_len, f_fill_name(v->type)); fline(stmnt); } } } fline(" "); for (ivar = 0; ivar < nvars; ivar++) { struct vars *v = &vars[ivar]; /* if a record variable, declare starts and counts */ if (v->ndims > 0 && v->dims[0] == rec_dim) { if (!v->has_data) continue; sprintf(stmnt, "* store %s", v->name); fline(stmnt); for (idim = 0; idim < v->ndims; idim++) { sprintf(stmnt, "%s_start(%d) = 1", v->lname, idim+1); fline(stmnt); } for (idim = v->ndims-1; idim > 0; idim--) { sprintf(stmnt, "%s_count(%d) = %s_len", v->lname, v->ndims - idim, dims[v->dims[idim]].lname); fline(stmnt); } sprintf(stmnt, "%s_count(%d) = %s_nr", v->lname, v->ndims, v->lname); fline(stmnt); if (v->type != NC_CHAR) { sprintf(stmnt, "iret = nf_put_vara_%s(ncid, %s_id, %s_start, %s_count, %s)", nfftype(v->type), v->lname, v->lname, v->lname, v->lname); } else { sprintf(stmnt, "iret = nf_put_vara_%s(ncid, %s_id, %s_start, %s_count, %s)", nfftype(v->type), v->lname, v->lname, v->lname, v->data_stmnt); } fline(stmnt); fline("call check_err(iret)"); } } fline(" "); fline("end"); fline(" "); } fline("subroutine check_err(iret)"); fline("integer iret"); fline("include 'netcdf.inc'"); fline("if (iret .ne. NF_NOERR) then"); fline("print *, nf_strerror(iret)"); fline("stop"); fline("endif"); fline("end"); }
bool Command::DoSelect( void ) { sqlite3_stmt* stmt = SqliteSelect( mpVM->mpDB, L"SELECT" + mParam.ToString() ); if( !stmt ) { mpVM->PrintError(); return false; } int fcount = sqlite3_column_count( stmt ); vector<vector<wstring> > tbl; vector<wstring> row( fcount ); vector<size_t> w( fcount, 2 ); // Header for( int i = 0 ; i < fcount ; ++i ) { const char* str = (const char*) sqlite3_column_name( stmt, i ); FROM_UTF8( str, row[ i ] ); w[ i ] = max( w[ i ], row[ i ].length() ); } tbl.push_back( row ); // Records int res = sqlite3_step( stmt ); while( res == SQLITE_ROW ) { for( int i = 0 ; i < fcount ; ++i ) { const char* str = (const char*) sqlite3_column_text( stmt, i ); if( str ) FROM_UTF8( str, row[ i ] ) else row[ i ].clear(); w[ i ] = max( w[ i ], row[ i ].length() ); } tbl.push_back( row ); res = sqlite3_step( stmt ); } // Horizontal line string fline( "+-" ); for( int j = 0 ; j < fcount ; ++j ) { fline.append( w[ j ], '-' ); if( j < fcount - 1 ) fline += "-+-"; } fline += "-+"; mpVM->Print( &fline[ 0 ] ); // Print records size_t count = tbl.size(); for( size_t i = 0 ; i < count ; ++i ) { wstring line( L"| " ); vector<wstring>& row = tbl[ i ]; for( int j = 0 ; j < fcount ; ++j ) { line += row[ j ]; line.append( w[ j ] - row[ j ].length(), ' ' ); if( j < fcount - 1 ) line += L" | "; } line += L" |"; mpVM->PrintW( line ); if( !i ) mpVM->Print( &fline[ 0 ] ); } if( count > 1 ) mpVM->Print( &fline[ 0 ] ); FINALIZE_STATEMENT( stmt ); return true; }