Dependencies:   mbed

Committer:
simon
Date:
Thu Jan 12 12:59:33 2012 +0000
Revision:
1:208803a150b2
Parent:
0:560a6744936c
fix on line 61 of diskio, sector -> s

Who changed what in which revision?

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