-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathim01.cpp
56 lines (45 loc) · 1.09 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
#include "pxt.h"
#include "mbed.h"
#include "SDFileSystem.h"
#include "MicroBit.h"
namespace im01
{
//%
void _mkdir(String s)
{
SDFileSystem sd(P0_21, P0_22, P0_23, P0_16, "sd");
microbit_create_heap(MICROBIT_SD_GATT_TABLE_START + MICROBIT_SD_GATT_TABLE_SIZE, MICROBIT_SD_LIMIT);
mkdir((const char *)s->getUTF8Data(), 0777);
}
//%
String _read(String s)
{
char * _word;
char* error_no_file = "ERROR! NO FILE";
char* cant_read_file = "ERROR! CANT READ FILE";
//char* path = "/sd/im01/";
int lSize;
size_t b_read;
SDFileSystem sd(P0_21, P0_22, P0_23, P0_16, "sd");
FILE *fp = fopen((const char *)s->getUTF8Data(), "rb");
if (fp == NULL)
{
return mkString(error_no_file, strlen(error_no_file));
}else{
// obtain file size:
fseek (fp , 0 , SEEK_END);
lSize = ftell(fp);
rewind (fp);
_word = (char*) malloc (sizeof(char)*(lSize));
b_read = fread(_word, sizeof(char), lSize, fp);
fclose(fp);
}
if(b_read != (lSize))
{
return mkString(cant_read_file, strlen(cant_read_file));
}
String str = mkString(_word, lSize);
free(_word);
return str;
}
} // namespace im01