/* ffapi.h */
/* Find file API. Copyright (c) 2004 by Troels K. */

#ifndef __FFAPI_H__
#define __FFAPI_H__

#ifndef ISSLASH
   #define ISSLASH(c) (((c) == '/') || ((c) == '\\'))
#endif

#ifndef EXTERN_C   
   #ifdef __cplusplus
      #define EXTERN_C    extern "C"
   #else
      #define EXTERN_C    extern
   #endif
#endif

#ifdef _WIN32
   struct _finddata_t;
   struct _wfinddata_t;
#elif _MSDOS
   struct _find_t;
   #define _finddata_t _find_t
#else /* posix */
   #define _MAX_PATH 260
   #define _A_NORMAL 0x00
   #define _A_RDONLY 0x01
   #define _A_HIDDEN 0x02
   #define _A_SYSTEM 0x04
   #define _A_SUBDIR 0x10
   #define _A_ARCH   0x20
   struct _wfinddata_t;
   #ifndef TCHAR
      #ifdef _UNICODE
         #define TCHAR wchar_t
      #else
         #define TCHAR char
      #endif
   #endif
   
   #ifndef _FINDDATA_T_DEFINED
   #include <time.h>
   #ifndef _FSIZE_T_DEFINED
   typedef unsigned long _fsize_t; /* Could be 64 bits for Win32 */
   #define _FSIZE_T_DEFINED
   #endif
   struct _finddata_t 
   {
       unsigned    attrib;
       time_t      time_create;    /* -1 for FAT file systems */
       time_t      time_access;    /* -1 for FAT file systems */
       time_t      time_write;
       _fsize_t    size;
       char        name[260];
   };
   struct _wfinddata_t 
   {
       unsigned    attrib;
       time_t      time_create;    /* -1 for FAT file systems */
       time_t      time_access;    /* -1 for FAT file systems */
       time_t      time_write;
       _fsize_t    size;
       wchar_t     name[260];
   };
   #ifdef _UNICODE
      #define _tfinddata_t _wfinddata_t
   #else
      #define _tfinddata_t _finddata_t
   #endif
   #define _FINDDATA_T_DEFINED
#endif /* posix */

#endif

struct _FFIMPLA;
typedef struct _FFIMPLA* FFHANDLEA;

typedef struct _FFAPIA
{
   FFHANDLEA (*open )(void* filesystem, const char* dir, struct _finddata_t*);
   int       (*next )(FFHANDLEA, struct _finddata_t*);
   void      (*close)(FFHANDLEA*);
   void*     filesystem;
} FFAPIA;

EXTERN_C void fill_host_filefind(FFAPIA*);
EXTERN_C int  host_stat(const char* file, struct _finddata_t*);

#if defined(_WCHAR_T_DEFINED) && !defined(_MSDOS)

struct _FFIMPLW;
typedef struct _FFIMPLW* FFHANDLEW;

typedef struct _FFAPIW
{
   FFHANDLEW (*open )(void* filesystem, const wchar_t* dir, struct _wfinddata_t*);
   int       (*next )(FFHANDLEW, struct _wfinddata_t*);
   void      (*close)(FFHANDLEW*);
   void*     filesystem;
} FFAPIW;

EXTERN_C void fill_host_filefindw(FFAPIW*);
EXTERN_C int host_statw(const wchar_t* file, struct _wfinddata_t*);
EXTERN_C void mbstowcs_finddata(struct _wfinddata_t*, const struct _finddata_t *);
EXTERN_C void wcstombs_finddata(struct _finddata_t *, const struct _wfinddata_t*);

#endif

#if defined(UNICODE) || defined(_UNICODE)
   #define FFHANDLE FFHANDLEW
   #define tfill_host_filefind fill_host_filefindw
   #define thost_stat host_statw
#else
   #define FFHANDLE FFHANDLEA
   #define tfill_host_filefind fill_host_filefind
   #define thost_stat host_stat
#endif

#ifndef FFAPI 
   #if defined(UNICODE) || defined(_UNICODE)
      #define FFAPI struct _FFAPIW
   #else
      #define FFAPI struct _FFAPIA
   #endif
#endif

#endif /* __FFAPI_H__ */
