Fix typo in .memon file saving that circumvented proper utf8 encoding of the path

This commit is contained in:
Stepland 2023-04-02 22:31:15 +02:00
parent 9cd1208366
commit e32ce26b58
14 changed files with 40 additions and 28 deletions

View File

@ -103,13 +103,13 @@ config::Config::Config(const std::filesystem::path& settings) :
}
toml::table tbl;
nowide::ifstream config_stream{to_utf8_encoded_string(config_path)};
nowide::ifstream config_stream{path_to_utf8_encoded_string(config_path)};
try {
tbl = toml::parse(config_stream);
} catch (const toml::parse_error& err) {
fmt::print(
"Error while parsing {} :\n{}",
settings.string(),
path_to_utf8_encoded_string(settings),
err.what()
);
return;
@ -138,7 +138,7 @@ toml::table config::Config::dump_as_v1_0_0() {
config::Config::~Config() {
std::filesystem::create_directories(config_path.parent_path());
const auto tbl = dump_as_v1_0_0();
nowide::ofstream config_file_stream{to_utf8_encoded_string(config_path)};
nowide::ofstream config_file_stream{path_to_utf8_encoded_string(config_path)};
const auto flags = (
toml::toml_formatter::default_flags
& ~toml::format_flags::allow_literal_strings

View File

@ -16,7 +16,10 @@ FakePitchedSoundStream::FakePitchedSoundStream(
sample(std::make_shared<sound_buffer_type>())
{
if (not sample->load_from_path(path_to_sample)) {
throw std::runtime_error(fmt::format("Could not load audio sample : {}", path_to_sample.string()));
throw std::runtime_error(fmt::format(
"Could not load audio sample : {}",
path_to_utf8_encoded_string(path_to_sample)
));
}
finish_initializing_the_sample();
}

View File

@ -28,7 +28,7 @@ OpenMusic::OpenMusic(const std::filesystem::path& filename) :
m_loopSpan(0, 0)
{
if (not openFromFile(filename)) {
throw std::runtime_error("Could not open "+filename.string());
throw std::runtime_error("Could not open " + path_to_utf8_encoded_string(filename));
}
}

View File

@ -1590,17 +1590,23 @@ TimingOrigin EditorState::timing_origin() {
void EditorState::save(const std::filesystem::path& path) {
const auto memon = song.dump_to_memon_1_0_0();
nowide::ofstream file{to_utf8_encoded_string(path.string())};
nowide::ofstream file{path_to_utf8_encoded_string(path)};
if (not file) {
throw std::runtime_error(
fmt::format("Cannot write to file {}", path.string())
fmt::format(
"Cannot write to file {}",
path_to_utf8_encoded_string(path)
)
);
}
file << memon.dump(4) << std::endl;
file.close();
if (not file) {
throw std::runtime_error(
fmt::format("Error while closing file {}", path.string())
fmt::format(
"Error while closing file {}",
path_to_utf8_encoded_string(path)
)
);
}
song_path = path;
@ -1658,11 +1664,14 @@ void feis::open_from_file(
config::Config& config
) {
try {
nowide::ifstream f{to_utf8_encoded_string(song_path)};
nowide::ifstream f{path_to_utf8_encoded_string(song_path)};
if (not f) {
tinyfd_messageBox(
"Error",
fmt::format("Could not open file {}", song_path.string()).c_str(),
fmt::format(
"Could not open file {}",
path_to_utf8_encoded_string(song_path)
).c_str(),
"ok",
"error",
1

View File

@ -44,7 +44,7 @@ const std::string beat_tick_stream = "beat_tick";
*/
class EditorState {
public:
explicit EditorState(
EditorState(
const std::filesystem::path& assets,
config::Config& config
);

View File

@ -73,7 +73,7 @@ std::array<LNMarker::texture_type, number> load_tex_with_prefix(
if (not tex.load_from_path(texFile)) {
throw std::runtime_error(fmt::format(
"Unable to load texture folder {}, failed on texture {}",
folder.string(),
path_to_utf8_encoded_string(folder),
filename
));
}

View File

@ -54,7 +54,7 @@ int main() {
if (not std::filesystem::exists(font_path)) {
tinyfd_messageBox(
"Error",
("Could not open "+to_utf8_encoded_string(font_path)).c_str(),
("Could not open "+path_to_utf8_encoded_string(font_path)).c_str(),
"ok",
"error",
1
@ -64,7 +64,7 @@ int main() {
ImGuiIO& IO = ImGui::GetIO();
IO.Fonts->Clear();
IO.Fonts->AddFontFromFileTTF(
to_utf8_encoded_string(assets_folder / "fonts" / "NotoSans-Medium.ttf").c_str(),
path_to_utf8_encoded_string(assets_folder / "fonts" / "NotoSans-Medium.ttf").c_str(),
16.f
);
ImGui::SFML::UpdateFontTexture();

View File

@ -26,7 +26,7 @@ Marker::Marker(const std::filesystem::path& folder_):
if (not tex.load_from_path(path)) {
throw std::runtime_error(fmt::format(
"Unable to load marker {} - failed on image {}",
folder.string(),
path_to_utf8_encoded_string(folder),
file
));
} else {

View File

@ -11,7 +11,7 @@ Playfield::Playfield(std::filesystem::path assets_folder) :
{
if (!base_texture.load_from_path(texture_path)) {
std::cerr << "Unable to load texture " << texture_path;
throw std::runtime_error("Unable to load texture " + texture_path.string());
throw std::runtime_error("Unable to load texture " + path_to_utf8_encoded_string(texture_path);
}
base_texture.setSmooth(true);

View File

@ -18,7 +18,7 @@ const std::string recent_files_file = "recent files.txt";
void Toolbox::pushNewRecentFile(std::filesystem::path file, std::filesystem::path settings) {
std::filesystem::create_directory(settings);
auto recent_files_path = settings / recent_files_file;
nowide::ifstream readFile(to_utf8_encoded_string(recent_files_path));
nowide::ifstream readFile(path_to_utf8_encoded_string(recent_files_path));
std::list<std::string> recent;
std::set<std::string> recent_set;
for (std::string line; getline(readFile, line);) {
@ -29,16 +29,16 @@ void Toolbox::pushNewRecentFile(std::filesystem::path file, std::filesystem::pat
}
readFile.close();
recent.remove(to_utf8_encoded_string(file));
recent.remove(path_to_utf8_encoded_string(file));
while (recent.size() >= 10) {
recent.pop_back();
}
recent.push_front(to_utf8_encoded_string(file));
recent.push_front(path_to_utf8_encoded_string(file));
nowide::ofstream writeFile(
to_utf8_encoded_string(recent_files_path),
path_to_utf8_encoded_string(recent_files_path),
std::ofstream::out | std::ofstream::trunc
);
for (const auto& line : recent) {
@ -48,7 +48,7 @@ void Toolbox::pushNewRecentFile(std::filesystem::path file, std::filesystem::pat
}
std::vector<std::string> Toolbox::getRecentFiles(std::filesystem::path settings) {
nowide::ifstream readFile{to_utf8_encoded_string(settings / recent_files_file)};
nowide::ifstream readFile{path_to_utf8_encoded_string(settings / recent_files_file)};
std::vector<std::string> recent;
for (std::string line; getline(readFile, line);) {
recent.push_back(line);

View File

@ -15,7 +15,7 @@ void feis::UTF8FileInputStream::FileCloser::operator()(std::FILE* file) {
}
bool feis::UTF8FileInputStream::open(const std::filesystem::path& filename) {
m_file.reset(nowide::fopen(to_utf8_encoded_string(filename).c_str(), "rb"));
m_file.reset(nowide::fopen(path_to_utf8_encoded_string(filename).c_str(), "rb"));
return m_file != nullptr;
}

View File

@ -2,13 +2,13 @@
#include <cstring>
std::string to_utf8_encoded_string(const std::u8string& u8s) {
std::string u8string_to_utf8_encoded_string(const std::u8string& u8s) {
std::string result{u8s.cbegin(), u8s.cend()};
return result;
}
std::string to_utf8_encoded_string(const std::filesystem::path& path) {
return to_utf8_encoded_string(path.u8string());
std::string path_to_utf8_encoded_string(const std::filesystem::path& path) {
return u8string_to_utf8_encoded_string(path.u8string());
}
std::u8string to_u8string(const std::string& utf8s) {

View File

@ -1,7 +1,7 @@
#include <filesystem>
#include <string>
std::string to_utf8_encoded_string(const std::u8string& u8s);
std::string to_utf8_encoded_string(const std::filesystem::path& path);
std::string u8string_to_utf8_encoded_string(const std::u8string& u8s);
std::string path_to_utf8_encoded_string(const std::filesystem::path& path);
std::u8string to_u8string(const std::string& utf8s);
std::filesystem::path to_path(const std::string& utf8s);

View File

@ -12,7 +12,7 @@ DensityGraph::DensityGraph(std::filesystem::path assets, const config::Config& c
{
if (!base_texture.load_from_path(texture_path)) {
std::cerr << "Unable to load texture " << texture_path;
throw std::runtime_error("Unable to load texture " + texture_path.string());
throw std::runtime_error("Unable to load texture " + path_to_utf8_encoded_string(texture_path));
}
base_texture.setSmooth(true);