allow file seek negative offset, implement helper readFile() writeFile()

This commit is contained in:
hathach committed 2017-03-05 19:06:48 +07:00
1 parent ed7ee448ee
commit bfca2655bb
4 files changed
+62 -4

No files matched your search

+40 -1
View File
@@ -39,7 +39,7 @@
#include "syscfg/syscfg.h"
#include "hal/hal_flash.h"
#define NFFS_AREA_MAX 7
#define NFFS_AREA_MAX 8
NewtNffs Nffs;
@@ -77,6 +77,7 @@ bool NewtNffs::begin(void)
// If not formatted, format it
if (FS_ECORRUPT == errnum)
{
PRINT_MESS("No FS detected, format");
errnum = nffs_format(descs);
VERIFY_STATUS( errnum, false );
}
@@ -143,3 +144,41 @@ bool NewtNffs::rename(const char* from, const char* to)
errnum = fs_rename(from, to);
return (errnum == FS_EOK);
}
uint32_t NewtNffs::readFile (const char* filename, void* buffer, uint32_t bufsize, int32_t offset)
{
NffsFile f(filename);
VERIFY ( f.exists(), 0 );
if (offset != 0)
{
VERIFY ( f.seek(offset), 0 );
}
uint32_t count = f.read( (uint8_t*) buffer, bufsize);
errnum = f.errnum;
f.close();
return count;
}
bool NewtNffs::writeFile(const char* filename, const void* data, uint32_t count, int32_t offset)
{
NffsFile f(filename, FS_ACCESS_WRITE);
// File must exists when offset not zero
if ( offset != 0 )
{
VERIFY ( f.seek(offset), 0 );
}
bool result = f.write( (const uint8_t*) data, count);
errnum = f.errnum;
f.close();
return result;
}
+8
View File
@@ -73,6 +73,14 @@ public:
// Remove a non-readonly file or an empty directory
bool rename(const char* from, const char* to);
bool mv (const char* from, const char* to) { return rename(from, to); }
// utils
uint32_t readFile (const char* filename, void* buffer, uint32_t bufsize, int32_t offset=0);
bool writeFile(const char* filename, const void* data, uint32_t count, int32_t offset=0);
bool writeFile(const char* filename, const char* str , int32_t offset=0)
{
return writeFile(filename, str, strlen(str), offset);
}
};
+10 -2
View File
@@ -53,6 +53,11 @@ NffsFile::NffsFile(const char* path, uint8_t flags)
open(path, flags);
}
NffsFile::~NffsFile()
{
if (_file) close();
}
bool NffsFile::open(const char* path, uint8_t flags)
{
errnum = fs_open(path, flags, &_file);
@@ -90,6 +95,7 @@ bool NffsFile::exists(void)
bool NffsFile::close(void)
{
errnum = fs_close(_file);
_file = NULL;
return (errnum == FS_EOK);
}
@@ -107,9 +113,11 @@ uint32_t NffsFile::tell(void)
return fs_getpos(_file);
}
bool NffsFile::seek(uint32_t offset)
bool NffsFile::seek(int32_t offset)
{
errnum = fs_seek(_file, offset);
uint32_t abs_offset = (offset >= 0) ? offset : (size() + 1 + offset);
errnum = fs_seek(_file, abs_offset);
return (errnum == FS_EOK);
}
+4 -1
View File
@@ -49,6 +49,8 @@ public:
int errnum;
NffsFile(void);
virtual ~NffsFile();
NffsFile(const char* path, uint8_t flags = FS_ACCESS_READ);
bool open(const char* path, uint8_t flags = FS_ACCESS_READ);
@@ -59,7 +61,8 @@ public:
uint32_t size(void);
uint32_t tell(void);
bool seek(uint32_t offset);
bool seek(int32_t offset);
bool seekForward (uint32_t offset);
bool seekBackward(uint32_t offset);