📄 fs_apivalidation.c
字号:
/*
**********************************************************************
* Micrium, Inc.
* 949 Crestview Circle
* Weston, FL 33327-1848
*
* uC/FS
*
* (c) Copyright 2001 - 2006, Micrium, Inc.
* All rights reserved.
*
***********************************************************************
----------------------------------------------------------------------
----------------------------------------------------------------------
File ...Start.c
Purpose...Start application for file system.
-------- END-OF-HEADER ---------------------------------------------
*/
#include <stdio.h>
#include <string.h>
#include "FS.h"
#include "FS_Int.h"
/*********************************************************************
*
* Defines
*
**********************************************************************
*/
#define NUM_FILES 40
/*********************************************************************
*
* Static data
*
**********************************************************************
*/
static U8 _aBuffer[4096];
/*********************************************************************
*
* Public code
*
**********************************************************************
*/
/*********************************************************************
*
* _Log
*/
static void _Log(const char * s) {
printf(s);
}
/*********************************************************************
*
* _ErrorOut
*/
static void _ErrorOut(const char * s) {
printf(s);
while(1);
}
/*********************************************************************
*
* _FillBuffer
*
*/
static void _FillBuffer(void) {
unsigned i;
for (i = 0; i < sizeof(_aBuffer); i++) {
_aBuffer[i] = i & 0xff;
}
}
/*********************************************************************
*
* _WriteFile
*
* Description
* Creates a file and writes the data to the file.
*
*
* Return value
* 0 - OK.
* 1 - Error, file could either not be created/written.
*/
static int _WriteFile(const char * sFileName, const char * sType, const void * pData, unsigned NumBytes) {
FS_FILE * pFile;
U32 NumBytesWritten;
int r;
r = 1;
pFile = FS_FOpen(sFileName, sType);
if (pFile) {
NumBytesWritten = FS_Write(pFile, pData, NumBytes);
if (NumBytesWritten == NumBytes) {
r = 0;
}
FS_FClose(pFile);
}
return r;
}
/*********************************************************************
*
* _WriteFile
*
* Description
* opens a file and read the data from the file.
*
*
* Return value
* 0 - OK.
* 1 - Error, file could not be found or less data read than expected.
*/
static int _ReadFile(const char * sFileName, const char * sType, void * pData, unsigned NumBytes) {
FS_FILE * pFile;
U32 NumBytesRead;
int r;
r = 1;
pFile = FS_FOpen(sFileName, sType);
if (pFile) {
NumBytesRead = FS_Read(pFile, pData, NumBytes);
if (NumBytesRead == NumBytes) {
r = 0;
}
FS_FClose(pFile);
}
return r;
}
/*********************************************************************
*
* _FileExist
*
* Description
* Tries to open a file. When we receive a valid file handle,
* we know that the file does exist.
*
* Return value
* 1 - File exists.
* 0 - File does not exist.
*/
static int _FileExist(const char * sFileName) {
FS_FILE * pFile;
int r;
r = 0;
//
// Open the file if it exists or not.
//
pFile = FS_FOpen(sFileName, "r");
if (pFile) {
//
// File exists, close the opened file handle.
//
FS_FClose(pFile);
r = 1;
}
return r;
}
/*********************************************************************
*
* _FormatTest
*
* Description
* Performs a high level format and checks for success.
*
*/
static void _FormatTest(void) {
_Log("High level formatting volume...");
if (FS_Format("", NULL)) {
_ErrorOut("Failed\n");
}
_Log("Ok\n\n");
}
/*********************************************************************
*
* _FileOpenModeTest
*
* Description
* Test different modes for opening a file.
*
*/
static void _FileOpenModeTest(void) {
int r;
const char * sString;
unsigned NumBytes;
//
// Mode "w"
//
_Log("Test FS_FOpen modes\n");
_Log("Mode 'w'...");
//
// Check if "w" can write a file and if we
// can read back its contents
//
sString = "1234567890";
NumBytes = strlen(sString);
r = _WriteFile("test.txt", "w", sString, NumBytes);
if (r != 0) {
_ErrorOut("Failed\n");
}
r = _ReadFile("test.txt", "r", _aBuffer, NumBytes);
if (r != 0) {
_ErrorOut("Read verification failure\n");
}
if (FS_MEMCMP(_aBuffer, "1234567890", NumBytes) !=0) {
_ErrorOut("Read verification failure\n");
}
//
// Mode "w" Test overwrite
//
sString = "abcde";
NumBytes = strlen(sString);
r = _WriteFile("test.txt", "w", sString, NumBytes);
if (r != 0) {
_ErrorOut("Failed\n");
}
r = _ReadFile("test.txt", "r", _aBuffer, NumBytes);
if (r != 0) {
_ErrorOut("Failed\n");
}
if (FS_MEMCMP(_aBuffer, "abcde", NumBytes) != 0) {
_ErrorOut("Failed\n");
}
_Log("OK\n");
//
//
// Mode "a"
//
_Log("Mode 'a'...");
//
// Check if "a" can append to file
//
sString = "fghij";
NumBytes = strlen(sString);
r = _WriteFile("test.txt", "a", sString, NumBytes);
if (r != 0) {
_ErrorOut("Failed\n");
}
r = _ReadFile("test.txt", "r", _aBuffer, NumBytes);
if (r != 0) {
_ErrorOut("Failed\n");
}
if (FS_MEMCMP(_aBuffer, "abcdefghij", NumBytes) != 0) {
_ErrorOut("Failed\n");
}
//
// Check if "a" can create a file
//
r = FS_Remove("test.txt");
if (r != 0) {
_ErrorOut("Failed\n");
}
sString = "12345";
NumBytes = strlen(sString);
r = _WriteFile("test.txt", "a", sString, NumBytes);
if (r != 0) {
_ErrorOut("Failed\n");
}
r = _ReadFile("test.txt", "r", _aBuffer, NumBytes);
if (r != 0) {
_ErrorOut("Failed\n");
}
if (FS_MEMCMP(_aBuffer, "12345", NumBytes) != 0) {
_ErrorOut("Failed\n");
}
_Log("OK\n");
//
// Mode "r+"
//
_Log("Mode 'r+'...");
//
// Check if partial overwrite is possible.
//
sString = "ab";
NumBytes = strlen(sString);
r = _WriteFile("test.txt", "r+", sString, NumBytes);
if (r != 0) {
_ErrorOut("Failed\n");
}
r = _ReadFile("test.txt", "r+", _aBuffer, NumBytes);
if (r != 0) {
_ErrorOut("Too few bytes read\n");
}
if (FS_MEMCMP(_aBuffer, "ab345", NumBytes) != 0) {
_ErrorOut("Failed\n");
}
//
// Verify "r+" cannot create a file.
//
r = FS_Remove("test.txt");
if (r != 0) {
_ErrorOut("Could not remove file\n");
}
sString = "dummytext";
NumBytes = strlen(sString);
r = _WriteFile("test.txt", "r+", sString, NumBytes);
if (r == 0) {
_ErrorOut("r+ did not report an error (Creating file ?)\n");
}
_Log("OK\n");
//
// Mode "w+"
//
_Log("Mode 'w+'...");
//
// Check if file is truncated to zero
//
sString = "12345";
NumBytes = strlen(sString);
r = _WriteFile("test.txt", "w", sString, NumBytes);
if (r != 0) {
_ErrorOut("Failed\n");
}
sString = "12";
NumBytes = strlen(sString);
r = _WriteFile("test.txt", "w+", sString, NumBytes);
if (r != 0) {
_ErrorOut("Failed\n");
}
r = _ReadFile("test.txt", "r", _aBuffer, NumBytes);
if (r != 0) {
_ErrorOut("Failed\n");
}
if (FS_MEMCMP(_aBuffer, "12", NumBytes) != 0) {
_ErrorOut("Failed\n");
}
_Log("OK\n");
//
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -