1
0
mirror of synced 2025-01-22 11:23:48 +01:00

Use std::filesystem

This commit is contained in:
Stepland 2023-08-05 15:02:45 +02:00
parent 3d6f46be6e
commit bc6b6fcf07
36 changed files with 278 additions and 28355 deletions

File diff suppressed because it is too large Load Diff

View File

@ -1,38 +0,0 @@
//---------------------------------------------------------------------------------------
//
// ghc::filesystem - A C++17-like filesystem implementation for C++11/C++14
//
//---------------------------------------------------------------------------------------
//
// Copyright (c) 2018, Steffen Schümann <s.schuemann@pobox.com>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
//---------------------------------------------------------------------------------------
// fs_fwd.hpp - The forwarding header for the header/implementation seperated usage of
// ghc::filesystem.
// This file can be include at any place, where ghc::filesystem api is needed while
// not bleeding implementation details (e.g. system includes) into the global namespace,
// as long as one cpp includes fs_impl.hpp to deliver the matching implementations.
//---------------------------------------------------------------------------------------
#ifndef GHC_FILESYSTEM_FWD_H
#define GHC_FILESYSTEM_FWD_H
#define GHC_FILESYSTEM_FWD
#include <ghc/filesystem.hpp>
#endif // GHC_FILESYSTEM_FWD_H

View File

@ -1,35 +0,0 @@
//---------------------------------------------------------------------------------------
//
// ghc::filesystem - A C++17-like filesystem implementation for C++11/C++14
//
//---------------------------------------------------------------------------------------
//
// Copyright (c) 2018, Steffen Schümann <s.schuemann@pobox.com>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
//---------------------------------------------------------------------------------------
// fs_impl.hpp - The implementation header for the header/implementation seperated usage of
// ghc::filesystem.
// This file can be used to hide the implementation of ghc::filesystem into a single cpp.
// The cpp has to include this before including fs_fwd.hpp directly or via a different
// header to work.
//---------------------------------------------------------------------------------------
#define GHC_FILESYSTEM_IMPLEMENTATION
#include <ghc/filesystem.hpp>

View File

@ -1,56 +0,0 @@
//---------------------------------------------------------------------------------------
//
// ghc::filesystem - A C++17-like filesystem implementation for C++11/C++14
//
//---------------------------------------------------------------------------------------
//
// Copyright (c) 2018, Steffen Schümann <s.schuemann@pobox.com>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
//---------------------------------------------------------------------------------------
// fs_std.hpp - The dynamic switching header that includes std::filesystem if detected
// or ghc::filesystem if not, and makes the resulting API available in the
// namespace fs.
//---------------------------------------------------------------------------------------
#ifndef GHC_FILESYSTEM_STD_H
#if defined(__cplusplus) && __cplusplus >= 201703L && defined(__has_include)
#if __has_include(<filesystem>)
#define GHC_USE_STD_FS
#include <filesystem>
namespace fs {
using namespace std::filesystem;
using ifstream = std::ifstream;
using ofstream = std::ofstream;
using fstream = std::fstream;
}
#endif
#endif
#ifndef GHC_USE_STD_FS
#define GHC_WIN_WSTRING_STRING_TYPE
#include <ghc/filesystem.hpp>
namespace fs {
using namespace ghc::filesystem;
using ifstream = ghc::filesystem::ifstream;
using ofstream = ghc::filesystem::ofstream;
using fstream = ghc::filesystem::fstream;
}
#endif
#endif // GHC_FILESYSTEM_STD_H

View File

@ -1,60 +0,0 @@
//---------------------------------------------------------------------------------------
//
// ghc::filesystem - A C++17-like filesystem implementation for C++11/C++14
//
//---------------------------------------------------------------------------------------
//
// Copyright (c) 2018, Steffen Schümann <s.schuemann@pobox.com>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
//---------------------------------------------------------------------------------------
// fs_std_fwd.hpp - The forwarding header for the header/implementation seperated usage of
// ghc::filesystem that uses std::filesystem if it detects it.
// This file can be include at any place, where fs::filesystem api is needed while
// not bleeding implementation details (e.g. system includes) into the global namespace,
// as long as one cpp includes fs_std_impl.hpp to deliver the matching implementations.
//---------------------------------------------------------------------------------------
#ifndef GHC_FILESYSTEM_STD_FWD_H
#define GHC_FILESYSTEM_STD_FWD_H
#if defined(__cplusplus) && __cplusplus >= 201703L && defined(__has_include)
#if __has_include(<filesystem>)
#define GHC_USE_STD_FS
#include <filesystem>
namespace fs {
using namespace std::filesystem;
using ifstream = std::ifstream;
using ofstream = std::ofstream;
using fstream = std::fstream;
}
#endif
#endif
#ifndef GHC_USE_STD_FS
#define GHC_WIN_WSTRING_STRING_TYPE
#define GHC_FILESYSTEM_FWD
#include <ghc/filesystem.hpp>
namespace fs {
using namespace ghc::filesystem;
using ifstream = ghc::filesystem::ifstream;
using ofstream = ghc::filesystem::ofstream;
using fstream = ghc::filesystem::fstream;
}
#endif
#endif // GHC_FILESYSTEM_STD_FWD_H

View File

@ -1,43 +0,0 @@
//---------------------------------------------------------------------------------------
//
// ghc::filesystem - A C++17-like filesystem implementation for C++11/C++14
//
//---------------------------------------------------------------------------------------
//
// Copyright (c) 2018, Steffen Schümann <s.schuemann@pobox.com>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
//---------------------------------------------------------------------------------------
// fs_std_impl.hpp - The implementation header for the header/implementation seperated usage of
// ghc::filesystem that does nothing if std::filesystem is detected.
// This file can be used to hide the implementation of ghc::filesystem into a single cpp.
// The cpp has to include this before including fs_std_fwd.hpp directly or via a different
// header to work.
//---------------------------------------------------------------------------------------
#if defined(__cplusplus) && __cplusplus >= 201703L && defined(__has_include)
#if __has_include(<filesystem>)
#define GHC_USE_STD_FS
#endif
#endif
#ifndef GHC_USE_STD_FS
#define GHC_WIN_WSTRING_STRING_TYPE
#define GHC_FILESYSTEM_IMPLEMENTATION
#include <ghc/filesystem.hpp>
#endif

File diff suppressed because it is too large Load Diff

View File

@ -19,7 +19,9 @@ dependencies = [
dependency('sfml-audio', version : '>=2.5.1'), dependency('sfml-audio', version : '>=2.5.1'),
dependency('threads'), dependency('threads'),
dependency('gl'), dependency('gl'),
cc.find_library('atomic') cc.find_library('atomic'),
dependency('nowide'),
dependency('nlohmann_json')
] ]
if host_machine.system() == 'linux' if host_machine.system() == 'linux'

View File

