Fray Engine
Loading...
Searching...
No Matches
application_style.hpp
Go to the documentation of this file.
1#ifndef FRAY_APPLICATION_STYLE_HPP
2#define FRAY_APPLICATION_STYLE_HPP
3
4#include <filesystem>
5#include <fstream>
6#include <string>
7
9
10namespace fray::style
11{
13 inline const std::filesystem::path buttonDirectory = std::filesystem::path("Fray Styles") / "01 Basic Components";
15 inline constexpr const char* buttonFile = "fray_button.css";
17 inline constexpr std::size_t maximumStyleSize = 1024 * 1024;
18
26 const std::filesystem::path& engineDirectory,
27 const std::string& css)
28 {
29 if (engineDirectory.empty() || css.size() > maximumStyleSize || css.find('\0') != std::string::npos)
30 {
31 log::engineError("Cannot save button stylesheet: invalid directory or contents.");
32 return errors::FAILED_SAVE_STATE;
33 }
34 const auto directory = engineDirectory / buttonDirectory;
35 std::error_code error;
36 std::filesystem::create_directories(directory, error);
37 if (error || !std::filesystem::is_directory(directory, error) || error)
38 {
39 log::engineError("Cannot create button stylesheet directory: %s", error.message().c_str());
40 return errors::FAILED_SAVE_STATE;
41 }
42 const auto status = std::filesystem::symlink_status(directory / buttonFile, error);
43 if ((error && error != std::errc::no_such_file_or_directory) ||
44 (!error && status.type() != std::filesystem::file_type::not_found &&
45 !std::filesystem::is_regular_file(status)))
46 {
47 log::engineError("Cannot save button stylesheet: destination is not a regular file.");
48 return errors::FAILED_SAVE_STATE;
49 }
50 return state::writeApplicationFile(directory, buttonFile, css);
51 }
52
61 const std::filesystem::path& engineDirectory,
62 const std::string& defaults,
63 std::string& css)
64 {
65 if (engineDirectory.empty() || defaults.size() > maximumStyleSize || defaults.find('\0') != std::string::npos)
66 {
67 log::engineError("Cannot load button stylesheet: invalid directory or defaults.");
68 return errors::FAILED_OPEN_STORE;
69 }
70 const auto file = engineDirectory / buttonDirectory / buttonFile;
71 std::error_code error;
72 auto status = std::filesystem::symlink_status(file, error);
73 if (error == std::errc::no_such_file_or_directory ||
74 (!error && status.type() == std::filesystem::file_type::not_found))
75 {
76 const auto directory = file.parent_path();
77 error.clear();
78 std::filesystem::create_directories(directory, error);
79 if (error || !std::filesystem::is_directory(directory, error) || error)
80 {
81 log::engineError("Cannot create button stylesheet directory: %s", error.message().c_str());
82 return errors::FAILED_SAVE_STATE;
83 }
84 const auto result = state::writeApplicationFile(directory, buttonFile, defaults, false);
85 if (result != errors::NONE) return result;
86 status = std::filesystem::symlink_status(file, error);
87 }
88 if (error || !std::filesystem::is_regular_file(status))
89 {
90 log::engineError("Cannot load button stylesheet: destination is not a regular file.");
91 return errors::FAILED_OPEN_STORE;
92 }
93 const auto size = std::filesystem::file_size(file, error);
94 if (error || size > maximumStyleSize)
95 {
96 log::engineError("Cannot load button stylesheet: invalid file size.");
97 return errors::FAILED_OPEN_STORE;
98 }
99 std::ifstream input(file, std::ios::binary);
100 if (!input)
101 {
102 log::engineError("Cannot open button stylesheet.");
103 return errors::FAILED_OPEN_STORE;
104 }
105 std::string value(static_cast<std::size_t>(size), '\0');
106 input.read(value.data(), static_cast<std::streamsize>(value.size()));
107 if (input.gcount() != static_cast<std::streamsize>(value.size()) ||
108 input.peek() != std::char_traits<char>::eof() || input.bad() || value.find('\0') != std::string::npos)
109 {
110 log::engineError("Cannot read button stylesheet.");
111 return errors::FAILED_OPEN_STORE;
112 }
113 css = std::move(value);
114 return errors::NONE;
115 }
116}
117
118#endif
F_long F_err
Error return type used across the Engine; the value is a fray::errors::code (0 / NONE means success).
Definition errors.hpp:13
F_void engineError(const F_char *fmt,...)
Log error for Engine.
F_err writeApplicationFile(const std::filesystem::path &directory, const std::string &filename, const std::string &value, bool replaceExisting=true)
Atomically replaces a named application-owned file.
F_err loadButtonStyle(const std::filesystem::path &engineDirectory, const std::string &defaults, std::string &css)
Reads the application button stylesheet, creating it from packaged defaults if absent.
constexpr std::size_t maximumStyleSize
Maximum stylesheet size in bytes.
F_err saveButtonStyle(const std::filesystem::path &engineDirectory, const std::string &css)
Writes the application button stylesheet atomically.
constexpr const char * buttonFile
Editable application button stylesheet name.
const std::filesystem::path buttonDirectory
Relative directory of the editable application button stylesheet.