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 \
}
};
+/*
+ * 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 */
--- /dev/null
+
+/*
+ * 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 */
--- /dev/null
+/*
+ * 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 */