
 /***************************************************************************
 *Rolf Schneider | Am Birkenwaeldchen 16/18 | 58 Hagen 7 | Tel.: 02331/45569*
 *                                                                          *
 *                      First Amiga Artists Program:                        *
 *                                                                          *
 *                          -- Artist's Copy I --                           *
 *AC4                                                                       *
 ***************************************************************************/


 /**** Includes... *********************************************************/

#include "exec/types.h"
#include "libraries/dosextens.h"
#include "intuition/intuition.h"

 /***************************************************************************
 *            Globals...                                                    *
 ***************************************************************************/

typedef char FN[108];
typedef char FName[32];
extern FName Src[200],Dst[200],*pSrc,*pDst,help;
extern FN NSrc,NDst;
extern UBYTE stSrc[200],stDst[200];
extern short zSrc,zDst;
extern UBYTE Buffer [35];

extern UBYTE buf[11264];

extern struct Window *Wini;
extern struct Gadget GInput;

char pA[140],pB[140],WT[80];

 /***** Funktionen *******************************************************/

Copy()
{
UBYTE *st;
short s=0,j,length,val=0;
FName *S;
FN *Fs,*Fd;
long SFile,DFile;

st = stSrc;
S  = &Src[0];
Fs = (FN *)NSrc;
Fd = (FN *)NDst;

Disk(0);
for (j=0;j<=zSrc;++j)
	{
	if ((*st & 128) != 0)
		{
		s=1;
		namcpy (Fs,pA);
		if (namlen(Fs) != 4) namcat (pA,"/");
		namcat (pA,S);
		namcpy (Fd,pB);
		if (namlen(Fd) != 4) namcat (pB,"/");
		namcat (pB,S);
		namcpy ("COPY: ",WT);
		namcat (WT,S);
		SetWindowTitles (Wini,WT,-1);
		SFile = Open (pA,MODE_OLDFILE);
		DFile = Open (pB,MODE_NEWFILE);
		if ((SFile == 0) || (DFile ==0)) goto Clean;
		while (  ((length = Read (SFile,buf,5632)) > 0) && (val != -1)  )
			{
			val = Write (DFile,buf,length);
			}
Clean:		Close (SFile);
		Close (DFile);
		}
	st++;
	S++;
	}
Disk (1);
if (s == 1) zDst = Dir (NDst,&Dst[0],stDst,1,help);
}


Delete()
{
FName *S;
UBYTE *sst;
short i,s=0;
FN *Fn;

S   = &Dst[0];
sst = stDst;
Fn  = (FN *)&NDst[0];

Disk (0);
for (i=0;i<=zDst;++i)
	{
	if ((*sst & 128) != 0) 
		{
		s=1;
		namcpy (Fn,pA);
		if (namlen (Fn) != 4) namcat (pA,"/");
		namcat (pA,S);
		namcpy ("DELETE: ",WT);
		namcat (WT,S);
		SetWindowTitles (Wini,WT,-1);
		DeleteFile (pA);
		}
	sst++;
	S++;
	}
Disk (1);
if (s == 1) zDst = Dir (NDst,&Dst[0],stDst,1,help);

s   = 0;
S   = &Src[0];
sst = stSrc;
Fn  = (FN *)&NSrc[0];

Disk (0);
for (i=0;i<=zDst;++i)
	{
	if ((*sst & 128) != 0) 
		{
		s=1;
		namcpy (Fn,pA);
		if (namlen (Fn) != 4) namcat (pA,"/");
		namcat (pA,S);
		namcpy ("DELETE: ",WT);
		namcat (WT,S);
		SetWindowTitles (Wini,WT,-1);
		DeleteFile (pA);
		}
	sst++;
	S++;
	}

Disk (1);
if (s == 1) zSrc = Dir (NSrc,&Src[0],stSrc,0,help);
}


Makedir()
{
long point;

SetWindowTitles (Wini,"Click in InputBox, and type in new Dir-name !",-1);
StrGadTST ();

namcpy (NDst,pA);
if (namlen(NDst) != 4) namcat(pA,"/");
namcat (pA,Buffer);

namcpy ("MAKEDIR: ",WT);
namcat (WT,pA);
SetWindowTitles (Wini,WT,-1);

Disk (0);
point = CreateDir (pA);
UnLock (point);
Disk (1);

zDst = Dir (NDst,&Dst[0],stDst,1,help);
}

rename()
{
UBYTE *st;
int i,s=0,j=0;
FName *S;
FN *F;

xloke:

s  = 0;
st = (j==0)?stSrc:stDst;
S  = (j==0)?&Src[0]:&Dst[0];
F  = (j==0)?(FN *)NSrc:(FN *)NDst;
for (i=0;(i <= ((j==0)?zSrc:zDst));++i)
	{
	if ((*st & 128) != 0)
		{
		s=1;
		namcpy (F,pA);
		if (namlen(F) != 4) namcat (pA,"/");
		namcpy (pA,pB);
		namcat (pA,S);
		namcpy ("Click in InputBox, and type in new name for ",WT);
		namcat (WT,S);
		namcat (WT," !");
		SetWindowTitles (Wini,WT,-1);
		StrGadTST ();
		namcpy ("RENAME: ",WT);
		namcat (WT,S);
		namcat (WT," to ");
		namcat (WT,Buffer);
		SetWindowTitles (Wini,WT,-1);
		Disk (0);
		namcat (pB,Buffer);
		Rename (pA,pB);
		Disk (1);
		}
	st++;
	S++;
	}
if ((s == 1)&&(j == 1)) zDst = Dir (NDst,&Dst[0],stDst,1,help);
if ((s == 1)&&(j == 0)) zSrc = Dir (NSrc,&Src[0],stSrc,0,help);
if (++j == 1) goto xloke;
}

KillDir ()
{

namcpy ("Click in InputBox, and type in Dirname to be killed !",WT);
SetWindowTitles (Wini,WT,-1);
StrGadTST ();

namcpy ((FN *)NDst,pA);
if (namlen((FN *)NDst) != 4) namcat (pA,"/");
namcat (pA,Buffer);

namcpy ("KILLDIR: ",WT);
namcat (WT,pA);
SetWindowTitles (Wini,WT,-1);

Disk (0);
DeleteFile (pA);
Disk (1);
zDst = Dir (NDst,&Dst[0],stDst,1,help);
}



StrGadTST ()
{
struct IntuiMessage *meld;
int i=0;
APTR Ad;
ULONG Cl;

Wait (1<<Wini->UserPort->mp_SigBit);
while (meld = (struct IntuiMessage *) GetMsg(Wini->UserPort))
	{
	Cl = meld->Class;
	Ad = meld->IAddress;
	ReplyMsg (meld);
	if ((Cl == GADGETUP) && (i == 1)) break;
	if ((Cl == GADGETDOWN) && (Ad == (APTR)&GInput)) ++i;
	}
}
