mbed-os

Fork of mbed-os by erkin yucel

Committer:
xuaner
Date:
Thu Jul 20 14:26:57 2017 +0000
Revision:
1:3deb71413561
Parent:
0:f269e3021894
mbed_os

Who changed what in which revision?

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