aboutsummaryrefslogtreecommitdiff
path: root/toolsrc/src/vcpkg_Files.cpp
diff options
context:
space:
mode:
authorAlexander Karatarakis <alkarata@microsoft.com>2016-09-18 20:50:08 -0700
committerAlexander Karatarakis <alkarata@microsoft.com>2016-09-18 20:54:03 -0700
commitccca198c1b1730b0241911cb56dc8e3504958b2a (patch)
treea2dd9b8b087a09afdcecc5cbb3377bed15127eb2 /toolsrc/src/vcpkg_Files.cpp
downloadvcpkg-ccca198c1b1730b0241911cb56dc8e3504958b2a.tar.gz
vcpkg-ccca198c1b1730b0241911cb56dc8e3504958b2a.zip
Initial commit
Diffstat (limited to 'toolsrc/src/vcpkg_Files.cpp')
-rw-r--r--toolsrc/src/vcpkg_Files.cpp38
1 files changed, 38 insertions, 0 deletions
diff --git a/toolsrc/src/vcpkg_Files.cpp b/toolsrc/src/vcpkg_Files.cpp
new file mode 100644
index 000000000..49a661157
--- /dev/null
+++ b/toolsrc/src/vcpkg_Files.cpp
@@ -0,0 +1,38 @@
+#include "vcpkg_Files.h"
+#include <fstream>
+#include <filesystem>
+
+namespace fs = std::tr2::sys;
+
+namespace vcpkg {namespace Files
+{
+ void check_is_directory(const fs::path& dirpath)
+ {
+ Checks::check_throw(fs::is_directory(dirpath), "The path %s is not a directory", dirpath.string());
+ }
+
+ expected<std::string> get_contents(const fs::path& file_path) noexcept
+ {
+ std::fstream file_stream(file_path, std::ios_base::in | std::ios_base::binary);
+ if (file_stream.fail())
+ {
+ return std::errc::no_such_file_or_directory;
+ }
+
+ file_stream.seekg(0, file_stream.end);
+ auto length = file_stream.tellg();
+ file_stream.seekg(0, file_stream.beg);
+
+ if (length > SIZE_MAX)
+ {
+ return std::errc::file_too_large;
+ }
+
+ std::string output;
+ output.resize(static_cast<size_t>(length));
+ file_stream.read(&output[0], length);
+ file_stream.close();
+
+ return std::move(output);
+ }
+}}