/* Returns the width of the widest cell, excluding headers, along AXIS in PAGE. */ static int max_cell_width (const struct render_page *page, int axis) { int n = page->n[axis]; int x0 = page->h[axis][0]; int x1 = n - page->h[axis][1]; int x, max; max = 0; for (x = x0; x < x1; x++) { int w = cell_width (page, axis, x); if (w > max) max = w; } return max; }
static int save_flat(char *fn, buffer * buf) { int i, j, lastcell, lr; int s; FILE *fp; int sw = buf->sw, sh = buf->sh, sf = std_fmt_get(buf); int intp, f; MwFmt fmt; char *taddr; property_list *p; char *used_fmt = MwCalloc(MwFormatCount+1, 1); char *used_style = MwCalloc(nstyle+1, 1); if ((fp = fopen(fn, "w")) == (FILE *) 0) return 1; /* start by saving standard values */ fprintf(fp, "# Creator: %s\n", version); fprintf(fp, ".sw %d\n", sw); fprintf(fp, ".sh %d\n", sh); fprintf(fp, ".margins %d %d %d %d %d %d\n", buf->top_margin, buf->bottom_margin, buf->left_margin, buf->right_margin, buf->header_margin, buf->footer_margin); fprintf(fp, ".paper %s %d %d %d\n", buf->paper_name, buf->paper_width, buf->paper_height, buf->orientation); fprintf(fp, ".header %s\n", buf->header); fprintf(fp, ".footer %s\n", buf->footer); fprintf(fp, ".header_on_first %d\n", buf->header_on_first); fprintf(fp, ".first_page %d\n", buf->first_page_number); fprintf(fp, ".a1_refs %d\n", buf->a1_refs); MwDecodeFormat(sf, ~0, &fmt); if (!used_style[fmt.style]) { /* haven't saved style before */ save_styles(fp, fmt.style); used_style[fmt.style] = 1; } if (!used_fmt[sf]) { /* we haven't saved this format before */ MwSaveFormats(fp, sf); used_fmt[sf] = 1; } fprintf(fp, ".nsf %d\n", sf); /* properties */ for (p = buf->p_list; p; p = p->next) { fprintf(fp, ".p%d %s\n", strlen(p->value), p->key); fwrite(p->value, strlen(p->value), 1, fp); fprintf(fp, "\n"); /* terminate last line */ } for (s = 0; s < buf->nsht; s++) { fprintf(fp, ".sheet %d %s\n", s, buf->sht[s].name); lr = line_last_used(buf, s); fprintf(fp, "# %s\n# %d lines\n#\n", fn, lr); for (i = 1; i <= buf->sht[s].used_lines; i++) { int height = cell_height(buf, s, i); if (height != sh) fprintf(fp, ".rh %d %d\n", i, height); } for (i = 1; i <= buf->sht[s].used_cols; i++) { int width = cell_width(buf, s, i); if (width != sw) fprintf(fp, ".cw %d %d\n", i, width); } for (i = 0; i < buf->sht[s].nplugin; i++) { int width, height; int n = plugin_size_get(buf->sht[s].plugin[i].ph, &width, &height); if (n != 0) continue; fprintf(fp, ".plugin %d %d %s\n", buf->sht[s].plugin[i].row, buf->sht[s].plugin[i].col, buf->sht[s].plugin[i].name); fprintf(fp, ".plugin-geometry %d %d\n", width, height); } for (i = 1; i <= lr; i++) { fprintf(fp, "# Line %d\n", i); lastcell = col_last_changed(buf, s, i, sf); for (j = 1; j <= lastcell; j++) { f = ret_format(buf, s, i, j); MwDecodeFormat(f, ~0, &fmt); if (!used_style[fmt.style]) { /* haven't saved style before */ save_styles(fp, fmt.style); used_style[fmt.style] = 1; } if (!used_fmt[f]) { /* we haven't saved this format before */ MwSaveFormats(fp, f); used_fmt[f] = 1; } switch (ret_type(buf, s, i, j)) { case EMPTY: if (f != sf) fprintf(fp, "%d %d %d\t#\n", i, j, f); break; case LABEL: fprintf(fp, "%d %d %d\t\"%s\n", i, j, f, ret_text(buf, s, i, j)); break; case EMBED: fprintf(fp, "%d %d %d\tm%s\n", i, j, f, ret_text(buf, s, i, j)); break; case MNUMBER: case MTEXT: break; /* not stored at all */ /* ERRORs and CONSTANTs are stored as EXPRESSION */ default: /* EXPRESSION */ /* Special case SIOD and C for backward compatibility. */ intp = ret_interpreter(buf, s, i, j); taddr = ret_text(buf, s, i, j); if (intp == siod_interpreter && taddr[0] == '=') { intp = C_interpreter; taddr++; } if (intp == siod_interpreter) { fprintf(fp, "%d %d %d\t=%s\n", i, j, f, taddr); } else if (intp == C_interpreter) { fprintf(fp, "%d %d %d\t==%s\n", i, j, f, taddr); } else { fprintf(fp, "%d %d %d\t+%s,%s\n", i, j, f, interpreter2name(intp), taddr); } } } } } fprintf(fp, "# End of file %s\n", fn); fclose(fp); MwFree(used_fmt); MwFree(used_style); return 0; } /* save_flat */
EXPORT void read_rectangular_grid( const IO_TYPE *io_type, RECT_GRID *gr, boolean bufzones, REMAP *remap) { FILE *file = io_type->file; char Line[2048]; char ss[120]; long offset; int i, c; int dim; int maxd = MAXD, size_float = FLOAT; boolean b_iput; int status; char *string; if (gr == NULL) { (void) printf("WARNING in read_rectangular_grid(), grid is null\n"); return; } zero_scalar(gr,sizeof(RECT_GRID)); gr->Remap = *remap; if (fgetstring(file,"Grid Dimension = ") == FUNCTION_FAILED) { gr->dim = 2; } else { status = fscanf(file,"%d",&dim); while ((c = getc(file)) != '\n'); if ((c = getc(file)) == '\f') /* Binary input */ { c = getc(file); if (c == 1) /*oldstyle printout*/ { size_t nbytes; (void) printf("WARNING in read_rectangular_grid(), " "old style binary IO only valid for\n" "reading from runs with same floating " "point precision and endian as output\n"); nbytes = fread((void *)gr,sizeof(RECT_GRID),1,file); return; } if (c != 0) { screen("ERROR in read_rectangular_grid(), " "Improper output format\n"); clean_up(ERROR); return; } b_iput = YES; status = fscanf(file,"%*s%*s%d%*s%*s%d",&maxd,&size_float); (void) getc(file); /* newline */ } else { b_iput = NO; (void) ungetc(c,file); } } gr->dim = dim; (void) sprintf(ss,"%10s = ",remap->Dnm[0]); if (fgetstring(file,ss) == FUNCTION_FAILED) { (void) printf("WARNING in read_rectangular_grid(), " "grid not found\n"); return; } string = fgets(Line,2046,file); /* clear end of X,Y line */ #define read_grid_float(x) \ if (b_iput) \ { \ (void) getc(file); /* get blank */ \ (void) read_binary_real_array(x,1,io_type); \ } \ else \ (void) fscan_float(file,(x)) /* Read grid endpoints */ for (i = 0; i < dim; ++i) { status = fscanf(file,"%*s%*s"); read_grid_float(gr->L+i); status = fscanf(file,"%*s%*s"); read_grid_float(gr->U+i); } /* Read grid spacings */ for (i = 0; i < dim; ++i) { status = fscanf(file,"%*s%*s"); read_grid_float(gr->h+i); status = fscanf(file,"%*s%*s%d",gr->gmax+i); } offset = ftell(file); (void) sprintf(ss," G%sL = ",remap->Dnm[0]); if (fgetstring(file,ss) == FUNCTION_FAILED) { for (i = 0; i < dim; ++i) { if (bufzones == YES) { gr->lbuf[i] = gr->ubuf[i] = 1; gr->GL[i] = gr->L[i]; gr->GU[i] = gr->U[i]; gr->VL[i] = gr->L[i] - cell_width(0,i,gr); gr->VU[i] = gr->U[i] + cell_width(gr->gmax[i]-1,i,gr); } else { gr->lbuf[i] = gr->ubuf[i] = 0; gr->GL[i] = gr->L[i]; gr->GU[i] = gr->U[i]; gr->VL[i] = gr->L[i]; gr->VU[i] = gr->U[i]; } } return; } (void) fseek(file,offset,SEEK_SET); /* Read global grid endpoints */ for (i = 0; i < dim; ++i) { status = fscanf(file,"%*s%*s"); read_grid_float(gr->GL+i); status = fscanf(file,"%*s%*s"); read_grid_float(gr->GU+i); } /* Read virtual domain endpoints */ for (i = 0; i < dim; ++i) { status = fscanf(file,"%*s%*s"); read_grid_float(gr->VL+i); status = fscanf(file,"%*s%*s"); read_grid_float(gr->VU+i); } /* Read buffer zone widths */ for (i = 0; i < dim; ++i) status = fscanf(file,"%*s%*s%d%*s%*s%d",gr->lbuf+i,gr->ubuf+i); set_rect_grid(gr->L,gr->U,gr->GL,gr->GU,gr->lbuf,gr->ubuf,gr->gmax, dim,&gr->Remap,gr); #undef read_grid_float return; } /*end read_rectangular_grid*/