Start font reading support.

This commit is contained in:
jmsgrogan 2022-07-31 20:01:13 +01:00
parent 92e7a78710
commit ed925afabf
22 changed files with 599 additions and 220 deletions

View file

@ -1,24 +1,26 @@
#pragma once
#include <cstring>
#include <iostream>
#include <stdint.h>
class ByteUtils
{
public:
using Word = int;
using DWord = int;
using Word = int16_t;
using DWord = int32_t;
using QWord = int64_t;
static bool MSBIsOne(char c)
static bool MostSignificantBitIsOne(char c)
{
return c & (1 << 7);
}
static int GetWordFirstBit(const Word word)
static Word GetWordFirstBit(const Word word)
{
return word & ByteUtils::WORD_FIRST_BIT;
};
static int GetWordLastByte(const Word word)
static Word GetWordLastByte(const Word word)
{
return word & ByteUtils::WORD_LAST_BYTE;
}
@ -38,30 +40,36 @@ public:
}
}
static int ToInt(char* buffer, const unsigned size, bool reverse = true)
template<typename T>
static T ToType(char* buffer, bool reverse = true)
{
int result {0};
T result {0};
if(reverse)
{
char reversed[sizeof(int)];
ReverseBuffer(buffer, reversed, size, sizeof(int));
std::memcpy(&result, reversed, sizeof(int));
char reversed[sizeof(T)];
ReverseBuffer(buffer, reversed, sizeof(T), sizeof(T));
std::memcpy(&result, reversed, sizeof(T));
}
else
{
std::memcpy(&result, buffer, sizeof(int));
std::memcpy(&result, buffer, sizeof(T));
}
return result;
}
static Word ToWord(char* buffer, bool reverse = true)
{
return ToInt(buffer, BYTES_PER_WORD, reverse);
return ToType<Word>(buffer, reverse);
}
static DWord ToDWord(char* buffer, bool reverse = true)
{
return ToInt(buffer, BYTES_PER_DWORD, reverse);
return ToType<DWord>(buffer, reverse);
}
static QWord ToQWord(char* buffer, bool reverse = true)
{
return ToType<QWord>(buffer, reverse);
}
static bool Compare(char* buffer, const char* tag, unsigned size)
@ -78,17 +86,15 @@ public:
static bool CompareDWords(char* buffer, const char* tag)
{
return Compare(buffer, tag, BYTES_PER_DWORD);
return Compare(buffer, tag, sizeof(DWord));
}
static bool CompareWords(char* buffer, const char* tag)
{
return Compare(buffer, tag, BYTES_PER_WORD);
return Compare(buffer, tag, sizeof(Word));
}
static const int BYTES_PER_WORD = 2;
static const int BYTES_PER_DWORD = 4;
static const int BYTE_FIRST_BIT = 0x40; // 1000 0000
static const int WORD_FIRST_BIT = 0x8000; // 1000 0000 - 0000 0000
static const int WORD_LAST_BYTE = 0x00FF; // 0000 0000 - 1111 1111
static const Word WORD_FIRST_BIT = 0x8000; // 1000 0000 - 0000 0000
static const Word WORD_LAST_BYTE = 0x00FF; // 0000 0000 - 1111 1111
};

View file

