-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathim01.cpp
104 lines (91 loc) · 1.77 KB
/
im01.cpp
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include "ff.h"
#include "diskio.h"
#include "inttypes.h"
#include "pxt.h"
#include "pxtcore.h"
namespace im01
{
//%
void _mkdir()
{
}
//%
void _remove(String s)
{
FATFS FatFs;
f_mount(&FatFs, "", 0);
f_unlink((const char *)s->getUTF8Data());
}
//%
bool _file(String s, String v, uint8_t x)
{
FRESULT fr;
FATFS FatFs;
fr = f_mount(&FatFs, "", 0);
FIL Fil;
UINT bw;
fr = f_open(&Fil, (const char *)s->getUTF8Data(), x);
if (fr == FR_OK)
{
f_write(&Fil, (const char *)v->getUTF8Data(), v->getUTF8Size(), &bw);
fr = f_close(&Fil);
if (fr == FR_OK && bw == v->getUTF8Size())
{
return true;
}
return false;
}
return false;
}
//%
uint32_t _size(String s)
{
FATFS FatFs;
f_mount(&FatFs, "", 0);
uint32_t lSize = 0;
FIL Fil;
FRESULT fr;
fr = f_open(&Fil, (const char *)s->getUTF8Data(), FA_READ | FA_WRITE);
lSize = f_size(&Fil);
f_close(&Fil);
return lSize;
}
//%
bool _exists(String s)
{
FATFS FatFs;
f_mount(&FatFs, "", 0);
FRESULT fr;
FILINFO fno;
fr = f_stat((const char *)s->getUTF8Data(), &fno);
if (fr == FR_OK)
return true;
return false;
}
//%
String _read(String s)
{
FATFS FatFs;
String cpy_string;
char* error_no_file = "ERROR! NO FILE";
UINT* br;
char* buff;
uint32_t lSize = 0;
FIL Fil;
FRESULT fr;
f_mount(&FatFs, "", 0);
fr = f_open(&Fil, (const char *)s->getUTF8Data(), FA_READ);
if(fr == FR_OK)
{
lSize = f_size(&Fil);
buff = (char*) malloc (sizeof(char)*lSize);
f_read(&Fil, buff, lSize, br);
}else{
cpy_string = mkString(error_no_file, strlen(error_no_file));
}
cpy_string = mkString(buff, lSize);
free(buff);
f_close(&Fil);
return cpy_string;
}
} // namespace cs11