2020-01-31 21:17:46 +00:00
|
|
|
#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)
|
|
|
|
{
|
2020-01-31 21:17:46 +00:00
|
|
|
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) {
|
2020-02-01 00:04:11 +00:00
|
|
|
//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;
|
|
|
|
}
|
|
|
|
}
|