mirror of
https://github.com/Dejvino/lilybook.git
synced 2024-11-14 20:33:28 +00:00
35 lines
743 B
C++
35 lines
743 B
C++
|
#include "TextReader.h"
|
||
|
|
||
|
#include "esp_log.h"
|
||
|
static const char *TAG = "TextReader";
|
||
|
|
||
|
TextReader::TextReader(FILE* file)
|
||
|
{
|
||
|
this->f = file;
|
||
|
}
|
||
|
|
||
|
void TextReader::close()
|
||
|
{
|
||
|
fclose(this->f);
|
||
|
}
|
||
|
|
||
|
size_t TextReader::read(long pos, char* text, size_t len)
|
||
|
{
|
||
|
if (this->f == NULL) {
|
||
|
ESP_LOGE(TAG, "File not opened.");
|
||
|
sprintf(text, "File could not be opened.");
|
||
|
return 0;
|
||
|
} else {
|
||
|
fseek(this->f, pos, SEEK_SET);
|
||
|
size_t read = fread(text, 1, len, this->f);
|
||
|
if (read > 0) {
|
||
|
ESP_LOGI(TAG, "Read content: %s", text);
|
||
|
} else {
|
||
|
ESP_LOGI(TAG, "End of file. Closing.");
|
||
|
fclose(this->f);
|
||
|
this->f = NULL;
|
||
|
}
|
||
|
return read;
|
||
|
}
|
||
|
}
|