1
0
Fork 0
mirror of https://git.rwth-aachen.de/acs/public/villas/node/ synced 2025-03-09 00:00:00 +01:00

add utils

Signed-off-by: Pascal Bauer <pascal.bauer@rwth-aachen.de>
This commit is contained in:
Pascal Bauer 2024-08-24 12:36:42 +02:00 committed by Niklas Eiling
parent 8d53e0c139
commit 82082c392a
2 changed files with 74 additions and 0 deletions

View file

@ -0,0 +1,26 @@
/* 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
*/
#pragma once
#include <filesystem>
#include <string>
#include <vector>
namespace villas {
namespace kernel {
namespace devices {
namespace utils {
void write_to_file(std::string data, const std::filesystem::path file);
std::vector<std::string> read_names_in_directory(const std::string &name);
} // namespace utils
} // namespace devices
} // namespace kernel
} // namespace villas

View file

@ -0,0 +1,48 @@
/* 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;
}