1
0
mirror of https://github.com/Dejvino/lilybook.git synced 2024-11-15 04:42:35 +00:00
lilybook/main/modes/reader/TextReader.cpp

44 lines
897 B
C++
Raw Normal View History

#include <string.h>
2020-01-31 19:54:19 +00:00
#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)
{
memset(text, 0, len);
if (pos < 0) {
len += pos;
pos = 0;
}
if (len <= 0) {
return 0;
}
2020-01-31 19:54:19 +00:00
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);
2020-01-31 19:54:19 +00:00
} else {
ESP_LOGI(TAG, "End of file. Closing.");
fclose(this->f);
this->f = NULL;
}
return read;
}
}