home *** CD-ROM | disk | FTP | other *** search
-
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- #include <string.h>
- #include <ctype.h>
- #include "cp.h"
-
- #define BLOCK_SIZE 512L /* Size for fread() */
- #define MAP_SIZE 1024L /* Up to 255 channels */
-
- /* function prototypes */
-
- float Get_Version(void);
- void Get_Creator(void);
- long Do_Map(short,char *,long *);
- void Get_Title(char *,char *);
- double Calc_Value(double,double,double,long);
-
- /* Global Data */
-
- struct EasyStruct ES =
- {
- sizeof(struct EasyStruct),
- 0,
- "rhp()",
- "%s",
- "Oh NO!"
- };
-
-
- char form[6],cpl[10],shot[30],keyword[10],origin[10];
- static char Creator[2][20];
- FILE *ifp, *fopen();
-
- BOOL rhp_Args( UBYTE *fn, UBYTE *Args)
- {
- UBYTE *tp = "CHAN/N/M";
- LONG ch_array[ 1 ] = { 0L };
- LONG len;
- UBYTE *ch_txt;
- struct RDArgs *ch_args = NULL;
- LONG **chan;
- WORD j = 0;
-
- len = strlen( Args);
-
- if (( ch_txt = AllocVec( max((len + 1),255), MEMF_CLEAR )))
- {
- strcpy( ch_txt, Args);
-
- if ( len > 0 )
- {
- ch_txt[len] = '\n';
- ch_txt[++len] = '\0';
-
- if ((ch_args = (struct RDArgs *)AllocDosObject(DOS_RDARGS, TAG_DONE)))
- {
- ch_args->RDA_Source.CS_Buffer = ch_txt;
- ch_args->RDA_Source.CS_Length = len;
- ch_args->RDA_Source.CS_CurChr = 0L;
-
- if( ReadArgs( tp, (LONG *)&ch_array[ 0 ], ch_args ))
- {
- chan = ( LONG ** ) ch_array[ 0 ];
- while ( chan[j] )
- {
- rhp( fn , *chan[j]);
- j++;
- }
- }
- else
- {
- EasyRequest (NULL,&ES,NULL,"Invalid Binary Arg String\n Must be Integers");
- }
- FreeArgs( ch_args );
- FreeDosObject(DOS_RDARGS, ch_args);
- }
- }
- FreeVec( ch_txt );
- }
- return (TRUE);
- }
- BOOL rhp(UBYTE*fname,LONG channel)
- {
- float version;
- double xinc,xref,xorg,yinc,yref,yorg;
- char Title[100]="NOTITLE",SubTitle[100]="NOSUBTITLE";
- unsigned char *pr;
- long c,j,k,pnts,len,nex,fpos,mode=0;
-
- struct Set *NewSet;
-
- struct Point *ThisPoint;
- struct Point *LastPoint = NULL;
- struct Point *FirstPoint;
-
- if (( ifp = fopen(fname,"rb"))==NULL) {
- EasyRequest (NULL,&ES,NULL,"Cant open binary file");
- Death(25);
- }
-
-
- version = Get_Version(); /* get binary version */
-
- if (version > 3.0) Get_Creator();
-
- if (version >= 2.31) {
- if ( version >= 3.20 ) {
- fscanf(ifp,"%5s",form);
- }
- Get_Title(Title,"TITLE");
- }
-
- /* Call Mapping Routine and map file if map is not found */
-
- fpos = Do_Map((short)channel,fname,&nex);
- if (fpos<0) {
- if (ifp) fclose(ifp);
- EasyRequest (NULL,&ES,NULL,"Scope Failed this time");
- return (FALSE);
- }
-
- fseek(ifp,fpos,mode);
-
- /* get format origin subtitle string */
-
- if (version>=3.20) {
- fscanf(ifp,"%5s",form);
- if(version>=3.29) fscanf(ifp,"%5s",origin);
-
- Get_Title(SubTitle,"SUBTITLE");
-
- }
-
- fscanf(ifp,"%9s",cpl);
-
- if (fscanf(ifp,"%lf%lf%lf%lf%lf%lf",&xinc,&xorg,&xref,&yinc,&yorg,&yref)!=6) {
- if (ifp) fclose(ifp);
- EasyRequest (NULL,&ES,NULL,"Format wrong in headers");
- return(45);
- }
-
- c = getc(ifp);
- if (c==13&&version<2.0) getc(ifp); /* swallow BS from older versions */
-
- len = ftell(ifp);
- len = labs(nex) - len; /* abs in case next was FAIL */
-
- if (version<3.20 || (strcmp(form,"BYTE")==0)) {
- if (len>8192 && version<3.20)pnts=8192;
- else pnts=len;
- j=255;
- }
- else if ((strcmp(form,"WORD")==0)&&(strcmp(&Creator[2][0],"DES4")==0)) {
- pnts=len/2;
- j=255;
- }
- else {
- pnts=len/2;
- j=32768;
- }
-
- /* Allocate memory for binary and read it in */
-
- pr = malloc((unsigned)len+BLOCK_SIZE);
- if (pr==NULL) {
- EasyRequest (NULL,&ES,NULL,"No More RAM");
- if (ifp) fclose(ifp);
- return(20);
- }
-
- fread(pr,BLOCK_SIZE,len/BLOCK_SIZE+1,ifp);
-
- if (ifp) fclose(ifp);
-
- NewSet = (struct Set *)AllocVec( sizeof( struct Set ), MEMF_CLEAR );
-
-
- if ( NewSet == NULL )
- {
- EasyRequest (NULL,&ES,NULL,"No More RAM");
- return ( FALSE );
- }
-
- NewSet-> snode.ln_Name = NewSet-> fn;
- NewSet-> snode.ln_Type = NT_DATA;
- NewSet-> snode.ln_Pri = 0;
-
- FirstPoint = AllocVec( sizeof( struct Point ), NULL );
- if( FirstPoint == NULL )
- {
- EasyRequest (NULL,&ES,NULL,"Out of Memory");
- FreeVec( NewSet );
- return ( FALSE );
- }
-
- NewSet-> FirstPoint = FirstPoint;
- ThisPoint = FirstPoint;
-
- if (version<3.20 || (strcmp(form,"BYTE")==0)) {
- for (k=0;k<pnts&&k<points;k+=thin) {
- c=pr[k];
-
- ThisPoint->yval = (float) Calc_Value(yinc,yorg,yref,c);
- ThisPoint->xval = (float) Calc_Value(xinc,xorg,xref,k);
-
- ThisPoint->NextPoint = AllocVec( sizeof( struct Point ), NULL );
- if( ThisPoint->NextPoint == NULL )
- {
- EasyRequest (NULL,&ES,NULL,"Out of Memory");
- FreePoints( FirstPoint );
- FreeVec( NewSet );
- free((char*) (pr));
- return ( FALSE );
- }
- if (LastPoint!=NULL)
- {
- NewSet-> xmax = max( NewSet-> xmax, ThisPoint->xval);
- NewSet-> xmin = min( NewSet-> xmin, ThisPoint->xval);
- NewSet-> ymax = max( NewSet-> ymax, ThisPoint->yval);
- NewSet-> ymin = min( NewSet-> ymin, ThisPoint->yval);
- }
- else
- {
- NewSet-> xmax = ThisPoint->xval;
- NewSet-> xmin = ThisPoint->xval;
- NewSet-> ymax = ThisPoint->yval;
- NewSet-> ymin = ThisPoint->yval;
- }
-
- LastPoint = ThisPoint;
- ThisPoint = ThisPoint->NextPoint;
-
- }
- }
- else if(strcmp(form,"WORD")==0) {
- for (k=0;k<pnts&&k<points;k+=thin) {
- c = ((pr[2*k]<<8)|pr[2*k+1]);
- /**/
- ThisPoint->yval = (float) Calc_Value(yinc,yorg,yref,c);
- ThisPoint->xval = (float) Calc_Value(xinc,xorg,xref,k);
-
- ThisPoint->NextPoint = AllocVec( sizeof( struct Point ), NULL );
- if( ThisPoint->NextPoint == NULL )
- {
- EasyRequest (NULL,&ES,NULL,"Out of Memory");
- FreePoints( FirstPoint );
- FreeVec( NewSet );
- free((char*) (pr));
- return ( FALSE );
- }
- if (LastPoint!=NULL)
- {
- NewSet-> xmax = max( NewSet-> xmax, ThisPoint->xval);
- NewSet-> xmin = min( NewSet-> xmin, ThisPoint->xval);
- NewSet-> ymax = max( NewSet-> ymax, ThisPoint->yval);
- NewSet-> ymin = min( NewSet-> ymin, ThisPoint->yval);
- }
- else
- {
- NewSet-> xmax = ThisPoint->xval;
- NewSet-> xmin = ThisPoint->xval;
- NewSet-> ymax = ThisPoint->yval;
- NewSet-> ymin = ThisPoint->yval;
- }
-
- LastPoint = ThisPoint;
- ThisPoint = ThisPoint->NextPoint;
- /**/
- }
- }
- else {
- EasyRequest (NULL,&ES,NULL,"Format Error Bailout");
- free((char*) (pr));
- return(32);
- }
-
- free((char*) (pr));
-
- LastPoint-> NextPoint = NULL;
- FreeVec( ThisPoint );
- NewSet-> npt = pnts;
-
- if ( NewSet-> FirstPoint )
- {
- if ( version >=3.20 )
- strncpy(NewSet->fn,SubTitle,strlen(SubTitle)-1);
- else
- {
- strcpy(NewSet->fn, fname);
- strcat(NewSet->fn, ".");
- sprintf(SubTitle,"%ld",channel);
- strcat(NewSet->fn, SubTitle);
- }
- AddTail( SetList, &NewSet-> snode);
- return ( TRUE );
- }
- else
- {
- EasyRequest (NULL,&ES,NULL,"No First Point");
- FreeVec( NewSet );
- return ( FALSE );
- }
-
- EasyRequest (NULL,&ES,NULL,"Can't Imagine how you got Here Oh well");
- return(TRUE);
- }
-
- /************************************/
- float Get_Version()
- {
- long apos;
- long mode=0;
- long y=0;
- char key[9];
- double VERSION=0.0;
-
- apos = ftell(ifp);
- while (fscanf(ifp,"%8s",key)) {
- if (y>10) {
- if (fseek(ifp,apos,mode)==-1) {
- EasyRequest (NULL,&ES,NULL,"Rewind Error in Get_Version()");
- Death(20);
- }
- else return(0.0);
- }
- if (strncmp("Version",key,7)==0) break;
- ++y;
- }
- if (fscanf(ifp,"%lf",&VERSION)!=1) EasyRequest (NULL,&ES,NULL,"Weird Version");
- return ((float)VERSION);
- }
- /**************************************/
- void Get_Title(char *title_string,char *keystring)
- {
- char key[12];
- fscanf(ifp,"%11s",key);
- if (strncmp(keystring,key,sizeof(keystring))!=0) {
- EasyRequest (NULL,&ES,NULL,"Format strang in Title or Sub");
- Death(20);
- }
- if(!(fgets(title_string,99,ifp))) {
- EasyRequest (NULL,&ES,NULL,"ERROR in GetTitle()");
- Death(20);
- }
- }
- /*****************************************/
- void Get_Creator()
- {
- long fpos;
- fpos = ftell(ifp);
- fseek(ifp,0L,SEEK_SET);
- if(fscanf(ifp,"%19s%19s",Creator,&Creator[1][0])!=2)
- EasyRequest (NULL,&ES,NULL,"Problem with Creator");
- fseek(ifp,fpos,SEEK_SET);
- }
- /*****************************************/
- long Do_Map(short chnl,char *ofn,long *next)
- {
- FILE *mfp;
- char filename[80],newext[5] = ".map";
- char drive[10],path[35],node[30],ext[5];
- long pos;
- long *ptr;
- long q;
- struct DosList *dl;
-
- strsfn(ofn,drive,path,node,ext);
-
- dl = LockDosList(LDF_READ | LDF_ASSIGNS);
-
- dl = FindDosEntry(dl,"Maps",LDF_ASSIGNS);
-
- if( dl )
- strcpy(filename,"Maps:");
- else
- {
- strcpy(filename,drive);
- strcat(filename,path);
- }
-
- UnLockDosList(LDF_READ | LDF_ASSIGNS);
-
- strcat(filename,node);
- strcat(filename,newext);
-
- ptr = (long *)malloc((long)MAP_SIZE);
- if (ptr==NULL) {
- EasyRequest (NULL,&ES,NULL,"malloc error in domap()");
- Death(20);
- }
-
-
- if ((mfp=fopen(filename,"rb"))==NULL) {
- if((mfp=fopen(filename,"wb"))==NULL) {
- EasyRequest (NULL,&ES,NULL,"Error in domap() cant open file");
- free((char*) (ptr));
- Death(20);
- }
- else
- for (q=1;q<MAP_SIZE/4;) {
- if (fscanf(ifp,"%5s",keyword)==EOF) {
- ptr[q] = ftell(ifp)+5;
- q++;
- break;
- }
- if ((strncmp("DATA",keyword,4)==0)) {
- ptr[q] = ftell(ifp);
- q++;
- }
- else if ((strncmp("FAIL",keyword,4)==0)) {
- pos = ftell(ifp);
- ptr[q] = pos*(-1.);
- q++;
- }
-
- else if ((strncmp("POST",keyword,4)==0)&&(strncmp(form,"WORD",4)==0)) {
- ptr[q] = ftell(ifp);
- q++;
- break;
- }
- }
- ptr[0] = q-2;
- fwrite((void *)ptr,sizeof(long),q,mfp);
-
- }
- else fread((void *)ptr,BLOCK_SIZE,MAP_SIZE/BLOCK_SIZE,mfp);
-
- if (ptr[0]<chnl) {
- EasyRequest (NULL,&ES,NULL,"Sorry don't have that many Channels");
- free((char*) (ptr));
- Death (20);
- }
- *next = ptr[chnl+1]-5;
- pos = ptr[chnl];
- free((char*) (ptr));
- fclose(mfp);
- return (pos);
- }
- /*********************************************/
- double Calc_Value(double inc,double org,double ref,long v)
- {
- return (((v-ref)*inc)+org);
- }
- /*********************************************/
-