Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
SDFileSystem/FATFileSystem/MemFileSystem.h@0:92b180c8d407, 2021-01-05 (annotated)
- Committer:
- jamesheavey
- Date:
- Tue Jan 05 01:14:11 2021 +0000
- Revision:
- 0:92b180c8d407
test
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jamesheavey | 0:92b180c8d407 | 1 | /* mbed Microcontroller Library - MemFileSystem |
jamesheavey | 0:92b180c8d407 | 2 | * Copyright (c) 2008, sford |
jamesheavey | 0:92b180c8d407 | 3 | */ |
jamesheavey | 0:92b180c8d407 | 4 | |
jamesheavey | 0:92b180c8d407 | 5 | |
jamesheavey | 0:92b180c8d407 | 6 | #ifndef MBED_MEMFILESYSTEM_H |
jamesheavey | 0:92b180c8d407 | 7 | #define MBED_MEMFILESYSTEM_H |
jamesheavey | 0:92b180c8d407 | 8 | |
jamesheavey | 0:92b180c8d407 | 9 | #include "FATFileSystem.h" |
jamesheavey | 0:92b180c8d407 | 10 | |
jamesheavey | 0:92b180c8d407 | 11 | namespace mbed |
jamesheavey | 0:92b180c8d407 | 12 | { |
jamesheavey | 0:92b180c8d407 | 13 | |
jamesheavey | 0:92b180c8d407 | 14 | class MemFileSystem : public FATFileSystem |
jamesheavey | 0:92b180c8d407 | 15 | { |
jamesheavey | 0:92b180c8d407 | 16 | public: |
jamesheavey | 0:92b180c8d407 | 17 | |
jamesheavey | 0:92b180c8d407 | 18 | // 2000 sectors, each 512 bytes (malloced as required) |
jamesheavey | 0:92b180c8d407 | 19 | char *sectors[2000]; |
jamesheavey | 0:92b180c8d407 | 20 | |
jamesheavey | 0:92b180c8d407 | 21 | MemFileSystem(const char* name) : FATFileSystem(name) { |
jamesheavey | 0:92b180c8d407 | 22 | memset(sectors, 0, sizeof(sectors)); |
jamesheavey | 0:92b180c8d407 | 23 | } |
jamesheavey | 0:92b180c8d407 | 24 | |
jamesheavey | 0:92b180c8d407 | 25 | virtual ~MemFileSystem() { |
jamesheavey | 0:92b180c8d407 | 26 | for(int i = 0; i < 2000; i++) { |
jamesheavey | 0:92b180c8d407 | 27 | if(sectors[i]) { |
jamesheavey | 0:92b180c8d407 | 28 | free(sectors[i]); |
jamesheavey | 0:92b180c8d407 | 29 | } |
jamesheavey | 0:92b180c8d407 | 30 | } |
jamesheavey | 0:92b180c8d407 | 31 | } |
jamesheavey | 0:92b180c8d407 | 32 | |
jamesheavey | 0:92b180c8d407 | 33 | // read a sector in to the buffer, return 0 if ok |
jamesheavey | 0:92b180c8d407 | 34 | virtual int disk_read(char *buffer, int sector) { |
jamesheavey | 0:92b180c8d407 | 35 | if(sectors[sector] == 0) { |
jamesheavey | 0:92b180c8d407 | 36 | // nothing allocated means sector is empty |
jamesheavey | 0:92b180c8d407 | 37 | memset(buffer, 0, 512); |
jamesheavey | 0:92b180c8d407 | 38 | } else { |
jamesheavey | 0:92b180c8d407 | 39 | memcpy(buffer, sectors[sector], 512); |
jamesheavey | 0:92b180c8d407 | 40 | } |
jamesheavey | 0:92b180c8d407 | 41 | return 0; |
jamesheavey | 0:92b180c8d407 | 42 | } |
jamesheavey | 0:92b180c8d407 | 43 | |
jamesheavey | 0:92b180c8d407 | 44 | // write a sector from the buffer, return 0 if ok |
jamesheavey | 0:92b180c8d407 | 45 | virtual int disk_write(const char *buffer, int sector) { |
jamesheavey | 0:92b180c8d407 | 46 | // if buffer is zero deallocate sector |
jamesheavey | 0:92b180c8d407 | 47 | char zero[512]; |
jamesheavey | 0:92b180c8d407 | 48 | memset(zero, 0, 512); |
jamesheavey | 0:92b180c8d407 | 49 | if(memcmp(zero, buffer, 512)==0) { |
jamesheavey | 0:92b180c8d407 | 50 | if(sectors[sector] != 0) { |
jamesheavey | 0:92b180c8d407 | 51 | free(sectors[sector]); |
jamesheavey | 0:92b180c8d407 | 52 | sectors[sector] = 0; |
jamesheavey | 0:92b180c8d407 | 53 | } |
jamesheavey | 0:92b180c8d407 | 54 | return 0; |
jamesheavey | 0:92b180c8d407 | 55 | } |
jamesheavey | 0:92b180c8d407 | 56 | // else allocate a sector if needed, and write |
jamesheavey | 0:92b180c8d407 | 57 | if(sectors[sector] == 0) { |
jamesheavey | 0:92b180c8d407 | 58 | char *sec = (char*)malloc(512); |
jamesheavey | 0:92b180c8d407 | 59 | if(sec==0) { |
jamesheavey | 0:92b180c8d407 | 60 | return 1; // out of memory |
jamesheavey | 0:92b180c8d407 | 61 | } |
jamesheavey | 0:92b180c8d407 | 62 | sectors[sector] = sec; |
jamesheavey | 0:92b180c8d407 | 63 | } |
jamesheavey | 0:92b180c8d407 | 64 | memcpy(sectors[sector], buffer, 512); |
jamesheavey | 0:92b180c8d407 | 65 | return 0; |
jamesheavey | 0:92b180c8d407 | 66 | } |
jamesheavey | 0:92b180c8d407 | 67 | |
jamesheavey | 0:92b180c8d407 | 68 | // return the number of sectors |
jamesheavey | 0:92b180c8d407 | 69 | virtual int disk_sectors() { |
jamesheavey | 0:92b180c8d407 | 70 | return sizeof(sectors)/sizeof(sectors[0]); |
jamesheavey | 0:92b180c8d407 | 71 | } |
jamesheavey | 0:92b180c8d407 | 72 | |
jamesheavey | 0:92b180c8d407 | 73 | }; |
jamesheavey | 0:92b180c8d407 | 74 | |
jamesheavey | 0:92b180c8d407 | 75 | } |
jamesheavey | 0:92b180c8d407 | 76 | |
jamesheavey | 0:92b180c8d407 | 77 | #endif |