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