MTK文件读写相关
文件操作函数的头文件为fs_func.h
常用的文件操作函数有:
extern int FS_Open(const WCHAR * FileName, UINT Flag);
extern int FS_Close(FS_HANDLE FileHandle);
extern int FS_Read(FS_HANDLE FileHandle, void * DataPtr, UINT Length, UINT * Read);
extern int FS_Write(FS_HANDLE FileHandle, void * DataPtr, UINT Length, UINT * Written);
extern int FS_Seek(FS_HANDLE FileHandle, int Offset, int Whence);
extern int FS_Commit(FS_HANDLE FileHandle);
extern int FS_Delete(const WCHAR * FileName);
extern int FS_GetCurrentDir(WCHAR * DirName, UINT MaxLength);
extern int FS_SetCurrentDir(const WCHAR * DirName);
extern int FS_CreateDir(const WCHAR * DirName);
extern int FS_RemoveDir(const WCHAR * DirName);
extern int FS_FindFirst(const WCHAR * NamePattern, BYTE Attr, BYTE AttrMask,
FS_DOSDirEntry * FileInfo, WCHAR * FileName, UINT MaxLength);
extern int FS_FindNext(FS_HANDLE FileHandle, FS_DOSDirEntry * FileInfo, WCHAR *
FileName, UINT MaxLength);
文件操作所用到的数据结构有都放在fs_type.h里,如FS_Open的第二个参数(也即打开模式)列表
如下:
#define FS_READ_WRITE 0x00000000L
#define FS_READ_ONLY 0x00000100L
#define FS_OPEN_SHARED 0x00000200L
#define FS_OPEN_NO_DIR 0x00000400L
#define FS_OPEN_DIR 0x00000800L
#define FS_CREATE 0x00010000L
#define FS_CREATE_ALWAYS 0x00020000L
#define FS_COMMITTED 0x01000000L
#define FS_CACHE_DATA 0x02000000L
#define FS_LAZY_DATA 0x04000000L
#define FS_NONBLOCK_MODE 0x10000000L
#define FS_PROTECTION_MODE 0x20000000L
文件操作的返回值定义放在文件fs_errcode.h,如:
#define FS_NO_ERROR 0
#define FS_ERROR_RESERVED -1
#define FS_PARAM_ERROR -2 /* User */
#define FS_INVALID_FILENAME -3 /* User */
#define FS_DRIVE_NOT_FOUND -4 /* User or Fatal */
#define FS_TOO_MANY_FILES -5 /* User or Normal: use over max file
handle number or more than 256 files in sort */
#define FS_NO_MORE_FILES -6 /* Normal */
#define FS_WRONG_MEDIA -7 /* Fatal */
#define FS_INVALID_FILE_SYSTEM -8 /* Fatal */
#define FS_FILE_NOT_FOUND -9 /* User or Normal */
#define FS_INVALID_FILE_HANDLE -10 /* User or Normal */
#define FS_UNSUPPORTED_DEVICE -11 /* User */
#define FS_UNSUPPORTED_DRIVER_FUNCTION -12 /* User or Fatal */
#define FS_CORRUPTED_PARTITION_TABLE -13 /* fatal */
#define FS_TOO_MANY_DRIVES -14 /* not use so far */
#define FS_INVALID_FILE_POS -15 /* User */
#define FS_ACCESS_DENIED -16 /* User or Normal */
#define FS_STRING_BUFFER_TOO_SMALL -17 /* User */
如果返回值大于0,说明返回正确。
文件操作函数实现放在FSSim_core.c里:如:
int FS_Close(FS_HANDLE File)
{
int map_fh=0;
/* verify file handle */
if (File > FSSIM_MAX_FILE || File <= 0)
return FS_INVALID_FILE_HANDLE;
else if (fssim_file[File].state != STATE_INUSE)
return FS_INVALID_FILE_HANDLE;
else if (fssim_vfgen.link[File] > 0)
map_fh = fssim_vfgen.link[File];
else if (fssim_file[File].handle == INVALID_HANDLE_VALUE)
return FS_INVALID_FILE_HANDLE;
/* close the file handle */
fssim_take_mutex(fssim_mutex); /* critical section { */
if (map_fh > 0) {
fssim_vfgen.link[File] = 0;
} else {
fssim_close_fh(File);
}
fssim_give_mutex(fssim_mutex); /* critical section } */
fssim_free_fh(File);
return FS_NO_ERROR;
}
**文件系统
MTK使用FAT文件系统,对于模拟器文件系统放在目录WIN32FS下,如DRIVE_C、DRIVE_D等等。例如:
FS_Open(L"C:\\a.txt", FS_READ_ONLY);
上面语句以读模式打开C盘下的a.txt文件,文件名的字符编码是UCS-2。
阅读(4124) | 评论(1) | 转发(0) |