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_PLATFORM_FILESYSTEM_FILE_HPP
21#define ORES_PLATFORM_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.platform/filesystem/io_error.hpp"
30
32
33class file final {
34public:
38 static std::string read_content(std::istream& s);
39
43 static std::string read_content(const std::filesystem::path& p);
44
48 static void write_content(const std::filesystem::path& path,
49 const std::string& content);
50
54 template<typename Ioable>
55 static void write(const std::filesystem::path& path, const Ioable& target) {
56 try {
57 std::ofstream os(path);
58 os.exceptions(std::ifstream::failbit | std::ifstream::badbit);
59 os << target;
60 } catch (const std::exception& e) {
61 std::ostringstream s;
62 s << "An error occurred whilst trying to write a file. Path: '"
63 << path.generic_string() << "'. Error: " << e.what();
64 throw io_error(s.str());
65 }
66 }
67
76 static std::set<std::filesystem::path>
77 find_files(const std::filesystem::path& dir);
78 static std::set<std::filesystem::path>
79 find_files(const std::vector<std::filesystem::path>& dirs);
80 static std::set<std::filesystem::path>
81 find_files(const std::list<std::filesystem::path>& dirs);
88 static std::filesystem::path find_file_recursively_upwards(
89 std::filesystem::path starting_directory,
90 const std::filesystem::path& relative_file_path);
91
95 static void remove(const std::list<std::filesystem::path>& files);
96
101 static void remove_empty_directories(const std::filesystem::path& dir);
102 static void remove_empty_directories(const std::list<std::filesystem::path>& dirs);
111 static void recreate_directory(const std::filesystem::path& dir);
112};
113
114}
115
116#endif
Contains filesystem related classes.
Definition file.hpp:31
An error occurred whilst performing an IO operation.
Definition io_error.hpp:32