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