diff --git a/rhubarb/CMakeLists.txt b/rhubarb/CMakeLists.txt index d02f439..412e07d 100644 --- a/rhubarb/CMakeLists.txt +++ b/rhubarb/CMakeLists.txt @@ -446,6 +446,8 @@ add_library(rhubarb-tools src/tools/EnumConverter.h src/tools/exceptions.cpp src/tools/exceptions.h + src/tools/fileTools.cpp + src/tools/fileTools.h src/tools/Lazy.h src/tools/nextCombination.h src/tools/NiceCmdLineOutput.cpp diff --git a/rhubarb/src/audio/WaveFileReader.cpp b/rhubarb/src/audio/WaveFileReader.cpp index bb63393..e910dad 100644 --- a/rhubarb/src/audio/WaveFileReader.cpp +++ b/rhubarb/src/audio/WaveFileReader.cpp @@ -1,8 +1,8 @@ #include -#include #include "WaveFileReader.h" #include "ioTools.h" #include "tools/platformTools.h" +#include "tools/fileTools.h" using std::runtime_error; using fmt::format; @@ -30,27 +30,6 @@ namespace Codec { constexpr int Float = 0x03; }; -std::ifstream openFile(path filePath) { - try { - std::ifstream file; - file.exceptions(std::ifstream::failbit | std::ifstream::badbit); - file.open(filePath.c_str(), std::ios::binary); - - // Error messages on stream exceptions are mostly useless. - // Read some dummy data so that we can throw a decent exception in case the file is missing, locked, etc. - file.seekg(0, std::ios_base::end); - if (file.tellg()) { - file.seekg(0); - file.get(); - file.seekg(0); - } - - return std::move(file); - } catch (const std::ifstream::failure&) { - throw runtime_error(errorNumberToString(errno)); - } -} - string codecToString(int codec); WaveFileReader::WaveFileReader(path filePath) : diff --git a/rhubarb/src/tools/fileTools.cpp b/rhubarb/src/tools/fileTools.cpp new file mode 100644 index 0000000..82a539f --- /dev/null +++ b/rhubarb/src/tools/fileTools.cpp @@ -0,0 +1,30 @@ +#include "fileTools.h" + +#include + +using boost::filesystem::path; + +std::ifstream openFile(path filePath) { + try { + std::ifstream file; + file.exceptions(std::ifstream::failbit | std::ifstream::badbit); + file.open(filePath.c_str(), std::ios::binary); + + // Read some dummy data so that we can throw a decent exception in case the file is missing, locked, etc. + file.seekg(0, std::ios_base::end); + if (file.tellg()) { + file.seekg(0); + file.get(); + file.seekg(0); + } + + return std::move(file); + } catch (const std::ifstream::failure&) { + // Error messages on stream exceptions are mostly useless. + throw std::runtime_error(errorNumberToString(errno)); + } +} + +void throwIfNotReadable(path filePath) { + openFile(filePath); +} diff --git a/rhubarb/src/tools/fileTools.h b/rhubarb/src/tools/fileTools.h new file mode 100644 index 0000000..8e6053f --- /dev/null +++ b/rhubarb/src/tools/fileTools.h @@ -0,0 +1,7 @@ +#pragma once +#include "platformTools.h" +#include + +std::ifstream openFile(boost::filesystem::path filePath); + +void throwIfNotReadable(boost::filesystem::path filePath); \ No newline at end of file