summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--libadhocutil/fileUtils.h8
-rw-r--r--libadhocutil/unittests/testFileUtils.cpp9
2 files changed, 17 insertions, 0 deletions
diff --git a/libadhocutil/fileUtils.h b/libadhocutil/fileUtils.h
index 4224643..56ece22 100644
--- a/libadhocutil/fileUtils.h
+++ b/libadhocutil/fileUtils.h
@@ -4,6 +4,7 @@
#include <boost/filesystem/path.hpp>
#include <sys/stat.h>
#include <fcntl.h>
+#include <string_view>
#include "visibility.h"
namespace AdHoc {
@@ -150,6 +151,13 @@ namespace AdHoc {
/// The file data.
void * const data;
+ template<typename T = char>
+ auto
+ sv() const
+ {
+ return std::basic_string_view<T>((const T *)data, st.st_size / sizeof(T));
+ }
+
private:
DLL_PUBLIC void * setupMapInt(int flags) const;
DLL_PUBLIC void * setupMap(int flags) const;
diff --git a/libadhocutil/unittests/testFileUtils.cpp b/libadhocutil/unittests/testFileUtils.cpp
index 973b9bb..01f1738 100644
--- a/libadhocutil/unittests/testFileUtils.cpp
+++ b/libadhocutil/unittests/testFileUtils.cpp
@@ -103,6 +103,15 @@ BOOST_AUTO_TEST_CASE( memmap )
BOOST_REQUIRE(f.fh);
BOOST_REQUIRE_EQUAL(f.getStat().st_mode, 0100644);
BOOST_REQUIRE_EQUAL(0, memcmp(f.data, "#define BOOST_TEST_MODULE FileUtils", 35));
+ // String things
+ auto s = f.sv();
+ BOOST_REQUIRE_EQUAL(s.substr(0, 35), "#define BOOST_TEST_MODULE FileUtils");
+ BOOST_REQUIRE_EQUAL(s.length(), f.getStat().st_size);
+ // Objects
+ auto i = f.sv<int>();
+ BOOST_REQUIRE_EQUAL(i[0], *reinterpret_cast<const int *>("#def"));
+ BOOST_REQUIRE_EQUAL(i[1], *reinterpret_cast<const int *>("ine "));
+ BOOST_REQUIRE_EQUAL(i.length(), f.getStat().st_size / sizeof(int));
}
BOOST_AUTO_TEST_CASE( openmode )