Added FileSystem::changeWorkingDirectory, FileSystem::getCurrentWorkingDirectory

and FileSystem::filePathRemoveCurrentWorkingDirectory.

--HG--
branch : dev
This commit is contained in:
Martín Lucas Golini
2017-12-29 10:52:58 -03:00
parent a7c1d587ed
commit aada050aa3
4 changed files with 59 additions and 17 deletions

View File

@@ -309,13 +309,13 @@ class EE_API String {
/** @brief Implicit cast operator to std::string (ANSI string)
** The current global locale is used for conversion. If you
** want to explicitely specify a locale, see ToAnsiString.
** want to explicitely specify a locale, see toAnsiString.
** Characters that do not fit in the target encoding are
** discarded from the returned string.
** This operator is defined for convenience, and is equivalent
** to calling ToAnsiString().
** to calling toAnsiString().
** @return Converted ANSI string
** @see ToAnsiString, operator String
** @see toAnsiString, operator String
**/
operator std::string() const;
@@ -323,12 +323,12 @@ class EE_API String {
** The UTF-32 string is converted to an ANSI string in
** the encoding defined by \a locale. If you want to use
** the current global locale, see the other overload
** of ToAnsiString.
** of toAnsiString.
** Characters that do not fit in the target encoding are
** discarded from the returned string.
** @param locale Locale to use for conversion
** @return Converted ANSI string
** @see ToWideString, operator std::string
** @see toWideString, operator std::string
**/
std::string toAnsiString( const std::locale& locale = std::locale() ) const;
@@ -337,7 +337,7 @@ class EE_API String {
** Characters that do not fit in the target encoding are
** discarded from the returned string.
** @return Converted wide string
** @see ToAnsiString, operator String
** @see toAnsiString, operator String
**/
std::wstring toWideString() const;
#endif
@@ -767,7 +767,7 @@ it is possible to use a custom locale if necessary:
std::locale locale;
EE::String s;
...
std::string s1 = s.ToAnsiString(locale);
std::string s1 = s.toAnsiString(locale);
s = EE::String("hello", locale);
@endcode

View File

@@ -97,6 +97,14 @@ class EE_API FileSystem {
*/
static std::string sizeToString(const Int64& Size );
/** Change the process current working directory */
static bool changeWorkingDirectory( const std::string& path );
/** Gets the current working directory */
static std::string getCurrentWorkingDirectory();
/** Removes the current working directory from a path */
static void filePathRemoveCurrentWorkingDirectory(std::string & path);
};
}}

View File

@@ -2,6 +2,8 @@
#include <eepp/system/iostreamfile.hpp>
#include <eepp/system/sys.hpp>
#include <sys/stat.h>
#include <unistd.h>
#include <climits>
#include <list>
#include <algorithm>
@@ -133,6 +135,13 @@ void FileSystem::filePathRemoveProcessPath( std::string& path ) {
}
}
void FileSystem::filePathRemoveCurrentWorkingDirectory( std::string& path ) {
std::string dirPath = getCurrentWorkingDirectory();
if ( String::startsWith( path, dirPath ) && dirPath.length() < path.size() ) {
path = path.substr( dirPath.length() );
}
}
bool FileSystem::fileWrite( const std::string& filepath, const Uint8* data, const Uint32& dataSize ) {
IOStreamFile fs( filepath, std::ios::out | std::ios::binary );
@@ -215,14 +224,8 @@ bool FileSystem::isDirectory( const String& path ) {
bool FileSystem::isDirectory( const std::string& path ) {
#ifndef EE_COMPILER_MSVC
DIR *dp = NULL;
bool isdir = !( ( dp = opendir( path.c_str() ) ) == NULL);
if ( NULL != dp )
closedir(dp);
return isdir;
struct stat st;
return ( stat( path.c_str(), &st ) == 0 ) && S_ISDIR( st.st_mode );
#else
return 0 != ( GetFileAttributes( (LPCTSTR) path.c_str() ) & FILE_ATTRIBUTE_DIRECTORY );
#endif
@@ -256,7 +259,7 @@ std::vector<String> FileSystem::filesGetInPath( const String& path, const bool&
}
WIN32_FIND_DATA findFileData;
HANDLE hFind = FindFirstFile( (LPCWSTR)mPath.ToWideString().c_str(), &findFileData );
HANDLE hFind = FindFirstFile( (LPCWSTR)mPath.toWideString().c_str(), &findFileData );
if( hFind != INVALID_HANDLE_VALUE ) {
String tmpstr( findFileData.cFileName );
@@ -378,7 +381,7 @@ std::vector<std::string> FileSystem::filesGetInPath( const std::string& path, co
}
WIN32_FIND_DATA findFileData;
HANDLE hFind = FindFirstFile( (LPCWSTR)mPath.ToWideString().c_str(), &findFileData );
HANDLE hFind = FindFirstFile( (LPCWSTR)mPath.toWideString().c_str(), &findFileData );
if( hFind != INVALID_HANDLE_VALUE ) {
String tmpstr( findFileData.cFileName );
@@ -512,4 +515,34 @@ std::string FileSystem::sizeToString( const Int64& Size ) {
return std::string( String::toStr( mem ) + size );
}
bool FileSystem::changeWorkingDirectory( const std::string & path ) {
int res = -1;
#ifdef EE_COMPILER_MSVC
#ifdef UNICODE
res = _wchdir( String::fromUtf8( path.c_str() ).toWideString() );
#else
res = _chdir( String::fromUtf8( path.c_str() ).toAnsiString() );
#endif
#else
res = chdir( path.c_str() );
#endif
return -1 != res;
}
std::string FileSystem::getCurrentWorkingDirectory() {
#ifdef EE_COMPILER_MSVC
#if defined( UNICODE ) AND !defined( EE_NO_WIDECHAR )
wchar_t dir[_MAX_PATH];
return ( 0 != GetCurrentDirectoryW( _MAX_PATH, dir ) ) ? String( dir )::toUtf8() : std::string();
#else
char dir[_MAX_PATH];
return ( 0 != GetCurrentDirectory( _MAX_PATH, dir ) ) ? String( dir, std::locale() ).toUtf8() : std::string();
#endif
#else
char dir[PATH_MAX + 1];
getcwd( dir, PATH_MAX + 1 );
return std::string( dir );
#endif
}
}}

View File

@@ -361,6 +361,7 @@ void Window::logSuccessfulInit(const std::string& BackendName ) {
"\n\tArch: " + Sys::getOSArchitecture() +
"\n\tCPU Cores: " + String::toStr( Sys::getCPUCount() ) +
"\n\tProcess Path: " + Sys::getProcessPath() +
"\n\tCurrent Working Directory: " + FileSystem::getCurrentWorkingDirectory() +
"\n\tDisk Free Space: " + String::toStr( FileSystem::sizeToString( Sys::getDiskFreeSpace( Sys::getProcessPath() ) ) ) +
"\n\tWindow/Input Backend: " + BackendName +
"\n\tGL Backend: " + GLi->versionStr() +