AP_Filesystem: add fgets

This commit is contained in:
Peter Barker 2021-09-29 13:01:26 +10:00 committed by Peter Barker
parent 3f37a21aaf
commit df7b2982d3
2 changed files with 20 additions and 0 deletions

View File

@ -246,6 +246,23 @@ FileData *AP_Filesystem::load_file(const char *filename)
return backend.fs.load_file(filename);
}
// returns null-terminated string; cr or lf terminates line
bool AP_Filesystem::fgets(char *buf, uint8_t buflen, int fd)
{
const Backend &backend = backend_by_fd(fd);
uint8_t i = 0;
for (; i<buflen-1; i++) {
if (backend.fs.read(fd, &buf[i], 1) != 1) {
break;
}
if (buf[i] == '\r' || buf[i] == '\n') {
break;
}
}
buf[i] = '\0';
return i != 0;
}
namespace AP
{

View File

@ -89,6 +89,9 @@ public:
// unmount filesystem for reboot
void unmount(void);
// returns null-terminated string; cr or lf terminates line
bool fgets(char *buf, uint8_t buflen, int fd);
/*
load a full file. Use delete to free the data
*/