@ -5,7 +5,6 @@ list(APPEND core_HEADERS
Color.h
CommandLineArgs.h
loggers/FileLogger.h
file_utilities/BinaryFile.h
file_utilities/File.h
file_utilities/FileFormats.h
StringUtils.h
@ -17,12 +16,12 @@ list(APPEND core_LIB_INCLUDES
Color.cpp
CommandLineArgs.cpp
loggers/FileLogger.cpp
file_utilities/BinaryFile.cpp
file_utilities/File.cpp
file_utilities/FileFormats.cpp
memory/SharedMemory.cpp
RandomUtils.cpp
StringUtils.cpp
streams/BinaryStream.cpp
http/HttpResponse.cpp
http/HttpHeader.cpp
http/HttpRequest.cpp)
@ -35,6 +34,7 @@ target_include_directories(core PUBLIC
"${CMAKE_CURRENT_SOURCE_DIR}/file_utilities"
"${CMAKE_CURRENT_SOURCE_DIR}/loggers"
"${CMAKE_CURRENT_SOURCE_DIR}/memory"
"${CMAKE_CURRENT_SOURCE_DIR}/streams"
)
set_target_properties( core PROPERTIES WINDOWS_EXPORT_ALL_SYMBOLS ON )
set_property(TARGET core PROPERTY FOLDER src)

View file

@ -1,88 +0,0 @@
#include "BinaryFile.h"
#include "ByteUtils.h"
bool BinaryFile::GetNextByteAsInt(std::ifstream* file, int& target)
{
char c;
file->get(c);
target = int(c);
return true;
}
bool BinaryFile::GetNextNBytes(std::ifstream* file, char* buffer, unsigned number)
{
char c;
for(unsigned idx=0; idx<number; idx++)
{
if(file->get(c))
{
buffer[idx] = c;
}
else
{
return false;
}
}
return true;
}
bool BinaryFile::GetNextWord(std::ifstream* file, char* buffer)
{
return GetNextNBytes(file, buffer, ByteUtils::BYTES_PER_WORD);
}
bool BinaryFile::GetNextDWord(std::ifstream* file, char* buffer)
{
return GetNextNBytes(file, buffer, ByteUtils::BYTES_PER_DWORD);
}
bool BinaryFile::GetNextWord(std::ifstream* file, int& target, bool reverse)
{
char buffer[ByteUtils::BYTES_PER_WORD];
if(!BinaryFile::GetNextWord(file, buffer))
{
return false;
}
target = ByteUtils::ToWord(buffer, reverse);
return true;
}
bool BinaryFile::GetNextDWord(std::ifstream* file, int& target)
{
char buffer[ByteUtils::BYTES_PER_DWORD];
if(!BinaryFile::GetNextDWord(file, buffer))
{
return false;
}
target = ByteUtils::ToDWord(buffer);
return true;
}
bool BinaryFile::CheckNextDWord(std::ifstream* file, const char* target)
{
char buffer[ByteUtils::BYTES_PER_DWORD];
if(!BinaryFile::GetNextDWord(file, buffer))
{
return false;
}
if(!ByteUtils::CompareDWords(buffer, target))
{
return false;
}
return true;
}
bool BinaryFile::GetNextString(std::ifstream* file, std::string& target, unsigned numBytes)
{
char c;
for(unsigned idx=0; idx<numBytes; idx++)
{
if(!file->get(c))
{
return false;
}
target += c;
}
return true;
}

View file

@ -1,32 +0,0 @@
#pragma once
#include <fstream>
#include <string>
class BinaryFile
{
public:
template<typename T>
static bool Write(std::ofstream* file, T data)
{
file->write(reinterpret_cast<char*>(&data), sizeof(data));
return true;
}
static bool GetNextByteAsInt(std::ifstream* file, int& target);
static bool GetNextNBytes(std::ifstream* file, char* buffer, unsigned numBytes);
static bool GetNextWord(std::ifstream* file, char* buffer);
static bool GetNextDWord(std::ifstream* file, char* buffer);
static bool GetNextWord(std::ifstream* file, int& target, bool reverse = true);
static bool GetNextDWord(std::ifstream* file, int& target);
static bool CheckNextDWord(std::ifstream* file, const char* target);
static bool GetNextString(std::ifstream* file, std::string& target, unsigned numBytes);
};

View file

@ -0,0 +1,97 @@
#include "BinaryStream.h"
std::optional<int> BinaryStream::getNextByteAsInt(std::basic_istream<char>* stream)
{
return stream->get();
}
bool BinaryStream::getNextNBytes(std::basic_istream<char>* stream, char* buffer, unsigned number)
{
char c;
for(unsigned idx=0; idx<number; idx++)
{
if(stream->get(c))
{
buffer[idx] = c;
}
else
{
return false;
}
}
return true;
}
bool BinaryStream::getNextWord(std::basic_istream<char>* stream, char* buffer)
{
return getNextNBytes(stream, buffer, sizeof(ByteUtils::Word));
}
bool BinaryStream::getNextDWord(std::basic_istream<char>* stream, char* buffer)
{
return getNextNBytes(stream, buffer, sizeof(ByteUtils::DWord));
}
bool BinaryStream::getNextQWord(std::basic_istream<char>* stream, char* buffer)
{
return getNextNBytes(stream, buffer, sizeof(ByteUtils::QWord));
}
std::optional<ByteUtils::Word> BinaryStream::getNextWord(std::basic_istream<char>* stream, bool reverse)
{
char buffer[sizeof(ByteUtils::Word)];
if(!BinaryStream::getNextWord(stream, buffer))
{
return false;
}
return ByteUtils::ToWord(buffer, reverse);
}
std::optional<ByteUtils::DWord> BinaryStream::getNextDWord(std::basic_istream<char>* stream)
{
char buffer[sizeof(ByteUtils::DWord)];
if(!BinaryStream::getNextDWord(stream, buffer))
{
return false;
}
return ByteUtils::ToDWord(buffer);;
}
std::optional<ByteUtils::QWord> BinaryStream::getNextQWord(std::basic_istream<char>* stream)
{
char buffer[sizeof(ByteUtils::QWord)];
if(!BinaryStream::getNextQWord(stream, buffer))
{
return false;
}
return ByteUtils::ToQWord(buffer);;
}
bool BinaryStream::checkNextDWord(std::basic_istream<char>* stream, const char* target)
{
char buffer[sizeof(ByteUtils::DWord)];
if(!BinaryStream::getNextDWord(stream, buffer))
{
return false;
}
if(!ByteUtils::CompareDWords(buffer, target))
{
return false;
}
return true;
}
bool BinaryStream::getNextString(std::basic_istream<char>* stream, std::string& target, unsigned numBytes)
{
char c;
for(unsigned idx=0; idx<numBytes; idx++)
{
if(!stream->get(c))
{
return false;
}
target += c;
}
return true;
}

View file

@ -0,0 +1,40 @@
#pragma once
#include <istream>
#include <ostream>
#include <string>
#include <optional>
#include "ByteUtils.h"
class BinaryStream
{
public:
template<typename T>
static bool write(std::basic_ostream<char>* stream, T data)
{
stream->write(reinterpret_cast<char*>(&data), sizeof(data));
return true;
}
static std::optional<int> getNextByteAsInt(std::basic_istream<char>* stream);
static bool getNextNBytes(std::basic_istream<char>* stream, char* buffer, unsigned numBytes);
static bool getNextWord(std::basic_istream<char>* stream, char* buffer);
static bool getNextDWord(std::basic_istream<char>* stream, char* buffer);
static bool getNextQWord(std::basic_istream<char>* stream, char* buffer);
static std::optional<ByteUtils::Word> getNextWord(std::basic_istream<char>* stream, bool reverse = true);
static std::optional<ByteUtils::DWord> getNextDWord(std::basic_istream<char>* stream);
static std::optional<ByteUtils::QWord> getNextQWord(std::basic_istream<char>* stream);
static bool checkNextDWord(std::basic_istream<char>* stream, const char* target);
static bool getNextString(std::basic_istream<char>* stream, std::string& target, unsigned numBytes);
};