/*********************************************************************/
/*** File system API                                               ***/
/*********************************************************************/


/*********************/
/***  FindFile     ***/
/*********************/
INT8 FindFile(UINT32 path, UINT32 filename);
	Input Args - path, filename
	Output Args - None
	Return Value - FAT index if file is found; -1 if not found

This function finds the file with the specified file name, and returns 
its index in FAT.  If no file can be found, -1 is returned. This is maintainde
for compatibility with the Renesas web server.


/*********************/
/***  FindFilePath ***/
/*********************/
INT8 FindFile(UINT32 path, UINT32 filename);
	Input Args - path, filename
	Output Args - None
	Return Value - FAT index if file is found; -1 if not found

This function finds the file with the specified file & path name, and returns 
its index in FAT.  If no file can be found, -1 is returned. This is different
than the FindFile function due to the directory requirement.


/*********************/
/***  GetFileSize  ***/
/*********************/
UINT16 GetFileSize( UINT8 fat_index );
	Input Args - FAT index
	Output Args - None
	Return Value - File size

This function returns the size of a file specified by the FAT index.  
The returned value is the total size of the file, which may include 
several file sectors.



/*********************/
/***  ReadFile     ***/
/*********************/
UINT16 ReadFile(UINT8 fat_index, UINT16 offset, UINT8 * buffer, UINT16 bytes2read);
	Input Args - FAT index, File offset, Buffer for returned data, # of bytes to read
	Output Args - None
	Return Value - Number of bytes read

This function reads from a specific file (found using the
FindFile command) and copies the byte count into the buffer.  The
calling function is responsible for allocating the buffer correctly to
prevent memory problems.  The function will use the given offset to
find the correct block to start copying from and if the byte count is
larger than the remaining bytes in the sector, it will advance to the
next block (up to the end of the file).



/*********************/
/***  CreateFile      ***/
/*********************/
INT8 CreateFile(UINT32 path, UINT32 filename);
	Input Args - path, filename
	Output Args - None
	Return Value - FAT index of the added file; -1 if failed

This function is used to add a new file with the specified file name 
into the file system, by creating a new entry in the FAT table.  The 
entry will indicate that the size of the new file is zero, and the 
file content is NULL.  The calling function can then use the WriteFile 
command with the returned FAT index to write into the file.



/*********************/
/***  DeleteFile   ***/
/*********************/
INT8 DeleteFile(UINT8 fat_index);
	Input Args - FAT index
	Output Args - None
	Return Value - FAT index of the deleted file; -1 if failed

This function deletes the file indicated by the FAT index.  The 
calling function can use the FindFile command to get the FAT index 
of a file.



/*********************/
/***  WriteFile    ***/
/*********************/
UINT16 WriteFile(UINT8 fat_index, UINT16 offset, UINT8 * buffer, UINT16 bytes2write);
	Input Args - FAT index, File offset, Buffer of data to be written, # of bytes to write
	Output Args - None
	Return Value - Number of bytes written

This function writes the content in the buffer to a specific 
file (found using the FindFile command).  The function will use 
the given offset to find the correct block to start writing, and 
if the byte count is larger than the block size, it will allocate 
a new empty block and continue the writing process.  If the starting 
address is in the middle of the file, it will overwrite the original 
content (for the given length).



/***************************/
/***  CreateDirectory    ***/
/***************************/
INT8 CreateDirectory(UINT32 path, UINT32 directory_name);
	Input Args - path, name of the directory
	Output Args - None
	Return Value - FAT index of the created directory; -1 is failed

This function creates an empty directory under the specified path.



/***************************/
/***  MoveFile           ***/
/***************************/
INT8 MoveFile(UINT32 path1, UINT32 file1, UINT32 path2, UINT32 file2);
	Input Args - the path and filename of the source and the destination
	Output Args - None
	Return Value - FAT index of the file after movement; -1 if failed

This function moves the file from 'path1/file1' to 'path2/file2'.
The current version of API doesn't support the movement of directories.



/***************************/
/***  RemoveDirectory    ***/
/***************************/
INT8 RemoveDirectory(UINT32 dirpath);
	Input Args - the path of the directory
	Output Args - None
	Return Value - FAT index of the removed directory; -1 if failed

This function removes the directory specified by the input argument.
The directory must be empty before being removed.



/***************************/
/***  IsDirectory    ***/
/***************************/
INT8 IsDirectory(UINT32 dirpath);
	Input Args - the path of the directory
	Output Args - None
	Return Value - 1 if the file specified by the input argument is a directory; 0 if it's not

This function can be used to determine whether a file is a directory or not.



/***************************/
/***  Format             ***/
/***************************/
INT8 Format();
	Input Args - None
	Output Args - None
	Return Value - 0 if the format succeeds;

This function is used to format/reinitialize the whole file system.
It will release all the file blocks and empty the FAT table.



/***************************/
/***  List               ***/
/***************************/
INT8* List(UINT32 dirpath);
	Input Args - the path of a directory
	Output Args - None
	Return Value - a pointer to an array containing the FAT index of the files in the directory

This function is used to get a list of files (represented by their FAT index) in a directory 
specified by the input argument. 



/***************************/
/***  SpaceStatus        ***/
/***************************/
void SpaceStatus(UINT16 * used_files, UINT16 * used_blocks, UINT16 * used_space, UINT16 * bytes_total)
	Input Args - None
	Output Args - # of files used, blocks used, space used, total RAM usage
	Return Value - None

This function is used to get a status of the file system and its utilization.  This is for debug
purposes to validate the sizes of certain files and the usage of the system.


/***************************/
/***  DirListing         ***/
/***************************/
void DirListing(UINT8 dir = 0);
	Input Args - which directory index to begin the listing from, default is root.
	Output Args - None
	Return Value - None

This function is for debug and provides a text listing of the directories.


/***************************/
/***  DumpFile           ***/
/***************************/

void DumpFile(UINT8 index, UINT8 * name);
	Input Args - index of the file to be dumped and the file name to dump it to.
	Output Args - None
	Return Value - None

This takes the file at the given FAT offset and dumps it to the given file name.  This 
will only work on an OS with a file I/O.


/*********************************************************************/
/*********************************************************************/
