diff --git a/common/include/villas/kernel/devices/utils.hpp b/common/include/villas/kernel/devices/utils.hpp new file mode 100644 index 000000000..9d4984892 --- /dev/null +++ b/common/include/villas/kernel/devices/utils.hpp @@ -0,0 +1,26 @@ +/* Utils + * + * Author: Pascal Bauer + * + * SPDX-FileCopyrightText: 2023-2024 Pascal Bauer + * SPDX-License-Identifier: Apache-2.0 + */ + +#pragma once + +#include +#include +#include + +namespace villas { +namespace kernel { +namespace devices { +namespace utils { + +void write_to_file(std::string data, const std::filesystem::path file); +std::vector read_names_in_directory(const std::string &name); + +} // namespace utils +} // namespace devices +} // namespace kernel +} // namespace villas diff --git a/common/lib/kernel/devices/utils.cpp b/common/lib/kernel/devices/utils.cpp new file mode 100644 index 000000000..16fb5e387 --- /dev/null +++ b/common/lib/kernel/devices/utils.cpp @@ -0,0 +1,48 @@ +/* Utils + * + * Author: Pascal Bauer + * + * SPDX-FileCopyrightText: 2023-2024 Pascal Bauer + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include +#include +#include +#include +#include + +namespace utils = villas::kernel::devices::utils; + +void utils::write_to_file(std::string data, const std::filesystem::path file) { + villas::Log::get("Filewriter")->debug("{} > {}", data, file.u8string()); + std::ofstream outputFile(file.u8string()); + + if (outputFile.is_open()) { + // Write to file + outputFile << data; + outputFile.close(); + } else { + throw std::filesystem::filesystem_error("Cannot open outputfile", + std::error_code()); + } +} + +std::vector +utils::read_names_in_directory(const std::string &name) { + DIR *directory = opendir(name.c_str()); + + struct dirent *dp; + std::vector names; + dp = readdir(directory); + while (dp != NULL) { + auto name = std::string(dp->d_name); + if (name != "." && name != "..") { + names.push_back(name); + } + dp = readdir(directory); + } + closedir(directory); + return names; +} \ No newline at end of file