#ifndef FILE_H
#define FILE_H
#include <nds.h>
#include <libpkm/types.h>
#ifdef __cplusplus
extern "C" {
#endif
/*-----------------------------------------------------------------------------
-- macros
-----------------------------------------------------------------------------*/
/*-----------------------------------------------------------------------------
-- type definitions
-----------------------------------------------------------------------------*/
typedef u8 FSFile[0x48];
typedef u8 FSArchive[0x5C];
/*-----------------------------------------------------------------------------
-- enums
-----------------------------------------------------------------------------*/
typedef enum SeekMode {
SEEK_SET,
SEEK_CUR,
SEEK_END
} SeekMode;
/*-----------------------------------------------------------------------------
-- structs
-----------------------------------------------------------------------------*/
typedef struct FileID {
FSArchive* arc;
u32 f_id;
} FileID;
/*-----------------------------------------------------------------------------
-- types
-----------------------------------------------------------------------------*/
/*-----------------------------------------------------------------------------
-- function declarations
-----------------------------------------------------------------------------*/
// official functions
void FS_InitFile (FSFile* p_file);
void FS_CloseFile(FSFile* p_file);
BOOL FS_OpenFile (FSFile* file, const char* path);
BOOL FS_SeekFile (FSFile* p_file, s32 offset, SeekMode mode);
s32 FS_ReadFile (FSFile* p_file, void* dst, s32 len);
u32 FS_GetLength (FSFile* p_file);
u32 FS_GetFilePosition(FSFile* p_file);
void FS_CancelFile(FSFile* p_file);
// use these as much as possible
// check the bottom for info on using these
/*---------------------------------------------------------------------------*
Name: ArchiveMoveImageTop
Description: takes an open file and seeks into a file number in the file, making that the new top - it can also return the file length
Arguments: FSFile* p_file : file to pull data from
int fileNum : number of file in narc to get data on
int offset : length into file to seek
int known_length : if this is set, return this and don't get the file size
Returns: either the length of the file number if known_length is 0 or known_length if set
*---------------------------------------------------------------------------*/
int ArchiveMoveImageTop(FSFile* p_file, u16 fileNum, int seek_length, int read_length);
/*---------------------------------------------------------------------------*
Name: getNarcToFile
Description: loads a narc into memory
Arguments: FSFile* p_file : file to pull data from
int narcPtrIdxNum : narc number 0 -> 308, or greater if you've added some - for info on how to convert a narc to a number, see narc.h
Returns: none
*---------------------------------------------------------------------------*/
void getNarcToFile(FSFile* p_file, int narcPtrIdxNum);
/*---------------------------------------------------------------------------*
Name: readFileToPresetBlk
Description: reads a file into a pre-allocated memblk
Arguments: void* pFileDest : destination buffer for fileNum
int narcPtrIdxNum : narc to read data from fileNum in from
int fileNum : file number to fetch in narc
Returns: none
*---------------------------------------------------------------------------*/
void readFileToPresetBlkViaNarc(void* pFileDest, int narcPtrIdxNum, u16 fileNum);
/*---------------------------------------------------------------------------*
Name: readFileToAllocatedBlk
Description: reads a file into an allocated memblk
Arguments: int narcPtrIdxNum : narc to read data from fileNum in from
int fileNum : file number to fetch in narc
int blkGroupID : block id to allocate memory from for the file that will be read in
Returns: pointer to the memblk that was allocated inside the function
*---------------------------------------------------------------------------*/
void* readFileToAllocatedBlk(int narcPtrIdxNum, u16 fileNum, s16 blkGroupID);
/*---------------------------------------------------------------------------*
Name: setReadLengthAndSeekIntoFile
Description: reads a file(or a piece of) into a pre-allocated memblk
Arguments: void* pFileDest : destination buffer for fileNum
int narcPtrIdxNum : narc to read data from fileNum in from
int fileNum : file number to fetch in narc
int offset : offset into the file to jump
Returns: none
*---------------------------------------------------------------------------*/
void setReadLengthAndSeekIntoFile(void* pFileDest, int narcPtrIdxNum, u16 fileNum, int seek_length);
/*---------------------------------------------------------------------------*
Name: readInFullFileFromNarc
Description: reads one entire file into memory from the given narc
Arguments: const char* path : narc path
int fileNum : file number to fetch in narc
int blkGroupID : block id to allocate memory from for the file that will be read in
Returns: pointer to the memblk with the file data that was allocated inside the function
*---------------------------------------------------------------------------*/
void* readInFullFileFromNarc(const char* path, u16 fileNum, s16 blkGroupID);
/*---------------------------------------------------------------------------*
Name: getLengthOfFileInNarc
Description: get file length
Arguments: int narcPtrIdxNum : narc to read data from fileNum in from
int fileNum : file number to fetch in narc
Returns: file length
*---------------------------------------------------------------------------*/
u32 getLengthOfFileInNarc(int narcPtrIdxNum, u16 fileNum);
/*---------------------------------------------------------------------------*
Name: readNarcIntoFile
Description: read an entire narc into a file in memory
Arguments: int narcPtrIdxNum : narc to read in
int fileNum : file number to fetch in narc
Returns: ptr to Arc_Tool struct with the FSFile and narc info- size, file count, etc(see narc.h for Arc_Tool info)
*---------------------------------------------------------------------------*/
void* readNarcIntoFile(int narcPtrIdxNum, s16 blkGroupID);
/*---------------------------------------------------------------------------*
Name: allocForAndReadInFile
Description: read a full file into memory
Arguments: FSFile* p_file : file to pull data from
u16 fileNum : file number to fetch in narc
s16 blkGroupID : block id to allocate memory from for the file that will be read in
Returns: ptr to file memory allocated inside function
*---------------------------------------------------------------------------*/
void* allocForAndReadInFile(Arc_Tool* arc, u16 fileNum, s16 blkGroupID);
/*---------------------------------------------------------------------------*
Name: readInFile
Description: read a file into pre-allocated memory
Arguments: FSFile* p_file : file to pull data from
u16 fileNum : file number to fetch in narc
void* dest : buffer to fill with the file data
Returns: none
*---------------------------------------------------------------------------*/
void readInFileToPresetBlkViaFile(FSFile* p_file, u16 fileNum, void* dest);
/*---------------------------------------------------------------------------*
Name: getFileLength
Description: get length of file number from file
Arguments: FSFile* p_file : file to pull data from
u16 fileNum : file number to fetch in narc
Returns: u32 length
*---------------------------------------------------------------------------*/
u32 getFileLength(FSFile* p_file, u16 fileNum);
/*---------------------------------------------------------------------------*
Name: readFileIntoBuffer
Description: simple read file into buffer
Arguments: FSFile* p_file : file to pull data from
s32 length : read length
void* pBuf : buffer to write the file into
Returns: none
*---------------------------------------------------------------------------*/
void readFileIntoBuffer(FSFile* p_file, s32 length, void* pBuf);
/*---------------------------------------------------------------------------*
Name: seekFileFromBase
Description: simple seek file from the beginning
Arguments: FSFile* p_file : file to pull data from
s32 seek_offset : amount to seek into the file
Returns: none
*---------------------------------------------------------------------------*/
void seekFileFromBase(FSFile* p_file, s32 seek_offset);
/*---------------------------------------------------------------------------*
Name: closeFileFreeBlk
Description: close an open file and free its memory
Arguments: FSFile* p_file : pointer to file memory to free
Returns: none
*---------------------------------------------------------------------------*/
void closeFileFreeBlk(FSFile* p_file);
/*-----------------------------------------------------------------------------
-- inline Functions
-----------------------------------------------------------------------------*/
#ifdef __cplusplus
} /* extern "C" */
#endif
#endif
/*
how to open a file from rom manually:
-create an FSFile: ex. FSFile file;
-FS_InitFile(file);
-FS_OpenFile(file, path_ptr) - you will need to provide a pointer to your path here. ideally by adding it to the end of the narc list using a hack that i should have implemented by the time you use this.
(-FS_SeekFile)
-FS_ReadFile - when you do this, you need to provide a destination pointer for it to write to. use the alloc header to create a block from free space.
*/
/*
how to open a file with a custom function: (using fetching evolution info as an example)
void* f_blk = allocateBlockFromExpHeap(4, sizeof(struct EvoData), FALSE, g_EvoSrc, BLK_TYPE_INDICATOR);
readFileToPresetBlk(f_blk, EVO_NARC, POKE_METANG_EVO_INFO);
* get info from evolution data block *
freeBlock(f_blk);
*/
/*
how to open a file with a custom function AND using an FSFile
FSFile p_file;
void* p_fileBlk;
FS_InitFile (p_file);
FS_OpenFile(p_file, p_narcPath);
p_fileBlk = allocForAndReadInFile(p_file, POKE_METAGROSS, BLK_GROUP_TYPE);
* get info from block *
closeFileFreeBlk(p_file);
*/
Comments