Skip to content

Commit

Permalink
Use filesystem::path; List fonts in FontPicker
Browse files Browse the repository at this point in the history
  • Loading branch information
nieznanysprawiciel committed Nov 15, 2024
1 parent 0129382 commit ea8be70
Show file tree
Hide file tree
Showing 12 changed files with 108 additions and 111 deletions.
17 changes: 17 additions & 0 deletions swCommonLib/System/Dir.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,21 @@ bool Dir::CreateDirectory ( const filesystem::Path& path )
return std::filesystem::create_directories( path.String() );
}

// ================================ //

std::vector< Path > Dir::ListFiles( const filesystem::Path& path )
{
if( std::filesystem::exists( path.String() ) )
{
std::vector< Path > files;
for( auto const& dir_entry : std::filesystem::directory_iterator{ path.GetStdPath() } )
{
if( dir_entry.is_regular_file() )
files.push_back( filesystem::Path( dir_entry.path() ) );
}
return files;
}
return std::vector< Path >();
}

}
5 changes: 3 additions & 2 deletions swCommonLib/System/Dir.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

#include "Path.h"


#include <vector>

namespace filesystem
{
Expand All @@ -23,7 +23,8 @@ class Dir
~Dir() = default;


static bool CreateDirectory ( const filesystem::Path& path );
static bool CreateDirectory ( const filesystem::Path& path );
static std::vector< Path > ListFiles ( const filesystem::Path& path );
};


Expand Down
145 changes: 45 additions & 100 deletions swCommonLib/System/Path.h
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
#pragma once
/**
@file Path.h
@author nieznanysprawiciel
@copyright Plik jest czêœci¹ silnika graficznego SWEngine.
@author
@copyright This file is part of the SWEngine graphics engine.
Implementacja klasy Path. Poniewa¿ Path nie znajduje siê jeszcze w standardzie c++, ale
zostanie do niego w³¹czone dopiero wraz z c++17, to potrzebny jest ten wrapper.
Na razie implementacje dla ró¿nych kompilatorów umieszczaj¹ tê klasê w ró¿nych namespacach,
wiêc nie da siê tego zaimplementowac w pe³ni przenoœnie.
*/
Implementation of the Path class. Since Path is not yet part of the C++ standard,
but will be included with C++17, this wrapper is needed.
Currently, implementations for different compilers place this class in different namespaces,
so it cannot be implemented fully portably.*/


// Supress deprecation warning. We nned to remember to use
// replacement in future when it will be introduced in standard.
#define _SILENCE_CXX17_CODECVT_HEADER_DEPRECATION_WARNING


//#include <filesystem>
#include "Filesystem/filesystem/path.h"
#include <filesystem>
//#include "Filesystem/filesystem/path.h"

#include <string>
#include <codecvt>
Expand All @@ -29,12 +28,10 @@
namespace filesystem
{

//namespace experimental = std::tr2::sys;

class Path
{
private:
path_impl m_path;
std::filesystem::path m_path;

public:

Expand All @@ -57,7 +54,6 @@ class Path
bool operator<= ( const Path& other ) const;
bool operator> ( const Path& other ) const;
bool operator>= ( const Path& other ) const;
//bool Compare ( const Path& path1, const Path& path2 );

friend Path operator/ ( const Path& path1, const Path& path2 );

Expand All @@ -73,7 +69,7 @@ class Path
void Normalize ();
void MakeAbsolute ();
Path ChangeExtension ( const std::string& extension ) const;

Path RelativeTo ( const Path& base ) const;

//bool HasRoot () const;
bool HasFileName () const;
Expand All @@ -94,18 +90,15 @@ class Path
public:

///@note This function may change or disapear in future.
const std::vector< std::string >& GetTokens () const;

Path ClipFromRoot ( int num ) const;
std::vector< std::string > GetTokens() const;
const std::filesystem::path GetStdPath() const;
};


// ================================ //
//
template< class Source >
inline Path::Path ( const Source& source )
: m_path( path_impl( source ) )
{}
inline Path::Path ( const Source& source ) : m_path( std::filesystem::path( source ) ) {}

// ================================ //
//
Expand All @@ -120,7 +113,7 @@ inline Path::Path ( const std::wstring& path )
//std::wstring_convert< ConvertType, wchar_t > converter;
//auto pathStr = converter.to_bytes( path );

m_path = path_impl( path );
m_path = std::filesystem::path( path );
}

// ================================ //
Expand Down Expand Up @@ -165,59 +158,52 @@ inline bool Path::operator!= ( const Path& other ) const
//
inline bool Path::operator< ( const Path& other ) const
{
return m_path.str() < other.m_path.str();
return m_path < other.m_path;
}

// ================================ //
//
inline bool Path::operator<= ( const Path& other ) const
{
return m_path.str() <= other.m_path.str();
return m_path <= other.m_path;
}

// ================================ //
//
inline bool Path::operator> ( const Path& other ) const
{
return m_path.str() > other.m_path.str();
return m_path > other.m_path;
}

// ================================ //
//
inline bool Path::operator>= ( const Path& other ) const
{
return m_path.str() >= other.m_path.str();
return m_path >= other.m_path;
}

///**@brief Porównuje œcie¿ki. Przed porównaniem œcie¿ki s¹ normalizowane.
//Paths must exist on file system.*/
//inline bool Path::Compare ( const Path& path1, const Path& path2 )
//{
// return experimental::equivalent( path1.m_path, path2.m_path );
//}

/**@brief */
inline std::string Path::String() const
{
return m_path.str();
return m_path.string();
}

