Core lib building

This commit is contained in:
jmsgrogan 2024-01-02 16:14:23 +00:00
parent 3ed195d7dd
commit 94fcc42aed
73 changed files with 625 additions and 661 deletions

View file

@ -54,7 +54,7 @@ std::pair<MarkdownContentParser::FileMetadata, Ptr<MarkdownDocument>> MarkdownCo
return {output_metadata, std::move(document)}; return {output_metadata, std::move(document)};
} }
std::optional<MarkdownContentParser::FileMetadataItem> MarkdownContentParser::checkForMetadataItem(const String& line) const Optional<MarkdownContentParser::FileMetadataItem> MarkdownContentParser::checkForMetadataItem(const String& line) const
{ {
unsigned char_count = 0; unsigned char_count = 0;
String prefix; String prefix;

View file

@ -18,5 +18,5 @@ public:
std::pair<FileMetadata, Ptr<MarkdownDocument>> run(const Path& path); std::pair<FileMetadata, Ptr<MarkdownDocument>> run(const Path& path);
private: private:
std::optional<FileMetadataItem> checkForMetadataItem(const String& line) const; Optional<FileMetadataItem> checkForMetadataItem(const String& line) const;
}; };

View file

@ -7,10 +7,10 @@ CORE_SRC_DIR=$SOURCE_DIR/base/core
g++ $SOURCE_DIR/main.cpp \ g++ $SOURCE_DIR/main.cpp \
$CORE_SRC_DIR/base_types/Error.cpp \ $CORE_SRC_DIR/base_types/Error.cpp \
$CORE_SRC_DIR/base_types/Index.cpp \ $CORE_SRC_DIR/base_types/Index.cpp \
$CORE_SRC_DIR/base_types/Char.cpp \
$SOURCE_DIR/base/compiler/BuildLibrary.cpp \ $SOURCE_DIR/base/compiler/BuildLibrary.cpp \
$SOURCE_DIR/base/compiler/BuildSession.cpp \ $SOURCE_DIR/base/compiler/BuildSession.cpp \
$CORE_SRC_DIR/data_structures/String.cpp \ $CORE_SRC_DIR/data_structures/String.cpp \
$CORE_SRC_DIR/encoding/CharUtils.cpp \
$CORE_SRC_DIR/filesystem/FileSystemPath.cpp \ $CORE_SRC_DIR/filesystem/FileSystemPath.cpp \
$CORE_SRC_DIR/filesystem/File.cpp \ $CORE_SRC_DIR/filesystem/File.cpp \
$CORE_SRC_DIR/filesystem/Directory.cpp \ $CORE_SRC_DIR/filesystem/Directory.cpp \

View file

@ -1,7 +1,7 @@
#include "MidiChannelEventAdapter.h" #include "MidiChannelEventAdapter.h"
#include "BinaryStream.h" #include "BinaryStream.h"
#include "ByteUtils.h" #include "Bits.h"
#include <iostream> #include <iostream>
#include <bitset> #include <bitset>
@ -19,7 +19,7 @@ int MidiChannelEventAdapter::readEvent(std::ifstream* file, char firstByte, Midi
unsigned byteCount = 0; unsigned byteCount = 0;
//std::cout << "Channel: " << midi_channel << std::endl; //std::cout << "Channel: " << midi_channel << std::endl;
const bool isStatusByte = ByteUtils::MostSignificantBitIsOne(firstByte); const bool isStatusByte = Bits::MostSignificantBitIsOne(firstByte);
if(isStatusByte) if(isStatusByte)
{ {
event->SetTypeAndChannel(firstByte); event->SetTypeAndChannel(firstByte);

View file

@ -1,7 +1,7 @@
#include "MidiMetaEventAdapter.h" #include "MidiMetaEventAdapter.h"
#include "BinaryStream.h" #include "BinaryStream.h"
#include "ByteUtils.h" #include "Bits.h"
#include <iostream> #include <iostream>
#include <bitset> #include <bitset>
@ -108,7 +108,7 @@ int MidiMetaEventAdapter::ReadIntEvent(std::ifstream* file, MetaMidiEvent* event
BinaryStream::getNextNBytes(file, buffer.data(), length); BinaryStream::getNextNBytes(file, buffer.data(), length);
byteCount += length; byteCount += length;
const int value = ByteUtils::ToType<ByteUtils::DWord>(buffer.data(), length); const int value = Bits::ToType<Bits::DWord>(buffer.data(), length);
event->SetValue(value); event->SetValue(value);
return byteCount; return byteCount;
} }
@ -123,7 +123,7 @@ int MidiMetaEventAdapter::ReadChannelPrefixEvent(std::ifstream* file, MetaMidiEv
BinaryStream::getNextNBytes(file, buffer.data(), length); BinaryStream::getNextNBytes(file, buffer.data(), length);
byteCount += length; byteCount += length;
const int value = ByteUtils::ToType<ByteUtils::DWord>(buffer.data(), length); const int value = Bits::ToType<Bits::DWord>(buffer.data(), length);
event->SetValue(value); event->SetValue(value);
lastMidiChannel = value; lastMidiChannel = value;
return byteCount; return byteCount;

View file

@ -1,7 +1,7 @@
#include "MidiReader.h" #include "MidiReader.h"
#include "MidiDocument.h" #include "MidiDocument.h"
#include "ByteUtils.h" #include "Bits.h"
#include "MidiTrack.h" #include "MidiTrack.h"
#include "BinaryStream.h" #include "BinaryStream.h"
#include "FileLogger.h" #include "FileLogger.h"

View file

@ -1,7 +1,7 @@
#include "MidiTimeAdapter.h" #include "MidiTimeAdapter.h"
#include "BinaryStream.h" #include "BinaryStream.h"
#include "ByteUtils.h" #include "Bits.h"
#include <iostream> #include <iostream>
#include <bitset> #include <bitset>
@ -13,7 +13,7 @@ int MidiTimeAdapter::ReadEventTimeDelta(std::ifstream* file, int& delta)
file->get(c); file->get(c);
byteCount++; byteCount++;
if(!ByteUtils::MostSignificantBitIsOne(c)) if(!Bits::MostSignificantBitIsOne(c))
{ {
delta = int(c); delta = int(c);
//std::cout << "Time delta final: " << delta << std::endl; //std::cout << "Time delta final: " << delta << std::endl;
@ -54,13 +54,13 @@ int MidiTimeAdapter::ReadTimeDivision(std::ifstream* file, MidiTimeDivision& div
return -1; return -1;
} }
division.mUseFps = ByteUtils::GetWordFirstBit(*time_division); division.mUseFps = Bits::GetWordFirstBit(*time_division);
if (division.mUseFps) if (division.mUseFps)
{ {
const int TOP_7_BITS = 0x7F00; // 0111 1111 - 0000 0000 const int TOP_7_BITS = 0x7F00; // 0111 1111 - 0000 0000
division.mFps = ((~(*time_division) & TOP_7_BITS) >> 8) - 1; // Reverse 2complement of next 7 bits division.mFps = ((~(*time_division) & TOP_7_BITS) >> 8) - 1; // Reverse 2complement of next 7 bits
} }
division.mTicks = ByteUtils::GetWordLastByte(*time_division); division.mTicks = Bits::GetWordLastByte(*time_division);
return 2; // Bytes advanced return 2; // Bytes advanced
} }

View file

@ -1,6 +0,0 @@
add_subdirectory(compiler)
add_subdirectory(compression)
add_subdirectory(core)
add_subdirectory(database)
add_subdirectory(geometry)
add_subdirectory(network)

View file

@ -16,6 +16,8 @@ BuildSession::BuildSession(const String& source_dir,
{ {
m_build_dir = build_dir; m_build_dir = build_dir;
} }
const auto build_str = m_build_dir.str() + "/build";
m_build_dir = FileSystemPath(build_str);
m_compiler_flags.push_back("-g"); m_compiler_flags.push_back("-g");
m_compiler_flags.push_back("-fno-exceptions"); m_compiler_flags.push_back("-fno-exceptions");
m_compiler_flags.push_back("-fno-rtti"); m_compiler_flags.push_back("-fno-rtti");
@ -24,23 +26,22 @@ BuildSession::BuildSession(const String& source_dir,
Status BuildSession::scan() Status BuildSession::scan()
{ {
LOG_INFO("Scanning sources at:" << m_source_dir); LOG_INFO("Scanning sources at:" << m_source_dir);
Vector<FileSystemPath> toml_files; Vector<FileSystemPath> ini_files;
STATUS_CHECK(Directory::getFilesWithExtension( STATUS_CHECK(Directory::getFilesWithExtension(
m_source_dir, m_source_dir,
".toml", ".ini",
toml_files, ini_files,
true), "Error looking for build files"); true), "Error looking for build files");
for(const auto& toml_file : toml_files) for(const auto& ini_file : ini_files)
{ {
if (toml_file.file_name() == "build") if (ini_file.file_name() == "build")
{ {
STATUS_CHECK(add_library(toml_file), STATUS_CHECK(add_library(ini_file),
"Error adding library"); "Error adding library");
} }
} }
return {}; return {};
} }
Status BuildSession::add_library(const FileSystemPath& config_path) Status BuildSession::add_library(const FileSystemPath& config_path)
@ -54,26 +55,52 @@ Status BuildSession::add_library(const FileSystemPath& config_path)
Status BuildSession::build() Status BuildSession::build()
{ {
for(const auto& library : m_libraries) Directory::create(m_build_dir, true);
for(auto& library : m_libraries)
{ {
for(const auto& source : library.get_sources()) const auto run_status = compile_library(library);
if (!run_status.ok())
{ {
Vector<String> args = m_compiler_flags; return run_status;
args.push_back("-c"); }
for(const auto& include_dir : library.get_include_dirs())
{
args.push_back(_s("-I") + include_dir.str());
}
args.push_back(source.str());
LOG_INFO("Compiling " << source.file_name());
const auto run_status = Process::launch(m_compiler_command, args);
if (!run_status.ok())
{
return run_status;
}
//break;
}
//break; //break;
} }
return {}; return {};
}
Status BuildSession::compile_library(BuildLibrary& lib)
{
for(const auto& source : lib.get_sources())
{
const auto run_status = compile_source_file(source, lib);
if (!run_status.ok())
{
return run_status;
}
//break;
}
return {};
}
Status BuildSession::compile_source_file(const FileSystemPath& source,
const BuildLibrary& lib)
{
Vector<String> args = m_compiler_flags;
args.push_back("-c");
add_include_dirs(args, lib);
args.push_back(source.str());
const auto output_path = m_build_dir / source.file_name();
args.push_back("-o");
args.push_back(output_path.str() + ".o");
LOG_INFO("Compiling " << source.file_name());
return Process::launch(m_compiler_command, args);
}
void BuildSession::add_include_dirs(Vector<String>& args, const BuildLibrary& lib)
{
for(const auto& include_dir : lib.get_include_dirs())
{
args.push_back(_s("-I") + include_dir.str());
}
} }

View file

@ -16,6 +16,13 @@ public:
Status add_library(const FileSystemPath& config_path); Status add_library(const FileSystemPath& config_path);
private: private:
Status compile_library(BuildLibrary& lib);
Status compile_source_file(const FileSystemPath& source,
const BuildLibrary& lib);
void add_include_dirs(Vector<String>& args, const BuildLibrary& lib);
String m_compiler_command{"/usr/bin/g++"}; String m_compiler_command{"/usr/bin/g++"};
Vector<String> m_compiler_flags; Vector<String> m_compiler_flags;
FileSystemPath m_source_dir; FileSystemPath m_source_dir;

View file

@ -1,28 +0,0 @@
set(MODULE_NAME compiler)
list(APPEND HEADERS
Lexer.h
template_engine/TemplatingEngine.h
template_engine/TemplateFile.h
template_engine/TemplateNode.h
template_engine/TemplateElements.h
)
list(APPEND SOURCES
Lexer.cpp
template_engine/TemplatingEngine.cpp
template_engine/TemplateFile.cpp
template_engine/TemplateNode.cpp
template_engine/TemplateElements.cpp
)
add_library(${MODULE_NAME} SHARED ${HEADERS} ${SOURCES})
target_include_directories(${MODULE_NAME} PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}
${CMAKE_CURRENT_SOURCE_DIR}/template_engine
)
target_link_libraries( ${MODULE_NAME} PUBLIC core)
set_target_properties( ${MODULE_NAME} PROPERTIES WINDOWS_EXPORT_ALL_SYMBOLS ON )
set_property(TARGET ${MODULE_NAME} PROPERTY FOLDER src/base)

View file

@ -2,7 +2,7 @@
#include "StringUtils.h" #include "StringUtils.h"
#include "BitStream.h" #include "BitStream.h"
#include "ByteUtils.h" #include "Bits.h"
#include "HuffmanEncoder.h" #include "HuffmanEncoder.h"
#include <iostream> #include <iostream>
@ -234,7 +234,7 @@ void Lz77Encoder::flushHitBuffer()
if (length == 0) if (length == 0)
{ {
code = *mCodeGenerator->getLiteralValue(next_char); code = *mCodeGenerator->getLiteralValue(next_char);
std::cout << "Writing symbol " << static_cast<int>(next_char) << " with code " << ByteUtils::toString(code.getData(), code.getLength()) << "\n"; std::cout << "Writing symbol " << static_cast<int>(next_char) << " with code " << Bits::toString(code.getData(), code.getLength()) << "\n";
mOutputStream->writeNBits(code.getData(), code.getLength()); mOutputStream->writeNBits(code.getData(), code.getLength());
} }
@ -243,16 +243,16 @@ void Lz77Encoder::flushHitBuffer()
code = *mCodeGenerator->getLengthValue(length); code = *mCodeGenerator->getLengthValue(length);
const auto distance_code = mCodeGenerator->getDistanceValue(distance); const auto distance_code = mCodeGenerator->getDistanceValue(distance);
std::cout << "Writing length " << length << " with code " << ByteUtils::toString(code.getData(), code.getLength()) << "\n"; std::cout << "Writing length " << length << " with code " << Bits::toString(code.getData(), code.getLength()) << "\n";
mOutputStream->writeNBits(code.getData(), code.getLength()); mOutputStream->writeNBits(code.getData(), code.getLength());
std::cout << "Writing distance " << distance << " with code " << ByteUtils::toString(distance_code.getData(), distance_code.getLength()) << "\n"; std::cout << "Writing distance " << distance << " with code " << Bits::toString(distance_code.getData(), distance_code.getLength()) << "\n";
mOutputStream->writeNBits(distance_code.getData(), distance_code.getLength()); mOutputStream->writeNBits(distance_code.getData(), distance_code.getLength());
} }
} }
auto eos_code = mCodeGenerator->getEndOfStreamValue(); auto eos_code = mCodeGenerator->getEndOfStreamValue();
std::cout << "Writing EOS value with code " << ByteUtils::toString(eos_code->getData(), eos_code->getLength()) << "\n"; std::cout << "Writing EOS value with code " << Bits::toString(eos_code->getData(), eos_code->getLength()) << "\n";
mOutputStream->writeNBits(eos_code->getData(), eos_code->getLength()); mOutputStream->writeNBits(eos_code->getData(), eos_code->getLength());
} }

View file

@ -1,6 +1,6 @@
#include "ZlibEncoder.h" #include "ZlibEncoder.h"
#include "ByteUtils.h" #include "Bits.h"
#include "DeflateEncoder.h" #include "DeflateEncoder.h"
#include "FileLogger.h" #include "FileLogger.h"
#include "BitStream.h" #include "BitStream.h"
@ -52,8 +52,8 @@ String ZlibEncoder::toString(CompressionMethod method) const
void ZlibEncoder::parseCompressionMethod(unsigned char method) void ZlibEncoder::parseCompressionMethod(unsigned char method)
{ {
//std::cout << "Got compression input " << static_cast<int>(method) << std::endl; //std::cout << "Got compression input " << static_cast<int>(method) << std::endl;
mCompressionMethod = static_cast<CompressionMethod>(ByteUtils::getLowerNBits(method, 4)); mCompressionMethod = static_cast<CompressionMethod>(Bits::getLowerNBits(method, 4));
auto compression_info = ByteUtils::getHigherNBits(method, 4); auto compression_info = Bits::getHigherNBits(method, 4);
if (mCompressionMethod == CompressionMethod::DEFLATE) if (mCompressionMethod == CompressionMethod::DEFLATE)
{ {
@ -71,9 +71,9 @@ void ZlibEncoder::parseExtraFlags(unsigned char extraFlags, unsigned char compre
//std::cout << "Invalid header. Mod is " << mod << std::endl; //std::cout << "Invalid header. Mod is " << mod << std::endl;
} }
mFlagCheck = ByteUtils::getLowerNBits(extraFlags, 5); mFlagCheck = Bits::getLowerNBits(extraFlags, 5);
mUseDictionary = bool(ByteUtils::getBitN(extraFlags, 5)); mUseDictionary = bool(Bits::getBitN(extraFlags, 5));
mFlagLevel = static_cast<CompressionLevel>(ByteUtils::getHigherNBits(extraFlags, 2)); mFlagLevel = static_cast<CompressionLevel>(Bits::getHigherNBits(extraFlags, 2));
} }
String ZlibEncoder::getData() const String ZlibEncoder::getData() const

View file

@ -1,6 +1,6 @@
#include "DeflateBlock.h" #include "DeflateBlock.h"
#include "ByteUtils.h" #include "Bits.h"
#include "AbstractChecksumCalculator.h" #include "AbstractChecksumCalculator.h"
#include <algorithm> #include <algorithm>
@ -72,16 +72,16 @@ bool DeflateBlock::readUncompressedStream()
auto byte1 = *mInputStream->readNextByte(); auto byte1 = *mInputStream->readNextByte();
mUncompressedBlockLength = (byte0 << 8) | byte1; mUncompressedBlockLength = (byte0 << 8) | byte1;
std::cout << "Check block 0: " << ByteUtils::toString(byte0) << std::endl; std::cout << "Check block 0: " << Bits::toString(byte0) << std::endl;
std::cout << "Check block 1: " << ByteUtils::toString(byte1) << std::endl; std::cout << "Check block 1: " << Bits::toString(byte1) << std::endl;
auto byte2 = *mInputStream->readNextByte(); auto byte2 = *mInputStream->readNextByte();
auto byte3 = *mInputStream->readNextByte(); auto byte3 = *mInputStream->readNextByte();
uint16_t len_check = (byte2 << 8) | byte3; uint16_t len_check = (byte2 << 8) | byte3;
(void) len_check; (void) len_check;
//std::cout << "Check block 2: " << ByteUtils::toString(byte2) << std::endl; //std::cout << "Check block 2: " << Bits::toString(byte2) << std::endl;
//std::cout << "Check block 3: " << ByteUtils::toString(byte3) << std::endl; //std::cout << "Check block 3: " << Bits::toString(byte3) << std::endl;
//if (!(byte0 ==(~byte2) && byte1 ==(~byte3))) //if (!(byte0 ==(~byte2) && byte1 ==(~byte3)))
//{ //{
//std::cout << "Uncompressed block length check failed - aborting." << std::endl; //std::cout << "Uncompressed block length check failed - aborting." << std::endl;
@ -141,13 +141,13 @@ void DeflateBlock::write(uint16_t datalength)
void DeflateBlock::writeUncompressedStream(unsigned char working_byte, uint16_t datalength) void DeflateBlock::writeUncompressedStream(unsigned char working_byte, uint16_t datalength)
{ {
//std::cout << "Writing compression block header " << ByteUtils::toString(working_byte) << std::endl; //std::cout << "Writing compression block header " << Bits::toString(working_byte) << std::endl;
mOutputStream->writeByte(working_byte); mOutputStream->writeByte(working_byte);
//std::cout << "Writing data length " << mUncompressedBlockLength << " " << ByteUtils::toString(mUncompressedBlockLength) << std::endl; //std::cout << "Writing data length " << mUncompressedBlockLength << " " << Bits::toString(mUncompressedBlockLength) << std::endl;
mOutputStream->writeWord(datalength); mOutputStream->writeWord(datalength);
//std::cout << "Writing iverse data length " << ~mUncompressedBlockLength << " " << ByteUtils::toString(~mUncompressedBlockLength) << std::endl; //std::cout << "Writing iverse data length " << ~mUncompressedBlockLength << " " << Bits::toString(~mUncompressedBlockLength) << std::endl;
mOutputStream->writeWord(static_cast<uint16_t>(~mUncompressedBlockLength)); mOutputStream->writeWord(static_cast<uint16_t>(~mUncompressedBlockLength));
for(unsigned idx=0; idx<mUncompressedBlockLength;idx++) for(unsigned idx=0; idx<mUncompressedBlockLength;idx++)

View file

@ -1,7 +1,7 @@
#include "DeflateEncoder.h" #include "DeflateEncoder.h"
#include "BitStream.h" #include "BitStream.h"
#include "ByteUtils.h" #include "Bits.h"
#include "DeflateBlock.h" #include "DeflateBlock.h"
#include "BufferBitStream.h" #include "BufferBitStream.h"
@ -45,7 +45,7 @@ bool DeflateEncoder::encode()
if (auto byte = mInputStream->readNextByte()) if (auto byte = mInputStream->readNextByte())
{ {
//std::cout << "Adding byte " << ByteUtils::toString(*byte) << " to deflate block input" << std::endl; //std::cout << "Adding byte " << Bits::toString(*byte) << " to deflate block input" << std::endl;
stream.writeByte(*byte); stream.writeByte(*byte);
} }
else else

View file

@ -1,6 +1,6 @@
#include "HuffmanCodeLengthTable.h" #include "HuffmanCodeLengthTable.h"
#include "ByteUtils.h" #include "Bits.h"
#include "RunLengthEncoder.h" #include "RunLengthEncoder.h"
#include "BitStream.h" #include "BitStream.h"
@ -94,7 +94,7 @@ const Vector<size_t> HuffmanCodeLengthTable::getCompressedLengthCounts() const
return mCompressedLengthCounts; return mCompressedLengthCounts;
} }
std::optional<PrefixCode> HuffmanCodeLengthTable::getCodeForSymbol(unsigned symbol) const Optional<PrefixCode> HuffmanCodeLengthTable::getCodeForSymbol(unsigned symbol) const
{ {
return mTree.getCode(symbol); return mTree.getCode(symbol);
} }
@ -118,11 +118,11 @@ bool HuffmanCodeLengthTable::readNextSymbol(unsigned& result, BitStream* stream)
while(!found) while(!found)
{ {
stream->readNextNBits(delta, buffer); stream->readNextNBits(delta, buffer);
//std::cout << "Got buffer " << ByteUtils::toString(buffer) << std::endl;; //std::cout << "Got buffer " << Bits::toString(buffer) << std::endl;;
unsigned hold = buffer; unsigned hold = buffer;
working_bits = working_bits | (hold << (length - delta)); working_bits = working_bits | (hold << (length - delta));
//std::cout << "Read " << delta << " bits with length " << length << " and value " << ByteUtils::toString(working_bits) << std::endl; //std::cout << "Read " << delta << " bits with length " << length << " and value " << Bits::toString(working_bits) << std::endl;
if (const auto symbol = findMatch(working_index, working_bits)) if (const auto symbol = findMatch(working_index, working_bits))
{ {
@ -146,13 +146,13 @@ bool HuffmanCodeLengthTable::readNextSymbol(unsigned& result, BitStream* stream)
if (found) if (found)
{ {
result = working_symbol; result = working_symbol;
// std::cout << "Found symbol " << working_symbol << " with bits " << ByteUtils::toString(working_bits) << std::endl; // std::cout << "Found symbol " << working_symbol << " with bits " << Bits::toString(working_bits) << std::endl;
// std::cout << "At Byte offset " << stream->getCurrentByteOffset() << " and bit offset " << stream->getCurrentBitOffset() << std::endl; // std::cout << "At Byte offset " << stream->getCurrentByteOffset() << " and bit offset " << stream->getCurrentBitOffset() << std::endl;
return true; return true;
} }
else else
{ {
//std::cout << "SYMBOL NOT FOUND " << " with bits " << ByteUtils::toString(working_bits) << " and index " << working_index << std::endl; //std::cout << "SYMBOL NOT FOUND " << " with bits " << Bits::toString(working_bits) << " and index " << working_index << std::endl;
return false; return false;
} }
} }
@ -177,7 +177,7 @@ void HuffmanCodeLengthTable::buildPrefixCodes()
for (unsigned bits = 1; bits <= max_length; bits++) for (unsigned bits = 1; bits <= max_length; bits++)
{ {
code = (code + counts[bits-1]) << 1; code = (code + counts[bits-1]) << 1;
//std::cout << "Start code for bit " << bits << " is " << ByteUtils::toString(code) << " | dec " << code << " count " << counts[bits-1] << std::endl; //std::cout << "Start code for bit " << bits << " is " << Bits::toString(code) << " | dec " << code << " count " << counts[bits-1] << std::endl;
next_code[bits] = code; next_code[bits] = code;
} }
@ -223,7 +223,7 @@ size_t HuffmanCodeLengthTable::getNumCodeLengths() const
return mTree.getNumCodeLengths(); return mTree.getNumCodeLengths();
} }
std::optional<HuffmanTree::Symbol> HuffmanCodeLengthTable::findMatch(size_t treeIndex, uint32_t code) const Optional<HuffmanTree::Symbol> HuffmanCodeLengthTable::findMatch(size_t treeIndex, uint32_t code) const
{ {
return mTree.findMatch(treeIndex, code); return mTree.findMatch(treeIndex, code);
} }

View file

@ -17,13 +17,13 @@ public:
String dumpPrefixCodes() const; String dumpPrefixCodes() const;
std::optional<HuffmanTree::Symbol> findMatch(size_t treeIndex, uint32_t code) const; Optional<HuffmanTree::Symbol> findMatch(size_t treeIndex, uint32_t code) const;
const HuffmanTree& getTree() const; const HuffmanTree& getTree() const;
const PrefixCode& getCode(size_t index) const; const PrefixCode& getCode(size_t index) const;
std::optional<PrefixCode> getCodeForSymbol(unsigned symbol) const; Optional<PrefixCode> getCodeForSymbol(unsigned symbol) const;
using CompressedSequenceEntry = std::pair<unsigned, unsigned>; using CompressedSequenceEntry = std::pair<unsigned, unsigned>;
const Vector<CompressedSequenceEntry>& getCompressedLengthSequence() const; const Vector<CompressedSequenceEntry>& getCompressedLengthSequence() const;

View file

@ -112,22 +112,22 @@ uint32_t HuffmanEncoder::getLengthValue(unsigned)
return 0; return 0;
} }
std::optional<PrefixCode> HuffmanEncoder::getLiteralValue(unsigned char value) const Optional<PrefixCode> HuffmanEncoder::getLiteralValue(unsigned char value) const
{ {
return mLiteralLengthTable.getCodeForSymbol(value); return mLiteralLengthTable.getCodeForSymbol(value);
} }
std::optional<PrefixCode> HuffmanEncoder::getLengthValue(unsigned length) const Optional<PrefixCode> HuffmanEncoder::getLengthValue(unsigned length) const
{ {
return mLiteralLengthTable.getCodeForSymbol(length); return mLiteralLengthTable.getCodeForSymbol(length);
} }
std::optional<PrefixCode> HuffmanEncoder::getDistanceValue(unsigned distance) const Optional<PrefixCode> HuffmanEncoder::getDistanceValue(unsigned distance) const
{ {
return mDistanceTable.getCodeForSymbol(distance); return mDistanceTable.getCodeForSymbol(distance);
} }
std::optional<PrefixCode> HuffmanEncoder::getEndOfStreamValue() const Optional<PrefixCode> HuffmanEncoder::getEndOfStreamValue() const
{ {
return mLiteralLengthTable.getCodeForSymbol(256); return mLiteralLengthTable.getCodeForSymbol(256);
} }

View file

@ -13,11 +13,11 @@ class PrefixCodeGenerator
{ {
public: public:
virtual ~PrefixCodeGenerator() = default; virtual ~PrefixCodeGenerator() = default;
virtual std::optional<PrefixCode> getLiteralValue(unsigned char symbol) const = 0; virtual Optional<PrefixCode> getLiteralValue(unsigned char symbol) const = 0;
virtual std::optional<PrefixCode> getLengthValue(unsigned length) const = 0; virtual Optional<PrefixCode> getLengthValue(unsigned length) const = 0;
virtual std::optional<PrefixCode> getDistanceValue(unsigned distance) const = 0; virtual Optional<PrefixCode> getDistanceValue(unsigned distance) const = 0;
virtual std::optional<PrefixCode> getEndOfStreamValue() const = 0; virtual Optional<PrefixCode> getEndOfStreamValue() const = 0;
}; };
class HuffmanEncoder : public PrefixCodeGenerator class HuffmanEncoder : public PrefixCodeGenerator
@ -31,13 +31,13 @@ public:
uint32_t getLengthValue(unsigned length); uint32_t getLengthValue(unsigned length);
std::optional<PrefixCode> getLiteralValue(unsigned char symbol) const override; Optional<PrefixCode> getLiteralValue(unsigned char symbol) const override;
std::optional<PrefixCode> getLengthValue(unsigned length) const override; Optional<PrefixCode> getLengthValue(unsigned length) const override;
std::optional<PrefixCode> getDistanceValue(unsigned distance) const override; Optional<PrefixCode> getDistanceValue(unsigned distance) const override;
std::optional<PrefixCode> getEndOfStreamValue() const override; Optional<PrefixCode> getEndOfStreamValue() const override;
void initializeTrees(const Vector<Hit>& hits); void initializeTrees(const Vector<Hit>& hits);

View file

@ -1,6 +1,6 @@
#include "HuffmanStream.h" #include "HuffmanStream.h"
#include "ByteUtils.h" #include "Bits.h"
#include "HuffmanFixedCodes.h" #include "HuffmanFixedCodes.h"
#include <iostream> #include <iostream>
@ -335,7 +335,7 @@ void HuffmanStream::readCodingsTable()
for(unsigned idx = 0; idx< num_code_lengths; idx++) for(unsigned idx = 0; idx< num_code_lengths; idx++)
{ {
mInputStream->readNextNBits(3, buffer); mInputStream->readNextNBits(3, buffer);
//std::cout << "Got coding table value " << idx << " | " << static_cast<int>(buffer) << " | " << ByteUtils::toString(buffer) << std::endl; //std::cout << "Got coding table value " << idx << " | " << static_cast<int>(buffer) << " | " << Bits::toString(buffer) << std::endl;
sequence[idx] = buffer; sequence[idx] = buffer;
} }

View file

@ -1,6 +1,6 @@
#include "HuffmanTree.h" #include "HuffmanTree.h"
#include "ByteUtils.h" #include "Bits.h"
#include <sstream> #include <sstream>
#include <algorithm> #include <algorithm>
@ -9,7 +9,7 @@
PrefixCode::PrefixCode(uint32_t data, unsigned length) PrefixCode::PrefixCode(uint32_t data, unsigned length)
: mLength(length) : mLength(length)
{ {
mData = ByteUtils::mirror(data, length); mData = Bits::mirror(data, length);
} }
bool PrefixCode::matches(unsigned length, uint32_t code) const bool PrefixCode::matches(unsigned length, uint32_t code) const
@ -23,22 +23,22 @@ String PrefixCode::toString(bool bitsAsRightToLeft) const
{ {
if (mLength <=8 ) if (mLength <=8 )
{ {
return ByteUtils::toString(mData).substr(8 - mLength, mLength); return Bits::toString(mData).substr(8 - mLength, mLength);
} }
else else
{ {
return ByteUtils::toString(mData, mLength); return Bits::toString(mData, mLength);
} }
} }
else else
{ {
if (mLength <=8 ) if (mLength <=8 )
{ {
return ByteUtils::toString(ByteUtils::mirror(mData, mLength)).substr(0, mLength); return Bits::toString(Bits::mirror(mData, mLength)).substr(0, mLength);
} }
else else
{ {
return ByteUtils::toString(mData, mLength); return Bits::toString(mData, mLength);
} }
} }
} }
@ -67,12 +67,12 @@ void HuffmanTree::sortTable()
std::sort(mTable.begin(), mTable.end(), [](CodeLengthData a, CodeLengthData b){return a.first < b.first;}); std::sort(mTable.begin(), mTable.end(), [](CodeLengthData a, CodeLengthData b){return a.first < b.first;});
} }
std::optional<HuffmanTree::Symbol> HuffmanTree::findMatch(size_t treeIndex, uint32_t code) const Optional<HuffmanTree::Symbol> HuffmanTree::findMatch(size_t treeIndex, uint32_t code) const
{ {
const auto& legth_data = mTable[treeIndex]; const auto& legth_data = mTable[treeIndex];
for(const auto& entry : legth_data.second) for(const auto& entry : legth_data.second)
{ {
//std::cout << "Checking if " << entry.second << " matches code " << ByteUtils::toString(code) << std::endl;; //std::cout << "Checking if " << entry.second << " matches code " << Bits::toString(code) << std::endl;;
if (entry.first.matches(legth_data.first, code)) if (entry.first.matches(legth_data.first, code))
{ {
return entry.second; return entry.second;
@ -81,7 +81,7 @@ std::optional<HuffmanTree::Symbol> HuffmanTree::findMatch(size_t treeIndex, uint
return std::nullopt; return std::nullopt;
} }
std::optional<PrefixCode> HuffmanTree::getCode(Symbol symbol) const Optional<PrefixCode> HuffmanTree::getCode(Symbol symbol) const
{ {
for(const auto& entry : mTable) for(const auto& entry : mTable)
{ {

View file

@ -41,13 +41,13 @@ public:
String dump(bool bitsAsRightToLeft = true) const; String dump(bool bitsAsRightToLeft = true) const;
std::optional<HuffmanTree::Symbol> findMatch(size_t treeIndex, uint32_t code) const; Optional<HuffmanTree::Symbol> findMatch(size_t treeIndex, uint32_t code) const;
size_t getNumCodeLengths() const; size_t getNumCodeLengths() const;
unsigned getCodeLength(size_t idx) const; unsigned getCodeLength(size_t idx) const;
std::optional<PrefixCode> getCode(Symbol symbol) const; Optional<PrefixCode> getCode(Symbol symbol) const;
void sortTable(); void sortTable();
private: private:

View file

@ -1,94 +0,0 @@
set(MODULE_NAME core)
list(APPEND HEADERS
AbstractApp.h
AbstractNamedItem.h
AbstractWebApp.h
Dictionary.h
Event.h
Color.h
CommandLineArgs.h
data_structures/RawTree.h
data_structures/List.h
data_structures/Tree.h
encoding/ByteUtils.h
encoding/StringUtils.h
encoding/UnicodeUtils.h
loggers/FileLogger.h
file_utilities/Directory.h
file_utilities/File.h
file_utilities/FileFormats.h
file_utilities/PathUtils.h
http/HttpResponse.h
http/HttpHeader.h
http/HttpRequest.h
http/HttpParser.h
http/HttpPreamble.h
serializers/TomlReader.h
Win32BaseIncludes.h
ThreadCollection.h
xml/XmlParser.h
xml/XmlDocument.h
xml/XmlWriter.h
xml/xml-elements/XmlElement.h
xml/xml-elements/XmlAttribute.h
xml/xml-elements/XmlProlog.h
)
list(APPEND SOURCES
Event.cpp
Dictionary.cpp
Color.cpp
CommandLineArgs.cpp
memory/Allocator.cpp
data_structures/RawTree.cpp
data_structures/Tree.cpp
data_structures/Vector.cpp
data_structures/String.cpp
loggers/FileLogger.cpp
file_utilities/Directory.cpp
file_utilities/File.cpp
file_utilities/FileFormats.cpp
file_utilities/PathUtils.cpp
memory/SharedMemory.cpp
RandomUtils.cpp
encoding/StringUtils.cpp
encoding/ByteUtils.cpp
encoding/UnicodeUtils.cpp
streams/BinaryStream.cpp
streams/BitStream.cpp
streams/InputBitStream.cpp
streams/OutputBitStream.cpp
streams/BufferBitStream.cpp
http/HttpResponse.cpp
http/HttpHeader.cpp
http/HttpRequest.cpp
http/HttpParser.cpp
serializers/TomlReader.cpp
ThreadCollection.cpp
xml/XmlParser.cpp
xml/XmlDocument.cpp
xml/XmlWriter.cpp
xml/xml-elements/XmlElement.cpp
xml/xml-elements/XmlAttribute.cpp
xml/xml-elements/XmlProlog.cpp
)
add_library(${MODULE_NAME} SHARED ${SOURCES} ${HEADERS})
target_include_directories(${MODULE_NAME} PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}
${CMAKE_CURRENT_SOURCE_DIR}/encoding
${CMAKE_CURRENT_SOURCE_DIR}/file_utilities
${CMAKE_CURRENT_SOURCE_DIR}/loggers
${CMAKE_CURRENT_SOURCE_DIR}/memory
${CMAKE_CURRENT_SOURCE_DIR}/streams
${CMAKE_CURRENT_SOURCE_DIR}/http
${CMAKE_CURRENT_SOURCE_DIR}/base_types
${CMAKE_CURRENT_SOURCE_DIR}/data_structures
${CMAKE_CURRENT_SOURCE_DIR}/serializers
${CMAKE_CURRENT_SOURCE_DIR}/xml
${CMAKE_CURRENT_SOURCE_DIR}/xml/xml-elements
)
set_target_properties( ${MODULE_NAME} PROPERTIES WINDOWS_EXPORT_ALL_SYMBOLS ON )
set_property(TARGET ${MODULE_NAME} PROPERTY FOLDER src/base)

View file

@ -1,35 +1,47 @@
#include "CharUtils.h" #include "Char.h"
bool CharUtils::is_alpha_upper(char c) bool Char::is_alpha_upper(char c)
{ {
const auto unsigned_c = static_cast<unsigned char>(c); const auto unsigned_c = static_cast<unsigned char>(c);
return unsigned_c >= 65 && unsigned_c <= 90; return unsigned_c >= 65 && unsigned_c <= 90;
} }
bool CharUtils::is_alpha_lower(char c) bool Char::is_alpha_lower(char c)
{ {
const auto unsigned_c = static_cast<unsigned char>(c); const auto unsigned_c = static_cast<unsigned char>(c);
return unsigned_c >= 97 && unsigned_c <= 122; return unsigned_c >= 97 && unsigned_c <= 122;
} }
bool CharUtils::is_alpha(char c) bool Char::is_alpha(char c)
{ {
return is_alpha_upper(c) || is_alpha_lower(c); return is_alpha_upper(c) || is_alpha_lower(c);
} }
bool CharUtils::is_alnum(char c) bool Char::is_alnum(char c)
{ {
return is_alpha(c) || is_digit(c); return is_alpha(c) || is_digit(c);
} }
bool CharUtils::is_digit(char c) bool Char::is_digit(char c)
{ {
const auto unsigned_c = static_cast<unsigned char>(c); const auto unsigned_c = static_cast<unsigned char>(c);
return unsigned_c >= 48 && unsigned_c <= 57; return unsigned_c >= 48 && unsigned_c <= 57;
} }
bool CharUtils::is_space(char c) bool Char::is_space(char c)
{ {
return c == ' ' || c == '\f' || c == '\n' return c == ' ' || c == '\f' || c == '\n'
|| c == '\r' || c == '\t' || c == '\v'; || c == '\r' || c == '\t' || c == '\v';
}
char Char::to_lower(char c)
{
if (is_alpha_upper(c))
{
return c + 32;
}
else
{
return c;
}
} }

View file

@ -0,0 +1,29 @@
#pragma once
class Char
{
public:
static constexpr char LEFT_BRACKET = '<';
static constexpr char RIGHT_BRACKET = '>';
static constexpr char FORWARD_SLASH = '/';
static constexpr char BACK_SLASH = '\\';
static constexpr char QUESTION_MARK = '?';
static constexpr char EQUALS = '=';
static constexpr char DOUBLE_QUOTE = '"';
static constexpr char SINGLE_QUOTE = '\'';
static constexpr char COLON = ':';
static bool is_alpha_upper(char c);
static bool is_alpha_lower(char c);
static bool is_alpha(char c);
static bool is_alnum(char c);
static bool is_digit(char c);
static bool is_space(char c);
static char to_lower(char c);
};

View file

@ -19,19 +19,23 @@ Color::Color(const String& hexString)
{ {
return; return;
} }
/*
mR = toDecimal(hexString.substr(1, 2)); mR = toDecimal(hexString.substr(1, 2));
mG = toDecimal(hexString.substr(3, 2)); mG = toDecimal(hexString.substr(3, 2));
mB = toDecimal(hexString.substr(5, 2)); mB = toDecimal(hexString.substr(5, 2));
*/
} }
unsigned char Color::toDecimal(const String& hex) const unsigned char Color::toDecimal(const String& hex) const
{ {
return static_cast<unsigned char>(std::stoul("0x" + hex, nullptr, 16)); //return static_cast<unsigned char>(std::stoul("0x" + hex, nullptr, 16));
return 0;
} }
Ptr<Color> Color::Create(unsigned char r, unsigned char g, unsigned char b, double a ) Ptr<Color> Color::Create(unsigned char r, unsigned char g, unsigned char b, double a )
{ {
return std::make_unique<Color>(r, g, b, a); //return Ptr<Color>::create(r, g, b, a);
return {};
} }
size_t Color::getSize() const size_t Color::getSize() const
@ -60,10 +64,6 @@ uint8_t Color::getByte(size_t index) const
{ {
if (mFormat == Format::GRAYSCALE || mFormat == Format::LUT) if (mFormat == Format::GRAYSCALE || mFormat == Format::LUT)
{ {
if (index > 0)
{
throw std::range_error("Color index out of range in getByte()");
}
return mValue; return mValue;
} }
else if (mFormat == Format::ARGB) else if (mFormat == Format::ARGB)
@ -79,7 +79,7 @@ uint8_t Color::getByte(size_t index) const
case 3: case 3:
return mB; return mB;
default: default:
throw std::range_error("Color index out of range in getByte()"); return mR;
} }
} }
else else
@ -95,7 +95,7 @@ uint8_t Color::getByte(size_t index) const
case 3: case 3:
return mAlpha * 255; return mAlpha * 255;
default: default:
throw std::range_error("Color index out of range in getByte()"); return mR;
} }
} }
} }
@ -107,7 +107,7 @@ void Color::setAlpha(float alpha)
Ptr<Color> Color::Create(const Color& color) Ptr<Color> Color::Create(const Color& color)
{ {
return std::make_unique<Color>(color); return Ptr<Color>::create(color);
} }
unsigned char Color::getR() const unsigned char Color::getR() const
@ -132,7 +132,8 @@ double Color::getAlpha() const
Vector<double> Color::getAsVectorDouble() const Vector<double> Color::getAsVectorDouble() const
{ {
return { mR / 255.0, mG / 255.0, mB / 255.0, mAlpha }; //return { mR / 255.0, mG / 255.0, mB / 255.0, mAlpha };
return {};
} }
uint32_t Color::getAsUInt32() const uint32_t Color::getAsUInt32() const
@ -142,5 +143,5 @@ uint32_t Color::getAsUInt32() const
String Color::toString() const String Color::toString() const
{ {
return std::to_string(static_cast<int>(mR)) + "," + std::to_string(static_cast<int>(mG)) + "," + std::to_string(static_cast<int>(mB)); return String::to_string(static_cast<int>(mR)) + "," + String::to_string(static_cast<int>(mG)) + "," + String::to_string(static_cast<int>(mB));
} }

