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

  1.  
  2. #include "cp.h"
  3.  
  4.  
  5. BOOL AddSet( UBYTE *fName )
  6. {
  7. struct Set *NewSet;
  8. BPTR f;
  9. struct EasyStruct ES =
  10.    {
  11.      sizeof(struct EasyStruct),
  12.      0,
  13.      "AddSet()",
  14.      "%s",
  15.      "Exit"
  16.     };
  17.  
  18.  
  19.      f = OpenFile( fName );
  20.  
  21.      if ( f && f != 1 )
  22.        {
  23.           NewSet = (struct Set *)AllocVec( sizeof( struct Set ), MEMF_CLEAR );
  24.  
  25.           if ( NewSet == NULL )
  26.             {
  27.                EasyRequest (NULL,&ES,NULL,"No More RAM");
  28.                Close( f );
  29.                return ( FALSE );
  30.             }
  31.  
  32.           NewSet-> snode.ln_Name = NewSet-> fn;
  33.           NewSet-> snode.ln_Type = NT_DATA;
  34.           NewSet-> snode.ln_Pri = 0;
  35.  
  36.           NewSet-> FirstPoint = File2Set( f , NewSet);
  37.  
  38.           if ( NewSet-> FirstPoint )
  39.             {
  40.                strcpy( NewSet-> fn, FilePart( fName ));
  41.                AddTail( SetList, &NewSet-> snode);
  42.                return ( TRUE );
  43.             }
  44.           else
  45.             {
  46.                FreeVec( NewSet );
  47.                return ( FALSE );
  48.             }
  49.        }
  50.      return ( ( BOOL ) f );
  51. }
  52.  
  53. BPTR OpenFile( UBYTE *fName)
  54. {
  55. BPTR fi;
  56. UBYTE str[128];
  57. WORD res;
  58.  
  59. struct EasyStruct ES =
  60.    {
  61.      sizeof(struct EasyStruct),
  62.      0,
  63.      "OpenFile()",
  64.      "%s",
  65.      "Continue | Bail"
  66.     };
  67.  
  68.      fi = Open( fName, MODE_OLDFILE );
  69.      if ( fi == NULL)
  70.      {
  71.        strcpy (str,"Can't Open file\n");
  72.        strcat (str,fName);
  73.        res = EasyRequest (NULL,&ES,NULL,str);
  74.        return( res );
  75.      }
  76.  
  77.      NameFromFH(fi, path, 255);
  78.      return( fi );
  79. }
  80.  
  81. struct Point *File2Set( BPTR fi , struct Set *ThisSet)
  82. {
  83.  
  84. struct EasyStruct ES =
  85.    {
  86.      sizeof(struct EasyStruct),
  87.      0,
  88.      "File2Set()",
  89.      "%s",
  90.      "Bye Bye"
  91.     };
  92.  
  93. char ch;
  94. char in[256];
  95. int c = 0;
  96. int k = 0;
  97. int n;
  98. double x, y;
  99.  
  100. struct Point *ThisPoint;
  101. struct Point *LastPoint = NULL;
  102. struct Point *FirstPoint = NULL;
  103.  
  104.  
  105.      while ( FGets( fi, in, 255 ))
  106.        {
  107.           n = sscanf( in, "%lf%c%lf", &x, &ch, &y );   /* ch to swallow csv */
  108.           if ( n == 3 ) k++;
  109.           if ( k >= points ) break;
  110.  
  111.           if ( n == 3 && k % thin == 0 )
  112.             {
  113.  
  114.                ThisPoint = AllocVec( sizeof( struct Point ), NULL );
  115.  
  116.                if( ThisPoint == NULL )
  117.                  {
  118.                     EasyRequest (NULL,&ES,NULL,"Out of Memory");
  119.                     FreePoints( FirstPoint );
  120.                     Close ( fi );
  121.                     return ( NULL );
  122.                  }
  123.  
  124.                if ( c )
  125.                  {
  126.                     LastPoint-> NextPoint = ThisPoint;
  127.                  }
  128.                else
  129.                  {
  130.                     FirstPoint = ThisPoint;
  131.                  }
  132.  
  133.                ThisPoint-> xval = x;
  134.                ThisPoint-> yval = y;
  135.  
  136.                LastPoint = ThisPoint;
  137.  
  138.                if ( c )
  139.                  {
  140.                     ThisSet-> xmax = max( ThisSet-> xmax, x);
  141.                     ThisSet-> xmin = min( ThisSet-> xmin, x);
  142.                     ThisSet-> ymax = max( ThisSet-> ymax, y);
  143.                     ThisSet-> ymin = min( ThisSet-> ymin, y);
  144.                  }
  145.                else
  146.                  {
  147.                     ThisSet-> xmax = x;
  148.                     ThisSet-> xmin = x;
  149.                     ThisSet-> ymax = y;
  150.                     ThisSet-> ymin = y;
  151.                  }
  152.  
  153.                c++;
  154.                ThisPoint-> NextPoint = NULL;
  155.             }
  156.        }
  157.      ThisSet-> npt = c;
  158.      Close ( fi );
  159.      return ( FirstPoint );
  160. }
  161.  
  162. void FreePoints( struct Point *First )
  163. {
  164. struct Point *Next;
  165.  
  166.        while ( First )
  167.          {
  168.  
  169.           Next = First -> NextPoint;
  170.           FreeVec( First);
  171.           First = Next;
  172.  
  173.          }
  174.  
  175. }
  176.  
  177. void FreeAllSets()
  178. {
  179. struct Set *node;
  180. struct Set *Next;
  181.  
  182.      node = (struct Set *)SetList-> lh_Head;
  183.  
  184.      while ( Next = (struct Set *)node-> snode.ln_Succ )
  185.        {
  186.           FreePoints( node -> FirstPoint);
  187.           Remove( (struct Node *)node );
  188.           FreeVec( node );
  189.           node = Next;
  190.        }
  191. }
  192.  
  193. BOOL AddNewSet()
  194. {
  195. struct FileRequester *fr;
  196. UBYTE *fpp;
  197. struct WBArg *frargs;
  198. BOOL res;
  199. LONG x;
  200.  
  201.      if ((fr = AllocAslRequest( ASL_FileRequest, TAG_DONE )))
  202.        {
  203.  
  204.           fpp = FilePart( path );
  205.  
  206.           *fpp = '\0';
  207.  
  208.           res = AslRequestTags( fr,
  209.                ASL_Window, PlotWindowWnd,
  210.                ASL_Hail, (ULONG)"Select Multiple Files",
  211.                ASL_Dir, path,
  212.                ASL_FuncFlags, FILF_MULTISELECT,
  213.                TAG_DONE );
  214.  
  215.           if ( res )
  216.             {
  217.                if ( fr-> rf_NumArgs )
  218.                  {
  219.                     frargs = fr-> rf_ArgList;
  220.  
  221.                     for( x = 0; x < fr-> rf_NumArgs; x++)
  222.                       {
  223.                          strcpy( path, fr-> rf_Dir);
  224.                          AddPart( path, frargs[x].wa_Name, 128);
  225.  
  226.                          res = AddSet( path );
  227.  
  228.                       }
  229.                  }
  230.                else if( strlen(fr-> rf_File))
  231.                  {
  232.                     strcpy( path, fr-> rf_Dir);
  233.                     AddPart( path, fr-> rf_File, 128);
  234.  
  235.                     res = AddSet( path );
  236.                  }
  237.             }
  238.           FreeAslRequest( fr );
  239.        }
  240.      return ( res );
  241. }
  242.