/***************************************LCKG
*                                          *
*      $VER: FileIdent C-Source 1.14       *
*                                          *
*       (C) Copyright 1993,1994,1995       *
*                                          *
*            sTRIDER sYNDIcATE             *
*                                          *
*           All Rights Reserved            *
*                                          *
* Desc.:                                   *
* Example of using the FileID.library      *
*                                          *
* Do what U want with the source.          *
* Note the copyright of the FileID.library *
*                                          *
* Note: Use tabulatorspace 2 to read the   *
*       code as it was written             *
*                                          *
* ---------------------------------------- *
* Name    : FileIdent                      *
* Filename: FileIdent.c                    *
* Version : 1.14                           *
* Part    : FileIdent (1)                  *
* Parts   : 2                              *
* Begin   : 07-Nov-94 20:50:01             *
* Date    : 26-Jul-95 13:45:23             *
*******************************************/

char *VerStr="$VER: FileIdent 1.14 bY sTRIDER sYNDIcATE";

/* includes ----------------------------------------------------------------- */

#include <stdlib.h>
#include <strings.h>
#include <proto/dos.h>
#include <proto/asl.h>
#include <proto/exec.h>
#include <proto/FileID.h>

/* defines -------------------------------------------------------------------*/

#define OPT_FILE  0
#define OPT_COUNT 2

/* structs ------------------------------------------------------------------ */

struct FI_FileInfo		*MyFileInfo=NULL;
struct FileIDBase			*FileIDBase=NULL;
struct Library				*AslBase=NULL;
struct FileRequester	*FileRequester;

/* Globale ------------------------------------------------------------------ */

LONG	*arg1;
char	Buffer[256],
			WorkStr[256];
short	int	error=0;

/* prototypes --------------------------------------------------------------- */

extern	LONG __asm	StrMid(register __a0 STRPTR, register __a1 STRPTR,
				register __d1 LONG, register __d0 LONG);

void MyPrintf(char *ctl, ...);
void Ident(char*,char*);
void ShutDown(short int);

/* main --------------------------------------------------------------------- */

/* I don't want this CON: Window in the code, because i won't use stdio.
 * This now reduces the codesize. Also other ANSI-C stuff
 * will be reduced to a minimum. I will use wherever i can the AmigaOS.
 *
 * FileIdent v1.12 size was: 8792 bytes.
 * Now FileIdent v1.14 size is: 2936 bytes.
 */

void __autoopenfail(void) { _XCEXIT(0);}

