#include #include #include #include #include "ged.h" #include "embed_ops.h" extern char *mkstemp (); extern int access (); /* input file formatted correctly */ static int ok_format = TRUE; static char *lbuf; void cgowrite ( char * filnam ) /* Purpose: CGOWRITE writes a CGO to a file. */ { FILE *fp; point *pptr; cpoint *cptr; char buf[STR_MAX]; int i; if ((fp = fopen(filnam, "w")) == NULL) { msg (sprintf (buf, "%s: no such file", filnam)); return; }; for (i = 0; i < GSQRD ; i++) { pptr = hash[i]; while (pptr->nextp != NULL) { pptr = pptr->nextp; fprintf(fp, "%-5u ", pptr->name); fprintf(fp, "%-.3f ", (float) pptr->X / WXMAX); fprintf(fp, "%-.3f ", (float) pptr->Y / WYMAX); cptr = pptr->cplist; while (cptr->nextcp != NULL) { cptr = cptr->nextcp; if (cptr->nextcp != NULL) fprintf(fp, "%-5u ", cptr->pname); else fprintf(fp, "%-5u", cptr->pname); } fprintf(fp, "\n"); } } if (fclose(fp) == EOF) { msg (sprintf (buf, "%s: no such file", filnam)); return; } sprintf(buf, "sort -n +0 %s -o %s", filnam, filnam); system(buf); } void cgo_ww ( char *filnam ) /* Purpose: CGO_WW writes a CGO to a file with # in the beginning. */ { FILE *fp; point *pptr; cpoint *cptr; char buf[STR_MAX]; int i; if ((fp = fopen(filnam, "w")) == NULL) { msg (sprintf (buf, "%s: no such file", filnam)); return; }; for (i = 0; i < GSQRD ; i++) { pptr = hash[i]; while (pptr->nextp != NULL) { pptr = pptr->nextp; fprintf(fp, "%-5u ", pptr->name); fprintf(fp, "%-.3f ", (float) pptr->X / WXMAX); fprintf(fp, "%-.3f ", (float) pptr->Y / WYMAX); cptr = pptr->cplist; while (cptr->nextcp != NULL) { cptr = cptr->nextcp; if (cptr->nextcp != NULL) fprintf(fp, "%-5u ", cptr->pname); else fprintf(fp, "%-5u", cptr->pname); } fprintf(fp, "\n"); } } fprintf(fp,"#"); fprintf(fp, "\n"); if (fclose(fp) == EOF) { msg (sprintf (buf, "%s: no such file", filnam)); return; } sprintf(buf, "sort -n +0 %s -o %s", filnam, filnam); system(buf); } /*--------------------------------------------------------------------------- * read -- reads a CGO into the hash table * CGO format is 'v x y v1 v2 v3 ...' *--------------------------------------------------------------------------- */ void cgoread ( char *filnam ) { int read_int (), read_flt (); FILE *fp; point *pptr; cpoint *cptr; char xbuf[STR_MAX], buf[STR_MAX]; int vname, xco, yco; int one, nextgraph; ok_format = TRUE; one = 0; nextgraph = 0; if ((fp = fopen(filnam, "r")) == NULL) { msg (sprintf (buf, "%s: no such file", filnam)); return; } while(one != 1) { deleteall (); while (fgets(xbuf, STR_MAX, fp) != NULL) { if (nextgraph == 1) { deleteallinavr (); nextgraph = 0; } if(xbuf[0] != '#') { lbuf = xbuf; if (lbuf[strlen(lbuf) - 1] != '\n') { msg (sprintf (buf, "%s: input line too long", filnam)); deleteall (); if (fclose(fp) == EOF) { msg (sprintf (buf,"%s: no such file", filnam)); } return; } else { if (!ok_format || (read_int(&vname) == EOLN)) { msg (sprintf (buf,"%s: format error", filnam)); deleteall (); if (fclose(fp) == EOF) msg (sprintf (buf,"%s: no such file", filnam)); return; } if (!ok_format || (read_flt(&xco, TRUE) == EOLN)) { msg (sprintf (buf,"%s: format error", filnam)); deleteall (); if (fclose(fp) == EOF) msg (sprintf (buf,"%s: no such file", filnam)); return; } if (!ok_format || (read_flt(&yco, FALSE) == EOLN)) { msg (sprintf (buf,"%s: format error", filnam)); deleteall (); if (fclose(fp) == EOF) msg (sprintf (buf,"%s: no such file", filnam)); return; } pptr = insertp(hash, vname, xco, yco); cptr = pptr->cplist; while (ok_format && (read_int(&vname) != EOLN)) insertc(cptr, vname); if (!ok_format) { msg (sprintf (buf,"%s: format error", filnam)); deleteall (); if (fclose(fp) == EOF) msg (sprintf (buf,"%s: no such file", filnam)); return; } } } else { drawgraph (); nextgraph = 1; } } drawgraph (); one = 1; } /* collapse (hash); */ if (fclose(fp) == EOF) msg (sprintf (buf,"%s: no such file", filnam)); } static int read_int ( int *num ) /* Purpose: READ_INT reads an unsigned integer. */ { int skipws (), atoi (); int i = 0; char tbuf[STR_MAX]; if (skipws () == EOLN) return (EOLN); if (ok_format) { while (!isspace (*lbuf)) { if (isdigit (*lbuf)) tbuf[i++] = *lbuf; else { ok_format = FALSE; return (GEDERR); } lbuf++; } tbuf[i] = '\0'; *num = atoi (tbuf); } return(OK); } /*--------------------------------------------------------------------------- * read_flt -- reads a float *--------------------------------------------------------------------------- */ static int read_flt ( int *num, int getxco ) { int skipws (); double atof (); int i = 0; char tbuf[STR_MAX]; int dec_pt_seen = FALSE; if (skipws () == EOLN) return (EOLN); if (ok_format) { while (!isspace (*lbuf)) { if (isdigit (*lbuf)) tbuf[i++] = *lbuf; else if (*lbuf == '.') { if (!dec_pt_seen) { dec_pt_seen = TRUE; tbuf[i++] = *lbuf; } else { ok_format = FALSE; return (GEDERR); } } else { ok_format = FALSE; return (GEDERR); } lbuf++; } tbuf[i] = '\0'; if (getxco) *num = (int) (((float) atof(tbuf)) * WXMAX); else *num = (int) (((float) atof(tbuf)) * WYMAX); } return (OK); } /*--------------------------------------------------------------------------- * skipws -- skips white space *--------------------------------------------------------------------------- */ static int skipws ( void ) { while (isspace (*lbuf)) { if (*lbuf == '\n') return (EOLN); lbuf++; } if (!isdigit (*lbuf)) /* if digit - fine */ if (*lbuf != '.') /* '.' only char allowed */ ok_format = FALSE; return (OK); } /*--------------------------------------------------------------------------- * cgoread_app -- reads a CGO from the specified file, appending it * to the current contents of the workspace * CGO format is 'v x y v1 v2 v3 ...' *--------------------------------------------------------------------------- */ void cgoread_app ( char *filnam ) { void merge_tbls (); FILE *fp; point *pptr, *tptr, *thash[GRID * GRID]; cpoint *cptr; char xbuf[STR_MAX], buf[STR_MAX]; int vname, xco, yco; ok_format = TRUE; if ((fp = fopen(filnam, "r")) == NULL) { msg (sprintf (buf, "%s: no such file", filnam)); return; } ds_init (thash); while (fgets (xbuf, STR_MAX, fp) != NULL) { lbuf = xbuf; if (lbuf[strlen(lbuf) - 1] != '\n') { msg (sprintf (buf, "%s: input line too long", filnam)); if (fclose(fp) == EOF) msg (sprintf (buf, "%s: no such file", filnam)); return; } else { if (!ok_format || (read_int(&vname) == EOLN)) { msg (sprintf (buf, "%s: format error", filnam)); if (fclose(fp) == EOF) msg (sprintf (buf, "%s: no such file", filnam)); return; } if (!ok_format || (read_flt(&xco, TRUE) == EOLN)) { msg (sprintf (buf, "%s: format error", filnam)); if (fclose(fp) == EOF) msg (sprintf (buf, "%s: no such file", filnam)); return; } if (!ok_format || (read_flt(&yco, FALSE) == EOLN)) { msg (sprintf (buf, "%s: format error", filnam)); if (fclose(fp) == EOF) msg (sprintf (buf, "%s: no such file", filnam)); return; } tptr = insertp (thash, vname, xco, yco); cptr = tptr->cplist; while (ok_format && (read_int(&vname) != EOLN)) insertc (cptr, vname); if (!ok_format) { msg (sprintf (buf, "%s: format error", filnam)); if (fclose(fp) == EOF) msg (sprintf (buf, "%s: no such file", filnam)); return; } } } merge_tbls (hash, thash); if (fclose(fp) == EOF) msg (sprintf (buf, "%s: no such file", filnam)); } /*--------------------------------------------------------------------------- * make_names_unique -- ensures that the name spaces of add_hash and * orig_hash are unique *--------------------------------------------------------------------------- */ static void make_names_unique ( point *add_hash[], point *orig_hash[] ) { int i, tname; point *pptra; for (i = 0; i < GSQRD; i++) { /* make names unique */ pptra = add_hash[i]; while (pptra->nextp != NULL) { pptra = pptra->nextp; if (name_exists (orig_hash, pptra->name)) { tname = getname (); change_clists (add_hash, pptra->name, tname); pptra->name = tname; } } } } /*--------------------------------------------------------------------------- * merge_points -- points in new_hash that have coincident points in * orig_hash, are replaced by a point with the exact * attributes of the point in orig_hash. *--------------------------------------------------------------------------- */ static void merge_points ( point *orig_hash[], point *new_hash[] ) { int i, new_name; point *new_point, *close; for (i = 0; i < GSQRD; i++) { new_point = new_hash[i]; while (new_point->nextp != NULL) { new_point = new_point->nextp; if ((close = closest(orig_hash, new_point->X, new_point->Y)) != NULL) if (coincident (new_point, close)) { new_name = new_point->name; transform (new_hash, &new_point, close); change_clists (new_hash, new_name, close->name); } } } } /*--------------------------------------------------------------------------- * combine -- The tables passed as parameters, each has a set of * connected points, in which some points in both tables * may exactly correspond. This routine combines these * tables into one, leaving the result in orig_hash. *--------------------------------------------------------------------------- */ void combine ( point *orig_hash[], point *new_hash[] ) { int i; point *new_point; for (i = 0; i < GSQRD; i++) { new_point = new_hash[i]; while (new_point->nextp != NULL) { new_point = new_point->nextp; remove_point (new_hash, new_point); add_to_graph (orig_hash, new_point); } } } /*--------------------------------------------------------------------------- * merge_tbls -- merges CGO in the workspace with that just read in * ensure all names in new table are aliased (if necessary) *--------------------------------------------------------------------------- */ static void merge_tbls ( point *orig_hash[], point *add_hash[] ) { void make_names_unique (), merge_points (); int i, j; int tname = 1; point *pptro, *pptra, *pptr, *tptr; cpoint *cptr, *cptra; make_names_unique (add_hash, orig_hash); merge_points (orig_hash, add_hash); combine (orig_hash, add_hash); } /*--------------------------------------------------------------------------- * filter -- filters a CGO using the filter in the file "filter" *--------------------------------------------------------------------------- */ void filter ( char *filnam ) { void cgoread(), cgowrite(); char buf[STR_MAX]; char tifname[STR_MAX], tofname[STR_MAX], start[STR_MAX]; char *lbuf; int sign, i;; i = 0; sign = 0; lbuf = filnam; while (*lbuf != '\0') { if (*lbuf == '>') sign = 1; if (*lbuf == '<') sign = 2; if (sign != 0) start[i] = '\0'; else start[i++] = *lbuf; lbuf++; } if (sign == 0) { if (strcpy (tifname, mkstemp ("/tmp/gedinXXXXXX")) != NULL) cgowrite (tifname); if (strcpy (tofname, mkstemp ("/tmp/gedoutXXXXXX")) != NULL) { (void) sprintf (buf, "( %s ) < %s > %s", filnam, tifname, tofname); system (buf); cgoread (tofname); } (void) sprintf (buf, "rm -f %s %s", tifname, tofname); (void) system (buf); } if (sign == 1) { if (strcpy (tifname, mkstemp ("/tmp/gedinXXXXXX")) != NULL) cgo_ww (tifname); /* sscanf(filnam, "%s", start); */ (void) sprintf (buf, "( %s ) < %s %s", start, tifname, strchr(filnam, '>')); if(system (buf) != 0) msg(sprintf (buf, "%s: could not execute",filnam)); (void) sprintf (buf, "rm -f %s", tifname); (void) system (buf); } if ( sign == 2 ) { printf("--- the case of < ---- \n"); } }