home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 2 / FFMCD02.bin / new / misc / sci / cp / source / rhp.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-21  |  12.3 KB  |  447 lines

  1.  
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <math.h>
  5. #include <string.h>
  6. #include <ctype.h>
  7. #include "cp.h"
  8.  
  9. #define BLOCK_SIZE 512L  /* Size for fread() */
  10. #define MAP_SIZE 1024L   /* Up to 255 channels */
  11.  
  12.      /* function prototypes */
  13.  
  14. float Get_Version(void);
  15. void Get_Creator(void);
  16. long Do_Map(short,char *,long *);
  17. void Get_Title(char *,char *);
  18. double Calc_Value(double,double,double,long);
  19.  
  20.      /* Global Data */
  21.  
  22. struct EasyStruct ES =
  23.    {
  24.      sizeof(struct EasyStruct),
  25.      0,
  26.      "rhp()",
  27.      "%s",
  28.      "Oh NO!"
  29.     };
  30.  
  31.  
  32. char  form[6],cpl[10],shot[30],keyword[10],origin[10];
  33. static char  Creator[2][20];
  34. FILE  *ifp, *fopen();
  35.  
  36. BOOL rhp_Args( UBYTE *fn, UBYTE *Args)
  37. {
  38. UBYTE *tp = "CHAN/N/M";
  39. LONG ch_array[ 1 ] = { 0L };
  40. LONG len;
  41. UBYTE *ch_txt;
  42. struct RDArgs *ch_args = NULL;
  43. LONG **chan;
  44. WORD j = 0;
  45.  
  46.      len = strlen( Args);
  47.  
  48.      if (( ch_txt = AllocVec( max((len + 1),255), MEMF_CLEAR )))
  49.        {
  50.           strcpy( ch_txt, Args);
  51.  
  52.           if ( len > 0 )
  53.             {
  54.                ch_txt[len]   = '\n';
  55.                ch_txt[++len] = '\0';
  56.  
  57.                if ((ch_args = (struct RDArgs *)AllocDosObject(DOS_RDARGS, TAG_DONE)))
  58.                  {
  59.                     ch_args->RDA_Source.CS_Buffer = ch_txt;
  60.                     ch_args->RDA_Source.CS_Length = len;
  61.                     ch_args->RDA_Source.CS_CurChr = 0L;
  62.  
  63.                     if( ReadArgs( tp, (LONG *)&ch_array[ 0 ], ch_args ))
  64.                       {
  65.                          chan = ( LONG ** ) ch_array[ 0 ];
  66.                          while ( chan[j] )
  67.                            {
  68.                               rhp( fn , *chan[j]);
  69.                               j++;
  70.                            }
  71.                       }
  72.                     else
  73.                       {
  74.                          EasyRequest (NULL,&ES,NULL,"Invalid Binary Arg String\n Must be Integers");
  75.                       }
  76.                     FreeArgs( ch_args );
  77.                     FreeDosObject(DOS_RDARGS, ch_args);
  78.                  }
  79.             }
  80.           FreeVec( ch_txt );
  81.        }
  82.      return (TRUE);
  83. }
  84. BOOL rhp(UBYTE*fname,LONG channel)
  85. {
  86. float  version;
  87. double  xinc,xref,xorg,yinc,yref,yorg;
  88. char  Title[100]="NOTITLE",SubTitle[100]="NOSUBTITLE";
  89. unsigned char  *pr;
  90. long  c,j,k,pnts,len,nex,fpos,mode=0;
  91.  
  92. struct Set *NewSet;
  93.  
  94. struct Point *ThisPoint;
  95. struct Point *LastPoint = NULL;
  96. struct Point *FirstPoint;
  97.  
  98.      if (( ifp = fopen(fname,"rb"))==NULL) {
  99.           EasyRequest (NULL,&ES,NULL,"Cant open binary file");
  100.           Death(25);
  101.      }
  102.  
  103.  
  104.      version = Get_Version();      /* get binary version */
  105.  
  106.      if (version > 3.0)  Get_Creator();
  107.  
  108.      if (version >= 2.31) {
  109.           if ( version >= 3.20 ) {
  110.                fscanf(ifp,"%5s",form);
  111.           }
  112.           Get_Title(Title,"TITLE");
  113.      }
  114.  
  115.      /* Call Mapping Routine and map file if map is not found */
  116.  
  117.      fpos = Do_Map((short)channel,fname,&nex);
  118.      if (fpos<0) {
  119.           if (ifp) fclose(ifp);
  120.           EasyRequest (NULL,&ES,NULL,"Scope Failed this time");
  121.           return (FALSE);
  122.      }
  123.  
  124.      fseek(ifp,fpos,mode);
  125.  
  126.      /* get format origin subtitle string */
  127.  
  128.      if (version>=3.20) {
  129.           fscanf(ifp,"%5s",form);
  130.           if(version>=3.29) fscanf(ifp,"%5s",origin);
  131.  
  132.           Get_Title(SubTitle,"SUBTITLE");
  133.  
  134.      }
  135.  
  136.      fscanf(ifp,"%9s",cpl);
  137.  
  138.      if (fscanf(ifp,"%lf%lf%lf%lf%lf%lf",&xinc,&xorg,&xref,&yinc,&yorg,&yref)!=6) {
  139.           if (ifp) fclose(ifp);
  140.           EasyRequest (NULL,&ES,NULL,"Format wrong in headers");
  141.           return(45);
  142.      }
  143.  
  144.      c = getc(ifp);
  145.      if (c==13&&version<2.0) getc(ifp);  /* swallow BS from older versions */
  146.  
  147.      len = ftell(ifp);
  148.      len = labs(nex) - len;   /* abs in case next was FAIL */
  149.  
  150.      if (version<3.20 || (strcmp(form,"BYTE")==0)) {
  151.           if (len>8192 && version<3.20)pnts=8192;
  152.           else pnts=len;
  153.           j=255;
  154.      }
  155.      else if ((strcmp(form,"WORD")==0)&&(strcmp(&Creator[2][0],"DES4")==0)) {
  156.           pnts=len/2;
  157.           j=255;
  158.      }
  159.      else {
  160.           pnts=len/2;
  161.           j=32768;
  162.      }
  163.  
  164.      /* Allocate memory for binary and read it in */
  165.  
  166.      pr = malloc((unsigned)len+BLOCK_SIZE);
  167.      if (pr==NULL) {
  168.           EasyRequest (NULL,&ES,NULL,"No More RAM");
  169.           if (ifp) fclose(ifp);
  170.           return(20);
  171.      }
  172.  
  173.      fread(pr,BLOCK_SIZE,len/BLOCK_SIZE+1,ifp);
  174.  
  175.      if (ifp) fclose(ifp);
  176.  
  177.      NewSet = (struct Set *)AllocVec( sizeof( struct Set ), MEMF_CLEAR );
  178.  
  179.  
  180.      if ( NewSet == NULL )
  181.        {
  182.           EasyRequest (NULL,&ES,NULL,"No More RAM");
  183.           return ( FALSE );
  184.        }
  185.  
  186.      NewSet-> snode.ln_Name = NewSet-> fn;
  187.      NewSet-> snode.ln_Type = NT_DATA;
  188.      NewSet-> snode.ln_Pri = 0;
  189.  
  190.      FirstPoint = AllocVec( sizeof( struct Point ), NULL );
  191.      if( FirstPoint == NULL )
  192.        {
  193.           EasyRequest (NULL,&ES,NULL,"Out of Memory");
  194.           FreeVec( NewSet );
  195.           return ( FALSE );
  196.        }
  197.  
  198.      NewSet-> FirstPoint = FirstPoint;
  199.      ThisPoint = FirstPoint;
  200.  
  201.      if (version<3.20 || (strcmp(form,"BYTE")==0)) {
  202.           for (k=0;k<pnts&&k<points;k+=thin) {
  203.                c=pr[k];
  204.  
  205.                ThisPoint->yval = (float) Calc_Value(yinc,yorg,yref,c);
  206.                ThisPoint->xval = (float) Calc_Value(xinc,xorg,xref,k);
  207.  
  208.                ThisPoint->NextPoint = AllocVec( sizeof( struct Point ), NULL );
  209.                if( ThisPoint->NextPoint == NULL )
  210.                  {
  211.                     EasyRequest (NULL,&ES,NULL,"Out of Memory");
  212.                     FreePoints( FirstPoint );
  213.                     FreeVec( NewSet );
  214.                     free((char*) (pr));
  215.                     return ( FALSE );
  216.                  }
  217.                if (LastPoint!=NULL)
  218.                  {
  219.                     NewSet-> xmax = max( NewSet-> xmax, ThisPoint->xval);
  220.                     NewSet-> xmin = min( NewSet-> xmin,  ThisPoint->xval);
  221.                     NewSet-> ymax = max( NewSet-> ymax,  ThisPoint->yval);
  222.                     NewSet-> ymin = min( NewSet-> ymin,  ThisPoint->yval);
  223.                  }
  224.                else
  225.                  {
  226.                     NewSet-> xmax =  ThisPoint->xval;
  227.                     NewSet-> xmin =  ThisPoint->xval;
  228.                     NewSet-> ymax =  ThisPoint->yval;
  229.                     NewSet-> ymin =  ThisPoint->yval;
  230.                  }
  231.  
  232.                LastPoint = ThisPoint;
  233.                ThisPoint = ThisPoint->NextPoint;
  234.  
  235.           }
  236.      }
  237.      else if(strcmp(form,"WORD")==0) {
  238.           for (k=0;k<pnts&&k<points;k+=thin) {
  239.                c = ((pr[2*k]<<8)|pr[2*k+1]);
  240. /**/
  241.                ThisPoint->yval = (float) Calc_Value(yinc,yorg,yref,c);
  242.                ThisPoint->xval = (float) Calc_Value(xinc,xorg,xref,k);
  243.  
  244.                ThisPoint->NextPoint = AllocVec( sizeof( struct Point ), NULL );
  245.                if( ThisPoint->NextPoint == NULL )
  246.                  {
  247.                     EasyRequest (NULL,&ES,NULL,"Out of Memory");
  248.                     FreePoints( FirstPoint );
  249.                     FreeVec( NewSet );
  250.                     free((char*) (pr));
  251.                     return ( FALSE );
  252.                  }
  253.                if (LastPoint!=NULL)
  254.                  {
  255.                     NewSet-> xmax = max( NewSet-> xmax,  ThisPoint->xval);
  256.                     NewSet-> xmin = min( NewSet-> xmin,  ThisPoint->xval);
  257.                     NewSet-> ymax = max( NewSet-> ymax,  ThisPoint->yval);
  258.                     NewSet-> ymin = min( NewSet-> ymin,  ThisPoint->yval);
  259.                  }
  260.                else
  261.                  {
  262.                     NewSet-> xmax =  ThisPoint->xval;
  263.                     NewSet-> xmin =  ThisPoint->xval;
  264.                     NewSet-> ymax =  ThisPoint->yval;
  265.                     NewSet-> ymin =  ThisPoint->yval;
  266.                  }
  267.  
  268.                LastPoint = ThisPoint;
  269.                ThisPoint = ThisPoint->NextPoint;
  270. /**/
  271.           }
  272.      }
  273.      else {
  274.           EasyRequest (NULL,&ES,NULL,"Format Error Bailout");
  275.           free((char*) (pr));
  276.           return(32);
  277.      }
  278.  
  279.      free((char*) (pr));
  280.  
  281.      LastPoint-> NextPoint = NULL;
  282.      FreeVec( ThisPoint );
  283.      NewSet-> npt = pnts;
  284.  
  285.      if ( NewSet-> FirstPoint )
  286.        {
  287.           if ( version >=3.20 )
  288.                strncpy(NewSet->fn,SubTitle,strlen(SubTitle)-1);
  289.           else
  290.             {
  291.                strcpy(NewSet->fn, fname);
  292.                strcat(NewSet->fn, ".");
  293.                sprintf(SubTitle,"%ld",channel);
  294.                strcat(NewSet->fn, SubTitle);
  295.             }
  296.           AddTail( SetList, &NewSet-> snode);
  297.           return ( TRUE );
  298.        }
  299.      else
  300.        {
  301.           EasyRequest (NULL,&ES,NULL,"No First Point");
  302.           FreeVec( NewSet );
  303.           return ( FALSE );
  304.        }
  305.  
  306.      EasyRequest (NULL,&ES,NULL,"Can't Imagine how you got Here Oh well");
  307.      return(TRUE);
  308. }
  309.  
  310. /************************************/
  311. float Get_Version()
  312. {
  313. long apos;
  314. long  mode=0;
  315. long y=0;
  316. char key[9];
  317. double VERSION=0.0;
  318.  
  319.      apos = ftell(ifp);
  320.      while (fscanf(ifp,"%8s",key)) {
  321.           if (y>10) {
  322.                if (fseek(ifp,apos,mode)==-1) {
  323.                     EasyRequest (NULL,&ES,NULL,"Rewind Error in Get_Version()");
  324.                     Death(20);
  325.                }
  326.                else return(0.0);
  327.           }
  328.           if (strncmp("Version",key,7)==0) break;
  329.           ++y;
  330.      }
  331.      if (fscanf(ifp,"%lf",&VERSION)!=1) EasyRequest (NULL,&ES,NULL,"Weird Version");
  332.      return ((float)VERSION);
  333. }
  334. /**************************************/
  335. void Get_Title(char *title_string,char *keystring)
  336. {
  337. char key[12];
  338.      fscanf(ifp,"%11s",key);
  339.      if (strncmp(keystring,key,sizeof(keystring))!=0) {
  340.           EasyRequest (NULL,&ES,NULL,"Format strang in Title or Sub");
  341.           Death(20);
  342.      }
  343.      if(!(fgets(title_string,99,ifp))) {
  344.           EasyRequest (NULL,&ES,NULL,"ERROR in GetTitle()");
  345.           Death(20);
  346.      }
  347. }
  348. /*****************************************/
  349. void Get_Creator()
  350. {
  351. long fpos;
  352.      fpos = ftell(ifp);
  353.      fseek(ifp,0L,SEEK_SET);
  354.      if(fscanf(ifp,"%19s%19s",Creator,&Creator[1][0])!=2)
  355.           EasyRequest (NULL,&ES,NULL,"Problem with Creator");
  356.      fseek(ifp,fpos,SEEK_SET);
  357. }
  358. /*****************************************/
  359. long Do_Map(short chnl,char *ofn,long *next)
  360. {
  361. FILE *mfp;
  362. char filename[80],newext[5] = ".map";
  363. char drive[10],path[35],node[30],ext[5];
  364. long pos;
  365. long *ptr;
  366. long q;
  367. struct DosList *dl;
  368.  
  369.      strsfn(ofn,drive,path,node,ext);
  370.  
  371.      dl = LockDosList(LDF_READ | LDF_ASSIGNS);
  372.  
  373.      dl = FindDosEntry(dl,"Maps",LDF_ASSIGNS);
  374.  
  375.      if( dl )
  376.           strcpy(filename,"Maps:");
  377.      else
  378.        {
  379.           strcpy(filename,drive);
  380.           strcat(filename,path);
  381.        }
  382.  
  383.      UnLockDosList(LDF_READ | LDF_ASSIGNS);
  384.  
  385.      strcat(filename,node);
  386.      strcat(filename,newext);
  387.  
  388.      ptr = (long *)malloc((long)MAP_SIZE);
  389.      if (ptr==NULL) {
  390.           EasyRequest (NULL,&ES,NULL,"malloc error in domap()");
  391.           Death(20);
  392.      }
  393.  
  394.  
  395.      if ((mfp=fopen(filename,"rb"))==NULL) {
  396.           if((mfp=fopen(filename,"wb"))==NULL) {
  397.                EasyRequest (NULL,&ES,NULL,"Error in domap() cant open file");
  398.                free((char*) (ptr));
  399.                Death(20);
  400.           }
  401.           else
  402.                for (q=1;q<MAP_SIZE/4;) {
  403.                     if (fscanf(ifp,"%5s",keyword)==EOF) {
  404.                          ptr[q] = ftell(ifp)+5;
  405.                          q++;
  406.                          break;
  407.                     }
  408.                     if ((strncmp("DATA",keyword,4)==0)) {
  409.                          ptr[q] = ftell(ifp);
  410.                          q++;
  411.                     }
  412.                     else if ((strncmp("FAIL",keyword,4)==0)) {
  413.                          pos = ftell(ifp);
  414.                          ptr[q] = pos*(-1.);
  415.                          q++;
  416.                     }
  417.  
  418.                     else if ((strncmp("POST",keyword,4)==0)&&(strncmp(form,"WORD",4)==0)) {
  419.                          ptr[q] = ftell(ifp);
  420.                          q++;
  421.                          break;
  422.                     }
  423.                }
  424.                ptr[0] = q-2;
  425.                fwrite((void *)ptr,sizeof(long),q,mfp);
  426.  
  427.      }
  428.      else fread((void *)ptr,BLOCK_SIZE,MAP_SIZE/BLOCK_SIZE,mfp);
  429.  
  430.      if (ptr[0]<chnl) {
  431.           EasyRequest (NULL,&ES,NULL,"Sorry don't have that many Channels");
  432.           free((char*) (ptr));
  433.           Death (20);
  434.      }
  435.      *next = ptr[chnl+1]-5;
  436.      pos = ptr[chnl];
  437.      free((char*) (ptr));
  438.      fclose(mfp);
  439.      return (pos);
  440. }
  441. /*********************************************/
  442. double Calc_Value(double inc,double org,double ref,long v)
  443. {
  444.      return (((v-ref)*inc)+org);
  445. }
  446. /*********************************************/
  447.