From: Francis Deslauriers Date: Fri, 9 Feb 2024 21:26:01 +0000 (-0500) Subject: cpp-common: add `dataFromFile()` X-Git-Url: http://git.efficios.com/?p=babeltrace.git;a=commitdiff_plain;h=0b9614ad2eff482d88d08fb2aa02ade0ffa5acbb cpp-common: add `dataFromFile()` Signed-off-by: Francis Deslauriers Change-Id: Ief289d7d12a1cc964af16dc7dd74a4422c81920a Reviewed-on: https://review.lttng.org/c/babeltrace/+/8159 Reviewed-by: Philippe Proulx Reviewed-on: https://review.lttng.org/c/babeltrace/+/12272 CI-Build: Simon Marchi Tested-by: jenkins --- diff --git a/src/Makefile.am b/src/Makefile.am index cde6d6d6..204bb6f1 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -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 \ diff --git a/src/cpp-common/bt2c/exc.hpp b/src/cpp-common/bt2c/exc.hpp index 3138fdbc..3400dfaf 100644 --- a/src/cpp-common/bt2c/exc.hpp +++ b/src/cpp-common/bt2c/exc.hpp @@ -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 index 00000000..93520182 --- /dev/null +++ b/src/cpp-common/bt2c/file-utils.cpp @@ -0,0 +1,38 @@ + +/* + * Copyright (c) 2022 Francis Deslauriers + * + * SPDX-License-Identifier: MIT + */ +#include + +#include "exc.hpp" +#include "file-utils.hpp" + +namespace bt2c { + +std::vector 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 buffer(static_cast(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(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 index 00000000..9890f0a8 --- /dev/null +++ b/src/cpp-common/bt2c/file-utils.hpp @@ -0,0 +1,22 @@ +/* + * Copyright (c) 2022 Francis Deslauriers + * + * SPDX-License-Identifier: MIT + */ + +#ifndef BABELTRACE_CPP_COMMON_FILE_UTILS_HPP +#define BABELTRACE_CPP_COMMON_FILE_UTILS_HPP + +#include +#include + +namespace bt2c { + +/* + * Returns a vector of all the bytes contained in `path`. + */ +std::vector dataFromFile(const char *path); + +} /* namespace bt2c */ + +#endif /* BABELTRACE_CPP_COMMON_FILE_UTILS_HPP */