#include "cp.h"


BOOL AddSet( UBYTE *fName )
{
struct Set *NewSet;
BPTR f;
UBYTE str[128];

     f = OpenFile( fName );

     if ( f && f != 1 )
       {
       
       	strcpy(str,"Reading File -> ");
       	strcat(str,fName);
          if ( PlotWindowWnd ) SetWindowTitles( PlotWindowWnd, str, str );

          NewSet = (struct Set *)AllocMem( sizeof( struct Set ), MEMF_CLEAR );

          if ( NewSet == NULL )
            {
	          if ( PlotWindowWnd ) SetWindowTitles( PlotWindowWnd, NULL, "No more RAM!" );
               Close( f );
               return ( FALSE );
            }

          NewSet-> snode.ln_Name = NewSet-> fn;
          NewSet-> snode.ln_Type = NT_DATA;
          NewSet-> snode.ln_Pri = 0;

          NewSet-> FirstPoint = File2Set( f , NewSet);

          if ( NewSet-> FirstPoint )
            {
               strcpy( NewSet-> fn, FilePart( fName ));
               AddTail( SetList, &NewSet-> snode);
	          if ( PlotWindowWnd ) SetWindowTitles( PlotWindowWnd, MyName, MyName );

               return ( TRUE );
            }
          else
            {
               FreeMem( NewSet , sizeof(struct Set));
               return ( FALSE );
            }
       }
     return ( FALSE );
}

BPTR OpenFile( UBYTE *fName)
{
BPTR fi;
UBYTE str[128];

     fi = Open( fName, MODE_OLDFILE );
     if ( fi == NULL)
     {
       strcpy (str,"Can't Open file -> ");
       strcat (str,fName);

       if ( PlotWindowWnd ) SetWindowTitles( PlotWindowWnd, NULL, str );
       return( NULL );
     }

     NameFromFH(fi, path, 255);

     return( fi );
}

struct Point *File2Set( BPTR fi , struct Set *ThisSet)
{

char ch;
char in[256];
int c = 0;
int k = 0;
int n;
BOOL good;
double x, y;

struct Point *ThisPoint;
struct Point *LastPoint = NULL;
struct Point *FirstPoint = NULL;


     while ( FGets( fi, in, 255 ))
       {
          n = sscanf( in, "%lf%c%lf", &x, &ch, &y );   /* ch to swallow csv */
          
          if ( n == 3 || n == 2 )
            {
            	good = TRUE;
            	k++;
            }
          else good = FALSE;
          
          if ( k >= points ) break;

          if ( good && k % thin == 0 )
            {

               ThisPoint = AllocMem( sizeof( struct Point ), NULL );

               if( ThisPoint == NULL )
                 {
		          if ( PlotWindowWnd ) SetWindowTitles( PlotWindowWnd, NULL, "Out of Memory" );
                    FreePoints( FirstPoint );
                    Close ( fi );
                    return ( NULL );
                 }

               if ( c )
                 {
                    LastPoint-> NextPoint = ThisPoint;
                 }
               else
                 {
                    FirstPoint = ThisPoint;
                 }

			if ( n == 2 )
			  {
			  	y = x;
			  	x = k;
			  }

               ThisPoint-> xval = x;
               ThisPoint-> yval = y;

               LastPoint = ThisPoint;

               if ( c )
                 {
                    ThisSet-> xmax = max( ThisSet-> xmax, x);
                    ThisSet-> xmin = min( ThisSet-> xmin, x);
                    ThisSet-> ymax = max( ThisSet-> ymax, y);
                    ThisSet-> ymin = min( ThisSet-> ymin, y);
                 }
               else
                 {
                    ThisSet-> xmax = x;
                    ThisSet-> xmin = x;
                    ThisSet-> ymax = y;
                    ThisSet-> ymin = y;
                 }

               c++;
               ThisPoint-> NextPoint = NULL;
            }
       }
     ThisSet-> npt = c;
     Close ( fi );

     return ( FirstPoint );
}

void FreePoints( struct Point *First )
{
struct Point *Next;

       while ( First )
         {

          Next = First -> NextPoint;
          FreeMem( First, sizeof(struct Point));
          First = Next;

         }

}

void FreeAllSets()
{
struct Set *node;
struct Set *Next;
UBYTE str[132];

     node = (struct Set *)SetList-> lh_Head;

     while ( Next = (struct Set *)node-> snode.ln_Succ )
       {

       	strcpy(str,"Freeing -> ");
    	  	strcat(str,node-> snode.ln_Name);
          if ( PlotWindowWnd ) SetWindowTitles( PlotWindowWnd, str, str );

          FreePoints( node -> FirstPoint);
          Remove( (struct Node *)node );
          FreeMem( node , sizeof(struct Set));
          node = Next;
       }
}

BOOL AddNewSet()
{
struct FileRequester *fr;
UBYTE *fpp;
struct WBArg *frargs;
BOOL res;
LONG x;
WORD rqW=180, rqH=120,   xo=100, yo=100;
struct Rectangle rect;
	

     if (PlotWindowWnd)
       {
		rqW  = fontPtr->tf_XSize * 30;
		rqH = cPFont->ta_YSize * 18;
		
		if (QueryOverscan(Disp, &rect, OSCAN_TEXT))
		  {

			 xo = -Scr->ViewPort.DxOffset+(rect.MaxX-rect.MinX)/2 - rqW/2;
			 yo = -Scr->ViewPort.DyOffset+(rect.MaxY-rect.MinY)/2 - rqH/2;
    		  }
    	  }

     if ((fr = AllocAslRequest( ASL_FileRequest, TAG_DONE )))
       {

          fpp = FilePart( path );

          *fpp = '\0';

          res = AslRequestTags( fr,
               ASL_Window, PlotWindowWnd,
               ASL_Hail, (ULONG)"Select Multiple Files",
               ASL_Dir, path,
               ASL_FuncFlags, FILF_MULTISELECT,
			ASL_LeftEdge, xo,
			ASL_TopEdge, yo,
			ASL_Width, rqW,
			ASL_Height, rqH,
               TAG_DONE );

          if ( res )
            {
               if ( fr-> rf_NumArgs )
                 {
                    frargs = fr-> rf_ArgList;

                    for( x = 0; x < fr-> rf_NumArgs; x++)
                      {
                         strcpy( path, fr-> rf_Dir);
                         AddPart( path, frargs[x].wa_Name, 128);

                         res = AddSet( path );

                      }
                 }
               else if( strlen(fr-> rf_File))
                 {
                    if (strlen(fr-> rf_Dir)) strcpy( path, fr-> rf_Dir);
                    AddPart( path, fr-> rf_File, 128);

                    res = AddSet( path );
                 }
            }
          FreeAslRequest( fr );
       }
     return ( res );
}
