Move file utilities into their own file

This commit is contained in:
Daniel Wolf 2018-07-14 20:23:29 +02:00
parent d077aae74c
commit 1625de64e2
4 changed files with 40 additions and 22 deletions

View File

@ -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

View File

@ -1,8 +1,8 @@
#include <format.h>
#include <string.h>
#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) :

View File

@ -0,0 +1,30 @@
#include "fileTools.h"
#include <cerrno>
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);
}

View File

@ -0,0 +1,7 @@
#pragma once
#include "platformTools.h"
#include <fstream>
std::ifstream openFile(boost::filesystem::path filePath);
void throwIfNotReadable(boost::filesystem::path filePath);