Files
ESP8266lib/data/formats/DataSourcePosixFile.h
kazu 331f9f3e6c added code for mp3 and jpeg parsing
added interface for data sources
2021-02-28 20:46:38 +01:00

51 lines
839 B
C++

#pragma once
#include <stdlib.h>
#include <string>
#include <stdexcept>
/** file on the desktop (posix) filesystem */
class DataSourcePosixFile {
FILE* f = nullptr;
public:
DataSourcePosixFile() {
}
DataSourcePosixFile(const char* file) {
f = fopen(file, "rw");
if (!f) {
throw std::runtime_error(std::string("failed to open file: ") + file);
}
}
~DataSourcePosixFile() {
if (f) {fclose(f);}
}
uint32_t curPos() const {
return ftell(f);
}
void seekTo(uint32_t pos) {
fseek(f, pos, SEEK_SET);
}
uint32_t read(uint32_t bytes, uint8_t* dst) {
return fread(dst, bytes, 1, f) * bytes;
}
uint32_t write(uint32_t bytes, const uint8_t* src) {
return fwrite(src, bytes, 1, f) * bytes;
}
/** read an entity */
template <typename T> void readType(T& dst) {
read(sizeof(T), (uint8_t*) &dst);
}
};