add MemFileSystem::dump and MemFileSystem::load
Fork of FATFileSystem by
Revision 5:3a1206d2cae6, committed 2015-04-01
- Comitter:
- mzta
- Date:
- Wed Apr 01 16:26:32 2015 +0000
- Parent:
- 4:3ff2606d5713
- Commit message:
- add void MemFilesystem::dump(FILE *fp); add void MemFilesystem::load(FILE *fp);
Changed in this revision
ChaN/ffconf.h | Show annotated file Show diff for this revision Revisions of this file |
MemFileSystem.h | Show annotated file Show diff for this revision Revisions of this file |
diff -r 3ff2606d5713 -r 3a1206d2cae6 ChaN/ffconf.h --- a/ChaN/ffconf.h Thu Aug 28 13:15:31 2014 +0100 +++ b/ChaN/ffconf.h Wed Apr 01 16:26:32 2015 +0000 @@ -126,7 +126,7 @@ / Physical Drive Configurations /----------------------------------------------------------------------------*/ -#define _VOLUMES 1 +#define _VOLUMES 2 /* Number of volumes (logical drives) to be used. */
diff -r 3ff2606d5713 -r 3a1206d2cae6 MemFileSystem.h --- a/MemFileSystem.h Thu Aug 28 13:15:31 2014 +0100 +++ b/MemFileSystem.h Wed Apr 01 16:26:32 2015 +0000 @@ -7,6 +7,10 @@ #define MBED_MEMFILESYSTEM_H #include "FATFileSystem.h" +#include "SDFileSystem.h" + +#define NUM_OF_SECTORS 2000 +#define SECTOR_SIZE 512 namespace mbed { @@ -16,37 +20,37 @@ public: // 2000 sectors, each 512 bytes (malloced as required) - char *sectors[2000]; + char *sectors[NUM_OF_SECTORS]; MemFileSystem(const char* name) : FATFileSystem(name) { memset(sectors, 0, sizeof(sectors)); } virtual ~MemFileSystem() { - for(int i = 0; i < 2000; i++) { + for(int i = 0; i < NUM_OF_SECTORS; i++) { if(sectors[i]) { free(sectors[i]); } } } - + // read a sector in to the buffer, return 0 if ok - virtual int disk_read(char *buffer, int sector) { + virtual int disk_read(uint8_t *buffer, uint64_t sector, uint8_t count) { if(sectors[sector] == 0) { // nothing allocated means sector is empty - memset(buffer, 0, 512); + memset(buffer, 0, SECTOR_SIZE); } else { - memcpy(buffer, sectors[sector], 512); + memcpy(buffer, sectors[sector], SECTOR_SIZE); } return 0; } // write a sector from the buffer, return 0 if ok - virtual int disk_write(const char *buffer, int sector) { + virtual int disk_write(const uint8_t *buffer, uint64_t sector, uint8_t count) { // if buffer is zero deallocate sector - char zero[512]; - memset(zero, 0, 512); - if(memcmp(zero, buffer, 512)==0) { + char zero[SECTOR_SIZE]; + memset(zero, 0, SECTOR_SIZE); + if(memcmp(zero, buffer, SECTOR_SIZE)==0) { if(sectors[sector] != 0) { free(sectors[sector]); sectors[sector] = 0; @@ -55,23 +59,42 @@ } // else allocate a sector if needed, and write if(sectors[sector] == 0) { - char *sec = (char*)malloc(512); + char *sec = (char*)malloc(SECTOR_SIZE); if(sec==0) { return 1; // out of memory } sectors[sector] = sec; } - memcpy(sectors[sector], buffer, 512); + memcpy(sectors[sector], buffer, SECTOR_SIZE); return 0; } // return the number of sectors - virtual int disk_sectors() { + virtual uint64_t disk_sectors() { return sizeof(sectors)/sizeof(sectors[0]); } - + + void dump(FILE *fp) { + for (int i = 0; i < NUM_OF_SECTORS; i++) { + fwrite(§ors[i], sizeof(int), 1, fp); + if (sectors[i] != 0) { + fwrite(sectors[i], sizeof(char), SECTOR_SIZE, fp); + } + } + } + + void load(FILE *fp) { + int sec_info = 0; + for (int i = 0; i < NUM_OF_SECTORS; i++) { + fread(&sec_info, sizeof(int), 1, fp); + if (sec_info != 0) { + char *sec = (char *)malloc(SECTOR_SIZE); + fread(sec, sizeof(char), SECTOR_SIZE, fp); + sectors[i] = sec; + } + } + } }; - } #endif \ No newline at end of file