]>
git.gir.st - tmk_keyboard.git/blob - tmk_core/tool/mbed/mbed-sdk/libraries/fs/fat/MemFileSystem.h
1 /* mbed Microcontroller Library - MemFileSystem
2 * Copyright (c) 2008, sford
6 #ifndef MBED_MEMFILESYSTEM_H
7 #define MBED_MEMFILESYSTEM_H
9 #include "FATFileSystem.h"
14 class MemFileSystem
: public FATFileSystem
18 // 2000 sectors, each 512 bytes (malloced as required)
21 MemFileSystem(const char* name
) : FATFileSystem(name
) {
22 memset(sectors
, 0, sizeof(sectors
));
25 virtual ~MemFileSystem() {
26 for(int i
= 0; i
< 2000; i
++) {
33 // read a sector in to the buffer, return 0 if ok
34 virtual int disk_read(char *buffer
, int sector
) {
35 if(sectors
[sector
] == 0) {
36 // nothing allocated means sector is empty
37 memset(buffer
, 0, 512);
39 memcpy(buffer
, sectors
[sector
], 512);
44 // write a sector from the buffer, return 0 if ok
45 virtual int disk_write(const char *buffer
, int sector
) {
46 // if buffer is zero deallocate sector
49 if(memcmp(zero
, buffer
, 512)==0) {
50 if(sectors
[sector
] != 0) {
51 free(sectors
[sector
]);
56 // else allocate a sector if needed, and write
57 if(sectors
[sector
] == 0) {
58 char *sec
= (char*)malloc(512);
60 return 1; // out of memory
62 sectors
[sector
] = sec
;
64 memcpy(sectors
[sector
], buffer
, 512);
68 // return the number of sectors
69 virtual int disk_sectors() {
70 return sizeof(sectors
)/sizeof(sectors
[0]);