@ -66,7 +66,7 @@ namespace Data {
} }
// RAII style class which loads preferences from the dedicated file when constructed and saves them when destructed // RAII style class which loads preferences from the dedicated file when constructed and saves them when destructed
Preferences::Preferences(const ghc::filesystem::path& t_jujube_path) : Preferences::Preferences(const std::filesystem::path& t_jujube_path) :
screen(), screen(),
layout(), layout(),
options(), options(),
@ -74,7 +74,7 @@ namespace Data {
jujube_path(t_jujube_path) jujube_path(t_jujube_path)
{ {
auto path = jujube_path/"data"/"preferences.json"; auto path = jujube_path/"data"/"preferences.json";
if (ghc::filesystem::exists(path)) { if (std::filesystem::exists(path)) {
std::ifstream prefs_file; std::ifstream prefs_file;
prefs_file.open(path); prefs_file.open(path);
nlohmann::json j; nlohmann::json j;
@ -92,10 +92,10 @@ namespace Data {
Preferences::~Preferences() { Preferences::~Preferences() {
auto data_folder = jujube_path/"data"; auto data_folder = jujube_path/"data";
if (not ghc::filesystem::exists(data_folder)) { if (not std::filesystem::exists(data_folder)) {
ghc::filesystem::create_directory(data_folder); std::filesystem::create_directory(data_folder);
} }
if (not ghc::filesystem::is_directory(data_folder)) { if (not std::filesystem::is_directory(data_folder)) {
std::cerr << "Can't create data folder to save preferences, a file named 'data' exists" << '\n'; std::cerr << "Can't create data folder to save preferences, a file named 'data' exists" << '\n';
} }
std::ofstream preferences_file; std::ofstream preferences_file;

View File

@ -1,8 +1,8 @@
#pragma once #pragma once
#include <cstddef> #include <cstddef>
#include <filesystem>
#include <ghc/filesystem.hpp>
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>
#include <SFML/System.hpp> #include <SFML/System.hpp>
@ -74,9 +74,9 @@ namespace Data {
Layout layout; Layout layout;
Options options; Options options;
Input::KeyMapping key_mapping; Input::KeyMapping key_mapping;
ghc::filesystem::path jujube_path; std::filesystem::path jujube_path;
Preferences(const ghc::filesystem::path& t_jujube_path); Preferences(const std::filesystem::path& t_jujube_path);
~Preferences(); ~Preferences();
}; };

View File

@ -8,7 +8,9 @@
#include <memon/memon.hpp> #include <memon/memon.hpp>
namespace fs = ghc::filesystem; #include "../Toolkit/UTF8SFMLRedefinitions.hpp"
namespace fs = std::filesystem;
namespace Data { namespace Data {
@ -105,8 +107,8 @@ namespace Data {
} }
TimeBounds time_bounds = {sf::Time::Zero, sf::seconds(1)}; TimeBounds time_bounds = {sf::Time::Zero, sf::seconds(1)};
time_bounds += chart->get_time_bounds_from_notes(); time_bounds += chart->get_time_bounds_from_notes();
sf::Music m; jujube::Music m;
if (m.openFromFile(*song.full_audio_path())) { if (m.open_from_path(*song.full_audio_path())) {
time_bounds += {sf::Time::Zero, m.getDuration()}; time_bounds += {sf::Time::Zero, m.getDuration()};
} }
time_bounds.end += sf::seconds(1); time_bounds.end += sf::seconds(1);

View File

@ -1,21 +1,22 @@
#pragma once #pragma once
#include <filesystem>
#include <iterator> #include <iterator>
#include <list> #include <list>
#include <map> #include <map>
#include <memory> #include <memory>
#include <optional> #include <optional>
#include <string> #include <string>
#include <variant>
#include <unordered_map> #include <unordered_map>
#include <variant>
#include <ghc/filesystem.hpp>
#include <SFML/Audio.hpp> #include <SFML/Audio.hpp>
#include "Chart.hpp" #include "Chart.hpp"
#include "TimeBounds.hpp" #include "TimeBounds.hpp"
namespace fs = ghc::filesystem; namespace fs = std::filesystem;
namespace Data { namespace Data {

View File

@ -21,7 +21,7 @@
#include <X11/Xlib.h> #include <X11/Xlib.h>
#endif #endif
int main(int, char const **) { int main() {
#if defined(__unix__) && defined(__linux__) #if defined(__unix__) && defined(__linux__)
XInitThreads(); XInitThreads();

View File

@ -5,7 +5,7 @@
#include <sstream> #include <sstream>
#include <stdexcept> #include <stdexcept>
namespace fs = ghc::filesystem; namespace fs = std::filesystem;
sf::Sprite Textures::CoverAltas::at(const fs::path& path) const { sf::Sprite Textures::CoverAltas::at(const fs::path& path) const {

View File

@ -1,6 +1,6 @@
#pragma once #pragma once
#include <ghc/filesystem.hpp> #include <filesystem>
#include <memory> #include <memory>
#include <string> #include <string>
#include <unordered_map> #include <unordered_map>
@ -8,8 +8,6 @@
#include <SFML/Graphics.hpp> #include <SFML/Graphics.hpp>
#include "../Toolkit/GHCFilesystemPathHash.hpp"
namespace Textures { namespace Textures {
/* /*

View File

@ -1,16 +1,17 @@
#pragma once #pragma once
#include <filesystem>
#include <string> #include <string>
#include <map> #include <map>
#include <optional> #include <optional>
#include <ghc/filesystem.hpp>
#include <SFML/Graphics.hpp> #include <SFML/Graphics.hpp>
#include "SpriteSheet.hpp" #include "SpriteSheet.hpp"
#include "SplitSpriteSheet.hpp" #include "SplitSpriteSheet.hpp"
namespace fs = ghc::filesystem; namespace fs = std::filesystem;
namespace Resources { namespace Resources {
struct LNMarker { struct LNMarker {

View File

@ -8,7 +8,7 @@
#include <sstream> #include <sstream>
#include <stdexcept> #include <stdexcept>
namespace fs = ghc::filesystem; namespace fs = std::filesystem;
namespace Resources { namespace Resources {

View File

@ -1,16 +1,16 @@
#pragma once #pragma once
#include <filesystem>
#include <string> #include <string>
#include <map> #include <map>
#include <optional> #include <optional>
#include <ghc/filesystem.hpp>
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>
#include <SFML/Graphics.hpp> #include <SFML/Graphics.hpp>
#include "SpriteSheet.hpp" #include "SpriteSheet.hpp"
namespace fs = ghc::filesystem; namespace fs = std::filesystem;
namespace Resources { namespace Resources {
enum class MarkerAnimation { enum class MarkerAnimation {

View File

@ -6,7 +6,7 @@
namespace Resources { namespace Resources {
FallbackFont::FallbackFont(const ghc::filesystem::path& jujube_path) : FallbackFont::FallbackFont(const std::filesystem::path& jujube_path) :
light(), light(),
medium(), medium(),
black() black()

View File

@ -1,10 +1,10 @@
#pragma once #pragma once
#include <cstddef> #include <cstddef>
#include <filesystem>
#include <functional> #include <functional>
#include <optional> #include <optional>
#include <ghc/filesystem.hpp>
#include <SFML/Graphics.hpp> #include <SFML/Graphics.hpp>
#include <SFML/Graphics/Font.hpp> #include <SFML/Graphics/Font.hpp>
#include <SFML/System.hpp> #include <SFML/System.hpp>
@ -32,7 +32,7 @@ namespace Resources {
}; };
struct FallbackFont { struct FallbackFont {
FallbackFont(const ghc::filesystem::path& jujube_path); FallbackFont(const std::filesystem::path& jujube_path);
sf::Font light; sf::Font light;
sf::Font medium; sf::Font medium;
sf::Font black; sf::Font black;

View File

@ -1,15 +1,15 @@
#pragma once #pragma once
#include <filesystem>
#include <optional> #include <optional>
#include <ghc/filesystem.hpp>
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>
#include <SFML/Graphics/Texture.hpp> #include <SFML/Graphics/Texture.hpp>
#include <SFML/Graphics/Sprite.hpp> #include <SFML/Graphics/Sprite.hpp>
#include "../Toolkit/DurationInFrames.hpp" #include "../Toolkit/DurationInFrames.hpp"
namespace fs = ghc::filesystem; namespace fs = std::filesystem;
namespace Resources { namespace Resources {
// SpriteSheet where individual sprites get their own texture // SpriteSheet where individual sprites get their own texture

View File

@ -1,15 +1,15 @@
#pragma once #pragma once
#include <filesystem>
#include <optional> #include <optional>
#include <ghc/filesystem.hpp>
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>
#include <SFML/Graphics/Texture.hpp> #include <SFML/Graphics/Texture.hpp>
#include <SFML/Graphics/Sprite.hpp> #include <SFML/Graphics/Sprite.hpp>
#include "../Toolkit/DurationInFrames.hpp" #include "../Toolkit/DurationInFrames.hpp"
namespace fs = ghc::filesystem; namespace fs = std::filesystem;
namespace Resources { namespace Resources {
struct SpriteSheet { struct SpriteSheet {

View File

@ -3,7 +3,7 @@
#include <stdexcept> #include <stdexcept>
namespace Textures { namespace Textures {
AutoloadedTexture load_texture_from_path(const fs::path& path) { AutoloadedTexture load_texture_from_path(const std::filesystem::path& path) {
auto texture = std::make_shared<sf::Texture>(); auto texture = std::make_shared<sf::Texture>();
if (!texture->loadFromFile(path.string())) { if (!texture->loadFromFile(path.string())) {
throw std::invalid_argument("Unable to load cover image : "+path.string()); throw std::invalid_argument("Unable to load cover image : "+path.string());

View File

@ -1,12 +1,11 @@
#pragma once #pragma once
#include <filesystem>
#include <memory> #include <memory>
#include <ghc/filesystem.hpp>
#include <SFML/Graphics.hpp> #include <SFML/Graphics.hpp>
#include "../Toolkit/Cache.hpp" #include "../Toolkit/Cache.hpp"
#include "../Toolkit/GHCFilesystemPathHash.hpp"
namespace Textures { namespace Textures {
@ -17,7 +16,7 @@ namespace Textures {
sf::Clock loaded_since; sf::Clock loaded_since;
}; };
AutoloadedTexture load_texture_from_path(const fs::path& path); AutoloadedTexture load_texture_from_path(const std::filesystem::path& path);
using TextureCache = Toolkit::Cache<fs::path, AutoloadedTexture, &load_texture_from_path>; using TextureCache = Toolkit::Cache<std::filesystem::path, AutoloadedTexture, &load_texture_from_path>;
} }

View File

@ -1,15 +1,15 @@
#pragma once #pragma once
#include <filesystem>
#include <memory> #include <memory>
#include <mutex> #include <mutex>
#include <optional> #include <optional>
#include <ghc/filesystem.hpp>
#include <SFML/Audio.hpp> #include <SFML/Audio.hpp>
#include "../../Toolkit/AffineTransform.hpp" #include "../../Toolkit/AffineTransform.hpp"
namespace fs = ghc::filesystem; namespace fs = std::filesystem;
// In this file define stuff to try to handle creating sf::Music objects // In this file define stuff to try to handle creating sf::Music objects
// and starting their playback asynchronously while trying to keep some thread-safety // and starting their playback asynchronously while trying to keep some thread-safety

View File

@ -1,17 +0,0 @@
#pragma once
#include <functional>
#include <ghc/filesystem.hpp>
namespace fs = ghc::filesystem;
// Define the way we hash fs::path for use in unordered maps
namespace std {
template <>
struct hash<fs::path> {
std::size_t operator()(const fs::path& p) const {
return std::hash<std::string>()(p.string());
}
};
}

View File

@ -0,0 +1,62 @@
/* Copied from src/SFML/System/UTF8FileInputStream.cpp */
#include "UTF8FileInputStream.hpp"
#include <cstddef>
#include <memory>
#include <nowide/cstdio.hpp>
#include "UTF8Strings.hpp"
void jujube::UTF8FileInputStream::FileCloser::operator()(std::FILE* file) {
std::fclose(file);
}
bool jujube::UTF8FileInputStream::open(const std::filesystem::path& filename) {
m_file.reset(nowide::fopen(path_to_utf8_encoded_string(filename).c_str(), "rb"));
return m_file != nullptr;
}
sf::Int64 jujube::UTF8FileInputStream::read(void* data, sf::Int64 size) {
if (m_file) {
return static_cast<sf::Int64>(std::fread(data, 1, static_cast<std::size_t>(size), m_file.get()));
} else {
return -1;
}
}
sf::Int64 jujube::UTF8FileInputStream::seek(sf::Int64 position) {
if (m_file) {
if (std::fseek(m_file.get(), static_cast<long>(position), SEEK_SET)) {
return -1;
}
return tell();
} else {
return -1;
}
}
sf::Int64 jujube::UTF8FileInputStream::tell() {
if (m_file) {
return std::ftell(m_file.get());
} else {
return -1;
}
}
sf::Int64 jujube::UTF8FileInputStream::getSize() {
if (m_file) {
sf::Int64 position = tell();
std::fseek(m_file.get(), 0, SEEK_END);
sf::Int64 size = tell();
if (seek(position) == -1) {
return -1;
}
return size;
} else {
return -1;
}
}

View File

@ -0,0 +1,31 @@
/*
Custom version of FileInputStream that adds an .open() overload that works with
non-ascii paths
*/
#pragma once
#include <cstdio>
#include <filesystem>
#include <memory>
#include <string>
#include <SFML/Config.hpp>
#include <SFML/System/Export.hpp>
#include <SFML/System/InputStream.hpp>
namespace jujube {
class UTF8FileInputStream : public sf::InputStream {
public:
[[nodiscard]] bool open(const std::filesystem::path& filename);
[[nodiscard]] sf::Int64 read(void* data, sf::Int64 size) override;
[[nodiscard]] sf::Int64 seek(sf::Int64 position) override;
[[nodiscard]] sf::Int64 tell() override;
sf::Int64 getSize() override;
private:
struct FileCloser {
void operator()(std::FILE* file);
};
std::unique_ptr<std::FILE, FileCloser> m_file;
};
}

69
src/Toolkit/UTF8SFML.hpp Normal file
View File

@ -0,0 +1,69 @@
/* UTF8-aware load_from_file functions for SFML objects,
uses nowide under the hood */
#pragma once
#include <filesystem>
#include <memory>
#include <optional>
#include <vector>
#include <nowide/fstream.hpp>
#include "UTF8FileInputStream.hpp"
namespace jujube {
/* UTF8-aware wrapper around SFML resource classes that "load" files, i.e.
resource classes that read the whole file at once when calling
load_from_file() and thus don't need the file stream to remain available
after the call to load_from_file() */
template<class T>
class UTF8Loader : public T {
public:
template<typename... Ts>
bool load_from_path(const std::filesystem::path& file, const Ts&... args) {
UTF8FileInputStream f;
if (not f.open(file)) {
return false;
}
return this->loadFromStream(f, args...);
}
};
class HoldsFileStream {
public:
virtual ~HoldsFileStream() = default;
protected:
UTF8FileInputStream file_stream;
};
/* UTF8-aware wrapper around SFML resource classes that "open" files, i.e.
resource classes that just store the file stream when open_from_path() is
called and stream the file contents on demand and hence require the file
stream to remain available for the whole lifetime of the resource */
template<class T>
class UTF8Streamer : public HoldsFileStream, public T {
/* The file_stream is kept in a base class to make sure it is destroyed
AFTER the SFML class that uses it */
public:
bool open_from_path(const std::filesystem::path& file) {
if (not file_stream.open(file)) {
return false;
};
return this->openFromStream(file_stream);
}
};
/* UTF8-aware wrapper around SFML resource classes that stream file contents
continously during their lifetime files but somehow still use loadFrom*
methods instead of openFrom* */
template<class T>
class UTF8StreamerUsingLoadFrom : public HoldsFileStream, public T {
public:
bool load_from_path(const std::filesystem::path& file) {
if (not file_stream.open(file)) {
return false;
};
return this->loadFromStream(file_stream);
}
};
}

View File

@ -0,0 +1,19 @@
#pragma once
#include <SFML/Audio.hpp>
#include <SFML/Audio/InputSoundFile.hpp>
#include <SFML/Audio/SoundBuffer.hpp>
#include <SFML/Graphics/Font.hpp>
#include <SFML/Graphics/Shader.hpp>
#include <SFML/Graphics/Texture.hpp>
#include "UTF8SFML.hpp"
namespace jujube {
using Music = jujube::UTF8Streamer<sf::Music>;
using InputSoundFile = jujube::UTF8Streamer<sf::InputSoundFile>;
using SoundBuffer = jujube::UTF8Loader<sf::SoundBuffer>;
using Texture = jujube::UTF8Loader<sf::Texture>;
using Shader = jujube::UTF8Loader<sf::Shader>;
using Font = jujube::UTF8StreamerUsingLoadFrom<sf::Font>;
}

View File

@ -0,0 +1,21 @@
#include "UTF8Strings.hpp"
#include <cstring>
std::string u8string_to_utf8_encoded_string(const std::u8string& u8s) {
std::string result{u8s.cbegin(), u8s.cend()};
return result;
}
std::string path_to_utf8_encoded_string(const std::filesystem::path& path) {
return u8string_to_utf8_encoded_string(path.u8string());
}
std::u8string utf8_encoded_string_to_u8string(const std::string& utf8s) {
std::u8string result{utf8s.cbegin(), utf8s.cend()};
return result;
}
std::filesystem::path utf8_encoded_string_to_path(const std::string& utf8s) {
return std::filesystem::path{utf8_encoded_string_to_u8string(utf8s)};
}

View File

@ -0,0 +1,7 @@
#include <filesystem>
#include <string>
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 utf8_encoded_string_to_u8string(const std::string& utf8s);
std::filesystem::path utf8_encoded_string_to_path(const std::string& utf8s);

View File

@ -2,5 +2,7 @@ sources += files(
'EasingFunctions.cpp', 'EasingFunctions.cpp',
'HSL.cpp', 'HSL.cpp',
'SFMLHelpers.cpp', 'SFMLHelpers.cpp',
'QuickRNG.cpp' 'QuickRNG.cpp',
'UTF8FileInputStream.cpp',
'UTF8Strings.cpp',
) )

2
subprojects/.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
/*/
!packagefiles/

View File

@ -0,0 +1,10 @@
[wrap-file]
directory = nlohmann_json-3.11.2
lead_directory_missing = true
source_url = https://github.com/nlohmann/json/releases/download/v3.11.2/include.zip
source_filename = nlohmann_json-3.11.2.zip
source_hash = e5c7a9f49a16814be27e4ed0ee900ecd0092bfb7dbfca65b5a421b774dccaaed
wrapdb_version = 3.11.2-1
[provide]
nlohmann_json = nlohmann_json_dep

12
subprojects/nowide.wrap Normal file
View File

@ -0,0 +1,12 @@
[wrap-file]
directory = nowide_standalone_v11.2.0
source_url = https://github.com/boostorg/nowide/releases/download/v11.2.0/nowide_standalone_v11.2.0.tar.gz
source_filename = nowide_standalone_v11.2.0.tar.gz
source_hash = 1869d176a8af389e4f7416f42bdd15d6a5db3c6e4ae77269ecb071a232304e1d
patch_filename = nowide_11.2.0-1_patch.zip
patch_url = https://wrapdb.mesonbuild.com/v2/nowide_11.2.0-1/get_patch
patch_hash = 0c4b4c00e3fd1ad8db710633ad259371abc88e3b76657c6cf92e55beb6baeffb
wrapdb_version = 11.2.0-1
[provide]
nowide = nowide_dep