home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Fish 'n' More 2
/
fishmore-publicdomainlibraryvol.ii1991xetec.iso
/
disks
/
disk373.lzh
/
Multiplot
/
source
/
mplot_src
/
src.zoo
/
old_getdat.c
< prev
next >
Wrap
C/C++ Source or Header
|
1990-08-02
|
12KB
|
344 lines
#include <stdio.h>
#include <ctype.h>
#include <exec/types.h>
#include <exec/memory.h>
#include <intuition/intuition.h>
#include "mp.h"
#include "struct.h"
#define MAXCOLUMNS 10
extern int debug;
extern int MAXHORIZ;
extern int MAXVERT;
extern char *getwrd();
extern void trch();
extern int same(), numeric();
extern struct Window *FrontWindow;
extern struct NewScreen newscreen;
extern struct Screen *screen;
struct Plot *GetStructPlot();
/***************************************************************************
* GetDat finds numeric data in a text file and stores it in arrays.
*
* GetDat reads a text file whose handle is pointed to by 'fp'. In
* this file are expected to be lines containing "words" [of arbitrary
* length, delimited by blanks, tabs, or commas.] In each line, the first
* word will be pointed to by 'wp[0]'; the user thinks of this
* word as being in column 1. 'wp[1]' will point to the next word,
* etc. GetDat is primarily interested in those words which look like
* numbers.
*
* The user has assigned column numbers to the variables x, y, and e.
* She wants GetDat to find all the lines which contain numbers in those
* columns and save the numbers in the arrays 'x[]', 'y[]', 'e[]'.
* Further, successive lines which contain numbers in the columns of
* interest are to be treated as a list [possibly delimited by lines
* which don't contain numbers in the columns of interest.]
* Finally, the number of lists and the numbers of lines
* in each list are to be saved in the data structure pointed to by the
* argument 'Pict'.
*
* Special handling: If the column associated with 'x[]' is given as 0,
* the 'x[]' array is to be filled as if the x-columns of each list
* contained [0,1,2...]. If the column associated with 'e[]' is 0, the
* 'e[]' array is not to be filled at all.
*
* Lines which begin with "*SCALFACT*" contain scale factors by which
* collected data is to be multiplied. Scale factor columns map to data
* columns after the keyword "*SCALFACT*" is discarded.
*
* Lines which begin with "*TITLE*", "*XLABEL*", and "*YLABEL*" are
* interpreted as text to be stored in the structure pointed to by
* 'Pict'. The keyword "*...*" and the blanks, tabs, or commas following
* it are discarded.
*
* Lines which begin with *LEGEND* should contain
* following text which will be a label for the following set.
*
*************************************/
#define COLUMN_OK(z) (z<0 ? TRUE : numeric(wp[z]))
#define LINE_IS_DATA (COLUMN_OK(xcol) && COLUMN_OK(ycol) && COLUMN_OK(ecol))
#define DATA(z) (fact[(z)]*atoFFP(wp[(z)]))
#define PLOT_OPENED (Plot->NPts && !PlotClosed)
FFP fact[MAXCOLUMNS]; /* SCALE FACTORS */
short maxcol; /* LARGEST OF ARGS: XCOL, YCOL, ECOL */
short xcoord;
short PlotClosed, Continued;
short SCRIPT_ON;
/********************************************/
void GetDat(fp, xcol, ycol, ecol, Pict)
FILE *fp;
int xcol, ycol, ecol; /* column numbers to be inspected */
struct Pict *Pict; /* contains info for plotting this data */
{
short i;
char cbuf[100], *cb, *wp[MAXCOLUMNS];
struct Plot *Plot, *PrevPlot;
FFP *x, *y, *e;
/*** INITIALIZE ***/
PlotClosed = FALSE; Continued = FALSE; xcoord = 0;
SCRIPT_ON=FALSE;
/* TRANSLATE from user column numbers to array indices, FIND max */
xcol--; ycol--; ecol--; maxcol = max(xcol,max(ycol,ecol));
for (i=0; i<=maxcol; i++) fact[i] = 1.; /* init scale factors */
Pict->NPlt = 0;
Pict->Plot = GetStructPlot();
Plot = Pict->Plot;
x = Plot->x; y = Plot->y; e = Plot->e;
/*** GET DATA ***/
/* FIND list(s) of numbers in file, GET numbers in x,y(,e) arrays */
while (cb = fgets(cbuf,100,fp)) {
trch('\n',0,cb); trch('\t',' ',cbuf); trch(',',' ',cbuf); trch('\r',' ',cbuf);
if (KeyWord(fp,cb,Pict,Plot)) ; /* Do nothing else */
else if (same(cb,"*LEGEND*",8))
{
if (PlotClosed) {
/* allocate, link, SET UP FOR NEW PLOT */
if (!(Plot->NextPlot = GetStructPlot()))
{
ErrorAlert(0);
FreeMemory(Pict); /*** DEALLOCATE MEMORY ***/
CloseScreen(screen);
sexit(FALSE);
}
else Plot = Plot->NextPlot;
x = Plot->x; y = Plot->y; e = Plot->e;
PlotClosed = FALSE; xcoord = 0;
}
cb+=8; while(*cb==' ') cb++; stccpy(Plot->Legend->String,cb,79);
Plot->Legend->x = Plot->Legend->y = NULL;
}
else
{
for (i=0; i<=maxcol; i++) wp[i] = getwrd(&cb);
if (LINE_IS_DATA) {
if (PlotClosed) {
/* allocate, link, SET UP FOR NEW PLOT */
if (!(Plot->NextPlot = GetStructPlot()))
{
ErrorAlert(0);
FreeMemory(Pict); /*** DEALLOCATE MEMORY ***/
CloseScreen(screen);
sexit(FALSE);
}
else Plot = Plot->NextPlot;
x = Plot->x; y = Plot->y; e = Plot->e;
PlotClosed = FALSE; xcoord = 0;
}
else if (Plot->NPts == MAXPOINTS) {
/* allocate, link, SET UP FOR CONTINUATION OF CURRENT PLOT */
Continued = TRUE; PrevPlot = Plot;
if (!(Plot->NextPlot = GetStructPlot()))
{
ErrorAlert(0);
FreeMemory(Pict); /*** DEALLOCATE MEMORY ***/
CloseScreen(screen);
sexit(FALSE);
}
else Plot = Plot->NextPlot;
x = Plot->x; y = Plot->y; e = Plot->e;
}
else if (Continued) {PrevPlot->Continued = TRUE; Continued = FALSE;}
/*** PAYLOAD.. STORE data in arrays, COUNT points */
*(x++) = ( xcol>=0 ? DATA(xcol) : (FFP)(xcoord++));
*(y++) = DATA(ycol);
if (ecol>=0) *(e++) = DATA(ecol);
Plot->NPts++;
}
else {
/* line does not contain plottable data */
if (PLOT_OPENED) {
Pict->NPlt++;
PlotClosed = TRUE;
}
}
}
}
if (PLOT_OPENED) Pict->NPlt++;
}
int CheckDat(fp, xcol, ycol, ecol)
FILE *fp;
int xcol, ycol, ecol; /* column numbers to be inspected */
{
char cbuf[100];
int i=0;
maxcol = max(xcol,max(ycol,ecol));
while ( fgets(cbuf,99,fp) && ( (i==0) || (i>=maxcol) ) )
{
strcat(cbuf,'\0');
i=countwords(&cbuf);
}
if ( (i<maxcol) && (i!=0) )
{
return(FALSE);
}
return(TRUE);
}
countwords(s)
char *s;
{
int x=0;
while ((*s!='\n')&&(*s !='\0')&&(!isdigit(*s)))
{
if (isalpha(*s)) return(0);
s++;
}
while ((*s !='\n')&&(*s !='\0'))
{
if (isdigit(*s))
{
x++;
while (isalnum(*s)||ispunct(*s)) s++;
}
else s++;
}
return(x);
}
extern short firstcall;
extern struct TextBox *ExtraText;
struct Remember *Key=NULL;
/******************/
int KeyWord(fp,cb,Pict,Plot,x,y,e)
FILE *fp;
char *cb;
struct Pict *Pict;
struct Plot *Plot;
FFP *x, *y, *e;
{
short i, tempLegX, tempLegY;
double tempXmin, tempXmax, tempYmin, tempYmax;
char cbuf[100];
struct TextBox *TempText;
if (same(cb,"*",1)) {
if (same(cb,"*SCALFACT*",10)) {
(void)getwrd(&cb);
for (i=0; i<=maxcol; i++) {
fact[i] = atoFFP(getwrd(&cb));
}
return (TRUE);
}
else if (same(cb,"*TITLE*",7))
{ cb+=7; while (*cb==' ') cb++; stccpy(Pict->Title->String,cb,79); return (TRUE); }
else if (same(cb,"*XLABEL*",8))
{ cb+=8; while(*cb==' ') cb++; stccpy(Pict->XLabel->String,cb,79); return (TRUE); }
else if (same(cb,"*YLABEL*",8))
{ cb+=8; while(*cb==' ') cb++; stccpy(Pict->YLabel->String,cb,79); return (TRUE); }
else if (same(cb,"*AUTOSCRIPT*",12))
{
getwrd(&cb);
if (SCRIPT_ON)
{
sscanf(cb,"%hd %hd %hd %hd %hd %hd %hd %hd",
&(Plot->Color),&(Plot->PlotType),&(Plot->Lines),&(Plot->PointSize),&(Plot->PointType),&(Plot->Enabled),&tempLegX,&(Plot->Legend->y));
Plot->Legend->x=MAXHORIZ-tempLegX;
}
else
{
sscanf(cb,"%hd %hd %hd %hd %hd %hd %lf %lf %lf %lf",
&(Pict->ErrBar),&(Pict->ShowErr),&(Pict->Grid),&(Pict->Tics->NX),
&(Pict->Tics->NY),&(Pict->RMargin), &tempXmin, &tempXmax, &tempYmin, &tempYmax);
Pict->CurrReg->XMin=(FFP)tempXmin; Pict->CurrReg->XMax=(FFP)tempXmax;
Pict->CurrReg->YMin=(FFP)tempYmin; Pict->CurrReg->YMax=(FFP)tempYmax;
SCRIPT_ON=TRUE;
}
return(TRUE);
}
else if (same(cb,"*EXTRATEXT*",11)) {
while (cb = fgets(cbuf,100,fp)) {
trch('\n',0,cb); trch('\t',' ',cbuf); trch(',',' ',cbuf); trch('\r',' ',cbuf);
if (ExtraText)
{
for (TempText=ExtraText;TempText->NextText;TempText=TempText->NextText);
if (TempText->NextText=(struct TextBox *)AllocRemember(&Key,(sizeof(struct TextBox)),MEMF_CLEAR))
{
TempText=TempText->NextText;
if (sscanf(cb,"%hd %hd",&tempLegX,&tempLegY))
{
TempText->x=MAXHORIZ-tempLegX;
TempText->y=MAXVERT-tempLegY;
getwrd(&cb); getwrd(&cb); while(*cb==' ') cb++;
}
stccpy(TempText->String,cb,79);
TempText->NextText=NULL;
}
else Message(" Not enough memory for text");
}
else
{
if (ExtraText=(struct TextBox *) AllocRemember(&Key,(sizeof(struct TextBox)),MEMF_CLEAR))
{
if (sscanf(cb,"%hd %hd",&tempLegX,&tempLegY))
{
ExtraText->x=MAXHORIZ-tempLegX;
ExtraText->y=MAXVERT-tempLegY;
getwrd(&cb); getwrd(&cb); while (*cb==' ') cb++;
}
stccpy(ExtraText->String,cb,79);
ExtraText->NextText=NULL;
}
else Message(" Not enough memory for text");
}
}
return(TRUE);
}
}
return (FALSE);
}
/*************************************************************************/
struct Plot *GetStructPlot()
{
struct Plot *P;
if( (!(P = (struct Plot *)AllocRemember(&Key,sizeof(struct Plot),MEMF_CLEAR)))
|| (!(P->x = (FFP *)AllocRemember(&Key,MAXPOINTS * sizeof(FFP),MEMF_CLEAR)))
|| (!(P->y = (FFP *)AllocRemember(&Key,MAXPOINTS * sizeof(FFP),MEMF_CLEAR)))
|| (!(P->e = (FFP *)AllocRemember(&Key,MAXPOINTS * sizeof(FFP),MEMF_CLEAR)))
|| (!(P->xp = (short *)AllocRemember(&Key,MAXPOINTS * sizeof(short),MEMF_CLEAR)))
|| (!(P->yp = (short *)AllocRemember(&Key,MAXPOINTS * sizeof(short),MEMF_CLEAR)))
|| (!(P->ep = (short *)AllocRemember(&Key,MAXPOINTS * sizeof(short),MEMF_CLEAR)))
|| (!(P->Legend = (struct TextBox *)AllocRemember(&Key,sizeof(struct TextBox),MEMF_CLEAR)))
|| (!(P->Reg = (struct PlotRegion *) AllocRemember(&Key,sizeof(struct PlotRegion),MEMF_CLEAR))) )
return(NULL);
else return(P);
}
/*******************/
void FreeStructPlot()
{
FreeRemember(&Key,TRUE);
ExtraText=NULL;
}