/* Wildcard.c */

#include "Wildcard.h"
#include <string.h>

#include <clib/exec_protos.h>
#include <clib/dos_protos.h>
#include <libraries/dos.h>
#include "exec/memory.h"

#define BUFFER_SIZE 8192
#define ENTRY_LEN 40
#define ANCHOR_SIZE sizeof(struct AnchorPath)+ENTRY_LEN

static BOOL patInstalled = FALSE;
static char *bufPtr = NULL,*endPtr,*curPtr;

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

void SetPattern (char * pat)
{
 LONG rc,fnl;
 BOOL ok = TRUE;
 struct AnchorPath *ap;
 char *fn;
 
 patInstalled = FALSE;
 if(bufPtr=(char *)AllocMem(BUFFER_SIZE,MEMF_PUBLIC|MEMF_CLEAR)) {
   endPtr = bufPtr;
   patInstalled = TRUE;
   if(ap=(struct AnchorPath *)AllocMem(ANCHOR_SIZE,MEMF_PUBLIC|MEMF_CLEAR)) {
     ap->ap_Strlen = ENTRY_LEN;
     ap->ap_BreakBits = 0;
     if((rc = MatchFirst(pat,ap))!=ERROR_OBJECT_NOT_FOUND)
       while(rc != ERROR_NO_MORE_ENTRIES && ok) {
         fn = ap->ap_Info.fib_FileName;
         fnl = strlen(fn);
         if(endPtr+fnl >= bufPtr+BUFFER_SIZE) {
           ok = FALSE;
           continue;
         }
         strcpy(endPtr,fn);
         endPtr += fnl+1;
         rc = MatchNext(ap);
       }
     MatchEnd(ap);
     FreeMem(ap,ANCHOR_SIZE);
   }
   curPtr = bufPtr;
 }
}

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

char *NextEntry (void)
{
 char *fn;

 if(!patInstalled)
   return NULL;
 if(curPtr == endPtr) {
   FreeMem(bufPtr,BUFFER_SIZE);
   patInstalled = FALSE;
   return NULL;
 }
 fn = curPtr;
 curPtr += strlen(curPtr)+1;
 return fn;
}
