-
Notifications
You must be signed in to change notification settings - Fork 3
/
MemFileSystem.h
80 lines (67 loc) · 1.46 KB
/
MemFileSystem.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#ifndef MBED_MEMFILESYSTEM_H
#define MBED_MEMFILESYSTEM_H
#include "FATFileSystem.h"
namespace mbed
{
class MemFileSystem : public FATFileSystem
{
public:
char *sectors[2000];
MemFileSystem(const char *name) : FATFileSystem(name)
{
memset(sectors, 0, sizeof(sectors));
}
virtual ~MemFileSystem()
{
for (int i = 0; i < 2000; i++)
{
if (sectors[i])
{
free(sectors[i]);
}
}
}
virtual int disk_read(char *buffer, int sector)
{
if (sectors[sector] == 0)
{
memset(buffer, 0, 512);
}
else
{
memcpy(buffer, sectors[sector], 512);
}
return 0;
}
virtual int disk_write(const char *buffer, int sector)
{
char zero[512];
memset(zero, 0, 512);
if (memcmp(zero, buffer, 512) == 0)
{
if (sectors[sector] != 0)
{
free(sectors[sector]);
sectors[sector] = 0;
}
return 0;
}
if (sectors[sector] == 0)
{
char *sec = (char *)malloc(512);
if (sec == 0)
{
return 1;
}
sectors[sector] = sec;
}
memcpy(sectors[sector], buffer, 512);
return 0;
}
virtual int disk_sectors()
{
return sizeof(sectors) / sizeof(sectors[0]);
}
};
}
#endif