summaryrefslogtreecommitdiff
path: root/src/mbgl/util/io.cpp
diff options
context:
space:
mode:
authorKonstantin Käfer <mail@kkaefer.com>2018-06-07 13:01:38 +0200
committerKonstantin Käfer <mail@kkaefer.com>2018-06-12 17:41:16 +0200
commit1fbf3f2d48df27d028d76fa4ff2c199da347f52c (patch)
treeeb2b2c239450e95c36d05dd81603c512fb96ce71 /src/mbgl/util/io.cpp
parent31953c4c8392c1869f5561e0e0a4a895412e6afc (diff)
downloadqtlocation-mapboxgl-1fbf3f2d48df27d028d76fa4ff2c199da347f52c.tar.gz
[test] standardize on database file name and move I/O functions to util
Diffstat (limited to 'src/mbgl/util/io.cpp')
-rw-r--r--src/mbgl/util/io.cpp21
1 files changed, 19 insertions, 2 deletions
diff --git a/src/mbgl/util/io.cpp b/src/mbgl/util/io.cpp
index 058cd0d202..c84634ac88 100644
--- a/src/mbgl/util/io.cpp
+++ b/src/mbgl/util/io.cpp
@@ -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,9 +47,21 @@ 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