This repository was archived by the owner on Apr 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
103 lines (77 loc) · 2.63 KB
/
main.cpp
File metadata and controls
103 lines (77 loc) · 2.63 KB
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
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
// For generated filesystem definitions
#include <ecl/core_generated.hpp>
// For ecl::cout and ecl::endl
#include <ecl/iostream.hpp>
extern "C"
void board_init()
{
// Init GPIO
gpio_init_generated();
}
int main()
{
ecl::cout << "Starting FATFS example..." << ecl::endl;
char buf[1024];
size_t total_files = 0;
size_t idx;
// Mount filesystems
auto rc = ecl::filesystem::mount_all();
ecl_assert_msg(is_ok(rc), "failed to mount");
// Open root directory of the filesystem
auto root = ecl::filesystem::open_dir("/sdcard/");
ecl_assert_msg(root, "failed to open root");
// Iterate over files in the root directory, and display their names
ecl::fs::inode_ptr node;
while ((node = root->read())) {
size_t name_len = sizeof(buf);
node->get_name(buf, name_len);
ecl::cout << "#. "
<< total_files
<< " type: "
<< (node->get_type() == ecl::fs::inode::type::dir ? "dir " : "file")
<< " name : " << buf << ecl::endl;
total_files++;
}
// Make sure some files are present in root directory,
// otherwise this example is meaningles.
ecl_assert_msg(total_files > 0, "no files in root directory");
ecl::cout << "Which file to open?" << ecl::endl;
ecl::cin >> idx;
ecl_assert_msg(idx < total_files, "index is too big");
// Back to the directory start.
root->rewind();
// Iterate all again to find appropriate file.
size_t i = 0;
do {
node = root->read();
} while (i++ < idx);
// Check that requested file is a file
ecl_assert_msg(node->get_type() == ecl::fs::inode::type::file, "selected item is not a file");
// Open a file
auto file = node->open();
// Display a file
while (true) {
// After `read()` call, buf_size variable will contain amount of bytes
// actually read. One byte at the end is reserver for NUL-terminator.
auto buf_size = sizeof(buf) - 1;
auto rc = file->read(reinterpret_cast<uint8_t*>(buf), buf_size);
ecl_assert_msg(is_ok(rc), "failed to read a file");
if (!buf_size) {
// End-of-file is reached.
break;
}
// NUL-terminate the buffer.
buf[buf_size] = 0;
// Print contents.
ecl::cout << buf;
}
ecl::cout << ecl::endl;
// Example is completed
file->close();
root->close();
for(;;);
return 0;
}