/***************************************LCKG
*                                          *
*      $VER: FileIdent C-Source 1.15       *
*                                          *
*       (C) Copyright 1994,1995,1996       *
*                                          *
*        Strider (tRSi/F-iNNOVATiON)       *
*                                          *
*           All Rights Reserved            *
*                                          *
*  - Watch out for FAME BBS Demoversion -  *
*                                          *
* Desc.:                                   *
* Example of using the FileID.library      *
*                                          *
* Do what you 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.15                           *
* Part    : FileIdent (1)                  *
* Parts   : 2                              *
* Begin   : 07-Nov-94 20:50:01             *
* Date    : 10-Apr-96 18:44:12             *
*******************************************/

char *VerStr="$VER: FileIdent 1.15 "__AMIGADATE__;

/* 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 TEMPLATE	"FILES/M"
#define OPT_FILE	0
#define OPT_COUNT	1

/* Structures --------------------------------------------------------------- */

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

/* Global Variables --------------------------------------------------------- */

LONG			*arg1;
char			Buffer[256],
					WorkStr[256];
short	int	error					= 0,
					HeaderFlag		= 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.
 */

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

/* FileIdent entry point.
 */

void main()
{
	struct	RDArgs				*rda;
	long		ArgArray[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 v7
	 */

	if( ! (FileIDBase	=	(struct FileIDBase *)OpenLibrary("FileID.library",7L)))
		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.
									*/

	MyPrintf("\n[mFileIdent 1.15 (C) Copyright 1993,1994,1995 by sTRIDER (tRSi/F-iNNOVATiON).\n\nFile identify with the use of [33mFileID.library v7.0+\n[m(C) Copyright 1993,1994,1995 BLOODROCK (tRSi/F-iNNOVATiON)\n");

	/* Clear the ArgArray, just to be on the safe side.
	 */

	memset((char *)ArgArray, 0, sizeof(ArgArray));

	/* Now it's time for ReadArgs().
	 */

	if(rda = ReadArgs(TEMPLATE,ArgArray,NULL)) {

		if( ! ArgArray[0]) {

			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)"",
			TAG_DONE)) {

			  if(AslRequestTags(FileRequester,TAG_DONE)) {

					/* Only one file selected.
					 */

					if(FileRequester -> fr_NumArgs < 2)
						Ident(FileRequester -> fr_Drawer,FileRequester -> fr_File);

					else {

						/* More files are selected, so ident them all.
						 */

						struct WBArg *fargs	= FileRequester -> fr_ArgList;
						long FileNr					= FileRequester -> fr_NumArgs;

						do
						{
							Ident(FileRequester -> fr_Drawer,fargs -> wa_Name);
							fargs ++;
						}
						while(-- FileNr);
					}
  			} else
					MyPrintf("\nNothing selected.");

				FreeAslRequest(FileRequester);
  		}
		} else {

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

			argptr = (char **)ArgArray[OPT_FILE];

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

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

	ShutDown(error);
}

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

/* Replacement for printf().
 */

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

/* Identiy a file.
 */

void Ident(char *InDir,char *InFile)
{
	STRPTR	BeginPtr;

	/* Print the header (once).
	 */

	if( ! HeaderFlag) {

		MyPrintf("\nIdentified file name[14CIdentification[22CID Num\n");
		memset(WorkStr,'-',76);
		WorkStr[76] = '\0';
		MyPrintf(WorkStr);
	}
	HeaderFlag = 1;

	/* Build the complete path + file
	 */

	if(strlen(InDir) > 225)
		InDir[225] = '\0';
	strcpy(WorkStr,InDir);

	if(InFile[0])
		AddPart(WorkStr,InFile,255);

	/* Call FIIdentifyFromName() from the FileID.library for identification.
	 */

	error = FIIdentifyFromName((struct FI_FileInfo *)MyFileInfo,WorkStr);

	/* Path + file longer than 33 chars ?
	 */

	if(strlen(WorkStr) > 33) {

		/* If path + file is longer than 33 chars we print three dots and
		 * the remaining 30 bytes of the path + file.
		 */

		BeginPtr = WorkStr + (strlen(WorkStr) - 33L);
		(*BeginPtr) = '.';
	  BeginPtr ++;
		(*BeginPtr) = '.';
  	BeginPtr ++;
		(*BeginPtr) = '.';
    BeginPtr -= 2;
	} else
		BeginPtr = WorkStr;

	/* Do the output.
	 */

	if( ! error)
		MyPrintf("\n%-33.33s %-35.35s %6ld",BeginPtr,MyFileInfo -> FI_Description,MyFileInfo -> FI_ID);
	else
		MyPrintf("\n%-33.33s [33m%-35.35s[m",BeginPtr,MyFileInfo -> FI_Description);

	/* Here you might react on errors from FIIdentifyFromName().
	 *
	 * ...
	 */
}

/* Freeing all resources.
 */

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");
	} else
		error = 0; /* To avoid the negativ FIIdentifyFromName() return codes
								* which may confuse the DOS (FileIdent unknown programm)
								*/

	/* Free allocated memory.
	 */

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

	/* Close FileID.library v7.0+.
	 */

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

	/* Close asl.library.
	 */

	if(AslBase)
		CloseLibrary(AslBase);

	exit(error);
}

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