From 2e8d460f1f132b475143fb9c9c62b28ff2075a43 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emmanuel=20Beno=C3=AEt?= Date: Tue, 26 Dec 2017 13:32:53 +0100 Subject: [PATCH] Filesystem abstraction prototype - Filesystem::Cwd() --- p-filesystem.cc | 20 ++++++++++++++++++++ p-filesystem.hh | 7 ++++--- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/p-filesystem.cc b/p-filesystem.cc index 5fc8f39..a6697e9 100644 --- a/p-filesystem.cc +++ b/p-filesystem.cc @@ -5,10 +5,12 @@ #include using namespace ebcl; + #ifdef _WIN32 # define M_PATHSEP_ '\\' #else # define M_PATHSEP_ '/' +# include #endif @@ -324,3 +326,21 @@ T_FSPath T_FSPath::canonical( ) const noexcept } return np; } + + +/*= Filesystem ===============================================================*/ + +T_FSPath Filesystem::Cwd( ) noexcept +{ + // TODO windows version + + T_Buffer< char > buffer{ 256 }; + while ( getcwd( &buffer[ 0 ] , buffer.bytes( ) ) == nullptr ) { + assert( errno == ERANGE ); + buffer.resize( buffer.size( ) + 256 ); + } + + T_FSPath path{ &buffer[ 0 ] }; + assert( path.isValid( ) ); + return path; +} diff --git a/p-filesystem.hh b/p-filesystem.hh index 83e35c4..7b7e9bc 100644 --- a/p-filesystem.hh +++ b/p-filesystem.hh @@ -116,11 +116,12 @@ M_LSHIFT_OP( T_StringBuilder , T_FSPath const& ); /*= FILESYSTEM ===============================================================*/ -class T_Filesystem +class Filesystem final { + Filesystem( ) = delete; public: - // Return the absolute path to the current directory - static T_FSPath currentDirectory( ) noexcept; + // Return the absolute path to the current working directory + static T_FSPath Cwd( ) noexcept; };