void main()
{
	struct	RDArgs	*rda;
	LONG		opts[OPT_COUNT];
	char		**argptr,
					*curarg;

		/* try to open asl.library v37
     */

	if(!(AslBase	=	OpenLibrary("asl.library",37L)))
	ShutDown(1); /* Shutdown if fail opening the asl.library
								*/

		/* try to open FileID.library v6
     */

	if(!(FileIDBase	=	(struct FileIDBase *)OpenLibrary("FileID.library",6L)))
	ShutDown(2); /* Shutdown if fail opening the FileID.library
								*/

		/* Allocate FileInfo (needed for the Library struct FI_FileInfo)
     */

	if(!(MyFileInfo = (struct FI_FileInfo *)FIAllocFileInfo()))
	ShutDown(3); /* Shutdown if fail allocating the struct FI_FileInfo
								*/

	if(rda=ReadArgs("FILES/M",opts,NULL))
	{
		argptr = (char **)opts[OPT_FILE];

			/* This is a bit difficult, ReadArgs gives every time a (struct RDArgs)
			 * pointer back to you if u use multiple arguments as an template,
			 * because no arguments given from an endless number of choices must
			 * also be true if no argument ist given. It's not defined! Or can
			 * you define the other side: "What is endless???" this seems to be
			 * the reason that the pointer is also true if no argument is given.
			 *
			 * Because of this, u must first look if the first argument has a
			 * stringlength higher than zero bytes. If not, like here at first,
			 * no arguments are given.
			 */

		if(strlen(1L+*argptr)<1)
		{
			MyPrintf("\n[33mFileIdent 1.14 [0m(C) Copyright 1993,1994,1995 by sTRIDER oF sYNDIcATE.\n\nFile identify with the use of [33mFileID.library v6.0+\n[0m(C) Copyright 1993,1994,1995 BLOODROCK of SYNDICATE.\n");

			if(FileRequester = AllocAslRequestTags(ASL_FileRequest,
			ASLFR_DoMultiSelect, TRUE,
			ASLFR_DoPatterns, TRUE,
			ASLFR_DoSaveMode, FALSE,
			ASLFR_DrawersOnly, FALSE,
			ASLFR_TitleText, (ULONG)"Select File(s) to be identified...",
			ASLFR_InitialDrawer, (ULONG)"PROGDIR:",
			TAG_DONE))
			{
			  if(AslRequestTags(FileRequester,TAG_DONE))
				{
					if(FileRequester->rf_NumArgs<2) Ident(FileRequester->rf_Dir,FileRequester->rf_File);
					else
					{
							/* More files are selected, so handle them all.
							 */

						int FileNr=FileRequester->rf_NumArgs;

						do
						{
							Ident(FileRequester->rf_Dir,FileRequester->rf_ArgList->wa_Name);
							FileRequester->rf_ArgList++;
							FileNr--;
						}
						while(FileNr);

							/* Be sure to set the pointer back, or the asl.library will free
							 * the ArgLists somewhere in your RAM! It's not good to free
							 * memory behind the real allocated ArgList. U can also take
							 * your own pointer: struct WBArg *fargs=FileRequester->rf_ArgList;
							 * to be sure nothing bad can happen!
							 */

						FileRequester->rf_ArgList -= FileRequester->rf_NumArgs;
					}
  			} else MyPrintf("\nNothing selected.");
				FreeAslRequest(FileRequester);
  		}
		}

			/* Here i know that arguments are given. As long as they are, i will
			 * use them now all in following while()
			 */

		else while(curarg = *argptr++) Ident(curarg,"");

		MyPrintf("\n\n");
		FreeArgs(rda);
	}

	ShutDown(error);
}


/* procedures --------------------------------------------------------------- */

void MyPrintf(char *ctl, ...)
{
	arg1 = (long *)(&ctl + 1);
	RawDoFmt(ctl, arg1, (void (*))"\x16\xc0\x4e\x75",Buffer);
	Write(Output(), Buffer, strlen(Buffer));
}

void Ident(char *InDir,char *InFile)
{
	if(strlen(InDir)>225) InDir[225]='\0';
	strcpy(WorkStr,InDir);
	if((WorkStr[strlen(WorkStr)-1]!=':') && (WorkStr[strlen(WorkStr)-1]!='/') && strlen(InFile)>0) strcat(WorkStr,"/");
	strcat(WorkStr,InFile);

	if(!(error=FIIdentifyFromName((struct FI_FileInfo *)MyFileInfo,WorkStr)))
		MyPrintf("\n%-40s %s %ld %ld %ld %ld %ld",WorkStr,MyFileInfo->FI_Description,MyFileInfo->FI_ID,MyFileInfo->FI_GlobalFileClass,MyFileInfo->FI_DLPackNum,MyFileInfo->FI_DLPackType,MyFileInfo->FI_PackFlags);
	else MyPrintf("\n%-40s %s",FilePart(WorkStr),MyFileInfo->FI_Description);
}

void ShutDown(short int error)
{
	if(error>0) {
		MyPrintf("\nError %ld! ",error);

		switch( error )
		{
			case 1: MyPrintf("Can't open asl.library!\n\n");
							error=RETURN_FAIL;
			break;
			case 2: MyPrintf("Can't open FileID.library!\n\n");
							error=RETURN_FAIL;
			break;
			case 3: MyPrintf("Can't allocate FI_FileInfo!\n\n");
							error=RETURN_FAIL;
			break;
			deafult: error=RETURN_ERROR;
			break;
		}
		MyPrintf("\n");
	}

		/* Free allocated memory
		 */

	if(MyFileInfo)	FIFreeFileInfo((struct FI_FileInfo *)MyFileInfo);

		/* Close FileID.library v6.0
		 */

	if(FileIDBase)	CloseLibrary((struct Library *)FileIDBase);

		/* Close asl.library
		 */

	if(AslBase)			CloseLibrary(AslBase);

	exit(error);
}

/* EOF ---------------------------------------------------------------------- */
