Fork of Adam Green's library with .cpp fix for current compiler

Dependents:   MSCUsbHost mpod_nhk_english mpod_picasa_photoframe mpod_nhk_english_spxml ... more

Fork of FatFileSystem by Adam Green

Committer:
igorsk
Date:
Mon Jul 30 13:45:05 2012 +0000
Revision:
1:88f22c32a456
Parent:
0:6ceefe1c53e4
renamed .c to .cpp

Who changed what in which revision?

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