|
| 1 | +/* |
| 2 | + ------------------------------------------------------------------------------- |
| 3 | + This file is part of the Private Message Database test suite. |
| 4 | + Copyright (C) 2025 Dirk Stolle |
| 5 | +
|
| 6 | + This program is free software: you can redistribute it and/or modify |
| 7 | + it under the terms of the GNU General Public License as published by |
| 8 | + the Free Software Foundation, either version 3 of the License, or |
| 9 | + (at your option) any later version. |
| 10 | +
|
| 11 | + This program is distributed in the hope that it will be useful, |
| 12 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | + GNU General Public License for more details. |
| 15 | +
|
| 16 | + You should have received a copy of the GNU General Public License |
| 17 | + along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 | + ------------------------------------------------------------------------------- |
| 19 | +*/ |
| 20 | + |
| 21 | +#include "functions.hpp" |
| 22 | +#include <fstream> |
| 23 | +#include <iostream> |
| 24 | +#include "../../libstriezel/filesystem/directory.hpp" |
| 25 | +#include "../../libstriezel/filesystem/file.hpp" |
| 26 | +#include "../paths.hpp" |
| 27 | +#include "../ReturnCodes.hpp" |
| 28 | +#include "defaults.hpp" |
| 29 | + |
| 30 | +namespace pmdb::tpl |
| 31 | +{ |
| 32 | + |
| 33 | +int ensureTemplateFile(const std::string& file_path, const std::string_view default_content) |
| 34 | +{ |
| 35 | + if (!libstriezel::filesystem::file::exists(file_path)) |
| 36 | + { |
| 37 | + std::ofstream stream(file_path, std::ios::out | std::ios::binary); |
| 38 | + if (!stream) |
| 39 | + { |
| 40 | + std::cerr << "Error: Could not create template file " << file_path << "!\n"; |
| 41 | + return rcFileError; |
| 42 | + } |
| 43 | + stream.write(default_content.data(), default_content.size()); |
| 44 | + stream.close(); |
| 45 | + if (!stream.good()) |
| 46 | + { |
| 47 | + std::cerr << "Error: Could not write template file " << file_path << "!\n"; |
| 48 | + return rcFileError; |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + // File already existed or was created successfully. |
| 53 | + return 0; |
| 54 | +} |
| 55 | + |
| 56 | +int ensureFilesExist() |
| 57 | +{ |
| 58 | + const auto directory = pmdb::paths::templates(); |
| 59 | + if (!libstriezel::filesystem::directory::exists(directory)) |
| 60 | + { |
| 61 | + if (!libstriezel::filesystem::directory::createRecursive(directory)) |
| 62 | + { |
| 63 | + std::cerr << "Error: Could not create missing template directory " |
| 64 | + << directory << ".\n"; |
| 65 | + return rcFileError; |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + const auto tpl_directory = libstriezel::filesystem::slashify(directory); |
| 70 | + |
| 71 | + int rc = ensureTemplateFile(tpl_directory + "message.tpl", pmdb::tpl::defaults::message); |
| 72 | + if (rc != 0) |
| 73 | + { |
| 74 | + return rc; |
| 75 | + } |
| 76 | + rc = ensureTemplateFile(tpl_directory + "folder.tpl", pmdb::tpl::defaults::folder); |
| 77 | + if (rc != 0) |
| 78 | + { |
| 79 | + return rc; |
| 80 | + } |
| 81 | + rc = ensureTemplateFile(tpl_directory + "index_entry.tpl", pmdb::tpl::defaults::index_entry); |
| 82 | + if (rc != 0) |
| 83 | + { |
| 84 | + return rc; |
| 85 | + } |
| 86 | + rc = ensureTemplateFile(tpl_directory + "folder_entry.tpl", pmdb::tpl::defaults::folder_entry); |
| 87 | + if (rc != 0) |
| 88 | + { |
| 89 | + return rc; |
| 90 | + } |
| 91 | + return ensureTemplateFile(tpl_directory + "folder_list.tpl", pmdb::tpl::defaults::folder_list); |
| 92 | +} |
| 93 | + |
| 94 | +} // namespace |
0 commit comments