/*=============================================================================
 *
 *  Filename:           BigFile.h
 *  Version:            1.1
 *  Date:               08/97
 *  Author:             1.0 / Vincent Lhuillier
 *                      1.1 / Adrian Silvescu, Laurentiu Barza, Yan Marchal
 *
 *  Description:        prototypes for accessing BigFile Info
 *
 *===========================================================================*/
#define _UNICODE
#ifndef __BIG_FILE__
#define __BIG_FILE__
 
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
 
#include <stdio.h>
 
#undef CPA_EXPORT
#if defined(CPA_WANTS_IMPORT)
 #define CPA_EXPORT __declspec(dllimport)
#elif defined(CPA_WANTS_EXPORT)
 #define CPA_EXPORT __declspec(dllexport)
#else
 #define CPA_EXPORT
#endif
 
/*=============================================================================
* DEFINES & CONSTANTS
=============================================================================*/
/* constant for file location */
#define MRC_C_wInexistentFile       -3
#define MRC_C_wInternalFile         -2
#define MRC_C_wExternalFile         -1
 
/* number of file description in table */
#define MRC_C_NumberMaxOfFileDescription    10
 
/* constant for bigfile managing mode */
#define MRC_C_ucNormalMode          0x00
#define MRC_C_ucExternalMode        0x01    
#define MRC_C_ucDebugMode                   0x02    
#define MRC_C_ucProfileMode         0x04
 
/* constant for data path mode */
#define MRC_C_ucDataPathIsCurrent                   0
#define MRC_C_ucDataPathIsNotCurrent        1
 
/*=============================================================================
* STRUCTURES
=============================================================================*/
/* Diverses enums */ 
typedef enum MRC_tdeFileMode_
{
        MRC_FM_Read ,       MRC_FM_Write,       MRC_FM_Append,
        MRC_FM_MaxFileModes
}MRC_tdeFileMode;
 
typedef enum MRC_tdeFileType_
{
        MRC_FT_Binary,      MRC_FT_Texte,       
        MRC_FT_MaxFileTypes
} MRC_tdeFileType;
 
typedef enum MRC_tdeFileState_
{
        MRC_FS_Close,       MRC_FS_Ready,       MRC_FS_NotReady,
        MRC_FS_MaxFileStates
} MRC_tdeFileState;
 
typedef enum MRC_tdeSeekOrigin_
{
        MRC_SO_SET , MRC_SO_CURRENT,  MRC_SO_END 
} MRC_tdeSeekOrigin;
 
/*
 * Structure that describes each type of data
 * in the bigfile file
 */
typedef struct MRC_tdstDLst_
{
        long    lNameOffset;
        long    lNumberOfSubDirs;
        long    lFirstSubDir;
        long    lNumberOfFiles;
        long    lFirstFile;
} MRC_tdstDLst;
 
typedef struct MRC_tdstFLst_
{
        long    lNameOffset;
        long    lLength;
        long    lOffset;
        long    lInfo;
} MRC_tdstFLst;
 
typedef struct MRC_tdstShortChunk_
{
        char    acChunk[4];
        long    lSize;
        char    acInfo[4];
} MRC_tdstShortChunk;
 
/*
 * Structure that describes a directory entry
 */
typedef struct MRC_tdstDirectoryEntry_
{
        char    *szName;                                        /* Directory name */
        short   wNumberOfSubDirs;       /* number of sub directories */
        short   wFirstSubDir;                   /* index of first sub directory in directories table */
        short   wNumberOfFiles;         /* number of files */
        short wFirstFile;                               /* index of first file in file table */
} MRC_tdstDirectoryEntry;
 
 
/*
 * Struct that describes a file entry
 */
typedef struct MRC_tdstFileEntry_
{
        char    *szName;                        /* File Name */
        char    *szExternFile;                  /* name of extern file if existent */
        long    lLength;                        /* length of file */
        long    lOffset;                        /* begin of the file in file data array */
        long    lInfos;                         /* info about the file */
        short   wPlace;                         /* Where is the file */
                                                                                        /* -3 = C_InexistantFile */
                                                                                        /* -2 = C_InternalFile */
                                                                                        /* -1 = C_ExternalFile */
                                                                                        /* other : nuber of patch file */
} MRC_tdstFileEntry;
 
/*
 * structure that describe a file
 */
 
typedef struct MRC_tdstBigFile_;
typedef struct MRC_tdstBigFileListEx_;
 
