123

Dependents:   SDFileSystem

Fork of FATFileSystem by mbed official

Committer:
screamer
Date:
Mon Mar 17 14:09:00 2014 +0000
Revision:
3:e960e2b81a3c
Added code documentation and fixed compile warning

Who changed what in which revision?

UserRevisionLine numberNew contents of line
screamer 3:e960e2b81a3c 1 /* mbed Microcontroller Library - MemFileSystem
screamer 3:e960e2b81a3c 2 * Copyright (c) 2008, sford
screamer 3:e960e2b81a3c 3 */
screamer 3:e960e2b81a3c 4
screamer 3:e960e2b81a3c 5
screamer 3:e960e2b81a3c 6 #ifndef MBED_MEMFILESYSTEM_H
screamer 3:e960e2b81a3c 7 #define MBED_MEMFILESYSTEM_H
screamer 3:e960e2b81a3c 8
screamer 3:e960e2b81a3c 9 #include "FATFileSystem.h"
screamer 3:e960e2b81a3c 10
screamer 3:e960e2b81a3c 11 namespace mbed
screamer 3:e960e2b81a3c 12 {
screamer 3:e960e2b81a3c 13
screamer 3:e960e2b81a3c 14 class MemFileSystem : public FATFileSystem
screamer 3:e960e2b81a3c 15 {
screamer 3:e960e2b81a3c 16 public:
screamer 3:e960e2b81a3c 17
screamer 3:e960e2b81a3c 18 // 2000 sectors, each 512 bytes (malloced as required)
screamer 3:e960e2b81a3c 19 char *sectors[2000];
screamer 3:e960e2b81a3c 20
screamer 3:e960e2b81a3c 21 MemFileSystem(const char* name) : FATFileSystem(name) {
screamer 3:e960e2b81a3c 22 memset(sectors, 0, sizeof(sectors));
screamer 3:e960e2b81a3c 23 }
screamer 3:e960e2b81a3c 24
screamer 3:e960e2b81a3c 25 virtual ~MemFileSystem() {
screamer 3:e960e2b81a3c 26 for(int i = 0; i < 2000; i++) {
screamer 3:e960e2b81a3c 27 if(sectors[i]) {
screamer 3:e960e2b81a3c 28 free(sectors[i]);
screamer 3:e960e2b81a3c 29 }
screamer 3:e960e2b81a3c 30 }
screamer 3:e960e2b81a3c 31 }
screamer 3:e960e2b81a3c 32
screamer 3:e960e2b81a3c 33 // read a sector in to the buffer, return 0 if ok
screamer 3:e960e2b81a3c 34 virtual int disk_read(char *buffer, int sector) {
screamer 3:e960e2b81a3c 35 if(sectors[sector] == 0) {
screamer 3:e960e2b81a3c 36 // nothing allocated means sector is empty
screamer 3:e960e2b81a3c 37 memset(buffer, 0, 512);
screamer 3:e960e2b81a3c 38 } else {
screamer 3:e960e2b81a3c 39 memcpy(buffer, sectors[sector], 512);
screamer 3:e960e2b81a3c 40 }
screamer 3:e960e2b81a3c 41 return 0;
screamer 3:e960e2b81a3c 42 }
screamer 3:e960e2b81a3c 43
screamer 3:e960e2b81a3c 44 // write a sector from the buffer, return 0 if ok
screamer 3:e960e2b81a3c 45 virtual int disk_write(const char *buffer, int sector) {
screamer 3:e960e2b81a3c 46 // if buffer is zero deallocate sector
screamer 3:e960e2b81a3c 47 char zero[512];
screamer 3:e960e2b81a3c 48 memset(zero, 0, 512);
screamer 3:e960e2b81a3c 49 if(memcmp(zero, buffer, 512)==0) {
screamer 3:e960e2b81a3c 50 if(sectors[sector] != 0) {
screamer 3:e960e2b81a3c 51 free(sectors[sector]);
screamer 3:e960e2b81a3c 52 sectors[sector] = 0;
screamer 3:e960e2b81a3c 53 }
screamer 3:e960e2b81a3c 54 return 0;
screamer 3:e960e2b81a3c 55 }
screamer 3:e960e2b81a3c 56 // else allocate a sector if needed, and write
screamer 3:e960e2b81a3c 57 if(sectors[sector] == 0) {
screamer 3:e960e2b81a3c 58 char *sec = (char*)malloc(512);
screamer 3:e960e2b81a3c 59 if(sec==0) {
screamer 3:e960e2b81a3c 60 return 1; // out of memory
screamer 3:e960e2b81a3c 61 }
screamer 3:e960e2b81a3c 62 sectors[sector] = sec;
screamer 3:e960e2b81a3c 63 }
screamer 3:e960e2b81a3c 64 memcpy(sectors[sector], buffer, 512);
screamer 3:e960e2b81a3c 65 return 0;
screamer 3:e960e2b81a3c 66 }
screamer 3:e960e2b81a3c 67
screamer 3:e960e2b81a3c 68 // return the number of sectors
screamer 3:e960e2b81a3c 69 virtual int disk_sectors() {
screamer 3:e960e2b81a3c 70 return sizeof(sectors)/sizeof(sectors[0]);
screamer 3:e960e2b81a3c 71 }
screamer 3:e960e2b81a3c 72
screamer 3:e960e2b81a3c 73 };
screamer 3:e960e2b81a3c 74
screamer 3:e960e2b81a3c 75 }
screamer 3:e960e2b81a3c 76
screamer 3:e960e2b81a3c 77 #endif