/*
** setffargs.c
**
** FileFinder Package Utilities: SetFileFinderArgsA()
**
** $Id: setffargs.c,v 1.0 94/08/09 17:05:57 GF Exp Locker: GF $
**
**************************************************************************
**
** Copyright © 1994 by Gabriele Falcioni. All Rights Reserved.
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
**
** For inquiries, comments, donations, bug reports, write to:
**
** Gabriele Falcioni
** via E. Cialdini, 50
** I-60122 Ancona
** ITALY
**
*/

#include <string.h>

#include <exec/types.h>
#include <exec/memory.h>

#include <dos/dos.h>
#include <dos/stdio.h>
#include <dos/dosasl.h>
#include <dos/rdargs.h>

#include <proto/exec.h>
#include <proto/utility.h>
#include <proto/dos.h>

#include "filefinder.h"

/* --- My favourite memory macros ------------------------------------- */

#define STDMEM (MEMF_PUBLIC|MEMF_CLEAR)

#define ConStruct(type)		((type *)AllocMem(sizeof(type),STDMEM))
#define DiStruct(ptr)		FreeMem((ptr),sizeof(*(ptr)))
#define ClearStruct(ptr)	memset((ptr),0,sizeof(*(ptr)))

/* --- Some speed-up macros ------------------------------------------- */

#define OPT_ALL(ff)			((ff)->ff_Flags & FILEFF_ALL)
#define DO_DIR(ff)			((ff)->ff_Flags & FILEFF_DODIR)

#define NEWSTATE(ff,s)		((ff)->ff_State = (s))

#define DIR_ENTRY_TYPE(ff)	((ff)->ff_AP.ap_Info.fib_DirEntryType)
#define DIR_ENTRY_LOCK(ff)	((ff)->ff_AP.ap_Current->an_Lock)
#define DIR_ENTRY_NAME(ff)	((ff)->ff_AP.ap_Info.fib_FileName)

#define SETFLAGS(ff,mask)	((ff)->ff_AP.ap_Flags |=  (mask))
#define CLRFLAGS(ff,mask)	((ff)->ff_AP.ap_Flags &= ~(mask))
#define INVFLAGS(ff,mask)	((ff)->ff_AP.ap_Flags ^=  (mask))

#define CHECKFLG(ff,mask)	((ff)->ff_AP.ap_Flags & (mask))

/* -------------------------------------------------------------------- */

/****** filefinder.lib/SetFileFinderArgsA ********************************
*
*	NAME
*	SetFileFinderArgsA -- Change FileFinder args.
*	SetFileFinderArgs  -- varargs stub for SetFileFinderArgsA().
*
*	SYNOPSIS
*		SetFileFinderArgsA(ff,taglist);
*		                   a0 a1
*
*		VOID SetFileFinderArgsA(struct FileFinder *,struct TagItem *);
*
*		SetFileFinderArgs(ff,tag1, ... );
*
*		VOID SetFileFinderArgs(struct FileFinder *,Tag, ... );
*
*	FUNCTION
*		It changes the args of a FileFinder structure.
*
*	INPUTS
*		ff - a pointer to a valid FileFinder structure. A NULL pointer is
*			tolerated and ignored.
*		taglist - a pointer to a standard tag list. The following tags are
*			supported: FILEFT_Arg, FILEFT_Argv, FILEFT_All. The other
*			parameters remain unchanged.
*
*	SEE ALSO
*		CreateFileFinderA()
*
*************************************************************************/

VOID ASM SetFileFinderArgsA(
	REG(a0) struct FileFinder *ff,
	REG(a1) struct TagItem *tl)
{
	if (ff) {
		switch (ff->ff_State) {
			case FFSTATE_NEXT:
			case FFSTATE_FIRST:
				MatchEnd(&ff->ff_AP);
		}

		ff->ff_Flags = 0;
		ff->ff_State = 0;

		if (ff->ff_SABuf[0] = GetTagData(FILEFT_Arg,NULL,tl))
			ff->ff_FList = (UBYTE **)ff->ff_SABuf;
		else
			ff->ff_FList = (UBYTE **)GetTagData(FILEFT_Argv,NULL,tl);

		if (GetTagData(FILEFT_All,FALSE,tl))
			ff->ff_Flags |= FILEFF_ALL;
	}
}

VOID __stdargs SetFileFinderArgs(struct FileFinder *ff,Tag tag1, ... )
{
	SetFileFinderArgsA(ff,(struct TagItem *)&tag1);
}