/**@brief */
inline std::wstring Path::WString() const
{
return m_path.wstr();
return m_path.wstring();
}

/**@brief */
inline std::string Path::GetFileName() const
{
return m_path.filename();
return m_path.filename().string();
}

/**@brief */
inline std::string Path::GetExtension() const
{
return m_path.extension();
return m_path.extension().string();
}

/**@brief */
Expand All @@ -236,75 +222,37 @@ inline Path Path::GetDirectory() const
}

/**@brief Normalizes path.
@attention Obecna implementacja mo¿e nie dzia³aæ do koñca.*/
@attention The current implementation may not work completely.*/
inline void Path::Normalize()
{
auto& tokens = m_path.get_tokens();
std::string normalized = "";

// Avoid reallocating string all the time.
size_t maxLen = 0;
for( auto& token : tokens )
maxLen += token.size();

normalized.reserve( maxLen );
normalized = *tokens.crbegin();

size_t tokensToOmit = 0;
for( auto i = ++tokens.crbegin(); i != tokens.crend(); ++i )
{
if( *i == ".." )
{
tokensToOmit++;
}
else
{
if( tokensToOmit == 0 )
{
// Add token to path from back to beginning.
normalized = *i + "/" + normalized;
}
else
{
tokensToOmit--;
}
}
}

// Add all .. signs that remained to the beginning.
while( tokensToOmit > 0 )
{
normalized = "../" + normalized;
tokensToOmit--;
}

m_path = path_impl( normalized );
m_path = m_path.lexically_normal();
}

// ================================ //
//
inline void Path::MakeAbsolute ()
{
m_path.make_absolute();
m_path = std::filesystem::absolute( m_path );
}

// ================================ //
//
inline Path Path::ChangeExtension( const std::string& extension ) const
{
auto ext = m_path.extension();
auto filename = m_path.filename();
auto filename = m_path.filename().string();

if( filename == "." || filename == ".." )
return Path( *this );

auto startPos = filename.rfind( ext );
if( startPos != std::string::npos )
{
filename.replace( startPos, filename.length(), extension );
return m_path.parent_path() / filename;
}
return Path( *this );
return std::filesystem::path( m_path ).replace_extension( std::filesystem::path( extension ) );
}

// ================================ //

inline Path Path::RelativeTo( const Path& base ) const
{
return m_path.lexically_relative( base.m_path );
}

///**@brief */
Expand Down Expand Up @@ -346,14 +294,14 @@ inline bool Path::IsAbsolut() const
/**@brief Check if file exists.*/
inline bool Path::Exists() const
{
return m_path.exists();
return std::filesystem::exists( m_path );
}

// ================================ //
//
inline Path Path::WorkingDirectory()
{
return path_impl::getcwd();
return std::filesystem::current_path();
}

/**@brief */
Expand All @@ -366,24 +314,21 @@ inline Path operator/( const Path& path1, const Path& path2 )

// ================================ //
//
inline const std::vector< std::string >& Path::GetTokens () const
inline std::vector< std::string > Path::GetTokens () const
{
return m_path.get_tokens();
}
std::vector< std::string > tokens;
for( auto it = m_path.begin(); it != m_path.end(); ++it )
tokens.push_back( it->string() );

// ================================ //
//
inline Path Path::ClipFromRoot ( int num ) const
{
return Path( m_path.clip_from_root( num ) );
return tokens;
}

///**@brief Returns standard library path.
//Use only if you must.*/
//inline const experimental::path& Path::GetStdPath() const
//{
// return m_path;
//}
inline const std::filesystem::path Path::GetStdPath() const
{
return m_path;
}

}

Expand Down
6 changes: 3 additions & 3 deletions swCommonLib/Tests/FileSystemTest/PathTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,12 @@ TEST_CASE( "Path - Aquire Extension" )
CHECK( path6.GetExtension() == "" );
CHECK( path7.GetExtension() == "" );
CHECK( path8.GetExtension() == "" );
CHECK( path9.GetExtension() == ".hidden" );
CHECK( path9.GetExtension() == "" );
}

// ================================ //
//
TEST_CASE( "Path.ChnageExtension" )
TEST_CASE( "Path.ChangeExtension" )
{
filesystem::Path path1( "/foo/bar.txt" );
filesystem::Path path2( "/foo/bar." );
Expand All @@ -69,6 +69,6 @@ TEST_CASE( "Path.ChnageExtension" )

CHECK( path7.ChangeExtension( ".png" ) == "/foo/." );
CHECK( path8.ChangeExtension( ".png" ) == "/foo/.." );
CHECK( path9.ChangeExtension( ".png" ) == "/foo/.png" );
CHECK( path9.ChangeExtension( ".png" ) == "/foo/.hidden.png" );
}

2 changes: 1 addition & 1 deletion swCommonLib/Tests/FileSystemTest/TestNormalizePath.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,6 @@ TEST_CASE( "Filesystem.Normalize.RamainingDots.DotsOnly" )
filesystem::Path path( "../../" );
path.Normalize();

CHECK( path == "../../" );
CHECK( path == filesystem::Path( "../../" ) );
}

2 changes: 1 addition & 1 deletion swGUI/Core/Controls/TextualControls/TextBlock.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#include "swGUI/Core/System/Rendering/Drawings/TextDrawing.h"

#include "swGraphicAPI/Assets/TextAsset/Text.h"
#include "swGraphicAPI/Assets/TextAsset/FontPicker.h"
#include "swGraphicAPI/Assets/TextAsset/Loader/FontPicker.h"


namespace sw {
Expand Down
Loading

0 comments on commit ea8be70

Please sign in to comment.