blog/cpp/fonts/reader/buffer.cpp

146 lines
2.6 KiB
C++
Raw Normal View History

2024-09-13 15:10:58 +08:00
#include "buffer.h"
#include <cstring>
#include <sstream>
#include "endian.h"
buffer::buffer(std::string data)
{
fp = std::stringstream(data);
length = data.length();
};
std::streamsize buffer::read(char *bytes, uint16_t l)
{
fp.read(reinterpret_cast<char *>(bytes), l);
std::streamsize bytesRead = fp.gcount();
return bytesRead;
};
int8_t buffer::readInt8()
{
int8_t ret = 0;
fp.read(reinterpret_cast<char*>(&ret), sizeof(ret));
return htobe16(ret);
}
int16_t buffer::readInt16()
{
int16_t ret = 0;
fp.read(reinterpret_cast<char*>(&ret), sizeof(ret));
return htobe16(ret);
};
int32_t buffer::readInt32()
{
int32_t ret = 0;
fp.read(reinterpret_cast<char*>(&ret), sizeof(ret));
return htobe16(ret);
}
int64_t buffer::readInt64()
{
uint64_t ret = 0;
fp.read(reinterpret_cast<char*>(&ret), sizeof(ret));
return htobe16(ret);
}
uint8_t buffer::readUint8()
{
uint8_t ret = 0;
fp.read(reinterpret_cast<char*>(&ret), sizeof(ret));
return ret;
}
uint16_t buffer::readUint16()
{
uint16_t ret = 0;
fp.read(reinterpret_cast<char*>(&ret), sizeof(ret));
return htobe16(ret);
};
uint24_t buffer::readUint24(){
uint24_t ret(0);
fp>>ret;
return ret;
}
uint32_t buffer::readUint32()
{
uint32_t ret = 0;
fp.read(reinterpret_cast<char*>(&ret), sizeof(ret));
return htobe32(ret);
}
uint64_t buffer::readUint64()
{
uint64_t ret = 0;
fp.read(reinterpret_cast<char*>(&ret), sizeof(ret));
return htobe64(ret);
}
std::string buffer::readString(int l)
{
char *data = new char[l];
fp.read(data, l);
std::streamsize bytesRead = fp.gcount();
auto ret = std::string(data, bytesRead);
delete[] data;
return ret;
}
FWORD buffer::readFWord(){
return readInt16();
}
UFWORD buffer::readUFWord(){
return readUint16();
}
Offset8 buffer::readOffset8(){
return readUint8();
}
Offset16 buffer::readOffset16(){
return readUint16();
}
Offset24 buffer::readOffset24(){
return readUint24();
}
Offset32 buffer::readOffset32(){
return readUint32();
}
int24_t buffer::readInt24(){
int24_t i;
fp>>i;
return i;
}
Fixed buffer::readFixed(){
Fixed f;
fp>>f;
return f;
}
F2DOT14 buffer::readF2Dot14(){
F2DOT14 f;
fp>>f;
return f;
}
Version16Dot16 buffer::readVersion16Dot16(){
Version16Dot16 v;
fp>>v;
return v;
}
Tag buffer::readTag(){
Tag t;
fp>>t;
return t;
}
bool buffer::seek(int64_t i)
{
if (i > length)
{
return false;
}
fp.seekg(i);
return true;
}
std::streampos buffer::curr()
{
return fp.tellg();
}