1
0
Fork 0
mirror of https://git.rwth-aachen.de/acs/public/villas/node/ synced 2025-03-30 00:00:11 +01:00
VILLASnode/common/lib/kernel/devices/utils.cpp
Pascal Bauer 82082c392a add utils
Signed-off-by: Pascal Bauer <pascal.bauer@rwth-aachen.de>
2024-11-15 10:36:34 +00:00

48 lines
No EOL
1.2 KiB
C++

/* Utils
*
* Author: Pascal Bauer <pascal.bauer@rwth-aachen.de>
*
* SPDX-FileCopyrightText: 2023-2024 Pascal Bauer <pascal.bauer@rwth-aachen.de>
* SPDX-License-Identifier: Apache-2.0
*/
#include <dirent.h>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <villas/kernel/devices/utils.hpp>
#include <villas/log.hpp>
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<std::string>
utils::read_names_in_directory(const std::string &name) {
DIR *directory = opendir(name.c_str());
struct dirent *dp;
std::vector<std::string> 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;
}