mirror of
https://github.com/DanielSWolf/rhubarb-lip-sync.git
synced 2026-11-21 12:25:08 +00:00
Move file utilities into their own file
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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) :
|
||||
|
||||
30
rhubarb/src/tools/fileTools.cpp
Normal file
30
rhubarb/src/tools/fileTools.cpp
Normal 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);
|
||||
}
|
||||
7
rhubarb/src/tools/fileTools.h
Normal file
7
rhubarb/src/tools/fileTools.h
Normal 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);
|
||||
Reference in New Issue
Block a user