[test] standardize on database file name and move I/O functions to util
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
|
||||
#include <cstdio>
|
||||
#include <cerrno>
|
||||
#include <cstring>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include <fstream>
|
||||
@@ -9,6 +10,10 @@
|
||||
namespace mbgl {
|
||||
namespace util {
|
||||
|
||||
IOException::IOException(int err, const std::string& msg)
|
||||
: std::runtime_error(msg + ": " + std::strerror(errno)), code(err) {
|
||||
}
|
||||
|
||||
void write_file(const std::string &filename, const std::string &data) {
|
||||
FILE *fd = fopen(filename.c_str(), "wb");
|
||||
if (fd) {
|
||||
@@ -42,10 +47,22 @@ optional<std::string> readFile(const std::string &filename) {
|
||||
|
||||
void deleteFile(const std::string& filename) {
|
||||
const int ret = std::remove(filename.c_str());
|
||||
if (ret != 0) {
|
||||
throw IOException(errno, "failed to unlink file");
|
||||
if (ret != 0 && errno != ENOENT) {
|
||||
throw IOException(errno, "Could not delete file " + filename);
|
||||
}
|
||||
}
|
||||
|
||||
void copyFile(const std::string& destination, const std::string& source) {
|
||||
std::ifstream src(source, std::ios::binary);
|
||||
if (!src.good()) {
|
||||
throw IOException(errno, "Cannot read file " + destination);
|
||||
}
|
||||
std::ofstream dst(destination, std::ios::binary);
|
||||
if (!dst.good()) {
|
||||
throw IOException(errno, "Cannot write file " + destination);
|
||||
}
|
||||
dst << src.rdbuf();
|
||||
}
|
||||
|
||||
} // namespace util
|
||||
} // namespace mbgl
|
||||
|
||||
@@ -9,8 +9,7 @@ namespace mbgl {
|
||||
namespace util {
|
||||
|
||||
struct IOException : std::runtime_error {
|
||||
IOException(int err, const char* msg) : std::runtime_error(msg), code(err) {
|
||||
}
|
||||
IOException(int err, const std::string& msg);
|
||||
const int code = 0;
|
||||
};
|
||||
|
||||
@@ -19,6 +18,7 @@ std::string read_file(const std::string &filename);
|
||||
|
||||
optional<std::string> readFile(const std::string &filename);
|
||||
void deleteFile(const std::string& filename);
|
||||
void copyFile(const std::string& destination, const std::string& source);
|
||||
|
||||
} // namespace util
|
||||
} // namespace mbgl
|
||||
|
||||
Reference in New Issue
Block a user