diff --git a/apps/sample-gui/text_editor/TextEditorController.cpp b/apps/sample-gui/text_editor/TextEditorController.cpp index d950311..36adacb 100644 --- a/apps/sample-gui/text_editor/TextEditorController.cpp +++ b/apps/sample-gui/text_editor/TextEditorController.cpp @@ -28,20 +28,18 @@ void TextEditorController::OnSave() { if(mSavePath.empty()) return; File outfile(mSavePath); - outfile.SetAccessMode(File::AccessMode::Write); - outfile.Open(); - outfile.WriteText(mModel->GetDocument()->GetContent()); - outfile.Close(); + + outfile.open(File::AccessMode::Write); + outfile.writeText(mModel->GetDocument()->GetContent()); } void TextEditorController::OnLoad() { if(mLoadPath.empty()) return; File infile(mLoadPath); - infile.SetAccessMode(File::AccessMode::Read); - infile.Open(); - mModel->GetDocument()->SetContent(infile.ReadText()); - infile.Close(); + + infile.open(File::AccessMode::Read); + mModel->GetDocument()->SetContent(infile.readText()); } void TextEditorController::SetSavePath(const std::filesystem::path& path) diff --git a/src/audio/AudioDevice.cpp b/src/audio/AudioDevice.cpp index 7394194..3670110 100644 --- a/src/audio/AudioDevice.cpp +++ b/src/audio/AudioDevice.cpp @@ -20,52 +20,63 @@ std::unique_ptr AudioDevice::Create() return std::make_unique(); } -void AudioDevice::SetNumChannels(unsigned numChannels) +bool AudioDevice::getIsOpen() const { - mNumChannels = numChannels; + return mIsOpen; } -void AudioDevice::SetPeriod(unsigned period) -{ - mPeriod = period; -} - -void AudioDevice::SetBufferSize(std::size_t bufferSize) -{ - mBufferSize = bufferSize; -} - -unsigned AudioDevice::GetNumChannels() const +unsigned AudioDevice::getNumChannels() const { return mNumChannels; } -unsigned AudioDevice::GetPeriod() const +unsigned AudioDevice::getPeriod() const { return mPeriod; } -std::size_t AudioDevice::GetBufferSize() const +std::size_t AudioDevice::getBufferSize() const { return mBufferSize; } -void AudioDevice::SetSampleRate(unsigned rate) -{ - mSampleRate = rate; -} - -unsigned AudioDevice::GetSampleRate() const +unsigned AudioDevice::getSampleRate() const { return mSampleRate; } -void AudioDevice::SetName(const std::string& name) +std::string AudioDevice::getName() const +{ + return mName; +} + +void AudioDevice::setSampleRate(unsigned rate) +{ + mSampleRate = rate; +} + +void AudioDevice::setName(const std::string& name) { mName = name; } -std::string AudioDevice::GetName() const +void AudioDevice::setNumChannels(unsigned numChannels) { - return mName; + mNumChannels = numChannels; } + +void AudioDevice::setPeriod(unsigned period) +{ + mPeriod = period; +} + +void AudioDevice::setBufferSize(std::size_t bufferSize) +{ + mBufferSize = bufferSize; +} + +void AudioDevice::setIsOpen(bool isOpen) +{ + mIsOpen = isOpen; +} + diff --git a/src/audio/AudioDevice.h b/src/audio/AudioDevice.h index c28297e..57e19d8 100644 --- a/src/audio/AudioDevice.h +++ b/src/audio/AudioDevice.h @@ -7,40 +7,42 @@ class AudioDevice { public: - AudioDevice(); ~AudioDevice(); static std::unique_ptr Create(); - void SetSampleRate(unsigned rate); + unsigned getSampleRate() const; - unsigned GetSampleRate() const; + unsigned getNumChannels() const; - void SetName(const std::string& name); + unsigned getPeriod() const; - void SetNumChannels(unsigned numChannels); + std::size_t getBufferSize() const; - void SetPeriod(unsigned period); + bool getIsOpen() const; - void SetBufferSize(std::size_t bufferSize); + std::string getName() const; - unsigned GetNumChannels() const; + void setName(const std::string& name); - unsigned GetPeriod() const; + void setNumChannels(unsigned numChannels); - std::size_t GetBufferSize() const; + void setPeriod(unsigned period); - std::string GetName() const; + void setBufferSize(std::size_t bufferSize); + void setSampleRate(unsigned rate); + + void setIsOpen(bool isOpen); private: - std::string mName {"unset"}; unsigned mSampleRate {44100}; unsigned mNumChannels {1}; unsigned mPeriod {2}; std::size_t mBufferSize{0}; + bool mIsOpen{false}; }; using AudioDevicePtr = std::unique_ptr; diff --git a/src/audio/AudioManager.cpp b/src/audio/AudioManager.cpp index 4e744e2..41cb17a 100644 --- a/src/audio/AudioManager.cpp +++ b/src/audio/AudioManager.cpp @@ -31,22 +31,17 @@ std::unique_ptr AudioManager::Create() return std::make_unique(); } -void AudioManager::AddAudioDevice(AudioDevicePtr device) +void AudioManager::addAudioDevice(AudioDevicePtr device) { mAudioDevices.push_back(std::move(device)); } -IAudioInterface* AudioManager::GetAudioInterface() -{ - return mAudioInterface.get(); -} - -std::size_t AudioManager::GetNumAudioDevices() const +std::size_t AudioManager::getNumAudioDevices() const { return mAudioDevices.size(); } -AudioDevice* AudioManager::GetAudioDevice(unsigned idx) const +AudioDevice* AudioManager::getAudioDevice(unsigned idx) const { if (idx < mAudioDevices.size()) { @@ -55,12 +50,11 @@ AudioDevice* AudioManager::GetAudioDevice(unsigned idx) const return nullptr; } -void AudioManager::Play() +void AudioManager::play(AudioSample* sample, unsigned duration) { if (mAudioDevices.size() == 0) { mAudioDevices.push_back(AudioDevice::Create()); } - mAudioInterface->OpenDevice(mAudioDevices[0]); - mAudioInterface->Play(mAudioDevices[0]); + mAudioInterface->play(mAudioDevices[0].get(), sample, duration); } diff --git a/src/audio/AudioManager.h b/src/audio/AudioManager.h index 78fd23e..dacef02 100644 --- a/src/audio/AudioManager.h +++ b/src/audio/AudioManager.h @@ -6,30 +6,29 @@ #include #include +class AudioSample; + class AudioManager { public: - AudioManager(); ~AudioManager(); static std::unique_ptr Create(); - void AddAudioDevice(AudioDevicePtr device); + void addAudioDevice(AudioDevicePtr device); - std::size_t GetNumAudioDevices() const; + std::size_t getNumAudioDevices() const; - AudioDevice* GetAudioDevice(unsigned idx) const; + AudioDevice* getAudioDevice(unsigned idx) const; - IAudioInterface* GetAudioInterface(); - - void Play(); + void play(AudioSample* sample, unsigned duration); private: std::vector mAudioDevices; - IAudioInterfaceUPtr mAudioInterface; + IAudioInterfacePtr mAudioInterface; }; using AudioManagerUPtr = std::unique_ptr; diff --git a/src/audio/AudioSample.cpp b/src/audio/AudioSample.cpp index a301e61..7a21f5c 100644 --- a/src/audio/AudioSample.cpp +++ b/src/audio/AudioSample.cpp @@ -10,22 +10,22 @@ std::unique_ptr AudioSample::Create() return std::make_unique(); } -std::size_t AudioSample::GetNumChannels() const +std::size_t AudioSample::getNumChannels() const { return mData.size(); } -unsigned AudioSample::GetSampleRate() const +unsigned AudioSample::getSampleRate() const { return mSampleRate; } -unsigned AudioSample::GetBitDepth() const +unsigned AudioSample::getBitDepth() const { return mBitDepth; } -void AudioSample::SetChannelData(const std::vector& data, std::size_t channel) +void AudioSample::setChannelData(const ChannelData& data, std::size_t channel) { if (mData.size() == channel) { @@ -37,7 +37,7 @@ void AudioSample::SetChannelData(const std::vector& data, std::size_t cha } } -std::vector AudioSample::GetChannelData(std::size_t channel) const +AudioSample::ChannelData AudioSample::getChannelData(std::size_t channel) const { if(mData.size() > channel) { diff --git a/src/audio/AudioSample.h b/src/audio/AudioSample.h index 66aba07..08cfc8c 100644 --- a/src/audio/AudioSample.h +++ b/src/audio/AudioSample.h @@ -6,22 +6,23 @@ class AudioSample { public: + using ChannelData = std::vector; AudioSample(); static std::unique_ptr Create(); - std::vector GetChannelData(std::size_t channel) const; - void SetChannelData(const std::vector& data, std::size_t channel); + ChannelData getChannelData(std::size_t channel) const; + std::size_t getNumChannels() const; + unsigned getSampleRate() const; + unsigned getBitDepth() const; - std::size_t GetNumChannels() const; - unsigned GetSampleRate() const; - unsigned GetBitDepth() const; + void setChannelData(const ChannelData& data, std::size_t channel); private: unsigned mSampleRate { 44100 }; unsigned mBitDepth{ 16 }; - std::vector > mData; + std::vector mData; }; using AudioSamplePtr = std::unique_ptr; diff --git a/src/audio/AudioSynth.cpp b/src/audio/AudioSynth.cpp index 9d9d742..dfe3b3c 100644 --- a/src/audio/AudioSynth.cpp +++ b/src/audio/AudioSynth.cpp @@ -1,39 +1,39 @@ #include "AudioSynth.h" + #define _USE_MATH_DEFINES #include #include #include -#include AudioSynth::AudioSynth() { } -AudioSamplePtr AudioSynth::GetConstant(unsigned amplitude, unsigned duration) +AudioSamplePtr AudioSynth::getConstant(unsigned amplitude, unsigned duration) const { auto sample = AudioSample::Create(); - auto num_samples = duration * sample->GetSampleRate(); - sample->SetChannelData(std::vector(num_samples, amplitude), 0); + auto num_samples = duration * sample->getSampleRate(); + sample->setChannelData(std::vector(num_samples, amplitude), 0); return sample; } -AudioSamplePtr AudioSynth::GetSineWave(double freq, unsigned duration) +AudioSamplePtr AudioSynth::getSineWave(double freq, unsigned duration) const { auto sample = AudioSample::Create(); - const auto sample_rate = sample->GetSampleRate(); + const auto sample_rate = sample->getSampleRate(); const auto num_samples = sample_rate*duration; std::vector data(num_samples, 0); - double tick_duration = 1.0/sample_rate; - double pi_2 = 2.0 * M_PI; - short max_short = std::numeric_limits::max(); + const double tick_duration = 1.0/sample_rate; + const double pi_2 = 2.0 * M_PI; + const short max_short = std::numeric_limits::max(); for(unsigned idx=0; idxSetChannelData(data, 0); + sample->setChannelData(data, 0); return sample; } diff --git a/src/audio/AudioSynth.h b/src/audio/AudioSynth.h index 5500786..e3479ce 100644 --- a/src/audio/AudioSynth.h +++ b/src/audio/AudioSynth.h @@ -7,10 +7,9 @@ class AudioSynth { public: - AudioSynth(); - AudioSamplePtr GetConstant(unsigned amplitude, unsigned duration); + AudioSamplePtr getConstant(unsigned amplitude, unsigned duration) const; - AudioSamplePtr GetSineWave(double freq, unsigned duration); + AudioSamplePtr getSineWave(double freq, unsigned duration) const; }; diff --git a/src/audio/AudioTrack.h b/src/audio/AudioTrack.h index 05fc3e1..42f013d 100644 --- a/src/audio/AudioTrack.h +++ b/src/audio/AudioTrack.h @@ -3,6 +3,5 @@ class AudioTrack { public: - AudioTrack(); }; diff --git a/src/audio/AudioWriter.cpp b/src/audio/AudioWriter.cpp index da8d46c..55ad1a5 100644 --- a/src/audio/AudioWriter.cpp +++ b/src/audio/AudioWriter.cpp @@ -3,36 +3,41 @@ #include "File.h" #include "BinaryStream.h" #include "AudioSample.h" +#include "FileLogger.h" AudioWriter::AudioWriter() { } -void AudioWriter::SetPath(const std::string& path) +void AudioWriter::setPath(const Path& path) { mPath = path; } -void AudioWriter::Write(const AudioSamplePtr& sample) +bool AudioWriter::write(const AudioSamplePtr& sample) { if (mPath.empty()) { - return; + MLOG_ERROR("Attempted to write audio with no output file path, aborting Write."); + return false; } - const auto sample_rate = sample->GetSampleRate(); - const auto num_channels = sample->GetNumChannels(); - const auto bytes_per_sample = sample->GetBitDepth() / 8; + File outfile(mPath); + if (!outfile.open(File::AccessMode::Write)) + { + MLOG_ERROR("Failed to open audio output file, aborting Write."); + return false; + } + auto handle = outfile.getOutHandle(); + + const auto sample_rate = sample->getSampleRate(); + const auto num_channels = sample->getNumChannels(); + const auto bytes_per_sample = sample->getBitDepth() / 8; unsigned byte_rate = sample_rate*num_channels*bytes_per_sample; - File outfile(mPath); - outfile.SetAccessMode(File::AccessMode::Write); - outfile.Open(true); - auto handle = outfile.GetOutHandle(); - handle->write("RIFF", 4); - auto data = sample->GetChannelData(0); + auto data = sample->getChannelData(0); const auto num_samples = data.size(); unsigned content_size = 36 + bytes_per_sample* num_samples*num_channels; @@ -54,5 +59,6 @@ void AudioWriter::Write(const AudioSamplePtr& sample) BinaryStream::write(handle, bytes_per_sample* num_samples*num_channels); // bits/sample handle->write(reinterpret_cast(&data[0]), data.size()*sizeof(short)); - outfile.Close(); + + return true; } diff --git a/src/audio/AudioWriter.h b/src/audio/AudioWriter.h index e55f33f..a23164b 100644 --- a/src/audio/AudioWriter.h +++ b/src/audio/AudioWriter.h @@ -2,20 +2,20 @@ #include #include +#include class AudioSample; using AudioSamplePtr = std::unique_ptr; +using Path = std::filesystem::path; class AudioWriter { public: AudioWriter(); - void SetPath(const std::string& path); - - void Write(const AudioSamplePtr& sample); + void setPath(const Path& path); + bool write(const AudioSamplePtr& sample); private: - - std::string mPath; + Path mPath; }; diff --git a/src/audio/audio_interfaces/AlsaInterface.cpp b/src/audio/audio_interfaces/AlsaInterface.cpp index de413a5..08e9a4a 100644 --- a/src/audio/audio_interfaces/AlsaInterface.cpp +++ b/src/audio/audio_interfaces/AlsaInterface.cpp @@ -1,5 +1,7 @@ #include "AlsaInterface.h" + #include "FileLogger.h" +#include "AudioDevice.h" #include "AudioSynth.h" @@ -23,13 +25,13 @@ std::unique_ptr AlsaInterface::Create() return std::make_unique(); } -void AlsaInterface::OpenDevice(const AudioDevicePtr& device) +void AlsaInterface::openDevice(AudioDevice* device) { MLOG_INFO("Opening Device"); snd_pcm_stream_t stream = SND_PCM_STREAM_PLAYBACK; - if (snd_pcm_open(&mHandle, device->GetName().c_str(), stream, 0) < 0) + if (snd_pcm_open(&mHandle, device->getName().c_str(), stream, 0) < 0) { - MLOG_ERROR("Error opening PCM device: " + device->GetName()); + MLOG_ERROR("Error opening PCM device: " + device->getName()); return; } @@ -40,92 +42,110 @@ void AlsaInterface::OpenDevice(const AudioDevicePtr& device) return; } - SetAccessType(device); - SetSampleFormat(device); - SetSampleRate(device); - SetPeriod(device); - SetBufferSize(device); - SetChannelNumber(device); + setAccessType(device); + setSampleFormat(device); + setSampleRate(device); + setPeriod(device); + setBufferSize(device); + setChannelNumber(device); /* Apply HW parameter settings to */ /* PCM device and prepare device */ - if (snd_pcm_hw_params(mHandle, mHardwareParams) < 0) { + if (snd_pcm_hw_params(mHandle, mHardwareParams) < 0) + { MLOG_ERROR("Error setting HW params."); return; } } -void AlsaInterface::SetAccessType(const AudioDevicePtr& device) +void AlsaInterface::setAccessType(AudioDevice* device) { - if (snd_pcm_hw_params_set_access(mHandle, mHardwareParams, SND_PCM_ACCESS_RW_INTERLEAVED) < 0) { + if (snd_pcm_hw_params_set_access(mHandle, mHardwareParams, SND_PCM_ACCESS_RW_INTERLEAVED) < 0) + { MLOG_ERROR("Error setting device access."); return; } } -void AlsaInterface::SetSampleFormat(const AudioDevicePtr& device) +bool AlsaInterface::setSampleFormat(AudioDevice* device) { /* Set sample format */ - if (snd_pcm_hw_params_set_format(mHandle, mHardwareParams, SND_PCM_FORMAT_S16_LE) < 0) { + if (snd_pcm_hw_params_set_format(mHandle, mHardwareParams, SND_PCM_FORMAT_S16_LE) < 0) + { MLOG_ERROR("Error setting format. "); - return; + return false; } + return true; } -void AlsaInterface::SetSampleRate(const AudioDevicePtr& device) +bool AlsaInterface::setSampleRate(AudioDevice* device) { - unsigned rate = device->GetSampleRate(); + unsigned rate = device->getSampleRate(); unsigned exact_rate = rate; if (snd_pcm_hw_params_set_rate_near(mHandle, mHardwareParams, &exact_rate, 0) < 0) { MLOG_ERROR("Error setting rate. "); - return; + return false; } - if (rate != exact_rate) { + + if (rate != exact_rate) + { MLOG_ERROR("The rate is not supported by your hardware."); + return false; } + return true; } -void AlsaInterface::SetPeriod(const AudioDevicePtr& device) +bool AlsaInterface::setPeriod(AudioDevice* device) { /* Set number of periods. Periods used to be called fragments. */ - if (snd_pcm_hw_params_set_periods(mHandle, mHardwareParams, device->GetPeriod(), 0) < 0) + if (snd_pcm_hw_params_set_periods(mHandle, mHardwareParams, device->getPeriod(), 0) < 0) { MLOG_ERROR("Error setting periods. "); - return; + return false; } + return true; } -void AlsaInterface::SetBufferSize(const AudioDevicePtr& device) +bool AlsaInterface::setBufferSize(AudioDevice* device) { - int periods = static_cast(device->GetPeriod()); + int periods = static_cast(device->getPeriod()); /* Set buffer size (in frames). The resulting latency is given by */ /* latency = periodsize * periods / (rate * bytes_per_frame) */ if (snd_pcm_hw_params_set_buffer_size(mHandle, mHardwareParams, (mPeriodSize * periods)>>2) < 0) { MLOG_ERROR("Error setting buffersize. "); - return; + return false; } + return true; } -void AlsaInterface::SetChannelNumber(const AudioDevicePtr& device) +bool AlsaInterface::setChannelNumber(AudioDevice* device) { /* Set number of channels */ - if (snd_pcm_hw_params_set_channels(mHandle, mHardwareParams, device->GetNumChannels()) < 0) + if (snd_pcm_hw_params_set_channels(mHandle, mHardwareParams, device->getNumChannels()) < 0) { MLOG_ERROR("Error setting channels"); - return; + return false; } + return true; } -void AlsaInterface::Play(const AudioDevicePtr& device) +void AlsaInterface::play(AudioDevice* device, AudioSample* sample, unsigned duration) { + if (!device->getIsOpen()) + { + openDevice(device); + } + + if (!device->getIsOpen()) + { + return; + } + MLOG_INFO("Playing audio"); - AudioSynth synth; - const unsigned duration = 100; - double freq = 440; - auto data = synth.GetSineWave(freq, duration)->GetChannelData(0); + const auto data = sample->getChannelData(0); int numFrames = mPeriodSize >> 2; for(int count = 0; count < duration; count++) diff --git a/src/audio/audio_interfaces/AlsaInterface.h b/src/audio/audio_interfaces/AlsaInterface.h index fe0eb2c..06c82ba 100644 --- a/src/audio/audio_interfaces/AlsaInterface.h +++ b/src/audio/audio_interfaces/AlsaInterface.h @@ -1,40 +1,39 @@ #pragma once #include "IAudioInterface.h" -#include "AudioDevice.h" -#include #include +#include class AlsaInterface : public IAudioInterface { - snd_pcm_t* mHandle; - snd_pcm_hw_params_t* mHardwareParams; - snd_pcm_uframes_t mPeriodSize; - public: - AlsaInterface(); ~AlsaInterface(); static std::unique_ptr Create(); - void OpenDevice(const AudioDevicePtr& device) override; + void play(AudioDevice* device, AudioSample* sample, unsigned duration) override; - void SetAccessType(const AudioDevicePtr& device); +private: + void openDevice(AudioDevice* device) override; - void SetSampleFormat(const AudioDevicePtr& device); + void setAccessType(AudioDevice* device); - void SetSampleRate(const AudioDevicePtr& device); + bool setSampleFormat(AudioDevice* device); - void SetPeriod(const AudioDevicePtr& device); + bool setSampleRate(AudioDevice* device); - void SetBufferSize(const AudioDevicePtr& device); + bool setPeriod(AudioDevice* device); - void SetChannelNumber(const AudioDevicePtr& device); + bool setBufferSize(AudioDevice* device); - void Play(const AudioDevicePtr& device) override; + bool setChannelNumber(AudioDevice* device); + + snd_pcm_t* mHandle; + snd_pcm_hw_params_t* mHardwareParams; + snd_pcm_uframes_t mPeriodSize; }; -using AlsaInterfacePtr = std::shared_ptr; +using AlsaInterfacePtr = std::unique_ptr; diff --git a/src/audio/audio_interfaces/IAudioInterface.h b/src/audio/audio_interfaces/IAudioInterface.h index 1cfad01..8c9aa18 100644 --- a/src/audio/audio_interfaces/IAudioInterface.h +++ b/src/audio/audio_interfaces/IAudioInterface.h @@ -3,19 +3,19 @@ #include class AudioDevice; -using AudioDevicePtr = std::unique_ptr; +class AudioSample; class IAudioInterface { public: - IAudioInterface() = default; virtual ~IAudioInterface() = default; - virtual void OpenDevice(const AudioDevicePtr& device) = 0; + virtual void play(AudioDevice* device, AudioSample* sample, unsigned duration) = 0; - virtual void Play(const AudioDevicePtr& device) = 0; +protected: + virtual void openDevice(AudioDevice* device) = 0; }; -using IAudioInterfaceUPtr = std::unique_ptr; +using IAudioInterfacePtr = std::unique_ptr; diff --git a/src/audio/audio_interfaces/NullAudioInterface.cpp b/src/audio/audio_interfaces/NullAudioInterface.cpp index fd5532f..68f01a5 100644 --- a/src/audio/audio_interfaces/NullAudioInterface.cpp +++ b/src/audio/audio_interfaces/NullAudioInterface.cpp @@ -20,12 +20,12 @@ std::unique_ptr NullAudioInterface::Create() return std::make_unique(); } -void NullAudioInterface::OpenDevice(const AudioDevicePtr& device) +void NullAudioInterface::openDevice(AudioDevice* device) { } -void NullAudioInterface::Play(const AudioDevicePtr& device) +void NullAudioInterface::play(AudioDevice* device, AudioSample* sample, unsigned duration) { } diff --git a/src/audio/audio_interfaces/NullAudioInterface.h b/src/audio/audio_interfaces/NullAudioInterface.h index 4d70cf9..bbe13c4 100644 --- a/src/audio/audio_interfaces/NullAudioInterface.h +++ b/src/audio/audio_interfaces/NullAudioInterface.h @@ -9,16 +9,18 @@ class NullAudioInterface : public IAudioInterface { public: - NullAudioInterface(); ~NullAudioInterface(); static std::unique_ptr Create(); - void OpenDevice(const AudioDevicePtr& device) override; + void openDevice(AudioDevice* device) override; - void Play(const AudioDevicePtr& device) override; + void play(AudioDevice* device, AudioSample* sample, unsigned duration) override; + +private: + void openDevice(const AudioDevicePtr& device) override; }; using NullAudioInterfacePtr = std::shared_ptr; diff --git a/src/audio/midi/reader/MidiChannelEventAdapter.cpp b/src/audio/midi/reader/MidiChannelEventAdapter.cpp index f06d540..fc37dd7 100644 --- a/src/audio/midi/reader/MidiChannelEventAdapter.cpp +++ b/src/audio/midi/reader/MidiChannelEventAdapter.cpp @@ -13,7 +13,7 @@ int MidiChannelEventAdapter::ReadEvent(std::ifstream* file, char firstByte, Midi int event_type = (firstByte & first_four_bits) >> 4; int midi_channel = (firstByte & second_four_bits) >> 4; unsigned byteCount = 0; - std::cout << "Channel: " << midi_channel << std::endl; + //std::cout << "Channel: " << midi_channel << std::endl; const bool isStatusByte = ByteUtils::MostSignificantBitIsOne(firstByte); if(isStatusByte) @@ -25,7 +25,7 @@ int MidiChannelEventAdapter::ReadEvent(std::ifstream* file, char firstByte, Midi { event->SetType(lastEventType); } - std::cout << "MC Type " << static_cast(event->GetType()) << std::endl; + //std::cout << "MC Type " << static_cast(event->GetType()) << std::endl; switch(event->GetType()) { case MidiChannelEvent::Type::NOTE_ON: @@ -49,7 +49,7 @@ int MidiChannelEventAdapter::ReadEvent(std::ifstream* file, char firstByte, Midi break; } default: - std::cout << "Unknown status event: " << std::bitset<8>(firstByte) << "|" << event_type <(firstByte) << "|" << event_type <SetType(c); - std::cout << "Meta event type: " << std::hex << int(c) << std::dec<GetType()) { diff --git a/src/audio/midi/reader/MidiReader.cpp b/src/audio/midi/reader/MidiReader.cpp index f6e8160..142bdca 100644 --- a/src/audio/midi/reader/MidiReader.cpp +++ b/src/audio/midi/reader/MidiReader.cpp @@ -22,32 +22,32 @@ MidiReader::MidiReader() } -MidiDocument* MidiReader::GetDocument() const +MidiDocument* MidiReader::getDocument() const { return mDocument.get(); } -bool MidiReader::ProcessHeader() +bool MidiReader::processHeader() { - if(!BinaryStream::checkNextDWord(mFile->GetInHandle(), HeaderLabel)) + if(!BinaryStream::checkNextDWord(mFile->getInHandle(), HeaderLabel)) { return false; } - const auto length = BinaryStream::getNextDWord(mFile->GetInHandle()); + const auto length = BinaryStream::getNextDWord(mFile->getInHandle()); if(!length) { return false; } - const auto formatType = BinaryStream::getNextWord(mFile->GetInHandle()); + const auto formatType = BinaryStream::getNextWord(mFile->getInHandle()); if(!formatType) { return false; } mDocument->SetFormatType(*formatType); - const auto expectedTracks = BinaryStream::getNextWord(mFile->GetInHandle()); + const auto expectedTracks = BinaryStream::getNextWord(mFile->getInHandle()); if(!expectedTracks) { return false; @@ -55,52 +55,52 @@ bool MidiReader::ProcessHeader() mDocument->SetExpectedTracks(*expectedTracks); MidiTimeDivision timeDivision; - MidiTimeAdapter::ReadTimeDivision(mFile->GetInHandle(), timeDivision); + MidiTimeAdapter::ReadTimeDivision(mFile->getInHandle(), timeDivision); mDocument->SetTimeDivision(timeDivision); return true; } -int MidiReader::ProcessEvent(MidiTrack* track) +int MidiReader::processEvent(MidiTrack* track) { int timeDelta {0}; unsigned byteCount {0}; - byteCount += MidiTimeAdapter::ReadEventTimeDelta(mFile->GetInHandle(), timeDelta); + byteCount += MidiTimeAdapter::ReadEventTimeDelta(mFile->getInHandle(), timeDelta); char c; - mFile->GetInHandle()->get(c); - std::cout << "Event check: " << std::bitset<8>(c) << std::endl; + mFile->getInHandle()->get(c); + //std::cout << "Event check: " << std::bitset<8>(c) << std::endl; byteCount++; if(MidiEvent::IsMetaEvent(c)) { auto event = std::make_unique(); event->SetTimeDelta(timeDelta); - std::cout << "Meta event " <GetInHandle(), event.get(), mLastMidiChannel); + //std::cout << "Meta event " <getInHandle(), event.get(), mLastMidiChannel); track->AddEvent(std::move(event)); } else if(MidiEvent::IsSysExEvent(c)) { - std::cout << "Sysex event" << std::endl; + //std::cout << "Sysex event" << std::endl; } else { // Midi event auto event = std::make_unique(); event->SetTimeDelta(timeDelta); - std::cout << "Midi event" << std::endl; - byteCount += MidiChannelEventAdapter::ReadEvent(mFile->GetInHandle(), c, event.get(), mLastChannelEventType); + //std::cout << "Midi event" << std::endl; + byteCount += MidiChannelEventAdapter::ReadEvent(mFile->getInHandle(), c, event.get(), mLastChannelEventType); track->AddEvent(std::move(event)); } return byteCount; } -bool MidiReader::ProcessTrackChunk(bool debug) +bool MidiReader::processTrackChunk(bool debug) { - if(!BinaryStream::checkNextDWord(mFile->GetInHandle(), TrackChunkLabel)) + if(!BinaryStream::checkNextDWord(mFile->getInHandle(), TrackChunkLabel)) { return false; } - const auto chunkSize = BinaryStream::getNextDWord(mFile->GetInHandle()); + const auto chunkSize = BinaryStream::getNextDWord(mFile->getInHandle()); if(!chunkSize) { return false; @@ -111,9 +111,9 @@ bool MidiReader::ProcessTrackChunk(bool debug) unsigned iter_count = 0; while(byteCount < static_cast(*chunkSize)) { - std::cout << "-------------" << std::endl; - byteCount += ProcessEvent(track.get()); - std::cout << "Track byte count: " << byteCount << " of " << *chunkSize << std::endl; + //std::cout << "-------------" << std::endl; + byteCount += processEvent(track.get()); + //std::cout << "Track byte count: " << byteCount << " of " << *chunkSize << std::endl; if(debug && iter_count == 40) { return true; @@ -124,30 +124,30 @@ bool MidiReader::ProcessTrackChunk(bool debug) return true; } -void MidiReader::Read(const std::string& path) +void MidiReader::read(const Path& path) { mFile = std::make_unique(path); - mFile->Open(true); - if(!ProcessHeader()) + mFile->open(File::AccessMode::Read); + if(!processHeader()) { MLOG_ERROR("Problem processing header"); return; } int trackCount = 0; - if(!ProcessTrackChunk(false)) + if(!processTrackChunk(false)) { MLOG_ERROR("Problem processing track chunk"); return; } trackCount++; - if(!ProcessTrackChunk(true)) + if(!processTrackChunk(true)) { MLOG_ERROR("Problem processing track chunk"); return; } trackCount++; - mFile->Close(); + mFile->close(); } diff --git a/src/audio/midi/reader/MidiReader.h b/src/audio/midi/reader/MidiReader.h index 57db097..ea7219f 100644 --- a/src/audio/midi/reader/MidiReader.h +++ b/src/audio/midi/reader/MidiReader.h @@ -5,29 +5,30 @@ #include "MidiTrack.h" #include "MidiChannelEvent.h" #include "File.h" + +#include #include +using Path = std::filesystem::path; + class MidiReader { static constexpr const char TrackChunkLabel[] = "MTrk"; static constexpr const char HeaderLabel[] = "MThd"; public: - MidiReader(); - void Read(const std::string& path); + MidiDocument* getDocument() const; - MidiDocument* GetDocument() const; + void read(const Path& path); private: - - bool ProcessHeader(); - bool ProcessTrackChunk(bool debug=false); - int ProcessEvent(MidiTrack* track); + bool processHeader(); + bool processTrackChunk(bool debug=false); + int processEvent(MidiTrack* track); private: - std::unique_ptr mFile; MidiDocumentPtr mDocument; int mLastMidiChannel {0}; diff --git a/src/audio/midi/reader/MidiTimeAdapter.cpp b/src/audio/midi/reader/MidiTimeAdapter.cpp index 3ce2978..106ebae 100644 --- a/src/audio/midi/reader/MidiTimeAdapter.cpp +++ b/src/audio/midi/reader/MidiTimeAdapter.cpp @@ -16,14 +16,14 @@ int MidiTimeAdapter::ReadEventTimeDelta(std::ifstream* file, int& delta) if(!ByteUtils::MostSignificantBitIsOne(c)) { delta = int(c); - std::cout << "Time delta final: " << delta << std::endl; + //std::cout << "Time delta final: " << delta << std::endl; return byteCount; } int working_c = c; int final_c = 0; unsigned count = 0; - std::cout << "Working " << std::bitset<8>(working_c) << std::endl; + //std::cout << "Working " << std::bitset<8>(working_c) << std::endl; while(unsigned(working_c >> 7) != 0) { char corrected = (working_c &= ~(1UL << 7)); @@ -33,16 +33,16 @@ int MidiTimeAdapter::ReadEventTimeDelta(std::ifstream* file, int& delta) file->get(file_c); byteCount++; working_c = int(file_c); - std::cout << "Working " << std::bitset<8>(working_c) << std::endl; + //std::cout << "Working " << std::bitset<8>(working_c) << std::endl; count++; } - std::cout << "Time delta start: " << std::bitset<16>(final_c) << std::endl; + //std::cout << "Time delta start: " << std::bitset<16>(final_c) << std::endl; final_c <<= 7; - std::cout << "Time delta pre: " << std::bitset<16>(final_c) << std::endl; + //std::cout << "Time delta pre: " << std::bitset<16>(final_c) << std::endl; final_c |= (working_c << 7*(count-1)); delta = int(final_c); - std::cout << "Time delta final: " << delta << "|" << std::bitset<16>(final_c)<< std::endl; + //std::cout << "Time delta final: " << delta << "|" << std::bitset<16>(final_c)<< std::endl; return byteCount; } diff --git a/src/compiler/TemplateFile.cpp b/src/compiler/TemplateFile.cpp index e6fd90c..113f6a6 100644 --- a/src/compiler/TemplateFile.cpp +++ b/src/compiler/TemplateFile.cpp @@ -21,7 +21,7 @@ std::string TemplateFile::getName() const void TemplateFile::loadContent() { - std::cout << "Trying to load file at: " << mPath << std::endl; + //std::cout << "Trying to load file at: " << mPath << std::endl; mRawContent = File(mPath).readLines(); mWorkingLine = 0; mWorkingNode = mRootNode.get(); @@ -183,5 +183,5 @@ void TemplateFile::onFoundExpression(const std::string& expression_string) void TemplateFile::dumpContent() { auto content = mRootNode->getRawContent(); - std::cout << content << std::endl; -} \ No newline at end of file + //std::cout << content << std::endl; +} diff --git a/src/console/MainApplication.cpp b/src/console/MainApplication.cpp index 51f98f4..dc61a19 100644 --- a/src/console/MainApplication.cpp +++ b/src/console/MainApplication.cpp @@ -49,7 +49,7 @@ void MainApplication::initialize(CommandLineArgsUPtr commandLineArgs, std::uniqu MLOG_INFO("Launched"); mDatabaseManager = DatabaseManager::Create(); - mDatabaseManager->CreateDatabase(launch_path + "/database.db"); + mDatabaseManager->openDatabase(launch_path + "/database.db"); MLOG_INFO("Created DB"); mNetworkManager = NetworkManager::Create(); @@ -122,7 +122,7 @@ void MainApplication::playAudio() { //MidiReader reader; //reader.Read("/home/james/sample.mid"); - mAudioManager->Play(); + //mAudioManager->Play(); } void MainApplication::convertDocument(const std::string& inputPath, const std::string& outputPath) @@ -131,7 +131,7 @@ void MainApplication::convertDocument(const std::string& inputPath, const std::s auto output_file = File(std::filesystem::path(outputPath)); MLOG_INFO("Converting: " + inputPath + " to " + outputPath); DocumentConverter converter; - converter.Convert(&input_file, &output_file); + converter.convert(&input_file, &output_file); } CommandLineArgs* MainApplication::getCommandLineArgs() const @@ -141,7 +141,7 @@ CommandLineArgs* MainApplication::getCommandLineArgs() const void MainApplication::shutDown() { - mDatabaseManager->OnShutDown(); + mDatabaseManager->onShutDown(); mNetworkManager->ShutDown(); MLOG_INFO("Shut down"); FileLogger::GetInstance().Close(); diff --git a/src/core/file_utilities/File.cpp b/src/core/file_utilities/File.cpp index b904a8b..e570fb4 100644 --- a/src/core/file_utilities/File.cpp +++ b/src/core/file_utilities/File.cpp @@ -4,32 +4,29 @@ #include "ByteUtils.h" #include -#include #include File::File(std::filesystem::path path) : mFullPath(path), mInHandle(), - mOutHandle(), - mAccessMode(AccessMode::Read) + mOutHandle() { } -std::string File::GetExtension() const +File::~File() +{ + close(); +} + +std::string File::getExtension() const { return mFullPath.extension().string(); } -void File::SetAccessMode(AccessMode mode) -{ - mAccessMode = mode; -} - std::string File::dumpBinary() { - mAccessMode = AccessMode::Read; - Open(); + open(AccessMode::Read); std::stringstream sstr; sstr << "Count | Binary | Decimal | ASCII \n"; @@ -46,22 +43,30 @@ std::string File::dumpBinary() count++; } const auto out = sstr.str(); - Close(); + close(); return out; } -std::ifstream* File::GetInHandle() const +std::ifstream* File::getInHandle() const { return mInHandle.get(); } -std::ofstream* File::GetOutHandle() const +std::ofstream* File::getOutHandle() const { return mOutHandle.get(); } std::optional File::readNextByte() { + if (!mInHandle) + { + if (!open(AccessMode::Read)) + { + return std::nullopt; + } + } + if (mInHandle->good()) { if (auto val = mInHandle->get(); val == EOF) @@ -79,31 +84,39 @@ std::optional File::readNextByte() } } -void File::Open(bool asBinary) +bool File::open(AccessMode accessMode) { - if(mAccessMode == AccessMode::Read) + if (mFullPath.is_absolute() && !std::filesystem::exists(mFullPath.parent_path())) + { + std::filesystem::create_directories(mFullPath.parent_path()); + } + + if(accessMode == AccessMode::Read) { auto flags = std::ifstream::in; - if (asBinary) - { - //flags |= std::ifstream::binary; - } mInHandle = std::make_unique(); mInHandle->open(mFullPath, flags); + if (!mInHandle->is_open()) + { + MLOG_ERROR("Failed to open file at :" << mFullPath); + return false; + } } else { auto flags = std::ofstream::out; - if (asBinary) - { - //flags |= std::ofstream::binary; - } mOutHandle = std::make_unique(); mOutHandle->open(mFullPath, flags); + if (!mOutHandle->is_open()) + { + MLOG_ERROR("Failed to open file at :" << mFullPath); + return false; + } } + return true; } -void File::Close() +void File::close() { if(mOutHandle) { @@ -115,27 +128,29 @@ void File::Close() } } -FileFormat::Format File::InferFormat() const +FileFormat::Format File::inferFormat() const { - const auto extension = GetExtension(); + const auto extension = getExtension(); return FileFormat::InferFormat(extension); } -void File::WriteText(const std::string& text) +void File::writeText(const std::string& text) { - bool had_to_open{ false }; + bool had_to_open{false}; if (!mOutHandle) { had_to_open = true; - SetAccessMode(File::AccessMode::Write); - Open(); + if (!open(File::AccessMode::Write)) + { + return; + } } (*mOutHandle) << text; if (had_to_open) { - Close(); + close(); } } @@ -143,12 +158,15 @@ std::vector File::readLines() { std::vector content; - if (!PathExists()) + if (!pathExists()) { - return content; + return {}; } - Open(false); + if(!open(AccessMode::Read)) + { + return {}; + } std::string str; while(std::getline(*mInHandle, str)) @@ -156,23 +174,26 @@ std::vector File::readLines() content.push_back(str); } - Close(); + close(); return content; } std::string File::read() { - if (!PathExists()) + if (!pathExists()) { return {}; } - Open(false); + if(!open(AccessMode::Read)) + { + return {}; + } std::stringstream buffer; buffer << mInHandle->rdbuf(); - Close(); + close(); return buffer.str(); } @@ -181,14 +202,24 @@ std::string File::getBaseFilename(const Path& path) return path.stem().string(); } -std::string File::ReadText() +std::string File::readText() { + if (!pathExists()) + { + return {}; + } + + if(!open(AccessMode::Read)) + { + return {}; + } + std::string str((std::istreambuf_iterator(*mInHandle)), std::istreambuf_iterator()); return str; } -bool File::PathExists() const +bool File::pathExists() const { return std::filesystem::exists(mFullPath); } diff --git a/src/core/file_utilities/File.h b/src/core/file_utilities/File.h index 2509be7..b5acba0 100644 --- a/src/core/file_utilities/File.h +++ b/src/core/file_utilities/File.h @@ -23,42 +23,38 @@ public: File(std::filesystem::path fullPath); - std::string GetExtension() const; + ~File(); - FileFormat::Format InferFormat() const; + void close(); - std::ifstream* GetInHandle() const; + std::string dumpBinary(); - std::ofstream* GetOutHandle() const; + static std::string getBaseFilename(const Path& path); - void WriteText(const std::string& text); + std::string getExtension() const; - std::string ReadText(); + std::ifstream* getInHandle() const; + + std::ofstream* getOutHandle() const; + + FileFormat::Format inferFormat() const; + + std::string readText(); std::vector readLines(); std::string read(); - static std::string getBaseFilename(const Path& path); + bool pathExists() const; - bool PathExists() const; - - void SetAccessMode(AccessMode mode); - - void Open(bool asBinary = false); - - void Close(); + bool open(AccessMode mode); std::optional readNextByte(); - std::string dumpBinary(); - + void writeText(const std::string& text); private: - std::filesystem::path mFullPath; std::unique_ptr mInHandle; std::unique_ptr mOutHandle; - AccessMode mAccessMode; - }; diff --git a/src/core/serializers/TomlReader.cpp b/src/core/serializers/TomlReader.cpp index ff40272..3693a4f 100644 --- a/src/core/serializers/TomlReader.cpp +++ b/src/core/serializers/TomlReader.cpp @@ -119,17 +119,15 @@ void TomlReader::processLine(const std::string& line) if (in_comment) { mWorkingTable->addComment({ mLastSectionOffset, working_string }); - } + } else if (in_header) { onHeader(working_string); } else if (found_key) { - std::locale locale; - key_string.erase(std::remove_if(key_string.begin(), key_string.end(), [locale](unsigned char c) {return std::isspace(c, locale); }), key_string.end()); - working_string.erase(std::remove_if(working_string.begin(), working_string.end(), [locale](unsigned char c) {return std::isspace(c, locale); }), working_string.end()); - + key_string.erase(std::remove_if(key_string.begin(), key_string.end(), [](char c) {return std::isspace(c); }), key_string.end()); + working_string.erase(std::remove_if(working_string.begin(), working_string.end(), [](char c) {return std::isspace(c); }), working_string.end()); if (working_string.size()>2 && working_string[0] == '"' && working_string[working_string.size() - 1] == '"') { working_string = working_string.substr(1, working_string.size() - 2); diff --git a/src/core/serializers/TomlReader.h b/src/core/serializers/TomlReader.h index 1f4a2de..346fd98 100644 --- a/src/core/serializers/TomlReader.h +++ b/src/core/serializers/TomlReader.h @@ -12,61 +12,61 @@ using Path = std::filesystem::path; class TomlTable { public: - using Comment = std::pair; - using KeyValuePairs = std::unordered_map; + using Comment = std::pair; + using KeyValuePairs = std::unordered_map; - TomlTable(const std::string& header); + TomlTable(const std::string& header); - void addComment(const Comment& comment); + void addComment(const Comment& comment); - void addTable(std::unique_ptr table); + void addTable(std::unique_ptr table); - void addKeyValuePair(const std::string& key, const std::string& value); + void addKeyValuePair(const std::string& key, const std::string& value); - std::string getHeader() const; + std::string getHeader() const; - TomlTable* getTable(const std::string& path); + TomlTable* getTable(const std::string& path); - KeyValuePairs getKeyValuePairs() const; + KeyValuePairs getKeyValuePairs() const; private: - unsigned mLineOffset{ 0 }; - std::string mHeader; - std::unordered_map > mTables; - KeyValuePairs mMap; - std::vector mComments; + unsigned mLineOffset{ 0 }; + std::string mHeader; + std::unordered_map > mTables; + KeyValuePairs mMap; + std::vector mComments; }; class TomlContent { public: - TomlContent(); + TomlContent(); - TomlTable* getRootTable() const; + TomlTable* getRootTable() const; - TomlTable* getTable(const std::string& path) const; + TomlTable* getTable(const std::string& path) const; private: - std::unique_ptr mRootTable; + std::unique_ptr mRootTable; }; class TomlReader { public: - TomlReader(); + TomlReader(); - TomlContent* getContent() const; + TomlContent* getContent() const; - void read(const Path& input_path); + void read(const Path& input_path); - void processLine(const std::string& line); + void processLine(const std::string& line); - void onHeader(const std::string& header); + void onHeader(const std::string& header); - void onKeyValuePair(const std::string key, const std::string value); + void onKeyValuePair(const std::string key, const std::string value); private: - unsigned mLastSectionOffset{ 0 }; - std::unique_ptr mContent; - TomlTable* mWorkingTable{nullptr}; -}; \ No newline at end of file + unsigned mLastSectionOffset{ 0 }; + std::unique_ptr mContent; + TomlTable* mWorkingTable{nullptr}; +}; diff --git a/src/database/Database.cpp b/src/database/Database.cpp index 33d067b..e99e037 100644 --- a/src/database/Database.cpp +++ b/src/database/Database.cpp @@ -16,12 +16,12 @@ std::unique_ptr Database::Create() return std::make_unique(); } -void Database::SetPath(const std::string& path) +void Database::setPath(const Path& path) { mPath = path; } -std::string Database::GetPath() const +const Path& Database::getPath() const { return mPath; } diff --git a/src/database/Database.h b/src/database/Database.h index a645ec1..29e3928 100644 --- a/src/database/Database.h +++ b/src/database/Database.h @@ -2,23 +2,25 @@ #include #include +#include + +using Path = std::filesystem::path; class Database { - - std::string mPath; - public: - Database(); ~Database(); static std::unique_ptr Create(); - void SetPath(const std::string& path); + const Path& getPath() const; - std::string GetPath() const; + void setPath(const Path& path); + +private: + Path mPath; }; using DatabasePtr = std::unique_ptr; diff --git a/src/database/DatabaseManager.cpp b/src/database/DatabaseManager.cpp index 025f790..4ef3017 100644 --- a/src/database/DatabaseManager.cpp +++ b/src/database/DatabaseManager.cpp @@ -18,24 +18,24 @@ std::unique_ptr DatabaseManager::Create() } -void DatabaseManager::CreateDatabase(const std::string& path) +void DatabaseManager::openDatabase(const Path& path) { mDatabase = Database::Create(); - mDatabase->SetPath(path); + mDatabase->setPath(path); mDatabaseInterface = SqliteInterface::Create(); - mDatabaseInterface->Open(mDatabase); + mDatabaseInterface->open(mDatabase.get()); } -void DatabaseManager::Run(const std::string& statement) +void DatabaseManager::run(const std::string& statement) { - mDatabaseInterface->Run(statement); + mDatabaseInterface->run(statement); } -void DatabaseManager::OnShutDown() +void DatabaseManager::onShutDown() { if(mDatabaseInterface) { - mDatabaseInterface->Close(); + mDatabaseInterface->close(); } } diff --git a/src/database/DatabaseManager.h b/src/database/DatabaseManager.h index 239763e..b8e2d61 100644 --- a/src/database/DatabaseManager.h +++ b/src/database/DatabaseManager.h @@ -1,25 +1,27 @@ #pragma once -#include - #include "SqliteInterface.h" #include "Database.h" +#include +#include + +using Path = std::filesystem::path; + class DatabaseManager { public: - DatabaseManager(); ~DatabaseManager(); static std::unique_ptr Create(); - void CreateDatabase(const std::string& path); + void openDatabase(const Path& path); - void Run(const std::string& statement); + void run(const std::string& statement); - void OnShutDown(); + void onShutDown(); private: DatabasePtr mDatabase; diff --git a/src/database/database_interfaces/SqliteInterface.cpp b/src/database/database_interfaces/SqliteInterface.cpp index 2c28fe6..955a437 100644 --- a/src/database/database_interfaces/SqliteInterface.cpp +++ b/src/database/database_interfaces/SqliteInterface.cpp @@ -18,9 +18,9 @@ std::unique_ptr SqliteInterface::Create() return std::make_unique(); } -void SqliteInterface::Open(const DatabasePtr& db) +void SqliteInterface::open(Database* db) { - int rc = sqlite3_open(db->GetPath().c_str(), &mSqliteDb); + int rc = sqlite3_open(db->getPath().c_str(), &mSqliteDb); if( rc ) { MLOG_ERROR("Can't open database: %s\n" << sqlite3_errmsg(mSqliteDb)); @@ -39,7 +39,7 @@ static int callback(void *NotUsed, int argc, char **argv, char **azColName) return 0; } -void SqliteInterface::Run(const std::string& statement) +void SqliteInterface::run(const std::string& statement) { char *zErrMsg = 0; int rc = sqlite3_exec(mSqliteDb, statement.c_str(), callback, 0, &zErrMsg); @@ -50,7 +50,7 @@ void SqliteInterface::Run(const std::string& statement) } } -void SqliteInterface::Close() +void SqliteInterface::close() { sqlite3_close(mSqliteDb); } diff --git a/src/database/database_interfaces/SqliteInterface.h b/src/database/database_interfaces/SqliteInterface.h index f43ba39..5eb9bef 100644 --- a/src/database/database_interfaces/SqliteInterface.h +++ b/src/database/database_interfaces/SqliteInterface.h @@ -6,21 +6,21 @@ class SqliteInterface { - sqlite3* mSqliteDb; - public: - SqliteInterface(); ~SqliteInterface(); static std::unique_ptr Create(); - void Open(const DatabasePtr& db); + void open(Database* db); - void Close(); + void close(); - void Run(const std::string& statement); + void run(const std::string& statement); + +private: + sqlite3* mSqliteDb; }; using SqliteInterfacePtr = std::unique_ptr; diff --git a/src/fonts/FontReader.cpp b/src/fonts/FontReader.cpp index b1c1385..8e67d65 100644 --- a/src/fonts/FontReader.cpp +++ b/src/fonts/FontReader.cpp @@ -19,19 +19,19 @@ void FontReader::setPath(const std::string& path) bool FontReader::readOffsetSubtable() { - mOffsetSubtable.scaler_type = *BinaryStream::getNextDWord(mFile->GetInHandle()); + mOffsetSubtable.scaler_type = *BinaryStream::getNextDWord(mFile->getInHandle()); mCurrentOffset += 4; - mOffsetSubtable.num_tables = *BinaryStream::getNextWord(mFile->GetInHandle()); + mOffsetSubtable.num_tables = *BinaryStream::getNextWord(mFile->getInHandle()); mCurrentOffset += 2; - mOffsetSubtable.search_range = *BinaryStream::getNextWord(mFile->GetInHandle()); + mOffsetSubtable.search_range = *BinaryStream::getNextWord(mFile->getInHandle()); mCurrentOffset += 2; - mOffsetSubtable.entry_selector = *BinaryStream::getNextWord(mFile->GetInHandle()); + mOffsetSubtable.entry_selector = *BinaryStream::getNextWord(mFile->getInHandle()); mCurrentOffset += 2; - mOffsetSubtable.range_shift = *BinaryStream::getNextWord(mFile->GetInHandle()); + mOffsetSubtable.range_shift = *BinaryStream::getNextWord(mFile->getInHandle()); mCurrentOffset += 2; return true; @@ -60,16 +60,16 @@ void FontReader::readTableDirectory() for (unsigned idx=0; idxGetInHandle(), table.name, 4); + BinaryStream::getNextString(mFile->getInHandle(), table.name, 4); mCurrentOffset += 4; - table.checksum = *BinaryStream::getNextDWord(mFile->GetInHandle()); + table.checksum = *BinaryStream::getNextDWord(mFile->getInHandle()); mCurrentOffset += 4; - table.offset = *BinaryStream::getNextDWord(mFile->GetInHandle()); + table.offset = *BinaryStream::getNextDWord(mFile->getInHandle()); mCurrentOffset += 4; - table.length = *BinaryStream::getNextDWord(mFile->GetInHandle()); + table.length = *BinaryStream::getNextDWord(mFile->getInHandle()); mCurrentOffset += 4; logTable(table); @@ -117,29 +117,29 @@ void FontReader::readHeadTable() TrueTypeFont::HeadTable table; - table.version = *BinaryStream::getNextDWord(mFile->GetInHandle()); - table.fontRevision = *BinaryStream::getNextDWord(mFile->GetInHandle()); - table.checksumAdjustment = *BinaryStream::getNextDWord(mFile->GetInHandle()); - table.magicNumber = *BinaryStream::getNextDWord(mFile->GetInHandle()); + table.version = *BinaryStream::getNextDWord(mFile->getInHandle()); + table.fontRevision = *BinaryStream::getNextDWord(mFile->getInHandle()); + table.checksumAdjustment = *BinaryStream::getNextDWord(mFile->getInHandle()); + table.magicNumber = *BinaryStream::getNextDWord(mFile->getInHandle()); mCurrentOffset += 16; - table.flags = *BinaryStream::getNextWord(mFile->GetInHandle()); - table.unitsPerEm = *BinaryStream::getNextWord(mFile->GetInHandle()); + table.flags = *BinaryStream::getNextWord(mFile->getInHandle()); + table.unitsPerEm = *BinaryStream::getNextWord(mFile->getInHandle()); mCurrentOffset += 4; - table.created = *BinaryStream::getNextQWord(mFile->GetInHandle()); - table.modified = *BinaryStream::getNextQWord(mFile->GetInHandle()); + table.created = *BinaryStream::getNextQWord(mFile->getInHandle()); + table.modified = *BinaryStream::getNextQWord(mFile->getInHandle()); mCurrentOffset += 16; - table.xMin = *BinaryStream::getNextWord(mFile->GetInHandle()); - table.yMin = *BinaryStream::getNextWord(mFile->GetInHandle()); - table.xMax = *BinaryStream::getNextWord(mFile->GetInHandle()); - table.yMax = *BinaryStream::getNextWord(mFile->GetInHandle()); - table.macStyle = *BinaryStream::getNextWord(mFile->GetInHandle()); - table.lowestRecPPEM = *BinaryStream::getNextWord(mFile->GetInHandle()); - table.fontDirectionHint = *BinaryStream::getNextWord(mFile->GetInHandle()); - table.indexToLocFormat = *BinaryStream::getNextWord(mFile->GetInHandle()); - table.glyphDataFormat = *BinaryStream::getNextWord(mFile->GetInHandle()); + table.xMin = *BinaryStream::getNextWord(mFile->getInHandle()); + table.yMin = *BinaryStream::getNextWord(mFile->getInHandle()); + table.xMax = *BinaryStream::getNextWord(mFile->getInHandle()); + table.yMax = *BinaryStream::getNextWord(mFile->getInHandle()); + table.macStyle = *BinaryStream::getNextWord(mFile->getInHandle()); + table.lowestRecPPEM = *BinaryStream::getNextWord(mFile->getInHandle()); + table.fontDirectionHint = *BinaryStream::getNextWord(mFile->getInHandle()); + table.indexToLocFormat = *BinaryStream::getNextWord(mFile->getInHandle()); + table.glyphDataFormat = *BinaryStream::getNextWord(mFile->getInHandle()); mCurrentOffset += 18; @@ -154,7 +154,7 @@ std::unique_ptr FontReader::read() mWorkingFont = std::make_unique(); mFile = std::make_unique(mPath); - mFile->Open(true); + mFile->open(File::AccessMode::Read); readOffsetSubtable(); //std::cout << "Current offset: " << mCurrentOffset << std::endl; @@ -171,7 +171,7 @@ std::unique_ptr FontReader::read() break; } - mFile->Close(); + mFile->close(); diff --git a/src/image/png/PngReader.cpp b/src/image/png/PngReader.cpp index 72e2cac..34b22b2 100644 --- a/src/image/png/PngReader.cpp +++ b/src/image/png/PngReader.cpp @@ -25,14 +25,14 @@ void PngReader::setPath(const Path& path) bool PngReader::checkSignature() { const int highBitCheck = 0x89; - const auto firstPos = mFile->GetInHandle()->get(); + const auto firstPos = mFile->getInHandle()->get(); if (firstPos != highBitCheck) { return false; } std::string fileType; - BinaryStream::getNextString(mFile->GetInHandle(), fileType, 3); + BinaryStream::getNextString(mFile->getInHandle(), fileType, 3); if (fileType != "PNG") { return false; @@ -41,7 +41,7 @@ bool PngReader::checkSignature() std::vector sequence{13, 10, 26, 10}; for (auto c : sequence) { - if (mFile->GetInHandle()->get() != c) + if (mFile->getInHandle()->get() != c) { return false; } @@ -51,10 +51,10 @@ bool PngReader::checkSignature() bool PngReader::readChunk() { - unsigned length = *BinaryStream::getNextDWord(mFile->GetInHandle()); + unsigned length = *BinaryStream::getNextDWord(mFile->getInHandle()); std::string chunkType; - BinaryStream::getNextString(mFile->GetInHandle(), chunkType, 4); + BinaryStream::getNextString(mFile->getInHandle(), chunkType, 4); //std::cout << "Got chunk with type: " << chunkType << " and length: " << length << std::endl; bool lastChunk = false; @@ -72,7 +72,7 @@ bool PngReader::readChunk() { decodeData(); } - unsigned crcCheck = *BinaryStream::getNextDWord(mFile->GetInHandle()); + unsigned crcCheck = *BinaryStream::getNextDWord(mFile->getInHandle()); } else if(chunkType == "IDAT") { @@ -91,31 +91,31 @@ bool PngReader::readChunk() for(unsigned idx=0;idxGetInHandle()->get(); + mFile->getInHandle()->get(); } - unsigned crcCheck = *BinaryStream::getNextDWord(mFile->GetInHandle()); + unsigned crcCheck = *BinaryStream::getNextDWord(mFile->getInHandle()); } return !lastChunk; } bool PngReader::readHeaderChunk() { - auto width = *BinaryStream::getNextDWord(mFile->GetInHandle()); - auto height = *BinaryStream::getNextDWord(mFile->GetInHandle()); - auto bitDepth = mFile->GetInHandle()->get(); + auto width = *BinaryStream::getNextDWord(mFile->getInHandle()); + auto height = *BinaryStream::getNextDWord(mFile->getInHandle()); + auto bitDepth = mFile->getInHandle()->get(); mHeader.setImageData(width, height, bitDepth); PngInfo info; - info.mColorType = static_cast(mFile->GetInHandle()->get()); - info.mCompressionMethod = static_cast(mFile->GetInHandle()->get()); - info.mFilterMethod = static_cast(mFile->GetInHandle()->get()); - info.mInterlaceMethod = static_cast(mFile->GetInHandle()->get()); + info.mColorType = static_cast(mFile->getInHandle()->get()); + info.mCompressionMethod = static_cast(mFile->getInHandle()->get()); + info.mFilterMethod = static_cast(mFile->getInHandle()->get()); + info.mInterlaceMethod = static_cast(mFile->getInHandle()->get()); mHeader.setPngInfo(info); mHeader.updateData(); - uint32_t file_crc = *BinaryStream::getNextDWord(mFile->GetInHandle()); + uint32_t file_crc = *BinaryStream::getNextDWord(mFile->getInHandle()); auto crc_calc = mHeader.getCrc(); //std::cout << mHeader.toString() << "*************\n"; @@ -147,7 +147,7 @@ bool PngReader::readIDATChunk(unsigned length) } mInputStream->clearChecksumCalculator(); - uint32_t file_crc = *BinaryStream::getNextDWord(mFile->GetInHandle()); + uint32_t file_crc = *BinaryStream::getNextDWord(mFile->getInHandle()); auto crc_calc = crc_check->getChecksum(); if (file_crc != crc_calc) @@ -166,7 +166,7 @@ std::unique_ptr > PngReader::read() auto image = std::make_unique >(5, 5); mFile = std::make_unique(mPath); - mFile->Open(true); + mFile->open(File::AccessMode::Read); if (!checkSignature()) { diff --git a/src/image/png/PngWriter.cpp b/src/image/png/PngWriter.cpp index 2b1e638..b95cbe2 100644 --- a/src/image/png/PngWriter.cpp +++ b/src/image/png/PngWriter.cpp @@ -161,9 +161,8 @@ void PngWriter::write(const std::unique_ptr >& image) if (!mPath.empty()) { mWorkingFile = std::make_unique(mPath); - mWorkingFile->SetAccessMode(File::AccessMode::Write); - mWorkingFile->Open(true); - mOutStream = std::make_unique(mWorkingFile->GetOutHandle()); + mWorkingFile->open(File::AccessMode::Write); + mOutStream = std::make_unique(mWorkingFile->getOutHandle()); } else { @@ -218,6 +217,6 @@ void PngWriter::write(const std::unique_ptr >& image) if (mWorkingFile) { - mWorkingFile->Close(); + mWorkingFile->close(); } } diff --git a/src/web/DocumentConverter.cpp b/src/web/DocumentConverter.cpp index 114e268..43a79ae 100644 --- a/src/web/DocumentConverter.cpp +++ b/src/web/DocumentConverter.cpp @@ -2,6 +2,7 @@ #include "MarkdownParser.h" #include "HtmlWriter.h" #include "FileLogger.h" +#include "File.h" #include DocumentConverter::DocumentConverter() @@ -9,19 +10,19 @@ DocumentConverter::DocumentConverter() } -void DocumentConverter::Convert(File* input, File* output) +void DocumentConverter::convert(File* input, File* output) { if(!input || !output) { return; } - const auto input_format = input->InferFormat(); + const auto input_format = input->inferFormat(); switch (input_format) { case FileFormat::Format::Markdown: { - ConvertMarkdown(input, output); + convertMarkdown(input, output); break; } default: @@ -31,14 +32,14 @@ void DocumentConverter::Convert(File* input, File* output) } } -void DocumentConverter::ConvertMarkdown(File* input, File* output) +void DocumentConverter::convertMarkdown(File* input, File* output) { - const auto output_format = output->InferFormat(); + const auto output_format = output->inferFormat(); switch (output_format) { case FileFormat::Format::Html: { - MarkdownToHtml(input, output); + markdownToHtml(input, output); break; } default: @@ -48,29 +49,27 @@ void DocumentConverter::ConvertMarkdown(File* input, File* output) } } -void DocumentConverter::MarkdownToHtml(File* input, File* output) +void DocumentConverter::markdownToHtml(File* input, File* output) { MLOG_INFO("Converting Markdown to Html"); - input->SetAccessMode(File::AccessMode::Read); - input->Open(); + input->open(File::AccessMode::Read); MarkdownParser parser; - auto handle = input->GetInHandle(); + auto handle = input->getInHandle(); while(handle->good()) { std::string line; std::getline(*handle, line); - parser.ProcessLine(line); + parser.processLine(line); }; - input->Close(); + input->close(); - auto html_document = parser.GetHtml(); + auto html_document = parser.getHtml(); HtmlWriter writer; std::string html_string = writer.ToString(html_document); - output->SetAccessMode(File::AccessMode::Write); - output->Open(); - *(output->GetOutHandle()) << html_string; - output->Close(); + output->open(File::AccessMode::Write); + *(output->getOutHandle()) << html_string; + output->close(); } diff --git a/src/web/DocumentConverter.h b/src/web/DocumentConverter.h index c987b2f..02b6341 100644 --- a/src/web/DocumentConverter.h +++ b/src/web/DocumentConverter.h @@ -1,16 +1,17 @@ #pragma once #include -#include "File.h" + +class File; class DocumentConverter { public: DocumentConverter(); - void Convert(File* input, File* output); + void convert(File* input, File* output); - void ConvertMarkdown(File* input, File* output); + void convertMarkdown(File* input, File* output); - void MarkdownToHtml(File* input, File* output); + void markdownToHtml(File* input, File* output); }; diff --git a/src/web/markdown/MarkdownParser.cpp b/src/web/markdown/MarkdownParser.cpp index 295a25e..736ac40 100644 --- a/src/web/markdown/MarkdownParser.cpp +++ b/src/web/markdown/MarkdownParser.cpp @@ -9,22 +9,22 @@ MarkdownParser::MarkdownParser() } -void MarkdownParser::ProcessLine(const std::string& line) +void MarkdownParser::processLine(const std::string& line) { } -void MarkdownParser::Run(const std::string& content) +void MarkdownParser::run(const std::string& content) { std::stringstream ss(content); std::string line; while (std::getline(ss, line, '\n')) { - ProcessLine(line); + processLine(line); } } -HtmlDocumentPtr MarkdownParser::GetHtml() +HtmlDocumentPtr MarkdownParser::getHtml() { return mHtmlDocument; } diff --git a/src/web/markdown/MarkdownParser.h b/src/web/markdown/MarkdownParser.h index 762847a..37c2381 100644 --- a/src/web/markdown/MarkdownParser.h +++ b/src/web/markdown/MarkdownParser.h @@ -14,17 +14,16 @@ class MarkdownParser None }; - - DocumentState mDocumentState {DocumentState::None}; - HtmlDocumentPtr mHtmlDocument; - public: - MarkdownParser(); - HtmlDocumentPtr GetHtml(); + HtmlDocumentPtr getHtml(); - void ProcessLine(const std::string& line); + void processLine(const std::string& line); - void Run(const std::string& content); + void run(const std::string& content); + +private: + DocumentState mDocumentState {DocumentState::None}; + HtmlDocumentPtr mHtmlDocument; }; diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 3491714..3828f1f 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -1,5 +1,7 @@ add_subdirectory(test_utils) +file(COPY data/ DESTINATION ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/test_data) + set(TEST_MODULES audio compiler @@ -21,7 +23,13 @@ foreach(module ${TEST_MODULES}) string(TOUPPER ${module} MODULE_UPPER) list(APPEND UNIT_TEST_FILES ${${MODULE_UPPER}_UNIT_TEST_FILES}) list(APPEND UNIT_TEST_DEPENDENCIES ${${MODULE_UPPER}_UNIT_TEST_DEPENDENCIES}) + + list(APPEND INTEGRATION_TEST_FILES ${${MODULE_UPPER}_INTEGRATION_TEST_FILES}) + list(APPEND INTEGRATION_TEST_DEPENDENCIES ${${MODULE_UPPER}_INTEGRATION_TEST_DEPENDENCIES}) endforeach() add_executable(unit_tests test_runner.cpp ${UNIT_TEST_FILES}) target_link_libraries(unit_tests PUBLIC test_utils ${UNIT_TEST_DEPENDENCIES}) + +add_executable(integration_tests test_runner.cpp ${INTEGRATION_TEST_FILES}) +target_link_libraries(integration_tests PUBLIC test_utils ${INTEGRATION_TEST_DEPENDENCIES}) diff --git a/test/audio/CMakeLists.txt b/test/audio/CMakeLists.txt index fcd13b2..6015545 100644 --- a/test/audio/CMakeLists.txt +++ b/test/audio/CMakeLists.txt @@ -1,10 +1,22 @@ -set(AUDIO_UNIT_TEST_FILES - audio/TestAudioWriter.cpp - audio/TestMidiReader.cpp - PARENT_SCOPE - ) - -set(AUDIO_UNIT_TEST_DEPENDENCIES - audio - PARENT_SCOPE - ) \ No newline at end of file +set(MODULE_NAME audio) + +set(UNIT_TESTS + ${MODULE_NAME}/unit/TestAudioWriter.cpp + ${MODULE_NAME}/unit/TestMidiReader.cpp +) + +if(UNIX) +set(INTEGETATION_TESTS + ${MODULE_NAME}/integration/TestAlsaInterface.cpp +) +else() +set(INTEGETATION_TESTS + ${MODULE_NAME}/integration/TestWasapiInterface.cpp +) +endif() + +set(AUDIO_UNIT_TEST_FILES ${UNIT_TESTS} PARENT_SCOPE) +set(AUDIO_INTEGRATION_TEST_FILES ${INTEGETATION_TESTS} PARENT_SCOPE) + +set(AUDIO_UNIT_TEST_DEPENDENCIES ${MODULE_NAME} PARENT_SCOPE) +set(AUDIO_INTEGRATION_TEST_DEPENDENCIES ${MODULE_NAME} PARENT_SCOPE) \ No newline at end of file diff --git a/test/audio/TestAudioWriter.cpp b/test/audio/TestAudioWriter.cpp deleted file mode 100644 index 43c927a..0000000 --- a/test/audio/TestAudioWriter.cpp +++ /dev/null @@ -1,34 +0,0 @@ -#include "AudioSample.h" -#include "AudioSynth.h" -#include "AudioWriter.h" -#include "WasapiInterface.h" - -#include "TestFramework.h" - -#include -#include -#ifdef _WIN32 -#include -#endif -#include - -TEST_CASE(TestWriteWav, "audio") -{ - AudioWriter writer; - writer.SetPath("test.wav"); - - AudioSynth synth; - auto sample = synth.GetSineWave(240, 5); - - writer.Write(sample); -}; - -TEST_CASE(TestAudioRender, "audio") -{ - -#ifdef _WIN32 - WasapiInterface audio_interface; - auto device = AudioDevice::Create(); - audio_interface.Play(device); -#endif -}; diff --git a/test/audio/TestMidiReader.cpp b/test/audio/TestMidiReader.cpp deleted file mode 100644 index 9f8ee6a..0000000 --- a/test/audio/TestMidiReader.cpp +++ /dev/null @@ -1,16 +0,0 @@ -#include "MidiReader.h" - -#include "TestFramework.h" - -#include -#include -#include - -TEST_CASE(TestReadMidi, "audio") -{ - MidiReader reader; - reader.Read("/home/jmsgrogan/Downloads/test.mid"); - - auto document = reader.GetDocument(); - // std::cout << document->Serialize() << std::endl; -}; diff --git a/test/audio/integration/TestAlsaInterface.cpp b/test/audio/integration/TestAlsaInterface.cpp new file mode 100644 index 0000000..e69de29 diff --git a/test/audio/integration/TestWasapiInterface.cpp b/test/audio/integration/TestWasapiInterface.cpp new file mode 100644 index 0000000..cdd3f3f --- /dev/null +++ b/test/audio/integration/TestWasapiInterface.cpp @@ -0,0 +1,13 @@ +#include "TestFramework.h" +#include "TestUtils.h" + +#include "AudioSample.h" +#include "AudioSynth.h" +#include "WasapiInterface.h" + +TEST_CASE(TestWasapiInterface, "audio") +{ + WasapiInterface audio_interface; + auto device = AudioDevice::Create(); + audio_interface.Play(device); +}; diff --git a/test/audio/unit/TestAudioWriter.cpp b/test/audio/unit/TestAudioWriter.cpp new file mode 100644 index 0000000..de46732 --- /dev/null +++ b/test/audio/unit/TestAudioWriter.cpp @@ -0,0 +1,17 @@ +#include "TestFramework.h" +#include "TestUtils.h" + +#include "AudioSample.h" +#include "AudioSynth.h" +#include "AudioWriter.h" + +TEST_CASE(TestAudioWriterWav, "audio") +{ + AudioWriter writer; + writer.setPath(TestUtils::getTestOutputDir() / "TestAudioWriterWav.wav"); + + AudioSynth synth; + const auto sample = synth.getSineWave(240, 5); + + writer.write(sample); +}; diff --git a/test/audio/unit/TestMidiReader.cpp b/test/audio/unit/TestMidiReader.cpp new file mode 100644 index 0000000..6fd872c --- /dev/null +++ b/test/audio/unit/TestMidiReader.cpp @@ -0,0 +1,12 @@ +#include "MidiReader.h" + +#include "TestFramework.h" +#include "TestUtils.h" + +TEST_CASE(TestReadMidi, "audio") +{ + MidiReader reader; + reader.read(TestUtils::getTestDataDir() / "test.mid"); + + auto document = reader.getDocument(); +}; diff --git a/test/compiler/TestTemplatingEngine.cpp b/test/compiler/TestTemplatingEngine.cpp index 9eb422a..3649455 100644 --- a/test/compiler/TestTemplatingEngine.cpp +++ b/test/compiler/TestTemplatingEngine.cpp @@ -1,19 +1,16 @@ #include "TemplatingEngine.h" #include "File.h" -#include "TestFramework.h" -#include -#include +#include "TestFramework.h" +#include "TestUtils.h" TEST_CASE(TestTemplatingEngine, "compiler") { - const auto data_loc = std::filesystem::path(__FILE__) / "../../data"; - - auto engine = TemplatingEngine(data_loc); + auto engine = TemplatingEngine(TestUtils::getTestDataDir()); engine.loadTemplateFiles(); const auto content = engine.processTemplate("index"); - File outfile("index.html"); - outfile.WriteText(content); + File outfile(TestUtils::getTestOutputDir() / "index.html"); + outfile.writeText(content); } diff --git a/test/core/TestTomlReader.cpp b/test/core/TestTomlReader.cpp index c49766c..e081e17 100644 --- a/test/core/TestTomlReader.cpp +++ b/test/core/TestTomlReader.cpp @@ -1,17 +1,12 @@ #include "TomlReader.h" #include "TestFramework.h" - -#include -#include +#include "TestUtils.h" TEST_CASE(TestTomlReader, "core") { - const auto data_loc = std::filesystem::path(__FILE__) / "../../data"; - const auto sample_toml_file = data_loc / "sample_toml.toml"; - auto reader = TomlReader(); - reader.read(sample_toml_file); + reader.read(TestUtils::getTestDataDir() / "sample_toml.toml"); auto themes_table = reader.getContent()->getTable("themes"); @@ -19,6 +14,6 @@ TEST_CASE(TestTomlReader, "core") for (const auto& items : themes_table->getKeyValuePairs()) { - std::cout << "Got entry with key: " << items.first << " and val " << items.second << std::endl; + //std::cout << "Got entry with key: " << items.first << " and val " << items.second << std::endl; } } diff --git a/test/data/index.png b/test/data/index.png new file mode 100644 index 0000000..8656d34 Binary files /dev/null and b/test/data/index.png differ diff --git a/test/data/test.mid b/test/data/test.mid new file mode 100644 index 0000000..a52838c Binary files /dev/null and b/test/data/test.mid differ diff --git a/test/data/test.png b/test/data/test.png new file mode 100644 index 0000000..8081ef1 Binary files /dev/null and b/test/data/test.png differ diff --git a/test/data/test_fixed.png b/test/data/test_fixed.png new file mode 100644 index 0000000..5cf1c2b Binary files /dev/null and b/test/data/test_fixed.png differ diff --git a/test/database/TestDatabase.cpp b/test/database/TestDatabase.cpp index bc1392f..1d4aece 100644 --- a/test/database/TestDatabase.cpp +++ b/test/database/TestDatabase.cpp @@ -1,12 +1,13 @@ #include "DatabaseManager.h" #include "TestFramework.h" +#include "TestUtils.h" TEST_CASE(TestDatabaseManager, "database") { DatabaseManager db_manager; - db_manager.CreateDatabase("test.db"); + db_manager.openDatabase(TestUtils::getTestOutputDir() / "test.db"); std::string statement = "CREATE TABLE corporation;"; - db_manager.Run(statement); + db_manager.run(statement); } diff --git a/test/fonts/TestFontReader.cpp b/test/fonts/TestFontReader.cpp index d47dd17..d18f819 100644 --- a/test/fonts/TestFontReader.cpp +++ b/test/fonts/TestFontReader.cpp @@ -1,8 +1,6 @@ #include "TrueTypeFont.h" #include "FontReader.h" -#include - #include "TestFramework.h" TEST_CASE(TestFontReader, "fonts") diff --git a/test/image/TestPngReader.cpp b/test/image/TestPngReader.cpp index f705cc2..a31f884 100644 --- a/test/image/TestPngReader.cpp +++ b/test/image/TestPngReader.cpp @@ -1,6 +1,7 @@ #include "PngReader.h" #include "BitStream.h" +#include "TestUtils.h" #include "Image.h" #include @@ -9,7 +10,7 @@ TEST_CASE(TestThirdPartyPng, "image") { - const auto path = "/home/jmsgrogan/Downloads/test.png"; + const auto path = TestUtils::getTestDataDir() / "test.png"; //const auto path = "/home/jmsgrogan/Downloads/index.png"; @@ -30,7 +31,7 @@ TEST_CASE(TestThirdPartyPng, "image") TEST_CASE(TestFxedCodePng, "image") { - const auto path = "/home/jmsgrogan/code/MediaTool-build/bin/test_fixed.png"; + const auto path = TestUtils::getTestDataDir() / "test_fixed.png"; //File file(path); //std::cout << file.dumpBinary(); diff --git a/test/image/TestPngWriter.cpp b/test/image/TestPngWriter.cpp index 7ad5e27..bdcaf1e 100644 --- a/test/image/TestPngWriter.cpp +++ b/test/image/TestPngWriter.cpp @@ -7,7 +7,7 @@ #include "ImagePrimitives.h" #include "TestFramework.h" - +#include "TestUtils.h" #include TEST_CASE(TestCompressedPng, "image") @@ -29,20 +29,18 @@ TEST_CASE(TestCompressedPng, "image") image->setData(data); PngWriter writer; - writer.setPath("test_compressed.png"); + writer.setPath(TestUtils::getTestOutputDir() / "test_compressed.png"); writer.setCompressionMethod(Deflate::CompressionMethod::NONE); writer.write(image); return; - File test_file("test_compressed.png"); - test_file.SetAccessMode(File::AccessMode::Read); - test_file.Open(true); + File test_file(TestUtils::getTestOutputDir() / "test_compressed.png"); + test_file.open(File::AccessMode::Read); while(auto byte = test_file.readNextByte()) { //std::cout << static_cast(*byte) << std::endl; } - test_file.Close(); } TEST_CASE(TestFixedPng, "image") @@ -65,12 +63,12 @@ TEST_CASE(TestFixedPng, "image") image->setData(data); PngWriter writer; - writer.setPath("test_fixed.png"); + writer.setPath(TestUtils::getTestOutputDir() / "test_fixed.png"); writer.setCompressionMethod(Deflate::CompressionMethod::FIXED_HUFFMAN); writer.write(image); //return; - File test_file("test_fixed.png"); + File test_file(TestUtils::getTestOutputDir() / "test_fixed.png"); //std::cout << test_file.dumpBinary(); } @@ -95,10 +93,10 @@ TEST_CASE(TestDynamicCompressedPng, "image") image->setData(data); PngWriter writer; - writer.setPath("test_dynamic.png"); + writer.setPath(TestUtils::getTestOutputDir() / "test_dynamic.png"); writer.write(image); //return; - File test_file("test_dynamic.png"); + File test_file(TestUtils::getTestOutputDir() / "test_dynamic.png"); //std::cout << test_file.dumpBinary(); } diff --git a/test/publishing/TestPdfWriter.cpp b/test/publishing/TestPdfWriter.cpp index dad2d8e..e7793cc 100644 --- a/test/publishing/TestPdfWriter.cpp +++ b/test/publishing/TestPdfWriter.cpp @@ -4,6 +4,7 @@ #include "File.h" #include "TestFramework.h" +#include "TestUtils.h" TEST_CASE(TestPdfWriter, "publishing") { @@ -12,8 +13,7 @@ TEST_CASE(TestPdfWriter, "publishing") PdfWriter writer; const auto output = writer.ToString(document); - File pdf_file("TestPdfWriter.pdf"); - pdf_file.SetAccessMode(File::AccessMode::Write); - pdf_file.Open(); - pdf_file.WriteText(output); + File pdf_file(TestUtils::getTestOutputDir() / "TestPdfWriter.pdf"); + pdf_file.open(File::AccessMode::Write); + pdf_file.writeText(output); } diff --git a/test/test_utils/TestUtils.h b/test/test_utils/TestUtils.h new file mode 100644 index 0000000..1524595 --- /dev/null +++ b/test/test_utils/TestUtils.h @@ -0,0 +1,19 @@ +#pragma once + +#include + +using Path = std::filesystem::path; + +class TestUtils +{ +public: + static Path getTestOutputDir() + { + return std::filesystem::current_path() / "test_output"; + } + + static Path getTestDataDir() + { + return std::filesystem::current_path() / "test_data"; + } +}; diff --git a/test/video/TestVideoDecoder.cpp b/test/video/TestVideoDecoder.cpp index 9ecdd7d..361d48e 100644 --- a/test/video/TestVideoDecoder.cpp +++ b/test/video/TestVideoDecoder.cpp @@ -2,24 +2,22 @@ #include "Image.h" #include "FfmpegInterface.h" #include "PngWriter.h" -#include #include "TestFramework.h" TEST_CASE(TestVideoDecoder, "video") { auto video = Video::Create(); - video->SetPath("/home/jmsgrogan/test.mp4"); + video->SetPath(TestUtils::getTestDataDir() / "test.mp4"); FfmpegInterface decoder; auto images = decoder.decodeToImages(video, 4); - std::cout << "Got " << std::to_string(images.size()) << std::endl; PngWriter writer; for(unsigned idx=0; idx<20; idx++) { auto filename = "test_out" + std::to_string(idx) + ".png"; - writer.SetPath(filename); - writer.Write(images[idx]); + writer.setPath(filename); + writer.write(images[idx]); } } diff --git a/test/web/TestMarkdownParser.cpp b/test/web/TestMarkdownParser.cpp index 9a2b667..5d5a170 100644 --- a/test/web/TestMarkdownParser.cpp +++ b/test/web/TestMarkdownParser.cpp @@ -3,23 +3,21 @@ #include "HtmlWriter.h" #include "TestFramework.h" +#include "TestUtils.h" TEST_CASE(TestMarkdownParser, "web") { - File md_file("/home/jmsgrogan/code/MediaTool/test/data/sample_markdown.md"); - md_file.Open(); - const auto md_content = md_file.ReadText(); + File md_file(TestUtils::getTestDataDir() / "sample_markdown.md"); + const auto md_content = md_file.readText(); MarkdownParser parser; - parser.Run(md_content); + parser.run(md_content); - auto html = parser.GetHtml(); + auto html = parser.getHtml(); HtmlWriter writer; const auto html_string = writer.ToString(html); - File html_file("TestMarkdownParserOut.html"); - html_file.SetAccessMode(File::AccessMode::Write); - html_file.Open(); - html_file.WriteText(html_string); + File html_file(TestUtils::getTestOutputDir() / "TestMarkdownParserOut.html"); + html_file.writeText(html_string); } diff --git a/test/web/TestXmlParser.cpp b/test/web/TestXmlParser.cpp index 79ace09..a96041e 100644 --- a/test/web/TestXmlParser.cpp +++ b/test/web/TestXmlParser.cpp @@ -6,13 +6,14 @@ #include "File.h" #include "TestFramework.h" +#include "TestUtils.h" TEST_CASE(TestXmlParser, "web") { XmlParser parser; std::ifstream xml_file; - xml_file.open("/home/jmsgrogan/test.xml", std::ifstream::in); + xml_file.open(TestUtils::getTestDataDir() / "test.xml", std::ifstream::in); while(xml_file.good()) { std::string line; @@ -21,11 +22,9 @@ TEST_CASE(TestXmlParser, "web") } xml_file.close(); - auto outFile = std::make_unique("test_out.xml"); - outFile->SetAccessMode(File::AccessMode::Write); - outFile->Open(); - XmlWriter writer; auto content = writer.ToString(parser.GetDocument().get()); - outFile->WriteText(content); + + auto outFile = std::make_unique(TestUtils::getTestOutputDir() / "test_out.xml"); + outFile->writeText(content); }