typedef struct MRC_tsdtFileDescription_
{
 
        struct MRC_tdstBigFile_                 *p_stBigFile;
 
        char                                                    *p_cAsyncBuffer;
        long                                                    lAsyncSize;
 
        MRC_tdstFileEntry                               *p_stFileEntry; /* File entry in BigFile */
        MRC_tdeFileType                                 eType;      /* file type */
        MRC_tdeFileMode                                 eMode;      /* file opening mode */
        MRC_tdeFileState                                eState;     /* file current state */
        
        long                            lPosition;  /* current position in buffer */
        long                            lFirst;     /* offset of first data */
 
                int                                                             bEndOfFile;  /* 0 if endoffile 1 if not end of file  */
 
                struct MRC_tsdtFileDescription_ *p_stNext;  /* pointer to the next file description if file is not in bigfile */ 
                struct MRC_tsdtFileDescription_ *p_stPrev;      /* pointer to the previous file description if file is not in bigfile */ 
        struct MRC_tdstBigFileListEx_   *p_stBFLEx; /* pointer to the extended big file */
 
                FILE                                                    *hpFile;
 
} MRC_tdstFileDescription;
 
/*
 * structure de description d'un patch de bigfile
 */
typedef struct MRC_tdstDataPatch_
{
                char    *szName;
                long    lVersion;
                long    lFirstData;
} MRC_tdstDataPatch;
 
/*
 * Structure of a BigFile
 */
typedef struct MRC_tdstBigFile_
{
        char                        *szName;        /* name of big file */
        FILE                        *hpBigFile;     /* Handler of big file */
        MRC_tdstDirectoryEntry      *pa_stDir;      /* Directory list */
        MRC_tdstFileEntry           *pa_stFile;     /* File List */
        long                        lVersion;
        long                        lFirstData;
        short                       wNumberOfDirs;  /* number of dir in dir table */
        short                       wNumberOfFiles; /* number of files in file table */
        short                       wNumberOfPatchs;
        MRC_tdstDataPatch           *pa_stPatch;
        unsigned char               ucMode;         /* bigfile managing mode, a combination between following values                */
                                                    /* C_ucNormalMode, C_ucDebugMode, C_ucProfileMode, C_ucExternalMode     */
 
        FILE*                       hpDebugBigFile;
        FILE*                       hpProfileBigFile;
 
        MRC_tdstFileDescription     aFileDescription[ MRC_C_NumberMaxOfFileDescription ];
 
} MRC_tdstBigFile;
/*
 * List of big files
 */
typedef MRC_tdstBigFile *MRC_tdstBigFileList;
/*
 * List of big files + external file list
 */
typedef struct MRC_tdstBigFileListEx_
{
        MRC_tdstBigFileList             *p_BigFileList;
        MRC_tdstFileDescription *p_stFileDesc;
        
}MRC_tdstBigFileListEx;
 
/*=============================================================================
* GLOBALS
=============================================================================*/
 
/*extern CPA_EXPORT MRC_tdstBigFile MRC_g_stBigFile;*/
 
/*=============================================================================
* FUNCTIONS
=============================================================================*/
/*
 * Fonction for managing debug file info
 */
CPA_EXPORT void MRC_fn_vPrintBigFileInfos( MRC_tdstBigFile *p_stBigFile );
CPA_EXPORT void MRC_fn_vPrintFileDescriptionInfos( MRC_tdstFileDescription *p_stFile );
CPA_EXPORT void MRC_fn_vOpenBigFileDebugFiles( MRC_tdstBigFile *p_stBigFile , char* szFileName);
CPA_EXPORT void MRC_fn_vCloseBigFileDebugFiles( MRC_tdstBigFile *p_stBigFile );
 
/*
 *      Fonctions for initialising and managing data and patch paths
 */
CPA_EXPORT long MRC_fn_lChangeDirectory( char *szPath );
CPA_EXPORT long MRC_fn_lVerifyPath( char *szPath );
 
CPA_EXPORT long MRC_fn_lInitDataPath( char *szDataPath, unsigned char ucMode );
CPA_EXPORT long MRC_fn_lInitPatchPath( char *szPatchPath );
CPA_EXPORT void MRC_fn_vChangeDataPathMode( unsigned char ucMode );
CPA_EXPORT void MRC_fn_vFreePath( void );
CPA_EXPORT long MRC_fn_lGotoDataDir( void );
CPA_EXPORT long MRC_fn_lGotoPatchDir( void );
CPA_EXPORT char *MRC_fn_p_szGetFullFileName( char *szFileName, unsigned char ucType );
 
/*
 * functions for opening / closing bigfile
 */
