#ifdefined OSX #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 #endif
/** Returns the specified path without its filename */
std::string Path_StripFilename( const std::string & sPath, char slash )
{ if( slash == 0 )
slash = Path_GetSlash();
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 );
}
/** returns just the filename from the provided full or relative path. */
std::string Path_StripDirectory( const std::string & sPath, char slash )
{ if( slash == 0 )
slash = Path_GetSlash();
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() );
}
/** 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 )
{ for( std::string::const_reverse_iterator i = sPath.rbegin(); i != sPath.rend(); i++ )
{ if( *i == '.' )
{ return std::string( sPath.begin(), i.base() - 1 );
}
// if we find a slash there is no extension if( *i == '\\' || *i == '/' ) break;
}
// we didn't find an extension return sPath;
}
/** returns just extension of the provided filename (if any). */
std::string Path_GetExtension( const std::string & sPath )
{ for ( std::string::const_reverse_iterator i = sPath.rbegin(); i != sPath.rend(); i++ )
{ if ( *i == '.' )
{ return std::string( i.base(), sPath.end() );
}
// if we find a slash there is no extension if ( *i == '\\' || *i == '/' ) break;
}
/** 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 )
{ if( slash == 0 )
slash = Path_GetSlash();
// strip out all /./ for( std::string::size_type i = 0; (i + 3) < sPath.length(); )
{ if( sPath[ i ] == slash && sPath[ i+1 ] == '.' && sPath[ i+2 ] == slash )
{
sPath.replace( i, 3, sSlashString );
} else
{
++i;
}
}
// get rid of trailing /. but leave the path separator if( sPath.length() > 2 )
{
std::string::size_type len = sPath.length(); if( sPath[ len-1 ] == '.' && sPath[ len-2 ] == slash )
{
sPath.pop_back(); //Not sure why the following line of code was used for a while. It causes problems with strlen. //sPath[len-1] = 0; // for now, at least
}
}
// 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() )
{ 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 ) return"";
// remove everything from iDirStart to i+2
sPath.replace( iDirStart, (i - iDirStart) + 3, "" );
// start over
i = 0;
} else
{
++i;
}
}
return sPath;
}
/** Returns true if these two paths are the same without respect for internal . or ..
* sequences, slash type, or case (on case-insensitive platforms). */ bool Path_IsSamePath( const std::string & sPath1, const std::string & sPath2 )
{
std::string sCompact1 = Path_Compact( sPath1 );
std::string sCompact2 = Path_Compact( sPath2 ); #ifdefined(WIN32) return !stricmp( sCompact1.c_str(), sCompact2.c_str() ); #else return !strcmp( sCompact1.c_str(), sCompact2.c_str() ); #endif
}
/** Returns the path to the current DLL or exe */
std::string Path_GetThisModulePath()
{ // gets the path of vrclient.dll itself #ifdef WIN32
HMODULE hmodule = NULL;
#elifdefined( 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 ); return info.dli_fname; #endif
//----------------------------------------------------------------------------- // Purpose: reading and writing files in the vortex directory //----------------------------------------------------------------------------- unsignedchar * Path_ReadBinaryFile( const std::string &strFilename, int *pSize )
{
FILE *f; #ifdefined( POSIX )
f = fopen( strFilename.c_str(), "rb" ); #else
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 ); #endif
unsignedchar* buf = NULL;
if ( f != NULL )
{
fseek(f, 0, SEEK_END); int size = ftell(f);
fseek(f, 0, SEEK_SET);
size_t written = 0; if (f != NULL) {
written = fwrite(pData, sizeof(unsignedchar), nSize, f);
fclose(f);
}
return written == nSize ? true : false;
}
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; unsignedchar* buf = Path_ReadBinaryFile( strFilename, &size ); if (!buf) return"";
// convert CRLF -> LF
size_t outsize = 1; for (int i=1; i < size; i++)
{ if (buf[i] == '\n' && buf[i-1] == '\r') // CRLF
buf[outsize-1] = '\n'; // ->LF else
buf[outsize++] = buf[i]; // just copy
}
// ----------------------------------------------------------------------------------------------------- // Purpose: Returns the root of the directory the system wants us to store user documents in // -----------------------------------------------------------------------------------------------------
std::string GetUserDocumentsPath()
{ #ifdefined( WIN32 )
WCHAR rwchPath[MAX_PATH];
return [[paths objectAtIndex:0] UTF8String];
} #elifdefined( LINUX ) // @todo: not solved/changed as part of OSX - still not real - just removed old class based steam cut and paste constchar *pchHome = getenv( "HOME" ); if ( pchHome == NULL )
{ return"";
} return pchHome; #endif
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.