ORE Studio 0.0.4
Loading...
Searching...
No Matches
file.hpp
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 *
3 * Copyright (C) 2024 Marco Craveiro <marco.craveiro@gmail.com>
4 *
5 * This program is free software; you can redistribute it and/or modify it under
6 * the terms of the GNU General Public License as published by the Free Software
7 * Foundation; either version 3 of the License, or (at your option) any later
8 * version.
9 *
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
13 * details.
14 *
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 51
17 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 *
19 */
20#ifndef ORES_UTILITY_FILESYSTEM_FILE_HPP
21#define ORES_UTILITY_FILESYSTEM_FILE_HPP
22
23#include <set>
24#include <list>
25#include <string>
26#include <fstream>
27#include <sstream>
28#include <filesystem>
29#include "ores.utility/log/make_logger.hpp"
30#include "ores.utility/filesystem/io_error.hpp"
31
33
34class file final {
35private:
36 inline static std::string_view logger_name =
37 "ores.utility.filesystem.file";
38
39 static auto& lg() {
40 using namespace ores::utility::log;
41 static auto instance = make_logger(logger_name);
42 return instance;
43 }
44
45public:
49 static std::string read_content(std::istream& s);
50
54 static std::string read_content(const std::filesystem::path& p);
55
59 static void write_content(const std::filesystem::path& path,
60 const std::string& content);
61
65 template<typename Ioable>
66 static void write(const std::filesystem::path& path, const Ioable& target) {
67 try {
68 std::ofstream os(path);
69 os.exceptions(std::ifstream::failbit | std::ifstream::badbit);
70 os << target;
71 } catch (const std::exception& e) {
72 std::ostringstream s;
73 s << "An error occurred whilst trying to write a file. Path: '"
74 << path.generic_string() << "'. Error: " << e.what();
75 throw io_error(s.str());
76 }
77 }
78
87 static std::set<std::filesystem::path>
88 find_files(const std::filesystem::path& dir);
89 static std::set<std::filesystem::path>
90 find_files(const std::vector<std::filesystem::path>& dirs);
91 static std::set<std::filesystem::path>
92 find_files(const std::list<std::filesystem::path>& dirs);
99 static std::filesystem::path find_file_recursively_upwards(
100 std::filesystem::path starting_directory,
101 const std::filesystem::path& relative_file_path);
102
106 static void remove(const std::list<std::filesystem::path>& files);
107
112 static void remove_empty_directories(const std::filesystem::path& dir);
113 static void remove_empty_directories(const std::list<std::filesystem::path>& dirs);
122 static void recreate_directory(const std::filesystem::path& dir);
123};
124
125}
126
127#endif
Contains filesystem related classes.
Definition file.hpp:32
Implements logging for ORE Studio.
Definition lifecycle_manager.hpp:30
An error occurred whilst performing an IO operation.
Definition io_error.hpp:32