CPA_EXPORT MRC_tdstBigFile *MRC_fn_p_stOpenBigFile( char *szFileName, unsigned char ucMode );
CPA_EXPORT void MRC_fn_vCloseBigFile ( MRC_tdstBigFile *p_stBigFile );
 
/*
 * function for opening / closing a file
 */
CPA_EXPORT MRC_tdstFileDescription *MRC_fn_p_stOpenFile( char *szFileName, MRC_tdstBigFile *p_stBigFile );
CPA_EXPORT void MRC_fn_vCloseFile( MRC_tdstFileDescription *p_stFile );
 
/*
 * Miscellaneous functions on a file
 */
CPA_EXPORT long         MRC_fn_lFileLength( MRC_tdstFileDescription *p_stFile );
CPA_EXPORT long         MRC_fn_lSeek( MRC_tdstFileDescription *p_stFile, long lOffset, MRC_tdeSeekOrigin eOrigin);
CPA_EXPORT long         MRC_fn_lTell ( MRC_tdstFileDescription *p_stFile );
CPA_EXPORT int          MRC_fn_nEof ( MRC_tdstFileDescription *p_stFile );
CPA_EXPORT int          MRC_fn_nFileExists ( MRC_tdstBigFileListEx *p_stBigFileListEx , char* szFileName );
 
/*
 * synchronous read on a file
 */
CPA_EXPORT long  MRC_fn_lSyncReadFile( MRC_tdstFileDescription *p_stFile, char *p_cBuffer, long lSize , long lCount );
 
/*
 * functions && macros for asynchronous read
 */
 
CPA_EXPORT long  MRC_fn_lAsyncReadFile( MRC_tdstFileDescription *p_stFile, char *p_cBuffer, long lSize );
CPA_EXPORT long    MRC_fn_lAsyncNumberOfBytesRead( MRC_tdstFileDescription *p_stFile );
CPA_EXPORT void    MRC_fn_vWaitEndOfAsyncRead( MRC_tdstFileDescription *p_stFile );
#define MRC_M_lIsAsyncReadFinished( p_stFile ) (p_stFile->eState == FS_Ready)
 
/*
 * functions for BigFileList management
 */
 
CPA_EXPORT MRC_tdstBigFileList *MRC_fn_p_stOpenBigFileList( char **szFileNames, unsigned char ucMode );
CPA_EXPORT void MRC_fn_vCloseBigFileList ( MRC_tdstBigFileList *p_stBigFileList );
CPA_EXPORT MRC_tdstFileDescription *MRC_fn_p_stOpenFileInList( char *szFileName, MRC_tdstBigFileList *p_stBigFileList );
 
/*
 * functions for BigFileListEx management
 */
 
CPA_EXPORT MRC_tdstBigFileListEx *MRC_fn_p_stOpenBigFileListEx( char **szFileNames, unsigned char ucMode );
CPA_EXPORT void MRC_fn_vCloseBigFileListEx ( MRC_tdstBigFileListEx *p_stBigFileListEx );
CPA_EXPORT MRC_tdstFileDescription *MRC_fn_p_stOpenFileInListEx( MRC_tdstBigFileListEx *p_stBigFileListEx , char *szFileName , char *fmt );
 
/*
 * Functions for extended file services
 */
 
CPA_EXPORT void MRC_fn_vCloseFileEx( MRC_tdstFileDescription *p_stFile );
CPA_EXPORT long MRC_fn_lSyncReadFileEx( char *p_cBuffer, long lSize , long lCount , MRC_tdstFileDescription *p_stFile);
CPA_EXPORT long MRC_fn_lFileLengthEx( MRC_tdstFileDescription *p_stFile );
CPA_EXPORT long MRC_fn_lSeekEx( MRC_tdstFileDescription *p_stFile, long lOffset, MRC_tdeSeekOrigin eOrigin);
CPA_EXPORT long MRC_fn_lTellEx ( MRC_tdstFileDescription *p_stFile );
CPA_EXPORT int MRC_fn_nEofEx ( MRC_tdstFileDescription *p_stFile );
CPA_EXPORT int MRC_fn_nFileExistsEx ( MRC_tdstBigFileListEx *p_stBigFileListEx , char* szFileName );
/*CPA_EXPORT char* MRC_fn_szFileGetsEx ( char* buffer ,int n , MRC_tdstFileDescription* p_stFile );*/
/*CPA_EXPORT int MRC_fn_nFileScanfEx ( MRC_tdstFileDescription* p_stFile , char* fmt , char* text);*/
#ifdef __cplusplus
};
 
#endif /* __cplusplus */
 
#endif /*__BIG_FILE__*/
