Code-style consistency improvement:

Apply clang-format-all.sh using the _clang-format file through all the cpp/.h files.
make sure not to apply it to certain serialization structures, since some parser expects the * as part of the name, instead of type.
This commit contains no other changes aside from adding and applying clang-format-all.sh
This commit is contained in:
erwincoumans
2018-09-23 14:17:31 -07:00
parent b73b05e9fb
commit ab8f16961e
1773 changed files with 1081087 additions and 474249 deletions

View File

@@ -3,7 +3,7 @@
#include "strtools.h"
#include "pathtools.h"
#if defined( _WIN32)
#if defined(_WIN32)
#include <windows.h>
#include <direct.h>
#include <shobjidl.h>
@@ -22,7 +22,7 @@
#include <Foundation/Foundation.h>
#include <AppKit/AppKit.h>
#include <mach-o/dyld.h>
#define _S_IFDIR S_IFDIR // really from tier0/platform.h which we dont have yet
#define _S_IFDIR S_IFDIR // really from tier0/platform.h which we dont have yet
#endif
#include <sys/stat.h>
@@ -32,32 +32,32 @@
/** Returns the path (including filename) to the current executable */
std::string Path_GetExecutablePath()
{
#if defined( _WIN32 )
#if defined(_WIN32)
wchar_t *pwchPath = new wchar_t[MAX_UNICODE_PATH];
char *pchPath = new char[MAX_UNICODE_PATH_IN_UTF8];
::GetModuleFileNameW( NULL, pwchPath, MAX_UNICODE_PATH );
WideCharToMultiByte( CP_UTF8, 0, pwchPath, -1, pchPath, MAX_UNICODE_PATH_IN_UTF8, NULL, NULL );
::GetModuleFileNameW(NULL, pwchPath, MAX_UNICODE_PATH);
WideCharToMultiByte(CP_UTF8, 0, pwchPath, -1, pchPath, MAX_UNICODE_PATH_IN_UTF8, NULL, NULL);
delete[] pwchPath;
std::string sPath = pchPath;
delete[] pchPath;
return sPath;
#elif defined( OSX )
#elif defined(OSX)
char rchPath[1024];
uint32_t nBuff = sizeof( rchPath );
uint32_t nBuff = sizeof(rchPath);
bool bSuccess = _NSGetExecutablePath(rchPath, &nBuff) == 0;
rchPath[nBuff-1] = '\0';
if( bSuccess )
rchPath[nBuff - 1] = '\0';
if (bSuccess)
return rchPath;
else
return "";
#elif defined LINUX
char rchPath[1024];
size_t nBuff = sizeof( rchPath );
ssize_t nRead = readlink("/proc/self/exe", rchPath, nBuff-1 );
if ( nRead != -1 )
size_t nBuff = sizeof(rchPath);
ssize_t nRead = readlink("/proc/self/exe", rchPath, nBuff - 1);
if (nRead != -1)
{
rchPath[ nRead ] = 0;
rchPath[nRead] = 0;
return rchPath;
}
else
@@ -65,78 +65,77 @@ std::string Path_GetExecutablePath()
return "";
}
#else
AssertMsg( false, "Implement Plat_GetExecutablePath" );
AssertMsg(false, "Implement Plat_GetExecutablePath");
return "";
#endif
}
/** Returns the path of the current working directory */
std::string Path_GetWorkingDirectory()
{
std::string sPath;
#if defined( _WIN32 )
#if defined(_WIN32)
wchar_t buf[MAX_UNICODE_PATH];
sPath = UTF16to8( _wgetcwd( buf, MAX_UNICODE_PATH ) );
sPath = UTF16to8(_wgetcwd(buf, MAX_UNICODE_PATH));
#else
char buf[ 1024 ];
sPath = getcwd( buf, sizeof( buf ) );
char buf[1024];
sPath = getcwd(buf, sizeof(buf));
#endif
return sPath;
}
/** Sets the path of the current working directory. Returns true if this was successful. */
bool Path_SetWorkingDirectory( const std::string & sPath )
bool Path_SetWorkingDirectory(const std::string &sPath)
{
bool bSuccess;
#if defined( _WIN32 )
std::wstring wsPath = UTF8to16( sPath.c_str() );
bSuccess = 0 == _wchdir( wsPath.c_str() );
#if defined(_WIN32)
std::wstring wsPath = UTF8to16(sPath.c_str());
bSuccess = 0 == _wchdir(wsPath.c_str());
#else
bSuccess = 0 == chdir( sPath.c_str() );
bSuccess = 0 == chdir(sPath.c_str());
#endif
return bSuccess;
}
/** Returns the specified path without its filename */
std::string Path_StripFilename( const std::string & sPath, char slash )
std::string Path_StripFilename(const std::string &sPath, char slash)
{
if( slash == 0 )
if (slash == 0)
slash = Path_GetSlash();
std::string::size_type n = sPath.find_last_of( slash );
if( n == std::string::npos )
std::string::size_type n = sPath.find_last_of(slash);
if (n == std::string::npos)
return sPath;
else
return std::string( sPath.begin(), sPath.begin() + n );
return std::string(sPath.begin(), sPath.begin() + n);
}
/** returns just the filename from the provided full or relative path. */
std::string Path_StripDirectory( const std::string & sPath, char slash )
std::string Path_StripDirectory(const std::string &sPath, char slash)
{
if( slash == 0 )
if (slash == 0)
slash = Path_GetSlash();
std::string::size_type n = sPath.find_last_of( slash );
if( n == std::string::npos )
std::string::size_type n = sPath.find_last_of(slash);
if (n == std::string::npos)
return sPath;
else
return std::string( sPath.begin() + n + 1, sPath.end() );
return std::string(sPath.begin() + n + 1, sPath.end());
}
/** returns just the filename with no extension of the provided filename.
* If there is a path the path is left intact. */
std::string Path_StripExtension( const std::string & sPath )
std::string Path_StripExtension(const std::string &sPath)
{
for( std::string::const_reverse_iterator i = sPath.rbegin(); i != sPath.rend(); i++ )
for (std::string::const_reverse_iterator i = sPath.rbegin(); i != sPath.rend(); i++)
{
if( *i == '.' )
if (*i == '.')
{
return std::string( sPath.begin(), i.base() - 1 );
return std::string(sPath.begin(), i.base() - 1);
}
// if we find a slash there is no extension
if( *i == '\\' || *i == '/' )
if (*i == '\\' || *i == '/')
break;
}
@@ -145,17 +144,17 @@ std::string Path_StripExtension( const std::string & sPath )
}
/** returns just extension of the provided filename (if any). */
std::string Path_GetExtension( const std::string & sPath )
std::string Path_GetExtension(const std::string &sPath)
{
for ( std::string::const_reverse_iterator i = sPath.rbegin(); i != sPath.rend(); i++ )
for (std::string::const_reverse_iterator i = sPath.rbegin(); i != sPath.rend(); i++)
{
if ( *i == '.' )
if (*i == '.')
{
return std::string( i.base(), sPath.end() );
return std::string(i.base(), sPath.end());
}
// if we find a slash there is no extension
if ( *i == '\\' || *i == '/' )
if (*i == '\\' || *i == '/')
break;
}
@@ -163,70 +162,67 @@ std::string Path_GetExtension( const std::string & sPath )
return "";
}
bool Path_IsAbsolute( const std::string & sPath )
bool Path_IsAbsolute(const std::string &sPath)
{
if( sPath.empty() )
if (sPath.empty())
return false;
#if defined( WIN32 )
if ( sPath.size() < 3 ) // must be c:\x or \\x at least
#if defined(WIN32)
if (sPath.size() < 3) // must be c:\x or \\x at least
return false;
if ( sPath[1] == ':' ) // drive letter plus slash, but must test both slash cases
if (sPath[1] == ':') // drive letter plus slash, but must test both slash cases
{
if ( sPath[2] == '\\' || sPath[2] == '/' )
if (sPath[2] == '\\' || sPath[2] == '/')
return true;
}
else if ( sPath[0] == '\\' && sPath[1] == '\\' ) // UNC path
else if (sPath[0] == '\\' && sPath[1] == '\\') // UNC path
return true;
#else
if( sPath[0] == '\\' || sPath[0] == '/' ) // any leading slash
if (sPath[0] == '\\' || sPath[0] == '/') // any leading slash
return true;
#endif
return false;
}
/** Makes an absolute path from a relative path and a base path */
std::string Path_MakeAbsolute( const std::string & sRelativePath, const std::string & sBasePath, char slash )
std::string Path_MakeAbsolute(const std::string &sRelativePath, const std::string &sBasePath, char slash)
{
if( slash == 0 )
if (slash == 0)
slash = Path_GetSlash();
if( Path_IsAbsolute( sRelativePath ) )
if (Path_IsAbsolute(sRelativePath))
return sRelativePath;
else
{
if( !Path_IsAbsolute( sBasePath ) )
if (!Path_IsAbsolute(sBasePath))
return "";
std::string sCompacted = Path_Compact( Path_Join( sBasePath, sRelativePath, slash ), slash );
if( Path_IsAbsolute( sCompacted ) )
std::string sCompacted = Path_Compact(Path_Join(sBasePath, sRelativePath, slash), slash);
if (Path_IsAbsolute(sCompacted))
return sCompacted;
else
return "";
}
}
/** Fixes the directory separators for the current platform */
std::string Path_FixSlashes( const std::string & sPath, char slash )
std::string Path_FixSlashes(const std::string &sPath, char slash)
{
if( slash == 0 )
if (slash == 0)
slash = Path_GetSlash();
std::string sFixed = sPath;
for( std::string::iterator i = sFixed.begin(); i != sFixed.end(); i++ )
for (std::string::iterator i = sFixed.begin(); i != sFixed.end(); i++)
{
if( *i == '/' || *i == '\\' )
if (*i == '/' || *i == '\\')
*i = slash;
}
return sFixed;
}
char Path_GetSlash()
{
#if defined(_WIN32)
@@ -237,65 +233,63 @@ char Path_GetSlash()
}
/** Jams two paths together with the right kind of slash */
std::string Path_Join( const std::string & first, const std::string & second, char slash )
std::string Path_Join(const std::string &first, const std::string &second, char slash)
{
if( slash == 0 )
if (slash == 0)
slash = Path_GetSlash();
// only insert a slash if we don't already have one
std::string::size_type nLen = first.length();
if( !nLen )
if (!nLen)
return second;
#if defined(_WIN32)
if( first.back() == '\\' || first.back() == '/' )
nLen--;
if (first.back() == '\\' || first.back() == '/')
nLen--;
#else
char last_char = first[first.length()-1];
char last_char = first[first.length() - 1];
if (last_char == '\\' || last_char == '/')
nLen--;
nLen--;
#endif
return first.substr( 0, nLen ) + std::string( 1, slash ) + second;
return first.substr(0, nLen) + std::string(1, slash) + second;
}
std::string Path_Join( const std::string & first, const std::string & second, const std::string & third, char slash )
std::string Path_Join(const std::string &first, const std::string &second, const std::string &third, char slash)
{
return Path_Join( Path_Join( first, second, slash ), third, slash );
return Path_Join(Path_Join(first, second, slash), third, slash);
}
std::string Path_Join( const std::string & first, const std::string & second, const std::string & third, const std::string &fourth, char slash )
std::string Path_Join(const std::string &first, const std::string &second, const std::string &third, const std::string &fourth, char slash)
{
return Path_Join( Path_Join( Path_Join( first, second, slash ), third, slash ), fourth, slash );
return Path_Join(Path_Join(Path_Join(first, second, slash), third, slash), fourth, slash);
}
std::string Path_Join(
const std::string & first,
const std::string & second,
const std::string & third,
const std::string & fourth,
const std::string & fifth,
char slash )
std::string Path_Join(
const std::string &first,
const std::string &second,
const std::string &third,
const std::string &fourth,
const std::string &fifth,
char slash)
{
return Path_Join( Path_Join( Path_Join( Path_Join( first, second, slash ), third, slash ), fourth, slash ), fifth, slash );
return Path_Join(Path_Join(Path_Join(Path_Join(first, second, slash), third, slash), fourth, slash), fifth, slash);
}
std::string Path_RemoveTrailingSlash( const std::string & sRawPath, char slash )
std::string Path_RemoveTrailingSlash(const std::string &sRawPath, char slash)
{
if ( slash == 0 )
if (slash == 0)
slash = Path_GetSlash();
std::string sPath = sRawPath;
std::string::size_type nCurrent = sRawPath.length();
if ( nCurrent == 0 )
if (nCurrent == 0)
return sPath;
int nLastFound = -1;
nCurrent--;
while( nCurrent != 0 )
while (nCurrent != 0)
{
if ( sRawPath[ nCurrent ] == slash )
if (sRawPath[nCurrent] == slash)
{
nLastFound = (int)nCurrent;
nCurrent--;
@@ -305,32 +299,31 @@ std::string Path_RemoveTrailingSlash( const std::string & sRawPath, char slash )
break;
}
}
if ( nLastFound >= 0 )
if (nLastFound >= 0)
{
sPath.erase( nLastFound, std::string::npos );
sPath.erase(nLastFound, std::string::npos);
}
return sPath;
}
/** Removes redundant <dir>/.. elements in the path. Returns an empty path if the
* specified path has a broken number of directories for its number of ..s */
std::string Path_Compact( const std::string & sRawPath, char slash )
std::string Path_Compact(const std::string &sRawPath, char slash)
{
if( slash == 0 )
if (slash == 0)
slash = Path_GetSlash();
std::string sPath = Path_FixSlashes( sRawPath, slash );
std::string sSlashString( 1, slash );
std::string sPath = Path_FixSlashes(sRawPath, slash);
std::string sSlashString(1, slash);
// strip out all /./
for( std::string::size_type i = 0; (i + 3) < sPath.length(); )
for (std::string::size_type i = 0; (i + 3) < sPath.length();)
{
if( sPath[ i ] == slash && sPath[ i+1 ] == '.' && sPath[ i+2 ] == slash )
if (sPath[i] == slash && sPath[i + 1] == '.' && sPath[i + 2] == slash)
{
sPath.replace( i, 3, sSlashString );
sPath.replace(i, 3, sSlashString);
}
else
{
@@ -338,49 +331,44 @@ std::string Path_Compact( const std::string & sRawPath, char slash )
}
}
// get rid of trailing /. but leave the path separator
if( sPath.length() > 2 )
if (sPath.length() > 2)
{
std::string::size_type len = sPath.length();
if( sPath[ len-1 ] == '.' && sPath[ len-2 ] == slash )
if (sPath[len - 1] == '.' && sPath[len - 2] == slash)
{
// sPath.pop_back();
sPath[len-1] = 0; // for now, at least
// sPath.pop_back();
sPath[len - 1] = 0; // for now, at least
}
}
// get rid of leading ./
if( sPath.length() > 2 )
// get rid of leading ./
if (sPath.length() > 2)
{
if( sPath[ 0 ] == '.' && sPath[ 1 ] == slash )
if (sPath[0] == '.' && sPath[1] == slash)
{
sPath.replace( 0, 2, "" );
sPath.replace(0, 2, "");
}
}
// each time we encounter .. back up until we've found the previous directory name
// then get rid of both
std::string::size_type i = 0;
while( i < sPath.length() )
while (i < sPath.length())
{
if( i > 0 && sPath.length() - i >= 2
&& sPath[i] == '.'
&& sPath[i+1] == '.'
&& ( i + 2 == sPath.length() || sPath[ i+2 ] == slash )
&& sPath[ i-1 ] == slash )
if (i > 0 && sPath.length() - i >= 2 && sPath[i] == '.' && sPath[i + 1] == '.' && (i + 2 == sPath.length() || sPath[i + 2] == slash) && sPath[i - 1] == slash)
{
// check if we've hit the start of the string and have a bogus path
if( i == 1 )
if (i == 1)
return "";
// find the separator before i-1
std::string::size_type iDirStart = i-2;
while( iDirStart > 0 && sPath[ iDirStart - 1 ] != slash )
std::string::size_type iDirStart = i - 2;
while (iDirStart > 0 && sPath[iDirStart - 1] != slash)
--iDirStart;
// remove everything from iDirStart to i+2
sPath.replace( iDirStart, (i - iDirStart) + 3, "" );
sPath.replace(iDirStart, (i - iDirStart) + 3, "");
// start over
i = 0;
@@ -394,7 +382,6 @@ std::string Path_Compact( const std::string & sRawPath, char slash )
return sPath;
}
/** Returns the path to the current DLL or exe */
std::string Path_GetThisModulePath()
{
@@ -402,57 +389,55 @@ std::string Path_GetThisModulePath()
#ifdef WIN32
HMODULE hmodule = NULL;
::GetModuleHandleEx( GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, reinterpret_cast<LPCTSTR>(Path_GetThisModulePath), &hmodule );
::GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, reinterpret_cast<LPCTSTR>(Path_GetThisModulePath), &hmodule);
wchar_t *pwchPath = new wchar_t[MAX_UNICODE_PATH];
char *pchPath = new char[ MAX_UNICODE_PATH_IN_UTF8 ];
::GetModuleFileNameW( hmodule, pwchPath, MAX_UNICODE_PATH );
WideCharToMultiByte( CP_UTF8, 0, pwchPath, -1, pchPath, MAX_UNICODE_PATH_IN_UTF8, NULL, NULL );
char *pchPath = new char[MAX_UNICODE_PATH_IN_UTF8];
::GetModuleFileNameW(hmodule, pwchPath, MAX_UNICODE_PATH);
WideCharToMultiByte(CP_UTF8, 0, pwchPath, -1, pchPath, MAX_UNICODE_PATH_IN_UTF8, NULL, NULL);
delete[] pwchPath;
std::string sPath = pchPath;
delete [] pchPath;
delete[] pchPath;
return sPath;
#elif defined( OSX ) || defined( LINUX )
#elif defined(OSX) || defined(LINUX)
// get the addr of a function in vrclient.so and then ask the dlopen system about it
Dl_info info;
dladdr( (void *)Path_GetThisModulePath, &info );
dladdr((void *)Path_GetThisModulePath, &info);
return info.dli_fname;
#endif
}
/** returns true if the specified path exists and is a directory */
bool Path_IsDirectory( const std::string & sPath )
bool Path_IsDirectory(const std::string &sPath)
{
std::string sFixedPath = Path_FixSlashes( sPath );
if( sFixedPath.empty() )
std::string sFixedPath = Path_FixSlashes(sPath);
if (sFixedPath.empty())
return false;
char cLast = sFixedPath[ sFixedPath.length() - 1 ];
if( cLast == '/' || cLast == '\\' )
sFixedPath.erase( sFixedPath.end() - 1, sFixedPath.end() );
char cLast = sFixedPath[sFixedPath.length() - 1];
if (cLast == '/' || cLast == '\\')
sFixedPath.erase(sFixedPath.end() - 1, sFixedPath.end());
// see if the specified path actually exists.
// see if the specified path actually exists.
#if defined(POSIX)
struct stat buf;
if ( stat( sFixedPath.c_str(), &buf ) == -1 )
struct stat buf;
if (stat(sFixedPath.c_str(), &buf) == -1)
{
return false;
}
#if defined( LINUX ) || defined( OSX )
return S_ISDIR( buf.st_mode );
#if defined(LINUX) || defined(OSX)
return S_ISDIR(buf.st_mode);
#else
return (buf.st_mode & _S_IFDIR) != 0;
#endif
#else
struct _stat buf;
std::wstring wsFixedPath = UTF8to16( sFixedPath.c_str() );
if ( _wstat( wsFixedPath.c_str(), &buf ) == -1 )
struct _stat buf;
std::wstring wsFixedPath = UTF8to16(sFixedPath.c_str());
if (_wstat(wsFixedPath.c_str(), &buf) == -1)
{
return false;
}
@@ -462,12 +447,12 @@ bool Path_IsDirectory( const std::string & sPath )
}
/** returns true if the specified path represents an app bundle */
bool Path_IsAppBundle( const std::string & sPath )
bool Path_IsAppBundle(const std::string &sPath)
{
#if defined(OSX)
NSBundle *bundle = [ NSBundle bundleWithPath: [ NSString stringWithUTF8String:sPath.c_str() ] ];
bool bisAppBundle = ( nullptr != bundle );
[ bundle release ];
NSBundle *bundle = [NSBundle bundleWithPath:[NSString stringWithUTF8String:sPath.c_str()]];
bool bisAppBundle = (nullptr != bundle);
[bundle release];
return bisAppBundle;
#else
return false;
@@ -477,22 +462,22 @@ bool Path_IsAppBundle( const std::string & sPath )
//-----------------------------------------------------------------------------
// Purpose: returns true if the the path exists
//-----------------------------------------------------------------------------
bool Path_Exists( const std::string & sPath )
bool Path_Exists(const std::string &sPath)
{
std::string sFixedPath = Path_FixSlashes( sPath );
if( sFixedPath.empty() )
std::string sFixedPath = Path_FixSlashes(sPath);
if (sFixedPath.empty())
return false;
#if defined( WIN32 )
struct _stat buf;
std::wstring wsFixedPath = UTF8to16( sFixedPath.c_str() );
if ( _wstat( wsFixedPath.c_str(), &buf ) == -1 )
#if defined(WIN32)
struct _stat buf;
std::wstring wsFixedPath = UTF8to16(sFixedPath.c_str());
if (_wstat(wsFixedPath.c_str(), &buf) == -1)
{
return false;
}
#else
struct stat buf;
if ( stat ( sFixedPath.c_str(), &buf ) == -1)
if (stat(sFixedPath.c_str(), &buf) == -1)
{
return false;
}
@@ -501,78 +486,75 @@ bool Path_Exists( const std::string & sPath )
return true;
}
//-----------------------------------------------------------------------------
// Purpose: helper to find a directory upstream from a given path
//-----------------------------------------------------------------------------
std::string Path_FindParentDirectoryRecursively( const std::string &strStartDirectory, const std::string &strDirectoryName )
std::string Path_FindParentDirectoryRecursively(const std::string &strStartDirectory, const std::string &strDirectoryName)
{
std::string strFoundPath = "";
std::string strCurrentPath = Path_FixSlashes( strStartDirectory );
if ( strCurrentPath.length() == 0 )
std::string strCurrentPath = Path_FixSlashes(strStartDirectory);
if (strCurrentPath.length() == 0)
return "";
bool bExists = Path_Exists( strCurrentPath );
std::string strCurrentDirectoryName = Path_StripDirectory( strCurrentPath );
if ( bExists && stricmp( strCurrentDirectoryName.c_str(), strDirectoryName.c_str() ) == 0 )
bool bExists = Path_Exists(strCurrentPath);
std::string strCurrentDirectoryName = Path_StripDirectory(strCurrentPath);
if (bExists && stricmp(strCurrentDirectoryName.c_str(), strDirectoryName.c_str()) == 0)
return strCurrentPath;
while( bExists && strCurrentPath.length() != 0 )
while (bExists && strCurrentPath.length() != 0)
{
strCurrentPath = Path_StripFilename( strCurrentPath );
strCurrentDirectoryName = Path_StripDirectory( strCurrentPath );
bExists = Path_Exists( strCurrentPath );
if ( bExists && stricmp( strCurrentDirectoryName.c_str(), strDirectoryName.c_str() ) == 0 )
strCurrentPath = Path_StripFilename(strCurrentPath);
strCurrentDirectoryName = Path_StripDirectory(strCurrentPath);
bExists = Path_Exists(strCurrentPath);
if (bExists && stricmp(strCurrentDirectoryName.c_str(), strDirectoryName.c_str()) == 0)
return strCurrentPath;
}
return "";
}
//-----------------------------------------------------------------------------
// Purpose: helper to find a subdirectory upstream from a given path
//-----------------------------------------------------------------------------
std::string Path_FindParentSubDirectoryRecursively( const std::string &strStartDirectory, const std::string &strDirectoryName )
std::string Path_FindParentSubDirectoryRecursively(const std::string &strStartDirectory, const std::string &strDirectoryName)
{
std::string strFoundPath = "";
std::string strCurrentPath = Path_FixSlashes( strStartDirectory );
if ( strCurrentPath.length() == 0 )
std::string strCurrentPath = Path_FixSlashes(strStartDirectory);
if (strCurrentPath.length() == 0)
return "";
bool bExists = Path_Exists( strCurrentPath );
while( bExists && strCurrentPath.length() != 0 )
bool bExists = Path_Exists(strCurrentPath);
while (bExists && strCurrentPath.length() != 0)
{
strCurrentPath = Path_StripFilename( strCurrentPath );
bExists = Path_Exists( strCurrentPath );
strCurrentPath = Path_StripFilename(strCurrentPath);
bExists = Path_Exists(strCurrentPath);
if( Path_Exists( Path_Join( strCurrentPath, strDirectoryName ) ) )
if (Path_Exists(Path_Join(strCurrentPath, strDirectoryName)))
{
strFoundPath = Path_Join( strCurrentPath, strDirectoryName );
strFoundPath = Path_Join(strCurrentPath, strDirectoryName);
break;
}
}
return strFoundPath;
}
//-----------------------------------------------------------------------------
// Purpose: reading and writing files in the vortex directory
//-----------------------------------------------------------------------------
unsigned char * Path_ReadBinaryFile( const std::string &strFilename, int *pSize )
unsigned char *Path_ReadBinaryFile(const std::string &strFilename, int *pSize)
{
FILE *f;
#if defined( POSIX )
f = fopen( strFilename.c_str(), "rb" );
#if defined(POSIX)
f = fopen(strFilename.c_str(), "rb");
#else
std::wstring wstrFilename = UTF8to16( strFilename.c_str() );
std::wstring wstrFilename = UTF8to16(strFilename.c_str());
// the open operation needs to be sharable, therefore use of _wfsopen instead of _wfopen_s
f = _wfsopen( wstrFilename.c_str(), L"rb", _SH_DENYNO );
f = _wfsopen(wstrFilename.c_str(), L"rb", _SH_DENYNO);
#endif
unsigned char* buf = NULL;
if ( f != NULL )
unsigned char *buf = NULL;
if (f != NULL)
{
fseek(f, 0, SEEK_END);
int size = ftell(f);
@@ -596,15 +578,15 @@ unsigned char * Path_ReadBinaryFile( const std::string &strFilename, int *pSize
return buf;
}
uint32_t Path_ReadBinaryFile( const std::string &strFilename, unsigned char *pBuffer, uint32_t unSize )
uint32_t Path_ReadBinaryFile(const std::string &strFilename, unsigned char *pBuffer, uint32_t unSize)
{
FILE *f;
#if defined( POSIX )
f = fopen( strFilename.c_str(), "rb" );
#if defined(POSIX)
f = fopen(strFilename.c_str(), "rb");
#else
std::wstring wstrFilename = UTF8to16( strFilename.c_str() );
errno_t err = _wfopen_s( &f, wstrFilename.c_str(), L"rb" );
if ( err != 0 )
std::wstring wstrFilename = UTF8to16(strFilename.c_str());
errno_t err = _wfopen_s(&f, wstrFilename.c_str(), L"rb");
if (err != 0)
{
f = NULL;
}
@@ -612,25 +594,25 @@ uint32_t Path_ReadBinaryFile( const std::string &strFilename, unsigned char *pB
uint32_t unSizeToReturn = 0;
if ( f != NULL )
if (f != NULL)
{
fseek( f, 0, SEEK_END );
uint32_t size = (uint32_t)ftell( f );
fseek( f, 0, SEEK_SET );
fseek(f, 0, SEEK_END);
uint32_t size = (uint32_t)ftell(f);
fseek(f, 0, SEEK_SET);
if ( size > unSize || !pBuffer )
if (size > unSize || !pBuffer)
{
unSizeToReturn = (uint32_t)size;
}
else
{
if ( fread( pBuffer, size, 1, f ) == 1 )
if (fread(pBuffer, size, 1, f) == 1)
{
unSizeToReturn = (uint32_t)size;
}
}
fclose( f );
fclose(f);
}
return unSizeToReturn;
@@ -639,11 +621,11 @@ uint32_t Path_ReadBinaryFile( const std::string &strFilename, unsigned char *pB
bool Path_WriteBinaryFile(const std::string &strFilename, unsigned char *pData, unsigned nSize)
{
FILE *f;
#if defined( POSIX )
#if defined(POSIX)
f = fopen(strFilename.c_str(), "wb");
#else
std::wstring wstrFilename = UTF8to16( strFilename.c_str() );
errno_t err = _wfopen_s( &f, wstrFilename.c_str(), L"wb" );
std::wstring wstrFilename = UTF8to16(strFilename.c_str());
errno_t err = _wfopen_s(&f, wstrFilename.c_str(), L"wb");
if (err != 0)
{
f = NULL;
@@ -651,7 +633,8 @@ bool Path_WriteBinaryFile(const std::string &strFilename, unsigned char *pData,
#endif
size_t written = 0;
if (f != NULL) {
if (f != NULL)
{
written = fwrite(pData, sizeof(unsigned char), nSize, f);
fclose(f);
}
@@ -659,24 +642,24 @@ bool Path_WriteBinaryFile(const std::string &strFilename, unsigned char *pData,
return written = nSize ? true : false;
}
std::string Path_ReadTextFile( const std::string &strFilename )
std::string Path_ReadTextFile(const std::string &strFilename)
{
// doing it this way seems backwards, but I don't
// see an easy way to do this with C/C++ style IO
// that isn't worse...
int size;
unsigned char* buf = Path_ReadBinaryFile( strFilename, &size );
unsigned char *buf = Path_ReadBinaryFile(strFilename, &size);
if (!buf)
return "";
// convert CRLF -> LF
size_t outsize = 1;
for (int i=1; i < size; i++)
for (int i = 1; i < size; i++)
{
if (buf[i] == '\n' && buf[i-1] == '\r') // CRLF
buf[outsize-1] = '\n'; // ->LF
if (buf[i] == '\n' && buf[i - 1] == '\r') // CRLF
buf[outsize - 1] = '\n'; // ->LF
else
buf[outsize++] = buf[i]; // just copy
buf[outsize++] = buf[i]; // just copy
}
std::string ret((char *)buf, outsize);
@@ -684,51 +667,50 @@ std::string Path_ReadTextFile( const std::string &strFilename )
return ret;
}
bool Path_WriteStringToTextFile( const std::string &strFilename, const char *pchData )
bool Path_WriteStringToTextFile(const std::string &strFilename, const char *pchData)
{
FILE *f;
#if defined( POSIX )
f = fopen( strFilename.c_str(), "w" );
#if defined(POSIX)
f = fopen(strFilename.c_str(), "w");
#else
std::wstring wstrFilename = UTF8to16( strFilename.c_str() );
errno_t err = _wfopen_s( &f, wstrFilename.c_str(), L"w" );
if ( err != 0 )
std::wstring wstrFilename = UTF8to16(strFilename.c_str());
errno_t err = _wfopen_s(&f, wstrFilename.c_str(), L"w");
if (err != 0)
{
f = NULL;
}
#endif
bool ok = false;
if ( f != NULL )
if (f != NULL)
{
ok = fputs( pchData, f) >= 0;
ok = fputs(pchData, f) >= 0;
fclose(f);
}
return ok;
}
bool Path_WriteStringToTextFileAtomic( const std::string &strFilename, const char *pchData )
bool Path_WriteStringToTextFileAtomic(const std::string &strFilename, const char *pchData)
{
std::string strTmpFilename = strFilename + ".tmp";
if ( !Path_WriteStringToTextFile( strTmpFilename, pchData ) )
if (!Path_WriteStringToTextFile(strTmpFilename, pchData))
return false;
// Platform specific atomic file replacement
#if defined( _WIN32 )
std::wstring wsFilename = UTF8to16( strFilename.c_str() );
std::wstring wsTmpFilename = UTF8to16( strTmpFilename.c_str() );
if ( !::ReplaceFileW( wsFilename.c_str(), wsTmpFilename.c_str(), nullptr, 0, 0, 0 ) )
// Platform specific atomic file replacement
#if defined(_WIN32)
std::wstring wsFilename = UTF8to16(strFilename.c_str());
std::wstring wsTmpFilename = UTF8to16(strTmpFilename.c_str());
if (!::ReplaceFileW(wsFilename.c_str(), wsTmpFilename.c_str(), nullptr, 0, 0, 0))
{
// if we couldn't ReplaceFile, try a non-atomic write as a fallback
if ( !Path_WriteStringToTextFile( strFilename, pchData ) )
if (!Path_WriteStringToTextFile(strFilename, pchData))
return false;
}
#elif defined( POSIX )
if ( rename( strTmpFilename.c_str(), strFilename.c_str() ) == -1 )
#elif defined(POSIX)
if (rename(strTmpFilename.c_str(), strFilename.c_str()) == -1)
return false;
#else
#error Do not know how to write atomic file
@@ -737,7 +719,6 @@ bool Path_WriteStringToTextFileAtomic( const std::string &strFilename, const cha
return true;
}
#if defined(WIN32)
#define FILE_URL_PREFIX "file:///"
#else
@@ -747,31 +728,29 @@ bool Path_WriteStringToTextFileAtomic( const std::string &strFilename, const cha
// ----------------------------------------------------------------------------------------------------------------------------
// Purpose: Turns a path to a file on disk into a URL (or just returns the value if it's already a URL)
// ----------------------------------------------------------------------------------------------------------------------------
std::string Path_FilePathToUrl( const std::string & sRelativePath, const std::string & sBasePath )
std::string Path_FilePathToUrl(const std::string &sRelativePath, const std::string &sBasePath)
{
if ( !strnicmp( sRelativePath.c_str(), "http://", 7 )
|| !strnicmp( sRelativePath.c_str(), "https://", 8 )
|| !strnicmp( sRelativePath.c_str(), "file://", 7 ) )
if (!strnicmp(sRelativePath.c_str(), "http://", 7) || !strnicmp(sRelativePath.c_str(), "https://", 8) || !strnicmp(sRelativePath.c_str(), "file://", 7))
{
return sRelativePath;
}
else
{
std::string sAbsolute = Path_MakeAbsolute( sRelativePath, sBasePath );
if ( sAbsolute.empty() )
std::string sAbsolute = Path_MakeAbsolute(sRelativePath, sBasePath);
if (sAbsolute.empty())
return sAbsolute;
return std::string( FILE_URL_PREFIX ) + sAbsolute;
return std::string(FILE_URL_PREFIX) + sAbsolute;
}
}
// -----------------------------------------------------------------------------------------------------
// Purpose: Strips off file:// off a URL and returns the path. For other kinds of URLs an empty string is returned
// -----------------------------------------------------------------------------------------------------
std::string Path_UrlToFilePath( const std::string & sFileUrl )
std::string Path_UrlToFilePath(const std::string &sFileUrl)
{
if ( !strnicmp( sFileUrl.c_str(), FILE_URL_PREFIX, strlen( FILE_URL_PREFIX ) ) )
if (!strnicmp(sFileUrl.c_str(), FILE_URL_PREFIX, strlen(FILE_URL_PREFIX)))
{
return sFileUrl.c_str() + strlen( FILE_URL_PREFIX );
return sFileUrl.c_str() + strlen(FILE_URL_PREFIX);
}
else
{
@@ -779,42 +758,41 @@ std::string Path_UrlToFilePath( const std::string & sFileUrl )
}
}
// -----------------------------------------------------------------------------------------------------
// Purpose: Returns the root of the directory the system wants us to store user documents in
// -----------------------------------------------------------------------------------------------------
std::string GetUserDocumentsPath()
{
#if defined( WIN32 )
#if defined(WIN32)
WCHAR rwchPath[MAX_PATH];
if ( !SUCCEEDED( SHGetFolderPathW( NULL, CSIDL_MYDOCUMENTS | CSIDL_FLAG_CREATE, NULL, 0, rwchPath ) ) )
if (!SUCCEEDED(SHGetFolderPathW(NULL, CSIDL_MYDOCUMENTS | CSIDL_FLAG_CREATE, NULL, 0, rwchPath)))
{
return "";
}
// Convert the path to UTF-8 and store in the output
std::string sUserPath = UTF16to8( rwchPath );
std::string sUserPath = UTF16to8(rwchPath);
return sUserPath;
#elif defined( OSX )
@autoreleasepool {
NSArray *paths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES );
if ( [paths count] == 0 )
#elif defined(OSX)
@autoreleasepool
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
if ([paths count] == 0)
{
return "";
}
return [[paths objectAtIndex:0] UTF8String];
}
#elif defined( LINUX )
#elif defined(LINUX)
// @todo: not solved/changed as part of OSX - still not real - just removed old class based steam cut and paste
const char *pchHome = getenv( "HOME" );
if ( pchHome == NULL )
const char *pchHome = getenv("HOME");
if (pchHome == NULL)
{
return "";
}
return pchHome;
#endif
}