📄 rawfile.c
字号:
skip(s); if (atodims(s, dims, &numdims)) { /* Something's wrong. */ fprintf(cp_err, "Warning: syntax error in dimensions, ignored.\n"); numdims = 0; continue; } if (numdims > MAXDIMS) { numdims = 0; continue; } /* Let's just make sure that the no. of points * and the dimensions are consistent. */ for (j = 0, ndimpoints = 1; j < numdims; j++) { ndimpoints *= dims[j]; } if (ndimpoints != npoints) { fprintf(cp_err, "Warning: dimensions inconsistent with no. of points, ignored.\n"); numdims = 0; } } else if (ciprefix("command:", buf)) { /* Note that we reverse these commands eventually... */ s = buf; skip(s); nonl(s); if (curpl) { wl = alloc(struct wordlist); wl->wl_word = copy(s); wl->wl_next = curpl->pl_commands; if (curpl->pl_commands) curpl->pl_commands->wl_prev = wl; curpl->pl_commands = wl; } else fprintf(cp_err, "Error: misplaced Command: line\n"); /* Now execute the command if we can. */ (void) cp_evloop(s); } else if (ciprefix("option:", buf)) { s = buf; skip(s); nonl(s); if (curpl) { wl = cp_lexer(s); for (vv = curpl->pl_env; vv && vv->va_next; vv = vv->va_next) ; if (vv) vv->va_next = cp_setparse(wl); else curpl->pl_env = cp_setparse(wl); } else fprintf(cp_err, "Error: misplaced Option: line\n"); } else if (ciprefix("variables:", buf)) { /* We reverse the dvec list eventually... */ if (!curpl) { fprintf(cp_err, "Error: no plot name given\n"); plots = NULL; break; } s = buf; skip(s); if (!*s) { (void) fgets(buf, BSIZE_SP, fp); s = buf; } if (numdims == 0) { numdims = 1; dims[0] = npoints; } /* Now read all the variable lines in. */ for (i = 0; i < nvars; i++) { v = alloc(struct dvec); ZERO(v, struct dvec); v->v_next = curpl->pl_dvecs; curpl->pl_dvecs = v; if (!curpl->pl_scale) curpl->pl_scale = v; v->v_flags = flags; v->v_plot = curpl; v->v_length = npoints; v->v_numdims = 0; /* Length and dims might be changed by options. */ if (!i) curpl->pl_scale = v; else { (void) fgets(buf, BSIZE_SP, fp); s = buf; } (void) gettok(&s); /* The index field. */ if (t = gettok(&s)) v->v_name = t; else fprintf(cp_err, "Error: bad var line %s\n", buf); t = gettok(&s); /* The type name. */ if (t) v->v_type = ft_typnum(t); else fprintf(cp_err, "Error: bad var line %s\n", buf); /* Fix the name... */ if (isdigit(*v->v_name) && (r = ft_typabbrev(v ->v_type))) { (void) sprintf(buf2, "%s(%s)", r, v->v_name); v->v_name = copy(buf2); } /* Now come the strange options... */ while (t = gettok(&s)) { if (ciprefix("min=", t)) { if (sscanf(t + 4, "%lf", &v->v_minsignal) != 1) fprintf(cp_err, "Error: bad arg %s\n", t); v->v_flags |= VF_MINGIVEN; } else if (ciprefix("max=", t)) { if (sscanf(t + 4, "%lf", &v->v_maxsignal) != 1) fprintf(cp_err, "Error: bad arg %s\n", t); v->v_flags |= VF_MAXGIVEN; } else if (ciprefix("color=", t)) { v->v_defcolor = copy(t + 6); } else if (ciprefix("scale=", t)) { /* This is bad, but... */ v->v_scale = (struct dvec *) copy(t + 6); } else if (ciprefix("grid=", t)) { v->v_gridtype = (GRIDTYPE) scannum(t + 5); } else if (ciprefix("plot=", t)) { v->v_plottype = (PLOTTYPE) scannum(t + 5); } else if (ciprefix("dims=", t)) { fixdims(v, t + 5); } else { fprintf(cp_err, "Warning: bad var param %s\n", t); } } /* Now we default any missing dimensions. */ if (!v->v_numdims) { v->v_numdims = numdims; for (j = 0; j < numdims; j++) v->v_dims[j] = dims[j]; } /* And allocate the data array. We would use * the desired vector length, but this would * be dangerous if the file is invalid. */ if (isreal(v)) v->v_realdata = (double *) tmalloc( npoints * sizeof (double)); else v->v_compdata = (complex *) tmalloc( npoints * sizeof (complex)); } } else if (ciprefix("values:", buf) || ciprefix("binary:", buf)) { if (!curpl) { fprintf(cp_err, "Error: no plot name given\n"); plots = NULL; break; } /* We'd better reverse the dvec list now... */ for (v = curpl->pl_dvecs, curpl->pl_dvecs = NULL; v; v = nv) { nv = v->v_next; v->v_next = curpl->pl_dvecs; curpl->pl_dvecs = v; } /* And fix the scale pointers. */ for (v = curpl->pl_dvecs; v; v = v->v_next) { if (v->v_scale) { for (nv = curpl->pl_dvecs; nv; nv = nv->v_next) if (cieq((char *) v->v_scale, nv->v_name)) { v->v_scale = nv; break; } if (!nv) { fprintf(cp_err, "Error: no such vector %s\n", (char *) v->v_scale); v->v_scale = NULL; } } } for (i = 0; i < npoints; i++) { if ((*buf == 'v') || (*buf == 'V')) { /* It's an ASCII file. */ (void) fscanf(fp, " %d", &j); for (v = curpl->pl_dvecs; v; v = v->v_next) { if (i < v->v_length) { if (flags & VF_REAL) { if (fscanf(fp, " %lf", &v->v_realdata[i]) != 1) fprintf(cp_err, "Error: bad rawfile\n"); } else { if (fscanf(fp, " %lf, %lf", &realpart(&v->v_compdata[i]), &imagpart(&v->v_compdata[i])) != 2) fprintf(cp_err, "Error: bad rawfile\n"); } } else if (raw_padded) { if (flags & VF_REAL) { if (fscanf(fp, " %lf", &junk) != 1) fprintf(cp_err, "Error: bad rawfile\n"); } else { if (fscanf(fp, " %lf, %lf", &junk, &junk) != 2) fprintf(cp_err, "Error: bad rawfile\n"); } } } } else { /* It's a Binary file. */ for (v = curpl->pl_dvecs; v; v = v->v_next) { if (i < v->v_length) { if (flags & VF_REAL) { if (fread((char *) &v->v_realdata[i], sizeof (double), 1, fp) != 1) fprintf(cp_err, "Error: bad rawfile\n"); } else { if (fread((char *) &v->v_compdata[i].cx_real, sizeof (double), 1, fp) != 1) fprintf(cp_err, "Error: bad rawfile\n"); if (fread((char *) &v->v_compdata[i].cx_imag, sizeof (double), 1, fp) != 1) fprintf(cp_err, "Error: bad rawfile\n"); } } else if (raw_padded) { if (flags & VF_REAL) { if (fread((char *) &junk, sizeof (double), 1, fp) != 1) fprintf(cp_err, "Error: bad rawfile\n"); } else { if (fread((char *) &junk, sizeof (double), 1, fp) != 1) fprintf(cp_err, "Error: bad rawfile\n"); if (fread((char *) &junk, sizeof (double), 1, fp) != 1) fprintf(cp_err, "Error: bad rawfile\n"); } } } } } } else { s = buf; skip(s); if (*s) { fprintf(cp_err, "Error: strange line in rawfile;\n\t\"%s\"\nload aborted.\n", s); return (NULL); } } } if (curpl) { /* reverse commands list */ for (wl=curpl->pl_commands, curpl->pl_commands=NULL; wl && wl->wl_next; wl=nwl) { nwl = wl->wl_next; wl->wl_next = curpl->pl_commands; curpl->pl_commands = wl; } } /* Fix everything up nicely again. */ cp_popcontrol(); cp_curin = lastin; cp_curout = lastout; cp_curerr = lasterr; (void) fclose(fp); return (plots);}/* s is a string of the form d1,d2,d3... */static voidfixdims(v, s) struct dvec *v; char *s;{ int i, ndimpoints; if (atodims(s, v->v_dims, &(v->v_numdims))) { /* Something's wrong. */ fprintf(cp_err, "Warning: syntax error in dimensions, ignored.\n"); return; } else if (v->v_numdims > MAXDIMS) { return; } /* If the no. of points is less than the the total data length, * truncate the vector length. If it's greater in length, we * have serious problems with this vector. Try to fix * by setting to default dimensions when we return. */ for (i = 0, ndimpoints = 1; i < v->v_numdims; i++) { ndimpoints *= v->v_dims[i]; } if (ndimpoints > v->v_length) { v->v_numdims = 0; } else { v->v_length = ndimpoints; } return;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -