Small cleaning.

This commit is contained in:
James Grogan 2022-12-04 18:13:32 +00:00
parent 70220fc6e9
commit d7fe11913f
26 changed files with 613 additions and 548 deletions

View file

@ -18,9 +18,6 @@ public:
void openDevice(AudioDevice* device) override;
void play(AudioDevice* device, AudioSample* sample, unsigned duration) override;
private:
void openDevice(const AudioDevicePtr& device) override;
};
using NullAudioInterfacePtr = std::shared_ptr<NullAudioInterface>;

View file

@ -2,6 +2,7 @@
#include "FileLogger.h"
#include "AudioSynth.h"
#include "AudioDevice.h"
#include <vector>
#include <iostream>
@ -26,37 +27,37 @@ std::unique_ptr<WasapiInterface> WasapiInterface::Create()
return std::make_unique<WasapiInterface>();
}
void WasapiInterface::OpenDevice(const AudioDevicePtr& device)
void WasapiInterface::openDevice(AudioDevice* device)
{
}
void WasapiInterface::SetAccessType(const AudioDevicePtr& device)
void WasapiInterface::setAccessType(AudioDevice* device)
{
}
void WasapiInterface::SetSampleFormat(const AudioDevicePtr& device)
void WasapiInterface::setSampleFormat(AudioDevice* device)
{
}
void WasapiInterface::SetSampleRate(const AudioDevicePtr& device)
void WasapiInterface::setSampleRate(AudioDevice* device)
{
}
void WasapiInterface::SetPeriod(const AudioDevicePtr& device)
void WasapiInterface::setPeriod(AudioDevice* device)
{
}
void WasapiInterface::SetBufferSize(const AudioDevicePtr& device)
void WasapiInterface::setBufferSize(AudioDevice* device)
{
}
void WasapiInterface::SetChannelNumber(const AudioDevicePtr& device)
void WasapiInterface::setChannelNumber(AudioDevice* device)
{
}
@ -76,7 +77,7 @@ const IID IID_IMMDeviceEnumerator = __uuidof(IMMDeviceEnumerator);
const IID IID_IAudioClient = __uuidof(IAudioClient);
const IID IID_IAudioRenderClient = __uuidof(IAudioRenderClient);
void WasapiInterface::Play(const AudioDevicePtr& device)
void WasapiInterface::play(AudioDevice* device, AudioSample* sample, unsigned duration)
{
std::cout << "Into wasapi play" << std::endl;
IMMDeviceEnumerator* pEnumerator = nullptr;

View file

@ -1,36 +1,35 @@
#pragma once
#include "IAudioInterface.h"
#include "AudioDevice.h"
#include <memory>
class AudioDevice;
class WasapiInterface : public IAudioInterface
{
public:
WasapiInterface();
~WasapiInterface();
static std::unique_ptr<WasapiInterface> Create();
void OpenDevice(const AudioDevicePtr& device) override;
void play(AudioDevice* device, AudioSample* sample, unsigned duration) override;
private:
void openDevice(AudioDevice* device) override;
void SetAccessType(const AudioDevicePtr& device);
void setAccessType(AudioDevice* device);
void SetSampleFormat(const AudioDevicePtr& device);
void setSampleFormat(AudioDevice* device);
void SetSampleRate(const AudioDevicePtr& device);
void setSampleRate(AudioDevice* device);
void SetPeriod(const AudioDevicePtr& device);
void setPeriod(AudioDevice* device);
void SetBufferSize(const AudioDevicePtr& device);
void setBufferSize(AudioDevice* device);
void SetChannelNumber(const AudioDevicePtr& device);
void Play(const AudioDevicePtr& device) override;
void setChannelNumber(AudioDevice* device);
};
using AlsaInterfacePtr = std::shared_ptr<WasapiInterface>;
using WasapiInterfacePtr = std::unique_ptr<WasapiInterface>;

View file

@ -6,7 +6,7 @@
#include <iostream>
#include <bitset>
int MidiChannelEventAdapter::ReadEvent(std::ifstream* file, char firstByte, MidiChannelEvent* event, MidiChannelEvent::Type& lastEventType)
int MidiChannelEventAdapter::readEvent(std::ifstream* file, char firstByte, MidiChannelEvent* event, MidiChannelEvent::Type& lastEventType)
{
int first_four_bits = 0xF0;
int second_four_bits = 0xF;
@ -34,11 +34,11 @@ int MidiChannelEventAdapter::ReadEvent(std::ifstream* file, char firstByte, Midi
{
if (isStatusByte)
{
byteCount += ReadEventData(file, event);
byteCount += readEventData(file, event);
}
else
{
byteCount += ReadEventData(file, event, firstByte);
byteCount += readEventData(file, event, firstByte);
}
break;
}
@ -55,7 +55,7 @@ int MidiChannelEventAdapter::ReadEvent(std::ifstream* file, char firstByte, Midi
return byteCount;
}
int MidiChannelEventAdapter::ReadEventData(std::ifstream* file, MidiChannelEvent* event, char c)
int MidiChannelEventAdapter::readEventData(std::ifstream* file, MidiChannelEvent* event, char c)
{
int value0 = int(c);
int value1 = *BinaryStream::getNextByteAsInt(file);
@ -63,7 +63,7 @@ int MidiChannelEventAdapter::ReadEventData(std::ifstream* file, MidiChannelEvent
return 1;
}
int MidiChannelEventAdapter::ReadEventData(std::ifstream* file, MidiChannelEvent* event)
int MidiChannelEventAdapter::readEventData(std::ifstream* file, MidiChannelEvent* event)
{
int value0 = *BinaryStream::getNextByteAsInt(file);
int value1 = *BinaryStream::getNextByteAsInt(file);

View file

@ -7,8 +7,8 @@
class MidiChannelEventAdapter
{
public:
static int ReadEvent(std::ifstream* file, char firstByte, MidiChannelEvent* event, MidiChannelEvent::Type& lastEventType);
static int readEvent(std::ifstream* file, char firstByte, MidiChannelEvent* event, MidiChannelEvent::Type& lastEventType);
static int ReadEventData(std::ifstream* file, MidiChannelEvent* event, char c);
static int ReadEventData(std::ifstream* file, MidiChannelEvent* event);
static int readEventData(std::ifstream* file, MidiChannelEvent* event, char c);
static int readEventData(std::ifstream* file, MidiChannelEvent* event);
};

View file

@ -3,11 +3,9 @@
#include "MetaMidiEvent.h"
#include <fstream>
class MidiMetaEventAdapter
{
public:
static int ReadEvent(std::ifstream* file, MetaMidiEvent* event, int& lastMidiChannel);
static int ReadIntEvent(std::ifstream* file, MetaMidiEvent* event, int length=-1);

View file

@ -87,7 +87,7 @@ int MidiReader::processEvent(MidiTrack* track)
auto event = std::make_unique<MidiChannelEvent>();
event->SetTimeDelta(timeDelta);
//std::cout << "Midi event" << std::endl;
byteCount += MidiChannelEventAdapter::ReadEvent(mFile->getInHandle(), c, event.get(), mLastChannelEventType);
byteCount += MidiChannelEventAdapter::readEvent(mFile->getInHandle(), c, event.get(), mLastChannelEventType);
track->AddEvent(std::move(event));
}
return byteCount;

View file

@ -29,6 +29,11 @@ AbstractApp* GuiApplication::getMainApplication() const
return mMainApplication.get();
}
AbstractUIInterface* GuiApplication::getUiInterface() const
{
return mUiInterface.get();
}
void GuiApplication::initializeViews()
{
mDesktopManager->getWindowManager()->getMainWindow()->setSize(800, 600);

View file

@ -21,10 +21,7 @@ public:
void run();
AbstractUIInterface* getUiInterface() const override
{
return mUiInterface.get();
}
AbstractUIInterface* getUiInterface() const override;
void setUiInterfaceBackend(UiInterfaceFactory::Backend backend);

View file

@ -5,7 +5,6 @@
class StatusBar : public Widget
{
public:
StatusBar();
static std::unique_ptr<StatusBar> Create();

View file

@ -6,7 +6,6 @@
class TabbedPanelWidget : public Widget
{
public:
TabbedPanelWidget();

View file

@ -1,187 +1,189 @@
#include "TemplateFile.h"
#include "StringUtils.h"
#include <iostream>
TemplateFile::TemplateFile(const Path& path)
: mPath(path),
mRootNode(std::make_unique<TemplateNode>(nullptr))
: mPath(path),
mRootNode(std::make_unique<TemplateNode>(nullptr))
{
}
std::vector<std::string> TemplateFile::getProcessedContent() const
{
return mProcessedContent;
return mProcessedContent;
}
std::string TemplateFile::getName() const
{
return mPath.stem().string();
return mPath.stem().string();
}
void TemplateFile::loadContent()
{
//std::cout << "Trying to load file at: " << mPath << std::endl;
mRawContent = File(mPath).readLines();
mWorkingLine = 0;
mWorkingNode = mRootNode.get();
mWorkingTextBody = std::make_unique<TemplateTextBody>(mWorkingNode);
for (const auto& line : mRawContent)
{
processLine(line);
mWorkingLine++;
}
onTextBodyFinished();
//std::cout << "Trying to load file at: " << mPath << std::endl;
mRawContent = File(mPath).readLines();
mWorkingLine = 0;
mWorkingNode = mRootNode.get();
mWorkingTextBody = std::make_unique<TemplateTextBody>(mWorkingNode);
for (const auto& line : mRawContent)
{
processLine(line);
mWorkingLine++;
}
onTextBodyFinished();
}
void TemplateFile::onTextBodyFinished(std::string working_string)
{
if (!working_string.empty())
{
mWorkingTextBody->addLine(working_string);
}
if (!working_string.empty())
{
mWorkingTextBody->addLine(working_string);
}
if (mWorkingTextBody->hasContent())
{
mWorkingNode->addChild(std::move(mWorkingTextBody));
mWorkingTextBody = std::make_unique<TemplateTextBody>(mWorkingNode);
}
if (mWorkingTextBody->hasContent())
{
mWorkingNode->addChild(std::move(mWorkingTextBody));
mWorkingTextBody = std::make_unique<TemplateTextBody>(mWorkingNode);
}
}
void TemplateFile::processLine(const std::string& line)
{
bool last_was_ldelimiter{ false };
bool last_was_statement_rdelimiter{ false };
bool last_was_expression_rdelimiter{ false };
bool in_statement{ false };
bool in_expression{ false };
std::string working_string;
std::string last_working_string;
for (auto c : line)
{
if (c == '{')
{
if (last_was_ldelimiter)
{
in_expression = true;
last_was_ldelimiter = false;
working_string = "";
}
else
{
last_was_ldelimiter = true;
}
}
else if (c == '%' && last_was_ldelimiter)
{
last_was_ldelimiter = false;
in_statement = true;
last_working_string = working_string;
working_string = "";
}
else if (c == '%' && in_statement)
{
last_was_statement_rdelimiter = true;
}
else if (c == '}' && (last_was_statement_rdelimiter || in_expression || last_was_expression_rdelimiter))
{
if (last_was_statement_rdelimiter)
{
onTextBodyFinished(last_working_string);
onFoundStatement(working_string);
last_was_statement_rdelimiter = false;
working_string = "";
}
else if (in_expression)
{
last_was_expression_rdelimiter = true;
in_expression = false;
}
else
{
onTextBodyFinished();
onFoundExpression(working_string);
last_was_expression_rdelimiter = false;
working_string = "";
}
}
else if (last_was_ldelimiter && (!in_statement && !in_expression))
{
last_was_ldelimiter = false;
working_string += '{';
working_string += c;
}
else
{
working_string += c;
}
}
bool last_was_ldelimiter{ false };
bool last_was_statement_rdelimiter{ false };
bool last_was_expression_rdelimiter{ false };
bool in_statement{ false };
bool in_expression{ false };
std::string working_string;
std::string last_working_string;
for (auto c : line)
{
if (c == '{')
{
if (last_was_ldelimiter)
{
in_expression = true;
last_was_ldelimiter = false;
working_string = "";
}
else
{
last_was_ldelimiter = true;
}
}
else if (c == '%' && last_was_ldelimiter)
{
last_was_ldelimiter = false;
in_statement = true;
last_working_string = working_string;
working_string = "";
}
else if (c == '%' && in_statement)
{
last_was_statement_rdelimiter = true;
}
else if (c == '}' && (last_was_statement_rdelimiter || in_expression || last_was_expression_rdelimiter))
{
if (last_was_statement_rdelimiter)
{
onTextBodyFinished(last_working_string);
onFoundStatement(working_string);
last_was_statement_rdelimiter = false;
working_string = "";
}
else if (in_expression)
{
last_was_expression_rdelimiter = true;
in_expression = false;
}
else
{
onTextBodyFinished();
onFoundExpression(working_string);
last_was_expression_rdelimiter = false;
working_string = "";
}
}
else if (last_was_ldelimiter && (!in_statement && !in_expression))
{
last_was_ldelimiter = false;
working_string += '{';
working_string += c;
}
else
{
working_string += c;
}
}
if (!working_string.empty())
{
mWorkingTextBody->addLine(working_string);
}
if (!working_string.empty())
{
mWorkingTextBody->addLine(working_string);
}
}
void TemplateFile::onFoundStatement(const std::string& statement_string)
{
const auto statement_elements = StringUtils::split(statement_string);
const auto statement_elements = StringUtils::split(statement_string);
if (statement_elements.size() == 0)
{
return;
}
const auto statement_type = statement_elements[0];
if (statement_type == "block" && statement_elements.size() == 2)
{
onFoundBlock(statement_elements);
}
else if (statement_type == "endblock")
{
onFoundEndBlock(statement_elements);
}
else if (statement_type == "extends")
{
onFoundExtends(statement_elements);
}
if (statement_elements.size() == 0)
{
return;
}
const auto statement_type = statement_elements[0];
if (statement_type == "block" && statement_elements.size() == 2)
{
onFoundBlock(statement_elements);
}
else if (statement_type == "endblock")
{
onFoundEndBlock(statement_elements);
}
else if (statement_type == "extends")
{
onFoundExtends(statement_elements);
}
}
void TemplateFile::onFoundBlock(const std::vector<std::string> args)
{
if (args.size() != 2)
{
return;
}
auto block = std::make_unique<TemplateBlock>(mWorkingNode, args[1]);
auto temp = block.get();
mWorkingNode->addChild(std::move(block));
mWorkingNode = temp;
if (args.size() != 2)
{
return;
}
auto block = std::make_unique<TemplateBlock>(mWorkingNode, args[1]);
auto temp = block.get();
mWorkingNode->addChild(std::move(block));
mWorkingNode = temp;
}
void TemplateFile::onFoundEndBlock(const std::vector<std::string> args)
{
mWorkingNode = mWorkingNode->getParent();
mWorkingNode = mWorkingNode->getParent();
}
void TemplateFile::onFoundExtends(const std::vector<std::string> args)
{
if (args.size() != 2)
{
return;
}
auto extends = std::make_unique<TemplateExtends>(mWorkingNode, args[1]);
mWorkingNode->addChild(std::move(extends));
if (args.size() != 2)
{
return;
}
auto extends = std::make_unique<TemplateExtends>(mWorkingNode, args[1]);
mWorkingNode->addChild(std::move(extends));
}
void TemplateFile::onFoundExpression(const std::string& expression_string)
{
auto expression = std::make_unique<TemplateExpression>(mWorkingNode, expression_string);
mWorkingNode->addChild(std::move(expression));
auto expression = std::make_unique<TemplateExpression>(mWorkingNode, expression_string);
mWorkingNode->addChild(std::move(expression));
}
void TemplateFile::dumpContent()
{
auto content = mRootNode->getRawContent();
//std::cout << content << std::endl;
auto content = mRootNode->getRawContent();
//std::cout << content << std::endl;
}

View file

@ -9,42 +9,42 @@
class TemplateFile
{
public:
TemplateFile(const Path& path);
TemplateFile(const Path& path);
std::vector<std::string> getProcessedContent() const;
std::vector<std::string> getProcessedContent() const;
std::string getName() const;
std::string getName() const;
void loadContent();
void loadContent();
void onTextBodyFinished(std::string working_string = {});
void onTextBodyFinished(std::string working_string = {});
void processLine(const std::string& line);
void processLine(const std::string& line);
void onFoundStatement(const std::string& statement_string);
void onFoundStatement(const std::string& statement_string);
void onFoundBlock(const std::vector<std::string> args);
void onFoundBlock(const std::vector<std::string> args);
void onFoundEndBlock(const std::vector<std::string> args);
void onFoundEndBlock(const std::vector<std::string> args);
void onFoundExtends(const std::vector<std::string> args);
void onFoundExtends(const std::vector<std::string> args);
void onFoundExpression(const std::string& expression_string);
void onFoundExpression(const std::string& expression_string);
void dumpContent();
void dumpContent();
TemplateNode* getContent() const
{
return mRootNode.get();
}
TemplateNode* getContent() const
{
return mRootNode.get();
}
private:
Path mPath;
unsigned mWorkingLine{ 0 };
std::string mParentName;
std::vector<std::string> mRawContent;
std::vector<std::string> mProcessedContent;
std::unique_ptr<TemplateNode> mRootNode;
TemplateNode* mWorkingNode{ nullptr };
std::unique_ptr<TemplateTextBody> mWorkingTextBody;
};
Path mPath;
unsigned mWorkingLine{ 0 };
std::string mParentName;
std::vector<std::string> mRawContent;
std::vector<std::string> mProcessedContent;
std::unique_ptr<TemplateNode> mRootNode;
TemplateNode* mWorkingNode{ nullptr };
std::unique_ptr<TemplateTextBody> mWorkingTextBody;
};

View file

@ -0,0 +1,169 @@
#include "TemplateNodes.h"
#include "StringUtils.h"
TemplateNode::TemplateNode(TemplateNode* parent)
: mParent(parent)
{
}
TemplateNode* TemplateNode::getParent() const
{
return mParent;
}
void TemplateNode::addChild(std::unique_ptr<TemplateNode> child)
{
mChildren.push_back(std::move(child));
}
std::size_t TemplateNode::getNumChildren() const
{
return mChildren.size();
}
std::string TemplateNode::getIdentifier() const
{
return {};
}
TemplateNode* TemplateNode::getChild(std::size_t index) const
{
return mChildren[index].get();
}
std::string TemplateNode::getRawContent() const
{
std::string content;
for (const auto& child : mChildren)
{
content += child->getRawContent() + "\n";
}
return content;
}
void TemplateNode::setExtensionParent(TemplateNode* parent)
{
mExtensionParent = parent;
}
std::string TemplateNode::render(TemplateNode* parentContext)
{
if (!parentContext && mExtensionParent)
{
parentContext = mExtensionParent;
}
std::string content;
for (size_t idx = 0; idx < mChildren.size(); idx++)
{
content += mChildren[idx]->render(parentContext);
}
return content;
}
TemplateExtends::TemplateExtends(TemplateNode* parent, const std::string& path)
: TemplateNode(parent)
{
mPath = StringUtils::stripQuotes(path);
};
std::string TemplateExtends::getRawContent() const
{
return "TemplateExtends: " + mPath;
}
std::string TemplateExtends::getPath() const
{
return mPath;
}
TemplateBlock::TemplateBlock(TemplateNode* parent, const std::string& name)
: TemplateNode(parent),
mName(name)
{
}
void TemplateBlock::addLine(const std::string& line)
{
mBody.push_back(line);
}
std::string TemplateBlock::getRawContent() const
{
std::string content = "TemplateBlock: " + mName + "\n";
content += TemplateNode::getRawContent();
return content;
}
std::string TemplateBlock::renderAsParent(TemplateNode* base)
{
return {};
}
std::string TemplateBlock::render(TemplateNode* parentContext)
{
std::string content;
if (parentContext)
{
if (auto parent_node = parentContext->getFirstChildShallow<TemplateBlock>(getIdentifier()))
{
content = dynamic_cast<TemplateBlock*>(parent_node)->renderAsParent(this);
}
}
else
{
}
return content;
}
TemplateExpression::TemplateExpression(TemplateNode* parent, const std::string& content)
: TemplateNode(parent),
mContent(content)
{
}
std::string TemplateExpression::getRawContent() const
{
return "TemplateExpression: " + mContent;
}
TemplateTextBody::TemplateTextBody(TemplateNode* parent)
: TemplateNode(parent)
{
}
void TemplateTextBody::addLine(const std::string& content)
{
mContent.push_back(content);
}
bool TemplateTextBody::hasContent() const
{
return !mContent.empty();
}
std::string TemplateTextBody::render(TemplateNode* parentContext)
{
std::string content;
for (const auto& line : mContent)
{
content += line + '\n';
}
return content;
}
std::string TemplateTextBody::getRawContent() const
{
std::string content;
for (const auto& line : mContent)
{
content += "TemplateBody: " + line + "\n";
}
return content;
}

View file

@ -1,97 +1,59 @@
#pragma once
#include <memory>
#include <string>
#include <vector>
class TemplateNode
{
public:
TemplateNode(TemplateNode* parent);
TemplateNode(TemplateNode* parent)
: mParent(parent)
{
virtual ~TemplateNode() = default;
}
TemplateNode* getParent() const;
TemplateNode* getParent() const
{
return mParent;
}
virtual void addChild(std::unique_ptr<TemplateNode> child);
virtual void addChild(std::unique_ptr<TemplateNode> child)
{
mChildren.push_back(std::move(child));
}
std::size_t getNumChildren() const;
std::size_t getNumChildren() const
{
return mChildren.size();
}
virtual std::string getIdentifier() const;
virtual std::string getIdentifier() const
{
return {};
}
TemplateNode* getChild(std::size_t index) const;
TemplateNode* getChild(std::size_t index) const
{
return mChildren[index].get();
}
virtual std::string getRawContent() const;
virtual std::string getRawContent() const
{
std::string content;
for (const auto& child : mChildren)
{
content += child->getRawContent() + "\n";
}
return content;
}
template<typename T>
T* getFirstChildShallow(const std::string& identifier = {}) const
{
for (const auto& child : mChildren)
{
if (auto ret = dynamic_cast<T*>(child.get()))
{
if (!identifier.empty())
{
if (child->getIdentifier() == identifier)
{
return ret;
}
}
else
{
return ret;
}
}
}
return nullptr;
}
template<typename T>
T* getFirstChildShallow(const std::string& identifier = {}) const
{
for (const auto& child : mChildren)
{
if (auto ret = dynamic_cast<T*>(child.get()))
{
if (!identifier.empty())
{
if (child->getIdentifier() == identifier)
{
return ret;
}
}
else
{
return ret;
}
}
}
return nullptr;
}
void setExtensionParent(TemplateNode* parent);
void setExtensionParent(TemplateNode* parent)
{
mExtensionParent = parent;
}
virtual std::string render(TemplateNode* parentContext = nullptr)
{
if (!parentContext && mExtensionParent)
{
parentContext = mExtensionParent;
}
std::string content;
for (size_t idx = 0; idx < mChildren.size(); idx++)
{
content += mChildren[idx]->render(parentContext);
}
return content;
}
virtual std::string render(TemplateNode* parentContext = nullptr);
protected:
std::vector<std::unique_ptr<TemplateNode> > mChildren;
TemplateNode* mParent{ nullptr };
TemplateNode* mExtensionParent{ nullptr };
std::vector<std::unique_ptr<TemplateNode> > mChildren;
TemplateNode* mParent{ nullptr };
TemplateNode* mExtensionParent{ nullptr };
};
using TemplateNodePtr = std::unique_ptr<TemplateNode>;
@ -99,133 +61,64 @@ using TemplateNodePtr = std::unique_ptr<TemplateNode>;
class TemplateExtends : public TemplateNode
{
public:
TemplateExtends(TemplateNode* parent, const std::string& path)
: TemplateNode(parent)
{
mPath = StringUtils::stripQuotes(path);
};
TemplateExtends(TemplateNode* parent, const std::string& path);
std::string getRawContent() const override
{
return "TemplateExtends: " + mPath;
}
virtual ~TemplateExtends() = default;
std::string getPath() const
{
return mPath;
}
std::string getRawContent() const override;
std::string getPath() const;
private:
std::string mPath;
std::string mPath;
};
class TemplateBlock : public TemplateNode
{
public:
TemplateBlock(TemplateNode* parent, const std::string& name)
: TemplateNode(parent),
mName(name)
{
TemplateBlock(TemplateNode* parent, const std::string& name);
}
virtual ~TemplateBlock() = default;
void addLine(const std::string& line)
{
mBody.push_back(line);
}
void addLine(const std::string& line);
std::string getRawContent() const override
{
std::string content = "TemplateBlock: " + mName + "\n";
content += TemplateNode::getRawContent();
return content;
}
std::string getRawContent() const override;
std::string renderAsParent(TemplateNode* base)
{
return {};
}
std::string renderAsParent(TemplateNode* base);
std::string render(TemplateNode* parentContext) override
{
std::string content;
if (parentContext)
{
if (auto parent_node = parentContext->getFirstChildShallow<TemplateBlock>(getIdentifier()))
{
content = dynamic_cast<TemplateBlock*>(parent_node)->renderAsParent(this);
}
}
else
{
}
return content;
}
std::string render(TemplateNode* parentContext) override;
private:
std::string mName;
std::vector<std::string> mBody;
std::string mName;
std::vector<std::string> mBody;
};
class TemplateExpression : public TemplateNode
{
public:
TemplateExpression(TemplateNode* parent, const std::string& content)
: TemplateNode(parent),
mContent(content)
{
TemplateExpression(TemplateNode* parent, const std::string& content);
}
virtual ~TemplateExpression() = default;
std::string getRawContent() const override
{
return "TemplateExpression: " + mContent;
}
std::string getRawContent() const override;
private:
std::string mContent;
std::string mContent;
};
class TemplateTextBody : public TemplateNode
{
public:
TemplateTextBody(TemplateNode* parent)
: TemplateNode(parent)
{
TemplateTextBody(TemplateNode* parent);
}
virtual ~TemplateTextBody() = default;
void addLine(const std::string& content)
{
mContent.push_back(content);
}
void addLine(const std::string& content);
bool hasContent() const
{
return !mContent.empty();
}
std::string getRawContent() const override;
std::string render(TemplateNode* parentContext) override
{
std::string content;
for (const auto& line : mContent)
{
content += line + '\n';
}
return content;
}
std::string getRawContent() const override
{
std::string content;
for (const auto& line : mContent)
{
content += "TemplateBody: " + line + "\n";
}
return content;
}
bool hasContent() const;
std::string render(TemplateNode* parentContext) override;
private:
std::vector<std::string> mContent;
};
std::vector<std::string> mContent;
};

View file

@ -9,20 +9,20 @@
class TemplatingEngine
{
public:
TemplatingEngine(const Path& workingDirectory);
TemplatingEngine(const Path& workingDirectory);
void loadTemplateFiles();
void loadTemplateFiles();
std::string processTemplate(const std::string& name);
std::string processTemplate(const std::string& name);
private:
TemplateFile* getTemplateFile(const std::string& name);
TemplateFile* getTemplateFile(const Path& path);
std::string render(TemplateNode* content);
TemplateFile* getTemplateFile(const std::string& name);
TemplateFile* getTemplateFile(const Path& path);
std::string render(TemplateNode* content);
std::string processTemplate(TemplateFile* file, TemplateNode* parent = nullptr);
std::string processTemplate(TemplateFile* file, TemplateNode* parent = nullptr);
std::vector<std::unique_ptr<TemplateFile> > mTemplateFiles;
Path mWorkingDirectory;
std::string mTemplateExtension{ ".html" };
};
std::vector<std::unique_ptr<TemplateFile> > mTemplateFiles;
Path mWorkingDirectory;
std::string mTemplateExtension{ ".html" };
};

View file

@ -131,7 +131,7 @@ void File::close()
FileFormat::Format File::inferFormat() const
{
const auto extension = getExtension();
return FileFormat::InferFormat(extension);
return FileFormat::inferFormat(extension);
}
void File::writeText(const std::string& text)

View file

@ -1,5 +1,7 @@
#include "FileFormats.h"
#include "StringUtils.h"
FileFormat::ExtensionMap FileFormat::mExtensions = []
{
ExtensionMap ret;
@ -8,3 +10,25 @@ FileFormat::ExtensionMap FileFormat::mExtensions = []
ret[Format::Wav] = ".wav";
return ret;
}();
bool FileFormat::isFormat(const std::string& extension, Format format)
{
return StringUtils::ToLower(extension) == mExtensions[format];
}
FileFormat::Format FileFormat::inferFormat(const std::string& query)
{
for(const auto& extension : mExtensions)
{
if(extension.second == query)
{
return extension.first;
}
}
return Format::Unknown;
}
std::string FileFormat::getExtension(Format format)
{
return mExtensions[format];
}

View file

@ -2,7 +2,6 @@
#include <map>
#include <string>
#include "StringUtils.h"
class FileFormat{
@ -27,31 +26,14 @@ public:
using ExtensionMap = std::map<Format, std::string>;
public:
static bool isFormat(const std::string& extension, Format format);
static Format inferFormat(const std::string& query);
static std::string getExtension(Format format);
private:
static ExtensionMap mExtensions;
public:
bool IsFormat(const std::string& extension, Format format)
{
return StringUtils::ToLower(extension) == mExtensions[format];
}
static Format InferFormat(const std::string& query)
{
for(const auto& extension : mExtensions)
{
if(extension.second == query)
{
return extension.first;
}
}
return Format::Unknown;
}
std::string GetExtension(Format format)
{
return mExtensions[format];
}
};

View file

@ -4,77 +4,77 @@
void HttpHeader::parse(const std::vector<std::string >& message)
{
std::string tag;
std::string value;
bool foundDelimiter{false};
for (const auto& line : message)
{
for(std::size_t idx = 0; idx< line.size(); idx++)
{
const auto c = line[idx];
if (c == StringUtils::COLON)
{
foundDelimiter = true;
}
else if(foundDelimiter)
{
value.push_back(c);
}
else
{
tag.push_back(c);
}
}
}
std::string tag;
std::string value;
bool foundDelimiter{false};
for (const auto& line : message)
{
for(std::size_t idx = 0; idx< line.size(); idx++)
{
const auto c = line[idx];
if (c == StringUtils::COLON)
{
foundDelimiter = true;
}
else if(foundDelimiter)
{
value.push_back(c);
}
else
{
tag.push_back(c);
}
}
}
if (tag.empty() || value.empty())
{
return;
}
if (tag.empty() || value.empty())
{
return;
}
if (tag == "Host")
{
mHost = value;
}
else if (tag == "User-Agent")
{
mUserAgent = value;
}
else if (tag == "Accept")
{
mAccept = value;
}
else if (tag == "Accept-Language")
{
mAcceptLanguage = value;
}
else if (tag == "Accept-Encoding")
{
mAcceptEncoding = value;
}
else if (tag == "Connection")
{
mConnection = value;
}
else if (tag == "Referer")
{
mReferer = value;
}
else if (tag == "Sec-Fetch-Dest")
{
mSecFetchDest = value;
}
else if (tag == "Sec-Fetch-Mode")
{
mSecFetchMode = value;
}
else if (tag == "Sec-Fetch-Site")
{
mSecFetchSite = value;
}
else
{
mOtherFields[tag] = value;
}
if (tag == "Host")
{
mHost = value;
}
else if (tag == "User-Agent")
{
mUserAgent = value;
}
else if (tag == "Accept")
{
mAccept = value;
}
else if (tag == "Accept-Language")
{
mAcceptLanguage = value;
}
else if (tag == "Accept-Encoding")
{
mAcceptEncoding = value;
}
else if (tag == "Connection")
{
mConnection = value;
}
else if (tag == "Referer")
{
mReferer = value;
}
else if (tag == "Sec-Fetch-Dest")
{
mSecFetchDest = value;
}
else if (tag == "Sec-Fetch-Mode")
{
mSecFetchMode = value;
}
else if (tag == "Sec-Fetch-Site")
{
mSecFetchSite = value;
}
else
{
mOtherFields[tag] = value;
}
}

View file

@ -7,20 +7,20 @@
class HttpHeader
{
public:
void parse(const std::vector<std::string >& message);
void parse(const std::vector<std::string >& message);
private:
std::string mHost;
std::string mUserAgent;
std::string mAccept;
std::string mAcceptLanguage;
std::string mAcceptEncoding;
std::string mConnection;
std::string mReferer;
std::string mSecFetchDest;
std::string mSecFetchMode;
std::string mSecFetchSite;
std::string mHost;
std::string mUserAgent;
std::string mAccept;
std::string mAcceptLanguage;
std::string mAcceptEncoding;
std::string mConnection;
std::string mReferer;
std::string mSecFetchDest;
std::string mSecFetchMode;
std::string mSecFetchSite;
std::map<std::string, std::string> mOtherFields;
std::map<std::string, std::string> mOtherFields;
};

View file

@ -6,63 +6,63 @@
void HttpRequest::parseMessage(const std::string& message)
{
std::stringstream ss(message);
std::stringstream ss(message);
std::string buffer;
bool firstLine {true};
std::string buffer;
bool firstLine {true};
std::vector<std::string> headers;
while(std::getline(ss, buffer, '\n'))
{
if (firstLine)
{
parseFirstLine(buffer);
firstLine = false;
}
else
{
headers.push_back(buffer);
}
}
mHeader.parse(headers);
std::vector<std::string> headers;
while(std::getline(ss, buffer, '\n'))
{
if (firstLine)
{
parseFirstLine(buffer);
firstLine = false;
}
else
{
headers.push_back(buffer);
}
}
mHeader.parse(headers);
}
void HttpRequest::parseFirstLine(const std::string& line)
{
bool inPath{false};
bool inMethod{true};
bool inProtocol{false};
bool inPath{false};
bool inMethod{true};
bool inProtocol{false};
for (std::size_t idx=0; idx<line.size();idx++)
{
const auto c = line[idx];
if (inPath)
{
if (StringUtils::IsSpace(c))
{
inPath = false;
inMethod = true;
}
else
{
mMethod.push_back(c);
}
}
else if (inMethod)
{
if (StringUtils::IsSpace(c))
{
inMethod = false;
inProtocol = true;
}
else
{
mPath.push_back(c);
}
}
else if (inProtocol)
{
mProtocolVersion.push_back(c);
}
}
for (std::size_t idx=0; idx<line.size();idx++)
{
const auto c = line[idx];
if (inPath)
{
if (StringUtils::IsSpace(c))
{
inPath = false;
inMethod = true;
}
else
{
mMethod.push_back(c);
}
}
else if (inMethod)
{
if (StringUtils::IsSpace(c))
{
inMethod = false;
inProtocol = true;
}
else
{
mPath.push_back(c);
}
}
else if (inProtocol)
{
mProtocolVersion.push_back(c);
}
}
}

View file

@ -7,16 +7,16 @@
class HttpRequest
{
public:
HttpRequest() = default;
HttpRequest() = default;
void parseMessage(const std::string& message);
void parseMessage(const std::string& message);
private:
void parseFirstLine(const std::string& line);
void parseFirstLine(const std::string& line);
HttpHeader mHeader;
std::string mMethod;
std::string mPath;
std::string mProtocolVersion;
HttpHeader mHeader;
std::string mMethod;
std::string mPath;
std::string mProtocolVersion;
};

View file

@ -4,9 +4,7 @@
class HttpResponse
{
public:
HttpResponse();
~HttpResponse();

View file

@ -8,6 +8,7 @@
#include "ZlibEncoder.h"
#include "CyclicRedundancyChecker.h"
#include "StringUtils.h"
#include <iostream>
#include <sstream>

View file

@ -5,6 +5,7 @@
#include "BufferBitStream.h"
#include "OutputBitStream.h"
#include "ImageBitStream.h"
#include "StringUtils.h"
#include "PngFilter.h"
#include "Lz77Encoder.h"