View file

@ -3,7 +3,6 @@
#include "Pointer.h" #include "Pointer.h"
#include "Vector.h" #include "Vector.h"
#include "String.h" #include "String.h"
#include <stdexcept>
class Color class Color
{ {
@ -69,6 +68,3 @@ private:
unsigned mBitDepth{8}; unsigned mBitDepth{8};
Format mFormat{Format::RGBA}; Format mFormat{Format::RGBA};
}; };
using ColorPtr = std::shared_ptr<Color>;
using ColorUPtr = Ptr<Color>;

View file

@ -23,6 +23,11 @@ public:
return m_is_set; return m_is_set;
} }
operator bool() const
{
return m_is_set;
}
private: private:
bool m_is_set{false}; bool m_is_set{false};
T m_value; T m_value;

View file

@ -1,5 +1,6 @@
#include "String.h" #include "String.h"
#include "Char.h"
#include <stdio.h> #include <stdio.h>
#include <stdarg.h> #include <stdarg.h>
@ -107,6 +108,22 @@ void String::eraseIf(erasePredicate func)
m_data[count] = '\0'; m_data[count] = '\0';
} }
bool String::is_whitespace() const
{
if (empty())
{
return true;
}
for(size_t idx=0;idx<size();idx++)
{
if (!Char::is_space(m_data[idx]))
{
return false;
}
}
return true;
}
const Vector<char>& String::data() const const Vector<char>& String::data() const
{ {
return m_data; return m_data;
@ -117,6 +134,18 @@ bool String::empty() const
return m_data.empty() || m_data[0] == '\0'; return m_data.empty() || m_data[0] == '\0';
} }
void String::to_bytes(Vector<Byte>& bytes) const
{
if (m_data.empty())
{
return;
}
for(size_t idx=0; idx<m_data.size() - 1; idx++)
{
bytes.push_back(m_data[idx]);
}
}
void String::append(const Vector<Byte>& data) void String::append(const Vector<Byte>& data)
{ {
if (data.empty()) if (data.empty())
@ -229,6 +258,34 @@ void String::reverse()
} }
} }
void String::split(Vector<String>& output, char delimiter) const
{
String working_string;
bool last_was_non_delimiter{ false };
for (size_t idx = 0; idx<size(); idx++)
{
const auto c = m_data[idx];
if (c == delimiter)
{
if (last_was_non_delimiter)
{
output.push_back(working_string);
working_string = "";
last_was_non_delimiter = false;
}
}
else
{
last_was_non_delimiter = true;
working_string += c;
}
}
if (!working_string.empty())
{
output.push_back(working_string);
}
}
String String::to_string(size_t input) String String::to_string(size_t input)
{ {
String conv; String conv;
@ -276,6 +333,12 @@ String& String::operator<<(size_t idx)
return *this; return *this;
} }
String& String::operator<<(const int idx)
{
*this += to_string(idx);
return *this;
}
void String::push_back(char c) void String::push_back(char c)
{ {
if (m_data.empty()) if (m_data.empty())
@ -286,6 +349,25 @@ void String::push_back(char c)
m_data[m_data.size()-2] = c; m_data[m_data.size()-2] = c;
} }
void String::to_lower()
{
if (empty())
{
return;
}
for(size_t idx=0; idx<size(); idx++)
{
m_data[idx] = Char::to_lower(m_data[idx]);
}
}
String String::to_lower(const String& input)
{
String ret = input;
ret.to_lower();
return ret;
}
String& String::operator+=(const String& str) String& String::operator+=(const String& str)
{ {
if (m_data.empty()) if (m_data.empty())

View file

@ -29,6 +29,8 @@ public:
using erasePredicate = std::function<bool(char)>; using erasePredicate = std::function<bool(char)>;
void eraseIf(erasePredicate func); void eraseIf(erasePredicate func);
bool is_whitespace() const;
void push_back(char c); void push_back(char c);
const char* raw() const; const char* raw() const;
@ -45,8 +47,16 @@ public:
bool slice(size_t start, size_t end, String& out) const; bool slice(size_t start, size_t end, String& out) const;
void split(Vector<String>& output, char delimiter = ' ') const;
static String to_string(size_t input); static String to_string(size_t input);
void to_bytes(Vector<Byte>& bytes) const;
void to_lower();
static String to_lower(const String& input);
char operator[](size_t idx) const; char operator[](size_t idx) const;
String& operator<<(const String& body); String& operator<<(const String& body);
@ -55,6 +65,8 @@ public:
String& operator<<(size_t idx); String& operator<<(size_t idx);
String& operator<<(const int idx);
template<typename T> template<typename T>
String& operator<<(const T& stringable) String& operator<<(const T& stringable)
{ {

View file

@ -1,32 +1,31 @@
#include "ByteUtils.h" #include "Bits.h"
bool Bits::MostSignificantBitIsOne(Byte c)
bool ByteUtils::MostSignificantBitIsOne(Byte c)
{ {
return c & (1 << 7); return c & (1 << 7);
} }
Word ByteUtils::GetWordFirstBit(const Word word) Word Bits::GetWordFirstBit(const Word word)
{ {
return word & ByteUtils::WORD_FIRST_BIT; return word & Bits::WORD_FIRST_BIT;
}; };
Word ByteUtils::GetWordLastByte(const Word word) Word Bits::GetWordLastByte(const Word word)
{ {
return word & ByteUtils::WORD_LAST_BYTE; return word & Bits::WORD_LAST_BYTE;
} }
unsigned char ByteUtils::getHigherNBits(Byte input, size_t num) unsigned char Bits::getHigherNBits(Byte input, size_t num)
{ {
return input >> (8 - num); return input >> (8 - num);
} }
unsigned char ByteUtils::getByteN(DWord input, size_t n) unsigned char Bits::getByteN(DWord input, size_t n)
{ {
return (input << 8*n) >> 24; return (input << 8*n) >> 24;
} }
uint32_t ByteUtils::mirror(DWord byte, size_t length) uint32_t Bits::mirror(DWord byte, size_t length)
{ {
uint32_t ret{0}; uint32_t ret{0};
for(unsigned idx=0; idx<length; idx++) for(unsigned idx=0; idx<length; idx++)
@ -39,7 +38,7 @@ uint32_t ByteUtils::mirror(DWord byte, size_t length)
return ret; return ret;
} }
unsigned char ByteUtils::getLowerNBits(DWord input, size_t num) unsigned char Bits::getLowerNBits(DWord input, size_t num)
{ {
switch (num) switch (num)
{ {
@ -64,12 +63,12 @@ unsigned char ByteUtils::getLowerNBits(DWord input, size_t num)
} }
} }
unsigned char ByteUtils::getTwoBitsAtN(Byte input, size_t n) unsigned char Bits::getTwoBitsAtN(Byte input, size_t n)
{ {
return (input & (0x03 << n)) >> n; return (input & (0x03 << n)) >> n;
} }
unsigned char ByteUtils::getMBitsAtN(Byte input, size_t m, size_t n) unsigned char Bits::getMBitsAtN(Byte input, size_t m, size_t n)
{ {
switch (m) switch (m)
{ {
@ -94,12 +93,12 @@ unsigned char ByteUtils::getMBitsAtN(Byte input, size_t m, size_t n)
} }
} }
bool ByteUtils::getBitN(DWord input, size_t n) bool Bits::getBitN(DWord input, size_t n)
{ {
return input & (1 << n); return input & (1 << n);
} }
unsigned char ByteUtils::getFromString(const String& string) unsigned char Bits::getFromString(const String& string)
{ {
unsigned char ret{0}; unsigned char ret{0};
@ -118,7 +117,7 @@ unsigned char ByteUtils::getFromString(const String& string)
return ret; return ret;
} }
String ByteUtils::toString(DWord input, size_t length) String Bits::toString(DWord input, size_t length)
{ {
String ret; String ret;
if (length > 8) if (length > 8)
@ -144,7 +143,7 @@ String ByteUtils::toString(DWord input, size_t length)
return ret; return ret;
} }
void ByteUtils::ReverseBuffer(Byte* buffer, char* reverse, size_t size, size_t targetSize) void Bits::ReverseBuffer(Byte* buffer, char* reverse, size_t size, size_t targetSize)
{ {
for(unsigned idx=0; idx<targetSize; idx++) for(unsigned idx=0; idx<targetSize; idx++)
{ {
@ -159,22 +158,22 @@ void ByteUtils::ReverseBuffer(Byte* buffer, char* reverse, size_t size, size_t t
} }
} }
Word ByteUtils::ToWord(Byte* buffer, bool reverse) Word Bits::ToWord(Byte* buffer, bool reverse)
{ {
return ToType<Word>(buffer, reverse); return ToType<Word>(buffer, reverse);
} }
DWord ByteUtils::ToDWord(Byte* buffer, bool reverse) DWord Bits::ToDWord(Byte* buffer, bool reverse)
{ {
return ToType<DWord>(buffer, reverse); return ToType<DWord>(buffer, reverse);
} }
QWord ByteUtils::ToQWord(Byte* buffer, bool reverse) QWord Bits::ToQWord(Byte* buffer, bool reverse)
{ {
return ToType<QWord>(buffer, reverse); return ToType<QWord>(buffer, reverse);
} }
bool ByteUtils::Compare(Byte* buffer, const char* tag, size_t size) bool Bits::Compare(Byte* buffer, const char* tag, size_t size)
{ {
for(unsigned idx=0; idx<size; idx++) for(unsigned idx=0; idx<size; idx++)
{ {
@ -186,12 +185,24 @@ bool ByteUtils::Compare(Byte* buffer, const char* tag, size_t size)
return true; return true;
} }
bool ByteUtils::CompareDWords(Byte* buffer, const char* tag) bool Bits::CompareDWords(Byte* buffer, DWord target)
{ {
return Compare(buffer, tag, sizeof(DWord)); for(size_t idx=0; idx<4;idx++)
{
if (buffer[idx] != getByteN(target, idx))
{
return false;
}
}
return true;
} }
bool ByteUtils::CompareWords(Byte* buffer, const char* tag) bool Bits::CompareWords(Byte* buffer, Word target)
{ {
return Compare(buffer, tag, sizeof(Word)); bool first_byte_matches = (buffer[0] == GetWordFirstBit(target));
if (!first_byte_matches)
{
return false;
}
return buffer[1] == GetWordLastByte(target);
} }

View file

@ -3,7 +3,7 @@
#include "ByteTypes.h" #include "ByteTypes.h"
#include "String.h" #include "String.h"
class ByteUtils class Bits
{ {
public: public:
static bool MostSignificantBitIsOne(Byte c); static bool MostSignificantBitIsOne(Byte c);
@ -57,9 +57,9 @@ public:
static bool Compare(Byte* buffer, const char* tag, size_t size); static bool Compare(Byte* buffer, const char* tag, size_t size);
static bool CompareDWords(Byte* buffer, const char* tag); static bool CompareDWords(Byte* buffer, DWord target);
static bool CompareWords(Byte* buffer, const char* tag); static bool CompareWords(Byte* buffer, Word target);
static const int BYTE_FIRST_BIT = 0x40; // 1000 0000 static const int BYTE_FIRST_BIT = 0x40; // 1000 0000
static const Word WORD_FIRST_BIT = static_cast<Word>(0x8000); // 1000 0000 - 0000 0000 static const Word WORD_FIRST_BIT = static_cast<Word>(0x8000); // 1000 0000 - 0000 0000

View file

@ -1,17 +0,0 @@
#pragma once
class CharUtils
{
public:
static bool is_alpha_upper(char c);
static bool is_alpha_lower(char c);
static bool is_alpha(char c);
static bool is_alnum(char c);
static bool is_digit(char c);
static bool is_space(char c);
};

View file

@ -1,60 +1,15 @@
#include "StringUtils.h" #include "StringUtils.h"
#include "Char.h"
#include <locale> #include <locale>
#include <algorithm> #include <algorithm>
#include <sstream> #include <sstream>
#include <iomanip> #include <iomanip>
bool StringUtils::isAlphaNumeric(char c)
{
return std::isalnum(c);
}
bool StringUtils::isSpace(char c)
{
return std::isspace(c);
}
bool StringUtils::isAlphabetical(char c)
{
return std::isalpha(c);
}
Vector<unsigned char> StringUtils::toBytes(const String& input)
{
return {input.begin(), input.end()};
}
String StringUtils::toString(const Vector<unsigned char>& bytes)
{
return {bytes.begin(), bytes.end()};
}
Vector<String> StringUtils::toLines(const String& input)
{
auto result = Vector<String>{};
auto ss = Stringstream{input};
for (String line; std::getline(ss, line, '\n');)
{
result.push_back(line);
}
return result;
}
bool StringUtils::isWhitespaceOnly(const String& input)
{
if (input.empty())
{
return true;
}
else
{
return std::all_of(input.cbegin(), input.cend(), [](char c){ return std::isspace(c); });
}
}
size_t StringUtils::countFirstConsecutiveHits(const String& input, char c) size_t StringUtils::countFirstConsecutiveHits(const String& input, char c)
{ {
/*
auto found_id = input.find(c); auto found_id = input.find(c);
if(found_id == String::npos) if(found_id == String::npos)
{ {
@ -76,10 +31,13 @@ size_t StringUtils::countFirstConsecutiveHits(const String& input, char c)
} }
return count; return count;
} }
*/
return 0;
} }
String StringUtils::stripSurroundingWhitepsace(const String& input) String StringUtils::stripSurroundingWhitepsace(const String& input)
{ {
/*
if (input.empty()) if (input.empty())
{ {
return {}; return {};
@ -110,54 +68,24 @@ String StringUtils::stripSurroundingWhitepsace(const String& input)
} }
} }
return input.substr(first_nonspace, last_nonspace-first_nonspace + 1); return input.substr(first_nonspace, last_nonspace-first_nonspace + 1);
}
*/
Vector<String> StringUtils::split(const String& input) return {};
{
Vector<String> substrings;
String working_string;
std::locale loc;
bool last_was_nonspace{ false };
for (auto c : input)
{
if (std::isspace(c, loc))
{
if (last_was_nonspace)
{
substrings.push_back(working_string);
working_string = "";
last_was_nonspace = false;
}
}
else
{
last_was_nonspace = true;
working_string += c;
}
}
if (!working_string.empty())
{
substrings.push_back(working_string);
}
return substrings;
}
String StringUtils::toLower(const String& s)
{
String ret;
std::transform(s.begin(), s.end(), ret.begin(), [](unsigned char c){ return std::tolower(c); });
return ret;
} }
String StringUtils::toPaddedString(unsigned numBytes, unsigned entry) String StringUtils::toPaddedString(unsigned numBytes, unsigned entry)
{ {
/*
Stringstream sstr; Stringstream sstr;
sstr << std::setfill('0') << std::setw(numBytes) << entry; sstr << std::setfill('0') << std::setw(numBytes) << entry;
return sstr.str(); return sstr.str();
*/
return {};
} }
String StringUtils::stripQuotes(const String& input) String StringUtils::stripQuotes(const String& input)
{ {
/*
if (input.size() < 3) if (input.size() < 3)
{ {
return input; return input;
@ -173,10 +101,13 @@ String StringUtils::stripQuotes(const String& input)
end_index = end_index - 1; end_index = end_index - 1;
} }
return input.substr(start_index, end_index - start_index + 1); return input.substr(start_index, end_index - start_index + 1);
*/
return {};
} }
String StringUtils::removeUpTo(const String& input, const String& prefix) String StringUtils::removeUpTo(const String& input, const String& prefix)
{ {
/*
size_t found = input.find(prefix); size_t found = input.find(prefix);
if (found != String::npos) if (found != String::npos)
{ {
@ -186,10 +117,13 @@ String StringUtils::removeUpTo(const String& input, const String& prefix)
{ {
return input; return input;
} }
*/
return {};
} }
bool StringUtils::startsWith(const String& input, const String& prefix, bool ignoreWhitespace) bool StringUtils::startsWith(const String& input, const String& prefix, bool ignoreWhitespace)
{ {
/*
if(ignoreWhitespace) if(ignoreWhitespace)
{ {
const auto loc = input.find(prefix); const auto loc = input.find(prefix);
@ -206,4 +140,6 @@ bool StringUtils::startsWith(const String& input, const String& prefix, bool ign
{ {
return input.find(prefix) == 0; return input.find(prefix) == 0;
} }
*/
return false;
} }

View file

@ -8,43 +8,15 @@
class StringUtils class StringUtils
{ {
public: public:
static constexpr char LEFT_BRACKET = '<';
static constexpr char RIGHT_BRACKET = '>';
static constexpr char FORWARD_SLASH = '/';
static constexpr char BACK_SLASH = '\\';
static constexpr char QUESTION_MARK = '?';
static constexpr char EQUALS = '=';
static constexpr char DOUBLE_QUOTE = '"';
static constexpr char SINGLE_QUOTE = '\'';
static constexpr char COLON = ':';
static size_t countFirstConsecutiveHits(const String& input, char c); static size_t countFirstConsecutiveHits(const String& input, char c);
static bool isAlphaNumeric(char c);
static bool isAlphabetical(char c);
static bool isSpace(char c);
static bool isWhitespaceOnly(const String& input);
static String removeUpTo(const String& input, const String& prefix); static String removeUpTo(const String& input, const String& prefix);
static Vector<String> split(const String& input);
static bool startsWith(const String& input, const String& prefix, bool ignoreWhitespace = false); static bool startsWith(const String& input, const String& prefix, bool ignoreWhitespace = false);
static String stripSurroundingWhitepsace(const String& input); static String stripSurroundingWhitepsace(const String& input);
static String stripQuotes(const String& input); static String stripQuotes(const String& input);
static Vector<unsigned char> toBytes(const String& input);
static String toLower(const String& s);
static Vector<String> toLines(const String& input);
static String toPaddedString(unsigned numBytes, unsigned entry); static String toPaddedString(unsigned numBytes, unsigned entry);
static String toString(const Vector<unsigned char>& bytes);
}; };

View file

@ -1,4 +1,4 @@
#include "UnicodeUtils.h" #include "Unicode.h"
#include "Win32BaseIncludes.h" #include "Win32BaseIncludes.h"
@ -45,7 +45,7 @@ std::wstring UnicodeUtils::utf8ToUtf16WString(const String& input)
} }
*/ */
Vector<uint32_t> UnicodeUtils::utf8ToUtf32(const String& input) Vector<uint32_t> Unicode::utf8ToUtf32(const String& input)
{ {
//const auto utf_16 = utf8ToUtf16WString(input); //const auto utf_16 = utf8ToUtf16WString(input);
@ -77,22 +77,22 @@ Vector<uint32_t> UnicodeUtils::utf8ToUtf32(const String& input)
return output; return output;
} }
bool UnicodeUtils::isSurrogate(wchar_t c) bool Unicode::isSurrogate(wchar_t c)
{ {
return (c - 0xd800u) < 2048u; return (c - 0xd800u) < 2048u;
} }
bool UnicodeUtils::isHighSurrogate(wchar_t c) bool Unicode::isHighSurrogate(wchar_t c)
{ {
return (c & 0xfffffc00) == 0xd800; return (c & 0xfffffc00) == 0xd800;
} }
bool UnicodeUtils::isLowSurrogate(wchar_t c) bool Unicode::isLowSurrogate(wchar_t c)
{ {
return (c & 0xfffffc00) == 0xdc00; return (c & 0xfffffc00) == 0xdc00;
} }
uint32_t UnicodeUtils::surrogateToUtf32(wchar_t high, wchar_t low) uint32_t Unicode::surrogateToUtf32(wchar_t high, wchar_t low)
{ {
return (high << 10) + low - 0x35fdc00; return (high << 10) + low - 0x35fdc00;
} }

View file

@ -4,7 +4,7 @@
#include "Vector.h" #include "Vector.h"
#include <cstdint> #include <cstdint>
class UnicodeUtils class Unicode
{ {
public: public:
//static String utf16ToUtf8String(const std::wstring& input); //static String utf16ToUtf8String(const std::wstring& input);

View file

@ -1,7 +1,6 @@
#include "File.h" #include "File.h"
//#include "FileLogger.h" #include "Char.h"
#include "ByteUtils.h"
#include "Directory.h" #include "Directory.h"
#include "Result.h" #include "Result.h"
@ -251,7 +250,7 @@ String File::dumpBinary()
String hex_sstr; String hex_sstr;
hex_sstr << std::setfill('0') << std::setw(2) << std::hex << static_cast<int>(byte); hex_sstr << std::setfill('0') << std::setw(2) << std::hex << static_cast<int>(byte);
sstr << count << " | " << ByteUtils::toString(byte) << " | " << static_cast<int>(byte) << " | " << hex_sstr.str() << " | " << ascii_val << '\n'; sstr << count << " | " << Bits::toString(byte) << " | " << static_cast<int>(byte) << " | " << hex_sstr.str() << " | " << ascii_val << '\n';
if (count % 10 == 0) if (count % 10 == 0)
{ {
sstr << "\n"; sstr << "\n";

View file

@ -13,7 +13,7 @@ FileFormat::ExtensionMap FileFormat::mExtensions = []
bool FileFormat::isFormat(const String& extension, Format format) bool FileFormat::isFormat(const String& extension, Format format)
{ {
return StringUtils::toLower(extension) == (*mExtensions.find(format)).value(); return String::to_lower(extension) == (*mExtensions.find(format)).value();
} }
FileFormat::Format FileFormat::inferFormat(const String& query) FileFormat::Format FileFormat::inferFormat(const String& query)

View file

@ -36,6 +36,20 @@ public:
FileSystemPath parent_path() const; FileSystemPath parent_path() const;
FileSystemPath operator/ (const char* body) const
{
String ret = m_path;
ret += m_delimiter + String(body);
return ret;
}
FileSystemPath operator/ (const String& body) const
{
String ret = m_path;
ret += m_delimiter + body;
return ret;
}
private: private:
String m_delimiter{"/"}; String m_delimiter{"/"};
String m_path; String m_path;

View file

@ -1,6 +1,6 @@
#include "FileLogger.h" #include "FileLogger.h"
#include <time.h> #include "FileSystemPath.h"
FileLogger::FileLogger() FileLogger::FileLogger()
:mWorkDirectory(), :mWorkDirectory(),
@ -9,13 +9,6 @@ FileLogger::FileLogger()
} }
FileLogger& FileLogger::GetInstance()
{
static FileLogger instance;
return instance;
}
FileLogger::~FileLogger() FileLogger::~FileLogger()
{ {
@ -35,28 +28,14 @@ void FileLogger::Open()
{ {
if (mWorkDirectory.empty()) if (mWorkDirectory.empty())
{ {
mWorkDirectory = std::filesystem::current_path().string(); mWorkDirectory = FileSystemPath::current_dir().value().str();
} }
mFileStream.open(mWorkDirectory + "/" + mFileName); //mFileStream.open(mWorkDirectory + "/" + mFileName);
} }
void FileLogger::Close() void FileLogger::Close()
{ {
mFileStream.close(); //mFileStream.close();
}
void FileLogger::LogLine(const std::ostringstream& line)
{
if (mDisabled)
{
return;
}
if (!mFileStream.is_open())
{
Open();
}
mFileStream << line.str() << std::endl;
} }
void FileLogger::disable() void FileLogger::disable()
@ -64,21 +43,28 @@ void FileLogger::disable()
mDisabled = true; mDisabled = true;
} }
void FileLogger::LogLine(const String& logType, const std::ostringstream& line, const String& fileName, const String& functionName, int lineNumber) void FileLogger::log_line(Level level,
const String& line,
const String& fileName,
const String& functionName,
int lineNumber)
{ {
if (mDisabled) if (mDisabled)
{ {
return; return;
} }
std::time_t t = std::time(nullptr); const auto log_msg = build_log_message(level,
const String cleanedFileName = fileName.substr(fileName.find_last_of("/\\") + 1); line,
std::tm time_buf{ }; fileName,
#ifdef WIN32 functionName,
gmtime_s(&time_buf, &t); lineNumber);
#else if (level == Level::INFO)
gmtime_r(&t, &time_buf); {
#endif //printf("%s\n", log_msg.raw());
std::cout << logType << "|" << std::put_time(&time_buf, "%T") << "|" << cleanedFileName << "::" << functionName << "::" << lineNumber << "|" << line.str() << std::endl; }
mFileStream << logType << "|" << std::put_time(&time_buf, "%T") << "|" << cleanedFileName << "::" << functionName << "::" << lineNumber << "|" << line.str() << std::endl; else
{
//fprintf(stderr, "%s\n", log_msg.raw());
}
} }

View file

@ -1,18 +1,11 @@
#pragma once #pragma once
#include "Pointer.h" #include "Logger.h"
#include "String.h"
class FileLogger class FileLogger : public Logger
{ {
FileLogger(); FileLogger();
public:
static FileLogger& GetInstance();
FileLogger(FileLogger const&) = delete;
void operator=(FileLogger const&) = delete;
~FileLogger(); ~FileLogger();
void disable(); void disable();
@ -25,9 +18,11 @@ public:
void Close(); void Close();
void LogLine(const String& line); void log_line(Level level,
const String& line,
void LogLine(const String& logType, const String& line, const String& fileName = "", const String& functionName = "", int lineNumber=-1); const String& fileName,
const String& functionName,
int lineNumber) override;
private: private:
bool mDisabled{false}; bool mDisabled{false};
String mWorkDirectory; String mWorkDirectory;

View file

@ -1,9 +1,11 @@
#include "SharedPointer.h" #include "SharedMemory.h"
#include "RandomUtils.h" #include "RandomUtils.h"
#ifdef __linux__ #ifdef __linux__
#include <unistd.h> #include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h> #include <sys/types.h>
#include <sys/mman.h> #include <sys/mman.h>
#include <sys/stat.h> #include <sys/stat.h>
@ -53,10 +55,10 @@ void SharedMemory::createFile(const String& namePrefix)
const auto name = getRandomName(namePrefix); const auto name = getRandomName(namePrefix);
--retries; --retries;
const int fd = shm_open(name.c_str(), O_RDWR | O_CREAT | O_EXCL, 0600); const int fd = shm_open(name.raw(), O_RDWR | O_CREAT | O_EXCL, 0600);
if (fd >= 0) if (fd >= 0)
{ {
shm_unlink(name.c_str()); shm_unlink(name.raw());
mFileDescriptor = fd; mFileDescriptor = fd;
mIsValid = true; mIsValid = true;
break; break;
@ -72,7 +74,7 @@ String SharedMemory::getRandomName(const String& namePrefix) const
String randomSuffix; String randomSuffix;
for (const auto entry : RandomUtils::getRandomVecUnsigned(6)) for (const auto entry : RandomUtils::getRandomVecUnsigned(6))
{ {
randomSuffix += std::to_string(entry); randomSuffix += String::to_string(entry);
} }
return namePrefix + randomSuffix; return namePrefix + randomSuffix;
} }

View file

@ -1,5 +1,5 @@
#include "HttpHeader.h" #include "HttpHeader.h"
#include "StringUtils.h" #include "Char.h"
HttpHeader::HttpHeader() HttpHeader::HttpHeader()
@ -29,7 +29,7 @@ void HttpHeader::parse(const Vector<String >& message)
for(size_t idx = 0; idx< line.size(); idx++) for(size_t idx = 0; idx< line.size(); idx++)
{ {
const auto c = line[idx]; const auto c = line[idx];
if (c == StringUtils::COLON) if (c == Char::COLON)
{ {
foundDelimiter = true; foundDelimiter = true;
} }
@ -91,7 +91,7 @@ void HttpHeader::parse(const Vector<String >& message)
} }
else else
{ {
mOtherFields[tag] = value; mOtherFields.insert(tag, value);
} }
} }

View file

@ -2,7 +2,7 @@
#include "String.h" #include "String.h"
#include "Vector.h" #include "Vector.h"
#include <map> #include "Map.h"
class HttpHeader class HttpHeader
{ {
@ -28,5 +28,5 @@ private:
String mSecFetchMode; String mSecFetchMode;
String mSecFetchSite; String mSecFetchSite;
std::map<String, String> mOtherFields; Map<String, String> mOtherFields;
}; };

View file

@ -1,8 +1,9 @@
#include "HttpParser.h" #include "HttpParser.h"
#include "StringUtils.h" #include "Char.h"
bool HttpParser::parsePreamble(const String& line, HttpPreamble& preamble) bool HttpParser::parsePreamble(const String& line,
HttpPreamble& preamble)
{ {
bool inPath{ false }; bool inPath{ false };
bool inMethod{ true }; bool inMethod{ true };
@ -12,7 +13,7 @@ bool HttpParser::parsePreamble(const String& line, HttpPreamble& preamble)
{ {
if (inPath) if (inPath)
{ {
if (StringUtils::isSpace(c)) if (Char::is_space(c))
{ {
inPath = false; inPath = false;
inMethod = true; inMethod = true;
@ -24,7 +25,7 @@ bool HttpParser::parsePreamble(const String& line, HttpPreamble& preamble)
} }
else if (inMethod) else if (inMethod)
{ {
if (StringUtils::isSpace(c)) if (Char::is_space(c))
{ {
inMethod = false; inMethod = false;
inProtocol = true; inProtocol = true;

View file

@ -5,5 +5,6 @@
class HttpParser class HttpParser
{ {
public: public:
static bool parsePreamble(const String& line, HttpPreamble& preamble); static bool parsePreamble(const String& line,
HttpPreamble& preamble);
}; };

View file

@ -3,8 +3,6 @@
#include "StringUtils.h" #include "StringUtils.h"
#include "HttpParser.h" #include "HttpParser.h"
#include <sstream>
HttpRequest::HttpRequest(Verb verb, const String& path) HttpRequest::HttpRequest(Verb verb, const String& path)
: mVerb(verb) : mVerb(verb)
{ {
@ -31,30 +29,30 @@ String HttpRequest::toString(const String& host) const
} }
auto path = mPreamble.mPath; auto path = mPreamble.mPath;
out += " /" + path + " HTTP/" + mHeader.getHttpVersion() + "\n"; out += _s(" /") + path + _s(" HTTP/") + mHeader.getHttpVersion() + _s("\n");
out += "Host: " + host + "\n"; out += _s("Host: ") + host + _s("\n");
out += "Accept - Encoding: \n"; out += "Accept - Encoding: \n";
return out; return out;
} }
void HttpRequest::fromString(const String& message) void HttpRequest::fromString(const String& message)
{ {
Stringstream ss(message);
String buffer; String buffer;
bool firstLine{ true }; bool firstLine{ true };
Vector<String> headers; Vector<String> headers;
while (std::getline(ss, buffer, '\n')) Vector<String> lines;
message.split(lines, '\n');
for(const auto& line : lines)
{ {
if (firstLine) if (firstLine)
{ {
HttpParser::parsePreamble(buffer, mPreamble); HttpParser::parsePreamble(line, mPreamble);
firstLine = false; firstLine = false;
} }
else else
{ {
headers.push_back(buffer); headers.push_back(line);
} }
} }
if (mPreamble.mMethod == "GET") if (mPreamble.mMethod == "GET")

View file

@ -3,8 +3,6 @@
#include "StringUtils.h" #include "StringUtils.h"
#include "HttpParser.h" #include "HttpParser.h"
#include <sstream>
HttpResponse::HttpResponse() HttpResponse::HttpResponse()
: mStatusCode(200), : mStatusCode(200),
mResponseReason("OK"), mResponseReason("OK"),
@ -40,22 +38,22 @@ void HttpResponse::setBody(const String& body)
void HttpResponse::fromMessage(const String& message) void HttpResponse::fromMessage(const String& message)
{ {
Stringstream ss(message);
String buffer; String buffer;
bool firstLine{ true }; bool firstLine{ true };
Vector<String> headers; Vector<String> headers;
while (std::getline(ss, buffer, '\n')) Vector<String> lines;
message.split(lines, '\n');
for(const auto& line : lines)
{ {
if (firstLine) if (firstLine)
{ {
HttpParser::parsePreamble(buffer, mPreamble); HttpParser::parsePreamble(line, mPreamble);
firstLine = false; firstLine = false;
} }
else else
{ {
headers.push_back(buffer); headers.push_back(line);
} }
} }
mHeader.parse(headers); mHeader.parse(headers);
@ -63,7 +61,7 @@ void HttpResponse::fromMessage(const String& message)
unsigned HttpResponse::getBodyLength() const unsigned HttpResponse::getBodyLength() const
{ {
return unsigned(mBody.length()); return unsigned(mBody.size());
} }
void HttpResponse::setClientError(const ClientError& error) void HttpResponse::setClientError(const ClientError& error)
@ -73,9 +71,9 @@ void HttpResponse::setClientError(const ClientError& error)
String HttpResponse::getHeaderString() const String HttpResponse::getHeaderString() const
{ {
String header = "HTTP/" + mHeader.getHttpVersion() + " " + std::to_string(mStatusCode) + " " + mResponseReason + "\n"; String header = _s("HTTP/") + mHeader.getHttpVersion() + _s(" ") + String::to_string(mStatusCode) + _s(" ") + mResponseReason + _s("\n");
header += "Content-Type: " + mHeader.getContentType() + "\n"; header += _s("Content-Type: ") + mHeader.getContentType() + _s("\n");
header += "Content-Length: " + std::to_string(getBodyLength()) + "\n"; header += _s("Content-Length: ") + String::to_string(getBodyLength()) + _s("\n");
return header; return header;
} }
@ -86,7 +84,7 @@ const String& HttpResponse::getResponseReason() const
String HttpResponse::toString() const String HttpResponse::toString() const
{ {
return getHeaderString() + "\n\n" + mBody; return getHeaderString() + _s("\n\n") + mBody;
} }
void HttpResponse::setStatusCode(unsigned short code) void HttpResponse::setStatusCode(unsigned short code)

View file

@ -1,6 +1,6 @@
#include "TomlReader.h" #include "TomlReader.h"
#include "CharUtils.h" #include "Char.h"
TomlTable::TomlTable(const String& header) TomlTable::TomlTable(const String& header)
: mHeader(header) : mHeader(header)
@ -128,8 +128,8 @@ void TomlReader::processLine(const String& line)
} }
else if (found_key) else if (found_key)
{ {
key_string.eraseIf([](char c) {return CharUtils::is_space(c); }); key_string.eraseIf([](char c) {return Char::is_space(c); });
working_string.eraseIf([](char c) {return CharUtils::is_space(c); }); working_string.eraseIf([](char c) {return Char::is_space(c); });
if (working_string.size()>2 && working_string[0] == '"' && working_string[working_string.size() - 1] == '"') if (working_string.size()>2 && working_string[0] == '"' && working_string[working_string.size() - 1] == '"')
{ {
working_string.slice(1, working_string.size() - 2, working_string); working_string.slice(1, working_string.size() - 2, working_string);

View file

@ -1,6 +1,6 @@
#include "XmlParser.h" #include "XmlParser.h"
#include "StringUtils.h" #include "Char.h"
#include "XmlDocument.h" #include "XmlDocument.h"
#include "XmlElement.h" #include "XmlElement.h"
@ -24,22 +24,22 @@ void XmlParser::processLine(const String& input)
{ {
switch (input[idx]) switch (input[idx])
{ {
case StringUtils::LEFT_BRACKET: case Char::LEFT_BRACKET:
onLeftBracket(); onLeftBracket();
break; break;
case StringUtils::RIGHT_BRACKET: case Char::RIGHT_BRACKET:
onRightBracket(); onRightBracket();
break; break;
case StringUtils::QUESTION_MARK: case Char::QUESTION_MARK:
onQuestionMark(); onQuestionMark();
break; break;
case StringUtils::FORWARD_SLASH: case Char::FORWARD_SLASH:
onForwardSlash(); onForwardSlash();
break; break;
case StringUtils::EQUALS: case Char::EQUALS:
onEquals(); onEquals();
break; break;
case StringUtils::DOUBLE_QUOTE: case Char::DOUBLE_QUOTE:
onDoubleQuote(); onDoubleQuote();
break; break;
default: default:
@ -51,11 +51,11 @@ void XmlParser::processLine(const String& input)
void XmlParser::onChar(char c) void XmlParser::onChar(char c)
{ {
if(StringUtils::isAlphaNumeric(c)) if(Char::is_alnum(c))
{ {
onAlphaNumeric(c); onAlphaNumeric(c);
} }
else if(StringUtils::isSpace(c)) else if(Char::is_space(c))
{ {
onSpace(c); onSpace(c);
} }
@ -178,7 +178,7 @@ void XmlParser::onLeftBracket()
break; break;
} }
default: default:
onNonAlphaNumeric(StringUtils::LEFT_BRACKET); onNonAlphaNumeric(Char::LEFT_BRACKET);
break; break;
} }
} }
@ -197,7 +197,7 @@ void XmlParser::onRightBracket()
break; break;
} }
default: default:
onNonAlphaNumeric(StringUtils::RIGHT_BRACKET); onNonAlphaNumeric(Char::RIGHT_BRACKET);
break; break;
} }
} }
@ -213,7 +213,7 @@ void XmlParser::onQuestionMark()
} }
else if(mDocumentState != DS::Build_Prolog) else if(mDocumentState != DS::Build_Prolog)
{ {
onNonAlphaNumeric(StringUtils::QUESTION_MARK); onNonAlphaNumeric(Char::QUESTION_MARK);
} }
} }
@ -232,7 +232,7 @@ void XmlParser::onForwardSlash()
} }
else else
{ {
onNonAlphaNumeric(StringUtils::FORWARD_SLASH); onNonAlphaNumeric(Char::FORWARD_SLASH);
} }
} }
@ -244,7 +244,7 @@ void XmlParser::onEquals()
} }
else else
{ {
onNonAlphaNumeric(StringUtils::EQUALS); onNonAlphaNumeric(Char::EQUALS);
} }
} }

View file

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

View file

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

View file

@ -1,6 +1,6 @@
#include "BitStream.h" #include "BitStream.h"
#include "ByteUtils.h" #include "Bits.h"
BitStream::~BitStream() BitStream::~BitStream()
{ {
@ -20,7 +20,7 @@ void BitStream::write(DWord data)
{ {
for(size_t idx=0; idx<sizeof(DWord); idx++) for(size_t idx=0; idx<sizeof(DWord); idx++)
{ {
writeByte(ByteUtils::getByteN(data, idx)); writeByte(Bits::getByteN(data, idx));
} }
} }
@ -45,23 +45,23 @@ size_t BitStream::getCurrentBitOffset() const
String BitStream::logLocation() String BitStream::logLocation()
{ {
String ret; String ret;
ret << "Byte offset " << mByteOffset<< " | Bit offset " << mBitOffset; ret << _s("Byte offset ") << mByteOffset<< _s(" | Bit offset ") << mBitOffset;
ret << " | Working byte " << ByteUtils::toString(getCurrentByte()) << '\n'; ret << _s(" | Working byte ") << Bits::toString(getCurrentByte()) << _s('\n');
return ret; return ret;
} }
String BitStream::logNextNBytes(size_t n) const String BitStream::logNextNBytes(size_t n) const
{ {
Stringstream sstr; String sstr;
size_t count{0}; size_t count{0};
VecBytes bytes; VecBytes bytes;
peekNextNBytes(n, bytes); peekNextNBytes(n, bytes);
for(auto byte : bytes) for(auto byte : bytes)
{ {
sstr << mByteOffset + count << " | " << ByteUtils::toString(byte) + '\n'; sstr << mByteOffset + count << _s(" | ") << Bits::toString(byte) + _s('\n');
count++; count++;
} }
return sstr.str(); return sstr;
} }
void BitStream::writeNBits(DWord data, size_t length) void BitStream::writeNBits(DWord data, size_t length)
@ -71,7 +71,7 @@ void BitStream::writeNBits(DWord data, size_t length)
if (overshoot > 0) if (overshoot > 0)
{ {
Byte lower_bits = ByteUtils::getLowerNBits(data, num_left); Byte lower_bits = Bits::getLowerNBits(data, num_left);
mCurrentByte |= lower_bits << mBitOffset; mCurrentByte |= lower_bits << mBitOffset;
writeByte(mCurrentByte, false); writeByte(mCurrentByte, false);
@ -79,13 +79,13 @@ void BitStream::writeNBits(DWord data, size_t length)
size_t num_bytes = overshoot / 8; size_t num_bytes = overshoot / 8;
for (size_t idx=0; idx< num_bytes; idx++) for (size_t idx=0; idx< num_bytes; idx++)
{ {
mCurrentByte = ByteUtils::getMBitsAtN(static_cast<Byte>(data), overshoot, idx*8 + num_left); mCurrentByte = Bits::getMBitsAtN(static_cast<Byte>(data), overshoot, idx*8 + num_left);
writeByte(mCurrentByte, false); writeByte(mCurrentByte, false);
} }
if (const auto remainder = overshoot % 8; remainder > 0) if (const auto remainder = overshoot % 8; remainder > 0)
{ {
mCurrentByte = ByteUtils::getMBitsAtN(static_cast<Byte>(data), remainder, num_bytes*8 + num_left); mCurrentByte = Bits::getMBitsAtN(static_cast<Byte>(data), remainder, num_bytes*8 + num_left);
mBitOffset = remainder; mBitOffset = remainder;
} }
else else
@ -127,8 +127,8 @@ bool BitStream::readNextNBits(size_t n, Byte& buffer)
} }
auto num_lower = 8 - mBitOffset; auto num_lower = 8 - mBitOffset;
const auto lower_bits = ByteUtils::getHigherNBits(last_byte, num_lower); const auto lower_bits = Bits::getHigherNBits(last_byte, num_lower);
const auto higher_bits = ByteUtils::getLowerNBits(mCurrentByte, overshoot); const auto higher_bits = Bits::getLowerNBits(mCurrentByte, overshoot);
buffer = (higher_bits << num_lower) | lower_bits; buffer = (higher_bits << num_lower) | lower_bits;
@ -137,7 +137,7 @@ bool BitStream::readNextNBits(size_t n, Byte& buffer)
} }
else else
{ {
buffer = ByteUtils::getMBitsAtN(mCurrentByte, n, mBitOffset); buffer = Bits::getMBitsAtN(mCurrentByte, n, mBitOffset);
mBitOffset += n; mBitOffset += n;
return true; return true;
} }
@ -169,7 +169,7 @@ void BitStream::flushRemainingBits()
} }
} }
std::pair<Byte, size_t> BitStream::getRemainingBits() const Pair<Byte, size_t> BitStream::getRemainingBits() const
{ {
return {mEndByte, mEndBitOffset}; return {mEndByte, mEndBitOffset};
} }

View file

@ -1,8 +1,6 @@
#include "BufferBitStream.h" #include "BufferBitStream.h"
#include "ByteUtils.h" #include "Bits.h"
#include <iostream>
bool BufferBitStream::isFinished() const bool BufferBitStream::isFinished() const
{ {
@ -32,11 +30,11 @@ void BufferBitStream::peekNextNBytes(size_t n, VecBytes& ret) const
} }
} }
std::optional<uint8_t> BufferBitStream::readNextByte() Optional<uint8_t> BufferBitStream::readNextByte()
{ {
if (mByteOffset + 1 == static_cast<int>(mBuffer.size())) if (mByteOffset + 1 == static_cast<int>(mBuffer.size()))
{ {
return std::nullopt; return {};
} }
else else
{ {
@ -56,10 +54,10 @@ void BufferBitStream::writeByte(uint8_t data, bool checkOverflow)
uint8_t out_byte{0}; uint8_t out_byte{0};
if (checkOverflow && mBitOffset > 0) if (checkOverflow && mBitOffset > 0)
{ {
out_byte = ByteUtils::getLowerNBits(mCurrentByte, mBitOffset); out_byte = Bits::getLowerNBits(mCurrentByte, mBitOffset);
out_byte |= data << mBitOffset; out_byte |= data << mBitOffset;
mCurrentByte = ByteUtils::getHigherNBits(data, mBitOffset); mCurrentByte = Bits::getHigherNBits(data, mBitOffset);
} }
else else
{ {
@ -70,14 +68,14 @@ void BufferBitStream::writeByte(uint8_t data, bool checkOverflow)
{ {
mChecksumCalculator->addValue(out_byte); mChecksumCalculator->addValue(out_byte);
} }
//std::cout << "Writing byte " << ByteUtils::toString(out_byte) << " had bitoffset of " << mBitOffset << std::endl; //std::cout << "Writing byte " << Bits::toString(out_byte) << " had bitoffset of " << mBitOffset << std::endl;
mBuffer.push_back(out_byte); mBuffer.push_back(out_byte);
} }
void BufferBitStream::writeBytes(const VecBytes& data) void BufferBitStream::writeBytes(const VecBytes& data)
{ {
std::copy(data.begin(), data.end(), std::back_inserter(mBuffer)); mBuffer = data;
} }
const VecBytes& BufferBitStream::getBuffer() const const VecBytes& BufferBitStream::getBuffer() const

View file

@ -11,7 +11,7 @@ public:
void peekNextNBytes(size_t n, VecBytes& bytes) const override; void peekNextNBytes(size_t n, VecBytes& bytes) const override;
std::optional<uint8_t> readNextByte() override; Optional<uint8_t> readNextByte() override;
void reset() override; void reset() override;

View file

@ -1,6 +1,6 @@
#include "InputBitStream.h" #include "InputBitStream.h"
InputBitStream::InputBitStream(std::basic_istream<unsigned char>* stream) InputBitStream::InputBitStream(InputStream<Byte>* stream)
: BitStream(), : BitStream(),
mStream(stream) mStream(stream)
{ {
@ -16,7 +16,7 @@ void InputBitStream::peekNextNBytes(size_t, VecBytes&) const
{ {
} }
std::optional<Byte> InputBitStream::readNextByte() Optional<Byte> InputBitStream::readNextByte()
{ {
if (mStream->good()) if (mStream->good())
{ {
@ -24,7 +24,7 @@ std::optional<Byte> InputBitStream::readNextByte()
} }
else else
{ {
return std::nullopt; return {};
} }
} }

View file

@ -1,18 +1,18 @@
#pragma once #pragma once
#include "BitStream.h" #include "BitStream.h"
#include "Stream.h"
#include <istream> #include "ByteTypes.h"
class InputBitStream : public BitStream class InputBitStream : public BitStream
{ {
InputBitStream(std::basic_istream<unsigned char>* stream); InputBitStream(InputStream<Byte>* stream);
bool isFinished() const override; bool isFinished() const override;
void peekNextNBytes(size_t n, VecBytes& bytes) const override; void peekNextNBytes(size_t n, VecBytes& bytes) const override;
std::optional<Byte> readNextByte() override; Optional<Byte> readNextByte() override;
void writeByte(Byte data, bool checkOverflow = true) override; void writeByte(Byte data, bool checkOverflow = true) override;
@ -22,5 +22,5 @@ class InputBitStream : public BitStream
} }
private: private:
std::basic_istream<unsigned char>* mStream{nullptr}; InputStream<Byte>* mStream{nullptr};
}; };

View file

@ -1,6 +1,6 @@
#include "OutputBitStream.h" #include "OutputBitStream.h"
OutputBitStream::OutputBitStream(std::basic_ostream<char>* stream) OutputBitStream::OutputBitStream(OutputStream<Byte>* stream)
: BitStream(), : BitStream(),
mStream(stream) mStream(stream)
{ {
@ -17,14 +17,14 @@ void OutputBitStream::peekNextNBytes(size_t, VecBytes&) const
} }
std::optional<uint8_t> OutputBitStream::readNextByte() Optional<uint8_t> OutputBitStream::readNextByte()
{ {
return std::nullopt; return {};
} }
void OutputBitStream::writeByte(uint8_t data, bool) void OutputBitStream::writeByte(uint8_t data, bool)
{ {
(*mStream) << data; //(*mStream) << data;
} }
void OutputBitStream::writeBytes(const VecBytes& data) void OutputBitStream::writeBytes(const VecBytes& data)

View file

@ -1,24 +1,24 @@
#pragma once #pragma once
#include "BitStream.h" #include "BitStream.h"
#include "Stream.h"
#include <ostream> #include "ByteTypes.h"
class OutputBitStream : public BitStream class OutputBitStream : public BitStream
{ {
public: public:
OutputBitStream(std::basic_ostream<char>* stream); OutputBitStream(OutputStream<Byte>* stream);
bool isFinished() const override; bool isFinished() const override;
void peekNextNBytes(size_t n, VecBytes& bytes) const override; void peekNextNBytes(size_t n, VecBytes& bytes) const override;
std::optional<Byte> readNextByte() override; Optional<Byte> readNextByte() override;
void writeByte(Byte data, bool checkOverflow = true) override; void writeByte(Byte data, bool checkOverflow = true) override;
void writeBytes(const VecBytes& data) override; void writeBytes(const VecBytes& data) override;
private: private:
std::basic_ostream<char>* mStream{nullptr}; OutputStream<Byte>* mStream{nullptr};
}; };

View file

@ -0,0 +1,27 @@
#pragma once
#include "Optional.h"
#include <cstddef>
template<typename T>
class Stream
{
public:
virtual bool good() const = 0;
};
template<typename T>
class InputStream : public Stream<T>
{
public:
virtual Optional<T> get() = 0;
virtual bool get(T& item) = 0;
};
template<typename T>
class OutputStream : public Stream<T>
{
public:
virtual void write(T* data, size_t size) = 0;
};

View file

@ -1,6 +1,6 @@
#include "CommandLineArgs.h" #include "CommandLineArgs.h"
#include "UnicodeUtils.h" #include "Unicode.h"
#ifdef _WIN32 #ifdef _WIN32
#include "Win32BaseIncludes.h" #include "Win32BaseIncludes.h"
@ -26,7 +26,7 @@ void CommandLineArgs::initialize(CommandLineArgs* args)
Vector<String> windowsArgs(nArgs); Vector<String> windowsArgs(nArgs);
for (int idx = 0; idx < nArgs; idx++) for (int idx = 0; idx < nArgs; idx++)
{ {
windowsArgs[idx] = UnicodeUtils::utf16ToUtf8String(szArglist[idx]); windowsArgs[idx] = Unicode::utf16ToUtf8String(szArglist[idx]);
} }
::LocalFree(szArglist); ::LocalFree(szArglist);

View file

@ -20,7 +20,7 @@ Vector<uint8_t> ImageBitStream::peekNextNBytes(size_t) const
return {}; return {};
} }
std::optional<uint8_t> ImageBitStream::readNextByte() Optional<uint8_t> ImageBitStream::readNextByte()
{ {
mByteOffset++; mByteOffset++;
if (isFinished() ) if (isFinished() )

View file

@ -15,7 +15,7 @@ public:
Vector<uint8_t> peekNextNBytes(size_t n) const override; Vector<uint8_t> peekNextNBytes(size_t n) const override;
std::optional<uint8_t> readNextByte() override; Optional<uint8_t> readNextByte() override;
void writeByte(uint8_t data, bool checkOverflow = true) override; void writeByte(uint8_t data, bool checkOverflow = true) override;

View file

@ -13,7 +13,7 @@
#include "HuffmanEncoder.h" #include "HuffmanEncoder.h"
#include "CyclicRedundancyChecker.h" #include "CyclicRedundancyChecker.h"
#include "ByteUtils.h" #include "Bits.h"
#include <iostream> #include <iostream>

View file

@ -1,6 +1,6 @@
#include "PngHeader.h" #include "PngHeader.h"
#include "ByteUtils.h" #include "Bits.h"
#include "StringUtils.h" #include "StringUtils.h"
#include "CyclicRedundancyChecker.h" #include "CyclicRedundancyChecker.h"
@ -74,12 +74,12 @@ void PngHeader::updateData()
for(size_t idx=0; idx<num_bytes;idx++) for(size_t idx=0; idx<num_bytes;idx++)
{ {
mData.push_back(ByteUtils::getByteN(mWidth, idx)); mData.push_back(Bits::getByteN(mWidth, idx));
} }
for(unsigned idx=0; idx<num_bytes;idx++) for(unsigned idx=0; idx<num_bytes;idx++)
{ {
mData.push_back(ByteUtils::getByteN(mHeight, idx)); mData.push_back(Bits::getByteN(mHeight, idx));
} }
mData.push_back(mBitDepth); mData.push_back(mBitDepth);
mData.push_back(static_cast<Byte>(mPngInfo.mColorType)); mData.push_back(static_cast<Byte>(mPngInfo.mColorType));

View file

@ -28,7 +28,7 @@ Map<String, LatexMathSymbol::Tag> LatexSymbolLookup::mTags = {
{"ket", LatexMathSymbol::Tag::KET}, {"ket", LatexMathSymbol::Tag::KET},
}; };
std::optional<String> LatexSymbolLookup::getLeftSymbolUtf8(LatexMathSymbol::Tag tag) Optional<String> LatexSymbolLookup::getLeftSymbolUtf8(LatexMathSymbol::Tag tag)
{ {
if (auto entry = getLeftSymbolUtf16(tag); entry) if (auto entry = getLeftSymbolUtf16(tag); entry)
{ {
@ -40,7 +40,7 @@ std::optional<String> LatexSymbolLookup::getLeftSymbolUtf8(LatexMathSymbol::Tag
} }
} }
std::optional<String> LatexSymbolLookup::getRightSymbolUtf8(LatexMathSymbol::Tag tag) Optional<String> LatexSymbolLookup::getRightSymbolUtf8(LatexMathSymbol::Tag tag)
{ {
if (auto entry = getRightSymbolUtf16(tag); entry) if (auto entry = getRightSymbolUtf16(tag); entry)
{ {
@ -52,7 +52,7 @@ std::optional<String> LatexSymbolLookup::getRightSymbolUtf8(LatexMathSymbol::Tag
} }
} }
std::optional<String> LatexSymbolLookup::getSymbolUtf8(const String& tag) Optional<String> LatexSymbolLookup::getSymbolUtf8(const String& tag)
{ {
if (auto entry = getSymbolUtf16(tag); entry) if (auto entry = getSymbolUtf16(tag); entry)
{ {
@ -64,7 +64,7 @@ std::optional<String> LatexSymbolLookup::getSymbolUtf8(const String& tag)
} }
} }
std::optional<std::wstring> LatexSymbolLookup::getLeftSymbolUtf16(LatexMathSymbol::Tag tag) Optional<std::wstring> LatexSymbolLookup::getLeftSymbolUtf16(LatexMathSymbol::Tag tag)
{ {
if (auto iter = mLeftSymbols.find(tag); iter != mLeftSymbols.end()) if (auto iter = mLeftSymbols.find(tag); iter != mLeftSymbols.end())
{ {
@ -76,7 +76,7 @@ std::optional<std::wstring> LatexSymbolLookup::getLeftSymbolUtf16(LatexMathSymbo
} }
} }
std::optional<std::wstring> LatexSymbolLookup::getRightSymbolUtf16(LatexMathSymbol::Tag tag) Optional<std::wstring> LatexSymbolLookup::getRightSymbolUtf16(LatexMathSymbol::Tag tag)
{ {
if (auto iter = mRightSymbols.find(tag); iter != mRightSymbols.end()) if (auto iter = mRightSymbols.find(tag); iter != mRightSymbols.end())
{ {
@ -89,7 +89,7 @@ std::optional<std::wstring> LatexSymbolLookup::getRightSymbolUtf16(LatexMathSymb
} }
std::optional<std::wstring> LatexSymbolLookup::getSymbolUtf16(const String& tag) Optional<std::wstring> LatexSymbolLookup::getSymbolUtf16(const String& tag)
{ {
if (auto iter = mSymbols.find(tag); iter != mSymbols.end()) if (auto iter = mSymbols.find(tag); iter != mSymbols.end())
{ {
@ -101,7 +101,7 @@ std::optional<std::wstring> LatexSymbolLookup::getSymbolUtf16(const String& tag)
} }
} }
std::optional<LatexMathSymbol::Tag> LatexSymbolLookup::getKnownTag(const String& tag) Optional<LatexMathSymbol::Tag> LatexSymbolLookup::getKnownTag(const String& tag)
{ {
if (auto iter = mTags.find(tag); iter != mTags.end()) if (auto iter = mTags.find(tag); iter != mTags.end())
{ {

View file

@ -40,19 +40,19 @@ struct LatexMathSymbol
class LatexSymbolLookup class LatexSymbolLookup
{ {
public: public:
static std::optional<String> getLeftSymbolUtf8(LatexMathSymbol::Tag tag); static Optional<String> getLeftSymbolUtf8(LatexMathSymbol::Tag tag);
static std::optional<String> getRightSymbolUtf8(LatexMathSymbol::Tag tag); static Optional<String> getRightSymbolUtf8(LatexMathSymbol::Tag tag);
static std::optional<String> getSymbolUtf8(const String& tag); static Optional<String> getSymbolUtf8(const String& tag);
static std::optional<std::wstring> getLeftSymbolUtf16(LatexMathSymbol::Tag tag); static Optional<std::wstring> getLeftSymbolUtf16(LatexMathSymbol::Tag tag);
static std::optional<std::wstring> getRightSymbolUtf16(LatexMathSymbol::Tag tag); static Optional<std::wstring> getRightSymbolUtf16(LatexMathSymbol::Tag tag);
static std::optional<std::wstring> getSymbolUtf16(const String& tag); static Optional<std::wstring> getSymbolUtf16(const String& tag);
static std::optional<LatexMathSymbol::Tag> getKnownTag(const String& tag); static Optional<LatexMathSymbol::Tag> getKnownTag(const String& tag);
static size_t getNumTagBodies(LatexMathSymbol::Tag tag) static size_t getNumTagBodies(LatexMathSymbol::Tag tag)
{ {

View file

@ -3,7 +3,7 @@ include(TestTargets)
unit_tests( unit_tests(
MODULE_NAME core MODULE_NAME core
FILES FILES
TestByteUtils.cpp TestBits.cpp
TestBitStream.cpp TestBitStream.cpp
TestDataStructures.cpp TestDataStructures.cpp
TestTomlReader.cpp TestTomlReader.cpp

View file

@ -1,4 +1,4 @@
#include "ByteUtils.h" #include "Bits.h"
#include "BufferBitStream.h" #include "BufferBitStream.h"
#include "TestFramework.h" #include "TestFramework.h"
@ -16,27 +16,27 @@ TEST_CASE(TestReadBitStream, "core")
BufferBitStream stream; BufferBitStream stream;
for(const auto& byte : bytes) for(const auto& byte : bytes)
{ {
stream.writeByte(ByteUtils::getFromString(byte)); stream.writeByte(Bits::getFromString(byte));
} }
unsigned char buffer{0} ; unsigned char buffer{0} ;
auto valid = stream.readNextNBits(1, buffer); auto valid = stream.readNextNBits(1, buffer);
//std::cout << "Slice0 is " << ByteUtils::toString(buffer) << std::endl; //std::cout << "Slice0 is " << Bits::toString(buffer) << std::endl;
valid = stream.readNextNBits(2, buffer); valid = stream.readNextNBits(2, buffer);
//std::cout << "Slice1 is " << ByteUtils::toString(buffer) << std::endl; //std::cout << "Slice1 is " << Bits::toString(buffer) << std::endl;
valid = stream.readNextNBits(5, buffer); valid = stream.readNextNBits(5, buffer);
//std::cout << "Slice2 is " << ByteUtils::toString(buffer) << std::endl; //std::cout << "Slice2 is " << Bits::toString(buffer) << std::endl;
valid = stream.readNextNBits(5, buffer); valid = stream.readNextNBits(5, buffer);
//std::cout << "Slice3 is " << ByteUtils::toString(buffer) << std::endl; //std::cout << "Slice3 is " << Bits::toString(buffer) << std::endl;
valid = stream.readNextNBits(4, buffer); valid = stream.readNextNBits(4, buffer);
//std::cout << "Slice3 is " << ByteUtils::toString(buffer) << " and int " << static_cast<int>(buffer) << std::endl; //std::cout << "Slice3 is " << Bits::toString(buffer) << " and int " << static_cast<int>(buffer) << std::endl;
valid = stream.readNextNBits(3, buffer); valid = stream.readNextNBits(3, buffer);
//std::cout << "Slice3 is " << ByteUtils::toString(buffer) << std::endl; //std::cout << "Slice3 is " << Bits::toString(buffer) << std::endl;
REQUIRE(valid); REQUIRE(valid);
} }
@ -44,30 +44,30 @@ TEST_CASE(TestWritingBitStream, "core")
{ {
BufferBitStream stream; BufferBitStream stream;
stream.writeByte(ByteUtils::getFromString("01100000")); stream.writeByte(Bits::getFromString("01100000"));
auto bits0 = ByteUtils::getFromString("00000111"); auto bits0 = Bits::getFromString("00000111");
stream.writeNBits(bits0, 3); stream.writeNBits(bits0, 3);
stream.writeByte(ByteUtils::getFromString("11110000")); stream.writeByte(Bits::getFromString("11110000"));
auto bits1 = ByteUtils::getFromString("01001101"); auto bits1 = Bits::getFromString("01001101");
stream.writeNBits(bits1, 7); stream.writeNBits(bits1, 7);
stream.writeByte(ByteUtils::getFromString("11110000")); stream.writeByte(Bits::getFromString("11110000"));
auto bits2 = ByteUtils::getFromString("00000001"); auto bits2 = Bits::getFromString("00000001");
stream.writeNBits(bits2, 1); stream.writeNBits(bits2, 1);
stream.flushRemainingBits(); stream.flushRemainingBits();
stream.resetOffsets(); stream.resetOffsets();
auto byte0 = ByteUtils::toString(*stream.readNextByte()); auto byte0 = Bits::toString(*stream.readNextByte());
auto byte1 = ByteUtils::toString(*stream.readNextByte()); auto byte1 = Bits::toString(*stream.readNextByte());
auto byte2 = ByteUtils::toString(*stream.readNextByte()); auto byte2 = Bits::toString(*stream.readNextByte());
auto byte3 = ByteUtils::toString(*stream.readNextByte()); auto byte3 = Bits::toString(*stream.readNextByte());
auto byte4 = ByteUtils::toString(*stream.readNextByte()); auto byte4 = Bits::toString(*stream.readNextByte());
//std::cout << "Got bytes 0 " << byte0 << std::endl; //std::cout << "Got bytes 0 " << byte0 << std::endl;
//std::cout << "Got bytes 1 " << byte1 << std::endl; //std::cout << "Got bytes 1 " << byte1 << std::endl;

View file

@ -1,44 +1,44 @@
#include "ByteUtils.h" #include "Bits.h"
#include "TestFramework.h" #include "TestFramework.h"
#include <iostream> #include <iostream>
TEST_CASE(TestReadByteUtils, "core") TEST_CASE(TestReadBits, "core")
{ {
auto byte = ByteUtils::getFromString("00110101"); auto byte = Bits::getFromString("00110101");
//std::cout << "Value is " << static_cast<unsigned>(byte) << std::endl; //std::cout << "Value is " << static_cast<unsigned>(byte) << std::endl;
auto string_rep = ByteUtils::toString(byte); auto string_rep = Bits::toString(byte);
//std::cout << "String rep is " << string_rep << std::endl; //std::cout << "String rep is " << string_rep << std::endl;
auto slice = ByteUtils::getMBitsAtN(byte, 3, 3); auto slice = Bits::getMBitsAtN(byte, 3, 3);
//std::cout << "Slice is " << ByteUtils::toString(slice) << std::endl; //std::cout << "Slice is " << Bits::toString(slice) << std::endl;
(void) slice; (void) slice;
uint32_t input {12345678}; uint32_t input {12345678};
auto byte0 = ByteUtils::getByteN(input, 0); auto byte0 = Bits::getByteN(input, 0);
auto byte1 = ByteUtils::getByteN(input, 1); auto byte1 = Bits::getByteN(input, 1);
auto byte2 = ByteUtils::getByteN(input, 2); auto byte2 = Bits::getByteN(input, 2);
auto byte3 = ByteUtils::getByteN(input, 3); auto byte3 = Bits::getByteN(input, 3);
(void)byte0; (void)byte0;
(void)byte1; (void)byte1;
(void)byte2; (void)byte2;
(void)byte3; (void)byte3;
//std::cout << "Byte0 is " << ByteUtils::toString(byte0) << std::endl; //std::cout << "Byte0 is " << Bits::toString(byte0) << std::endl;
//std::cout << "Byte1 is " << ByteUtils::toString(byte1) << std::endl; //std::cout << "Byte1 is " << Bits::toString(byte1) << std::endl;
//std::cout << "Byte2 is " << ByteUtils::toString(byte2) << std::endl; //std::cout << "Byte2 is " << Bits::toString(byte2) << std::endl;
//std::cout << "Byte3 is " << ByteUtils::toString(byte3) << std::endl; //std::cout << "Byte3 is " << Bits::toString(byte3) << std::endl;
//std::cout << "Mirroring" << std::endl; //std::cout << "Mirroring" << std::endl;
//auto out = ByteUtils::mirror(byte); //auto out = Bits::mirror(byte);
//std::cout << "Mirror is " << ByteUtils::toString(out) << std::endl; //std::cout << "Mirror is " << Bits::toString(out) << std::endl;
unsigned hold = byte; unsigned hold = byte;
hold = (hold << 5) + 3; hold = (hold << 5) + 3;
(void)hold; (void)hold;
//std::cout << "Big val is " << ByteUtils::toString(hold, 16) << std::endl; //std::cout << "Big val is " << Bits::toString(hold, 16) << std::endl;
} }

View file

@ -3,7 +3,7 @@
#include "File.h" #include "File.h"
#include "BitStream.h" #include "BitStream.h"
#include "ByteUtils.h" #include "Bits.h"
#include "Grid.h" #include "Grid.h"
#include "ImagePrimitives.h" #include "ImagePrimitives.h"