cpp-common: add `dataFromFile()`
authorFrancis Deslauriers <francis.deslauriers@efficios.com>
Fri, 9 Feb 2024 21:26:01 +0000 (16:26 -0500)
committerSimon Marchi <simon.marchi@efficios.com>
Wed, 17 Apr 2024 17:57:53 +0000 (13:57 -0400)
Signed-off-by: Francis Deslauriers <francis.deslauriers@efficios.com>
Change-Id: Ief289d7d12a1cc964af16dc7dd74a4422c81920a
Reviewed-on: https://review.lttng.org/c/babeltrace/+/8159
Reviewed-by: Philippe Proulx <eeppeliteloop@gmail.com>
Reviewed-on: https://review.lttng.org/c/babeltrace/+/12272
CI-Build: Simon Marchi <simon.marchi@efficios.com>
Tested-by: jenkins <jenkins@lttng.org>
src/Makefile.am
src/cpp-common/bt2c/exc.hpp
src/cpp-common/bt2c/file-utils.cpp [new file with mode: 0644]
src/cpp-common/bt2c/file-utils.hpp [new file with mode: 0644]

index cde6d6d6c33d50183b06938fe30133237600ae28..204bb6f17db8303a91ebbbbe32de5e58207ca34c 100644 (file)
@@ -164,6 +164,8 @@ cpp_common_libcpp_common_la_SOURCES = \
        cpp-common/bt2c/dummy.cpp \
        cpp-common/bt2c/endian.hpp \
        cpp-common/bt2c/exc.hpp \
+       cpp-common/bt2c/file-utils.cpp \
+       cpp-common/bt2c/file-utils.hpp \
        cpp-common/bt2c/fmt.hpp \
        cpp-common/bt2c/glib-up.hpp \
        cpp-common/bt2c/libc-up.hpp \
index 3138fdbcd44060d36b8afa67f8f9be205151dcd1..3400dfafffe1d3d24981481a0b04486c83374d38 100644 (file)
@@ -69,6 +69,17 @@ public:
     }
 };
 
+/*
+ * No such file or directory.
+ */
+class NoSuchFileOrDirectoryError : public Error
+{
+public:
+    explicit NoSuchFileOrDirectoryError() noexcept : Error {"No such file or directory"}
+    {
+    }
+};
+
 } /* namespace bt2c */
 
 #endif /* BABELTRACE_CPP_COMMON_BT2C_EXC_HPP */
diff --git a/src/cpp-common/bt2c/file-utils.cpp b/src/cpp-common/bt2c/file-utils.cpp
new file mode 100644 (file)
index 0000000..9352018
--- /dev/null
@@ -0,0 +1,38 @@
+
+/*
+ * Copyright (c) 2022 Francis Deslauriers <francis.deslauriers@efficios.com>
+ *
+ * SPDX-License-Identifier: MIT
+ */
+#include <fstream>
+
+#include "exc.hpp"
+#include "file-utils.hpp"
+
+namespace bt2c {
+
+std::vector<std::uint8_t> dataFromFile(const char * const filePath)
+{
+    /*
+     * Open a file stream and seek to the end of the stream to compute the size
+     * of the buffer required.
+    */
+    std::ifstream file {filePath, std::ios::binary | std::ios::ate};
+
+    if (!file) {
+        throw NoSuchFileOrDirectoryError {};
+    }
+
+    const auto size = file.tellg();
+    std::vector<uint8_t> buffer(static_cast<std::size_t>(size));
+
+    /*
+     * Seek the reading head back at the beginning of the stream to actually
+     * read the content.
+     */
+    file.seekg(0, std::ios::beg);
+    file.read(reinterpret_cast<char *>(buffer.data()), size);
+    return buffer;
+}
+
+} /* namespace bt2c */
diff --git a/src/cpp-common/bt2c/file-utils.hpp b/src/cpp-common/bt2c/file-utils.hpp
new file mode 100644 (file)
index 0000000..9890f0a
--- /dev/null
@@ -0,0 +1,22 @@
+/*
+ * Copyright (c) 2022 Francis Deslauriers <francis.deslauriers@efficios.com>
+ *
+ * SPDX-License-Identifier: MIT
+ */
+
+#ifndef BABELTRACE_CPP_COMMON_FILE_UTILS_HPP
+#define BABELTRACE_CPP_COMMON_FILE_UTILS_HPP
+
+#include <cstdint>
+#include <vector>
+
+namespace bt2c {
+
+/*
+ * Returns a vector of all the bytes contained in `path`.
+ */
+std::vector<std::uint8_t> dataFromFile(const char *path);
+
+} /* namespace bt2c */
+
+#endif /* BABELTRACE_CPP_COMMON_FILE_UTILS_HPP */
This page took 0.025921 seconds and 4 git commands to generate.