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