/* --------------------------------------------------------------------- */
/* Amiga IFF/SMF Search Utility: PAUL OVERAA             September 90    */
/* --------------------------------------------------------------------- */

/* includes */

#include <exec/types.h>
#include <exec/memory.h>
#include <libraries/dos.h>
#include <libraries/dosextens.h>

/* prototypes */

void CreateLevel(TEXT *imported_path); /* prototypes */
void FileCheck(TEXT *filename);

/* defines */

#define MakeID(a,b,c,d)  ( (LONG) (a)<<24L | (LONG) (b)<<16L | (c)<<8 | (d) )
#define  FIB_SIZE  (LONG)(sizeof(struct FileInfoBlock)) 
#define  ID_FORM MakeID('F','O','R','M')
#define  ID_SMF  MakeID('M','T','h','d')
#define BUFFERSIZE 12

/* some globals */

UBYTE buffer[12]; /* some space for partial file load */

UBYTE global_error=0; /* NON-ZERO values indicates error conditions... */
 
TEXT *error[6] = {
                 "Amiga Format IFF/SMF SEARCH Utility (Paul Overaa 1990)... \n\n", 
                 "Format... search <device>\n",
                 "Device not found \n",
                 "Sorry, not enough memory to continue\n",
                 "Integrity error occured during searching \n"
        };
 
/* --------------------------------------------------------------------- */

main(int argc, TEXT *argv[])

{

printf(error[0]); /* sign on message */

if (argc!=2) {printf(error[1]);}
   
  else { 
        
        CreateLevel(argv[1]);

        if(global_error) {printf(error[global_error]);}

       }
        
} /* logical end of program */


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

void CreateLevel(TEXT *imported_path)

{

TEXT   exported_path[300];

struct FileLock *FrameLock_p,*Lock(); 
struct FileInfoBlock *fib_p;

  if(FrameLock_p=Lock(imported_path,ACCESS_READ))

         {

          if (fib_p=(struct FileInfoBlock *)AllocMem(FIB_SIZE,MEMF_PUBLIC))

               {

                 if(Examine(FrameLock_p,fib_p))

                     {

                      while(ExNext(FrameLock_p,fib_p))

                         {

                         strcpy(exported_path,imported_path); /* copy name */

                         if(exported_path[strlen(exported_path)-1]!=':') 

                             {strcat(exported_path,"/");}

                         strcat(exported_path,fib_p->fib_FileName);

                         if(fib_p->fib_DirEntryType<0) 

                             { /* FILE FOUND SO LOOK AT IT */
             
                              FileCheck(exported_path);

                              }

                               else { /* SET UP A NEW FRAME */
            
                                     CreateLevel(exported_path);
            
                                     }

                         }  

                     }  else {global_error=4;} /* Examine() failed */

               FreeMem(fib_p,FIB_SIZE);

               } else {global_error=3;} /* AllocMem() failed */

          UnLock(FrameLock_p);

     } else {global_error=2;} /* Lock() failed */

}

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

void FileCheck(TEXT *filename)

{
	
struct FileHandle *fh;
LONG   length;

if (fh=(struct FileHandle *)Open(filename, MODE_OLDFILE))

               {

                length=Read(fh,buffer,BUFFERSIZE);

                if(length==BUFFERSIZE)

                     { /* check for SMF or FORM identifiers */

                     if(*((LONG *)buffer)==ID_SMF) 
                         
                          { 
                          
                           printf("MIDI File: %s\n",filename); 
                         
                          }

                     else { 
                         
                           if(*((LONG *)buffer)==ID_FORM) 
                         
                              { 
                                
                               printf("IFF ");
                               printf("%c", *(buffer+8));
                               printf("%c", *(buffer+9));
                               printf("%c", *(buffer+10));
                               printf("%c:  ", *(buffer+11));
                               printf("%s\n", filename); 
                          
                              }

                          }
                     
                     } 

                Close(fh);
         
                }

}

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

