19 static int atodims(
char*,
int*,
int*);
43 int length, numdims, dims[
MAXDIMS];
44 int nvars, i, j, prec;
48 struct dvlist *dl0, *dl, *tl;
55 raw_padding = !raw_padding;
59 fprintf(
cp_err,
"Error: plot is empty, nothing written.\n");
68 if (!(fp = fopen(name, app ?
"a" :
"w"))) {
73 numdims = nvars = length = 0;
79 for (nvars = 0, dl = dl0; dl; dl = dl->
dl_next) {
96 for (j = 0; j < numdims; j++) {
102 fprintf(fp,
"Title: %s\n", pl->
pl_title);
103 fprintf(fp,
"Date: %s\n", pl->
pl_date);
104 fprintf(fp,
"Plotname: %s\n", pl->
pl_name);
105 fprintf(fp,
"Flags: %s%s\n",
106 realflag ?
"real" :
"complex", raw_padding ?
"" :
" unpadded" );
107 fprintf(fp,
"No. Variables: %d\n", nvars);
108 fprintf(fp,
"No. Points: %d\n", length);
110 fprintf(fp,
"Dimensions: %s\n",
dimstring(dims, numdims, buf));
114 fprintf(fp,
"Command: %s\n", wl->
wl_word);
119 fprintf(fp,
"Option: %s\n", vv->
va_name);
122 fprintf(fp,
"Option: %s = ", vv->
va_name);
128 (void) putc(
'\n', fp);
136 for (tl =
NULL,dl = dl0; dl; tl = dl, dl = dl->
dl_next) {
147 fprintf(fp,
"Variables:");
148 for (i = 0, dl = dl0; dl; dl = dl->
dl_next) {
150 fprintf(fp,
" %d %s %s", i++, v->
v_name,
166 for ( ; j < numdims; j++)
167 if (dims[j] != v->
v_dims[j])
173 (void) putc(
'\n', fp);
177 fprintf(fp,
"Binary:\n");
178 for (i = 0; i < length; i++) {
179 for (dl = dl0; dl; dl = dl->
dl_next) {
182 if (i < v->v_length) {
186 (void) fwrite((
char *) &dd,
sizeof 191 (void) fwrite((
char *) &dd,
sizeof 194 (void) fwrite((
char *) &dd,
sizeof 199 (void) fwrite((
char *) &dd,
sizeof 202 (void) fwrite((
char *) &dd,
sizeof 206 else if (raw_padding) {
209 (void) fwrite((
char *) &dd,
sizeof 213 (void) fwrite((
char *) &dd,
sizeof 215 (void) fwrite((
char *) &dd,
sizeof 223 fprintf(fp,
"Values:\n");
224 for (i = 0; i < length; i++) {
225 fprintf(fp,
" %d", i);
226 for (dl = dl0; dl; dl = dl->
dl_next) {
228 if (i < v->v_length) {
230 fprintf(fp,
"\t%.*e\n", prec,
235 fprintf(fp,
"\t%.*e,0.0\n", prec,
239 fprintf(fp,
"\t%.*e,%.*e\n", prec,
245 else if (raw_padding) {
247 fprintf(fp,
"\t%.*e\n", prec, 0.0);
250 fprintf(fp,
"\t%.*e,%.*e\n",
251 prec, 0.0, prec, 0.0);
268 #define skip(s) while (*(s) && !isspace(*(s)))(s)++; while (isspace(*(s)))(s)++ 269 #define nonl(s) r = (s); while (*r && (*r != '\n')) r++; *r = '\0' 276 char *title =
"default title";
280 int flags, nvars, npoints, i, j;
281 int numdims = 0, dims[
MAXDIMS];
285 FILE *fp, *lastin, *lastout, *lasterr;
286 bool raw_padded =
true;
288 char *errbr =
"Error: bad rawfile\n";
290 if (!(fp = fopen(name,
"r"))) {
319 else if (
ciprefix(
"plotname:", buf)) {
324 for (wl=curpl->pl_commands,
325 curpl->pl_commands=
NULL; wl &&
328 wl->
wl_next = curpl->pl_commands;
329 curpl->pl_commands = wl;
333 curpl->pl_next = plots;
338 curpl->pl_date = date;
339 curpl->pl_title = title;
347 if (
cieq(buf2,
"real"))
349 else if (
cieq(buf2,
"complex"))
351 else if (
cieq(buf2,
"unpadded"))
353 else if (
cieq(buf2,
"padded"))
357 "Warning: unknown flag %s\n",buf2);
360 else if (
ciprefix(
"no. variables:", buf)) {
366 else if (
ciprefix(
"no. points:", buf)) {
372 else if (
ciprefix(
"dimensions:", buf)) {
375 "Error: misplaced Dimensions: line\n");
380 if (
atodims(s, dims, &numdims)) {
382 "Warning: syntax error in dimensions, ignored.\n");
393 for (j = 0, i = 1; j < numdims; j++) {
399 "Warning: dimensions inconsistent with no. of points, ignored.\n");
403 else if (
ciprefix(
"command:", buf)) {
411 wl->
wl_next = curpl->pl_commands;
412 if (curpl->pl_commands)
413 curpl->pl_commands->
wl_prev = wl;
414 curpl->pl_commands = wl;
418 "Error: misplaced Command: line\n");
422 else if (
ciprefix(
"option:", buf)) {
428 for (vv = curpl->pl_env; vv && vv->
va_next;
439 "Error: misplaced Command: line\n");
441 else if (
ciprefix(
"variables:", buf)) {
444 fprintf(
cp_err,
"Error: no plot name given\n");
459 for (i = 0; i < nvars; i++) {
461 v->
v_next = curpl->pl_dvecs;
463 if (!curpl->pl_scale)
483 "Error: bad var line %s\n", buf);
491 "Error: bad var line %s\n", buf);
494 if (isdigit(*v->
v_name) &&
496 (void) sprintf(buf2,
"%s(%s)", r, v->
v_name);
503 fprintf(
cp_err,
"Error: bad arg %s\n", buf2);
508 fprintf(
cp_err,
"Error: bad arg %s\n", buf2);
511 else if (
ciprefix(
"color=", buf2)) {
514 else if (
ciprefix(
"scale=", buf2)) {
528 fprintf(
cp_err,
"Warning: bad var param %s\n", buf2);
535 for (j = 0; j < numdims; j++)
545 (
double *)
tmalloc(npoints *
sizeof (
double));
551 else if (
ciprefix(
"values:", buf) ||
554 fprintf(
cp_err,
"Error: no plot name given\n");
559 for (v = curpl->pl_dvecs, curpl->pl_dvecs =
NULL; v;
562 v->
v_next = curpl->pl_dvecs;
566 for (v = curpl->pl_dvecs; v; v = v->
v_next) {
568 for (nv = curpl->pl_dvecs; nv; nv = nv->
v_next)
574 fprintf(
cp_err,
"Error: no such vector %s\n",
580 for (i = 0; i < npoints; i++) {
581 if ((*buf ==
'v') || (*buf ==
'V')) {
583 (void) fscanf(fp,
" %d", &j);
584 for (v = curpl->pl_dvecs; v; v = v->
v_next) {
587 if (fscanf(fp,
" %lf",
592 if (fscanf(fp,
" %lf, %lf",
598 else if (raw_padded) {
600 if (fscanf(fp,
" %lf", &junk) != 1)
604 if (fscanf(fp,
" %lf, %lf",
613 for (v = curpl->pl_dvecs; v; v = v->
v_next) {
617 sizeof (
double), 1, fp) != 1)
622 cx_real, sizeof (
double), 1, fp) != 1)
625 cx_imag, sizeof (
double), 1, fp) != 1)
629 else if (raw_padded) {
631 if (fread((
char *) &junk,
632 sizeof (
double), 1, fp) != 1)
636 if (fread((
char *) &junk,
637 sizeof (
double), 1, fp) != 1)
639 if (fread((
char *) &junk,
640 sizeof (
double), 1, fp) != 1)
653 "Error: strange line in rawfile -- load aborted\n");
660 for (wl=curpl->pl_commands,
661 curpl->pl_commands=
NULL; wl &&
664 wl->
wl_next = curpl->pl_commands;
665 curpl->pl_commands = wl;
677 for (curpl = plots; curpl; curpl = curpl->
pl_next) {
679 for (v = curpl->pl_dvecs; v; v = nv) {
684 curpl->pl_dvecs =
NULL;
697 if (
atodims(s, v->v_dims, &(v->v_numdims)))
700 "Warning: syntax error in dimensions, ignored.\n");
719 if (!data || length < 1)
722 for (i = 0; i < length; i++) {
723 sprintf(buf + strlen(buf),
"%d%s", data[i],
724 (i < length - 1) ?
"," :
"");
758 if (!data || !outlength)
766 while (*p && isspace(*p))
771 while (*p && isspace(*p))
776 while (*p && state != 3) {
781 printf(
"Error: maximum of %d dimensions allowed.\n",
785 else if (!isdigit(*p)) {
789 data[length++] = atoi(p);
800 if (*p ==
']' && *p == sep) {
804 else if (*p ==
',' && *p == sep) {
823 while (*p && isspace(*p))
static char * dimstring()
static char buf[MAXPROMPT]
bool cp_getvar(char *n, int t, char *r)
struct variable * cp_setparse()
struct wordlist * wl_prev
struct dvec * vec_fromplot()
struct wordlist * wl_next
struct plot * raw_read(char *name)
void raw_write(char *name, struct plot *pl, bool app, bool binary)
struct variable * va_next