#include <exec/types.h>
#include <stdlib.h>
#include <string.h>

#include <link/io.h>

#include <proto/dlg.h>

#include <pragmas/dlg.h>

extern   BPTR                 sout;

extern   int                  AreaType;

extern   struct   Library    *DLGBase;
extern   struct   USER_DATA   UserDat;

extern   void GetNameOnly(char *filename);

int   dosort(char *, char *);

struct Name_Array
{
   char Name[36];
};

///   Create_Array()
int  Create_Array(char **Array)
{
   char                   *filename;
   LONG                    count = 0;
   struct   SearchCookie  *sc;
   struct   Name_Array    *temparray;

   sc = SearchStart("DLGConfig:Template",(AreaType ? "*.file" : "*.msg"));

   if(!sc)  { AFPrintf(&UserDat,sout,"Create_Array(1): Memory allocation error!\n\n"); return(0); }

   while(filename = SearchNext(sc)) count++;

   SearchEnd(sc);

   temparray = calloc(count,sizeof(struct Name_Array));

   if(!temparray)  { AFPrintf(&UserDat,sout,"Create_Array(2): Memory allocation error!\n\n"); return(0); }

   sc = SearchStart("DLGConfig:Template",(AreaType ? "*.file" : "*.msg"));

   if(!sc)  { AFPrintf(&UserDat,sout,"Create_Array(3): Memory allocation error!\n\n"); return(0); }

   count = 0;

   while(filename = SearchNext(sc))
   {
      char  t[36];

      strcpy(t,filename);
      GetNameOnly(t);
      Upper(t);
      strcpy(temparray[count].Name,t);
      count++;
   }

   SearchEnd(sc);

   qsort(temparray,count,36,dosort);
   *Array = (char *)temparray;
   return(count);
}
//-

int dosort(char *A, char *B)
{
   return(Stricmp(A,B));
}
