MCPcopy Create free account
hub / github.com/SmingHub/Sming / readFile

Function readFile

samples/LiveDebug/app/application.cpp:145–170  ·  view source on GitHub ↗

* Demonstrate opening and reading a file from the host. */

Source from the content-addressed store, hash-verified

143 * Demonstrate opening and reading a file from the host.
144 */
145void readFile(const char* filename, bool display)
146{
147 int file = gdbfs.open(filename, File::ReadOnly);
148 Serial << _F("gdbfs.open(\"") << filename << "\") = " << file << endl;
149 if(file >= 0) {
150 OneShotFastMs timer;
151 char buf[256];
152 size_t total{0};
153 int len;
154 do {
155 len = gdbfs.read(file, buf, sizeof(buf));
156 if(len > 0) {
157 total += size_t(len);
158 if(display) {
159 Serial.write(buf, len);
160 }
161 }
162 } while(len == sizeof(buf));
163 auto elapsed = timer.elapsedTime();
164 Serial.println();
165 Serial << _F("gdbfs.read() = ") << len << _F(", total = ") << total << _F(", elapsed = ") << elapsed.toString()
166 << _F(", av. ") << (total == 0 ? 0 : 1000U * total / elapsed) << _F(" bytes/sec") << endl;
167
168 gdbfs.close(file);
169 }
170}
171
172/*
173 * A more advanced way to use host File I/O using asynchronous syscalls.

Callers 1

COMMAND_HANDLERFunction · 0.85

Calls 7

elapsedTimeMethod · 0.80
printlnMethod · 0.80
openMethod · 0.45
readMethod · 0.45
writeMethod · 0.45
toStringMethod · 0.45
closeMethod · 0.45

Tested by

no test coverage detected