File System
>w## Notice
> * please include the header file "uFile.h" first when you want to use file System
`#include "uFile.h"`
### data structure
```
typedef enum
{
DBA_RFile = 0, ///< file type (read)
DFA_DataDB, ///< trans db (read/write)
DFA_RecordDB ///< trans data table (read/write)
}DATA_FILE_ATTR;
typedef struct
{
unsigned short fileVer; ///< version(+1 while changed)
DATA_FILE_ATTR attr;
char fileName[30];
unsigned char *fileData;
unsigned int fileSize;
int (*loadDefData)(unsigned int fileIndex);
}DATA_FILE_INFO;
typedef struct _DB_EXEC
{
unsigned char exitLoop; /*!< flag: exit exec loop */
unsigned int count; /*!< */
unsigned int currIndex;
unsigned char *recordData;
unsigned int recordDataSize;
unsigned char *inData;
unsigned char *outData;
int (*execFunc)(struct _DB_EXEC *pExec);
}tsDBExec;
/*!< file error code */
typedef enum
{
UFILE_UPDATE_FAIL = -11, /*!< update file fail */
UFILE_NO_EXIST = -10, /*!< file exist*/
UFILE_PARAERROR = -9, /*!< param error */
UFILE_NO_RECORD = -8, /*!< record no find */
UFILE_REMOVE_FAIL = -7, /*!< remove file fail */
UFILE_CLOSE_FAIL = -6, /*!< close file fail */
UFILE_DELETE_FAIL = -5, /*!< delete file record fail */
UFILE_READ_FAIL = -4, /*!< read file fail */
UFILE_WRITE_FAIL = -3, /*!< write file fail */
UFILE_CREATE_FAIL = -2, /*!< file create fail */
UFILE_OPEN_FAIL = -1, /*!< file open fail */
UFILE_SUCCESS = 0 /*!< success */
}FileRet;
```
>i## UFile_CheckValidFile
### Prototype
`int UFile_CheckValidFile(unsigned int fileIndex);`
### Function
* check file by file index, then the file will be created and initialized when it do not exist
### Parameter
|Name|Type|Description|
|-|-|-|
|fileIndex|unsigned int|index of DATA_FILE_INFO array|
### Retval
|Value|Type|Description|
|-|-|-|
|SUCCESS|int|success|
|ERR_FILE|int|fail|
>i## UFile_CheckAllValidFiles
### Prototype
`int UFile_CheckAllValidFiles(DATA_FILE_INFO fileInfoList[], unsigned int fileCount);`
### Function
* check file by file list array, then they will be created and initialized when they do not exist
### Parameter
|Name|Type|Description|
|-|-|-|
|fileInfoList|DATA_FILE_INFO *|DATA_FILE_INFO array|
|fileCount|unsigned int|the total number of files|
### Retval
|Value|Type|Description|
|-|-|-|
|SUCCESS|int|success|
|ERR_FILE|int|fail|
>i## UFile_WriteFile
### Prototype
`int UFile_WriteFile(const char *fileName, unsigned char *fileData, unsigned int fileSize);`
### Function
* write data into file by file name
### Parameter
|Name|Type|Description|
|-|-|-|
|fileName|const char *|file name|
|fileData|unsigned char *|the data will be written into file|
|fileSize|unsigned int|file data length|
### Retval
|Value|Type|Description|
|-|-|-|
|SUCCESS|int|success|
|ERR_FILE|int|fail|
>i## UFile_ReadFile
### Prototype
`int UFile_ReadFile(const char *fileName, unsigned char *fileData, unsigned int fileSize);`
### Function
* read data from file by file name
### Parameter
|Name|Type|Description|
|-|-|-|
|fileName|const char *|file name|
|fileData|unsigned char *|return the file data|
|fileSize|unsigned int|the file data length you want to read|
### Retval
|Value|Type|Description|
|-|-|-|
|SUCCESS|int|success|
|ERR_FILE|int|fail|
>i## UFile_ClearFile
### Prototype
`int UFile_ClearFile(const char *fileName);`
### Function
* clear file data by file name
### Parameter
|Name|Type|Description|
|-|-|-|
|fileName|const char *|file name|
### Retval
|Value|Type|Description|
|-|-|-|
|SUCCESS|int|success|
|ERR_FILE|int|fail|
>i## UFile_RemoveFile
### Prototype
`int UFile_RemoveFile(const char *fileName);`
### Function
* remove file by file name
### Parameter
|Name|Type|Description|
|-|-|-|
|fileName|const char *|file name|
### Retval
|Value|Type|Description|
|-|-|-|
|SUCCESS|int|success|
|ERR_FILE|int|fail|
>i## UFile_WriteFileEx
### Prototype
`int UFile_WriteFileEx(const char *_fileName, unsigned int _offset, unsigned char *_fileData, unsigned int _fileSize);`
### Function
* write data to the specified position in the file
### Parameter
|Name|Type|Description|
|-|-|-|
|_fileName|const char *|file name|
|_offset|unsigned int|_offset >= 0|
|_fileData|unsigned char *|the data will be written into file|
|_fileSize|unsigned int|file data length|
### Retval
|Value|Type|Description|
|-|-|-|
|SUCCESS|int|success|
|ERR_FILE|int|fail|
>i## UFile_ReadFileEx
### Prototype
`int UFile_ReadFileEx(const char *_fileName, unsigned int _offset, unsigned char *_fileData, unsigned int _fileSize);`
### Function
* read data from the specified position in the file
### Parameter
|Name|Type|Description|
|-|-|-|
|_fileName|const char *|file name|
|_offset|unsigned int|_offset >= 0|
|_fileData|unsigned char *|return the file data|
|_fileSize|unsigned int|the file data length you want to read|
### Retval
|Value|Type|Description|
|-|-|-|
|SUCCESS|int|success|
|ERR_FILE|int|fail|
>i## UFile_WriteRecord
### Prototype
`int UFile_WriteRecord(const char *fileName, unsigned int recordIndex, unsigned char *dat, int datLen);`
### Function
* write data into record file by file name
### Parameter
|Name|Type|Description|
|-|-|-|
|fileName|const char *|record file name|
|recordIndex|unsigned int|recordIndex >= 0|
|dat|unsigned char *|the data will be written into record file|
|datLen|int|dat length|
### Retval
|Value|Type|Description|
|-|-|-|
|SUCCESS|int|success|
|ERR_FILE|int|fail|
>i## UFile_ReadRecord
### Prototype
`int UFile_ReadRecord(const char *fileName, unsigned int recordIndex, unsigned char *dat, int datLen);`
### Function
* read data from record file by file name
### Parameter
|Name|Type|Description|
|-|-|-|
|fileName|const char *|record file name|
|recordIndex|unsigned int|recordIndex >= 0|
|dat|unsigned char *|return the record file data|
|datLen|unsigned int|the record file data length you want to read|
### Retval
|Value|Type|Description|
|-|-|-|
|SUCCESS|int|success|
|ERR_FILE|int|fail|
>i## UFile_GetResource
### Prototype
`char *UFile_GetResource(char* sourceName, int* width, int* height);`
### Function
* get resource data by resource name
### Parameter
|Name|Type|Description|
|-|-|-|
|sourceName|char *|record file name|
|width|int *|the bmp resource width|
|height|int *|the bmp resource height|
### Retval
|Value|Type|Description|
|-|-|-|
|resource data pointer|char *|success|
|NULL|char *|fail|
>s## Example
```
///<check file
const DATA_FILE_INFO fileList[] = {{0001, DFA_DataDB, "db_smartpeak", NULL, 0, NULL}};
typedef enum {
DB_SMARTPEAK, DB_MaxIndex
} tsDBIndex;
int iRet = 0;
iRet = UFile_CheckValidFile(DB_SMARTPEAK);
///<check all file in file list
const DATA_FILE_INFO fileList[] = {{0001, DFA_DataDB, "db_smartpeak", NULL, 0, NULL}};
typedef enum {
DB_SMARTPEAK, DB_MaxIndex
} tsDBIndex;
int iRet = 0;
iRet = UFile_CheckAllValidFiles(fileList, DB_MaxIndex);
///<write data into file "db_smartpeak" int iRet = 0;
iRet = UFile_WriteFile("db_smartpeak", "smartpeak example", strlen("smartpeak example"));
///<read data from file "db_smartpeak"
int iRet = 0;
unsigned char buff[32] = {0};
iRet = UFile_ReadFile("db_smartpeak", buff, sizeof(buff));
///<clear file data
int iRet = 0;
iRet = UFile_ClearFile("db_smartpeak");
///<remove file data
int iRet = 0;
iRet = UFile_RemoveFile("db_smartpeak");
///<write data in file "db_smartpeak"
int iRet = 0;
iRet = UFile_WriteFileEx("db_smartpeak", 0, "smartpeak example", strlen("smartpeak example"));
///<read data from head in file "db_smartpeak"
int iRet = 0;
unsigned char buff[32] = {0};
iRet = UFile_ReadFileEx("db_smartpeak", 0, buff, sizeof(buff));
///<write data into record file "db_transTable"
typedef struct {
unsigned char transType;
unsigned char cardNum[20];
unsigned long ulAmount;
} tsTransData;
tsTransData transData = {1, "6212345678901234", 100};
const DATA_FILE_INFO fileList[] = {{0001, DFA_RecordDB, "db_transTable", &transData, sizeof(tsTransData), NULL}};
int iRet = 0;
iRet = UFile_WriteRecord("db_transTable", 0, &transData, sizeof(tsTransData));
///<read data from record file "db_transTable"
typedef struct {
unsigned char transType;
unsigned char cardNum[20];
unsigned long ulAmount;
} tsTransData;
tsTransData transData = {0};
const DATA_FILE_INFO fileList[] = {{0001, DFA_RecordDB, "db_transTable", &transData, sizeof(tsTransData), NULL}};
int iRet = 0;
iRet = UFile_ReadRecord("db_transTable", 0, &transData, sizeof(tsTransData));
///< get bmp resource from file
unsigned char *imgData = NULL;
unsigned short imgW = 0;
unsigned short imgH = 0;
imgData = (unsigned char *)UFile_GetResource("yinglian", (int *)&imgW, (int *)&imgH);
```