pathtools_public.h (6145B)
1 //========= Copyright Valve Corporation ============// 2 #pragma once 3 4 #include <string> 5 #include <stdint.h> 6 7 /** Returns the path (including filename) to the current executable */ 8 std::string Path_GetExecutablePath(); 9 10 /** Returns the path of the current working directory */ 11 std::string Path_GetWorkingDirectory(); 12 13 /** Sets the path of the current working directory. Returns true if this was successful. */ 14 bool Path_SetWorkingDirectory( const std::string & sPath ); 15 16 /** Gets the path to a temporary directory. */ 17 std::string Path_GetTemporaryDirectory(); 18 19 /** returns the path (including filename) of the current shared lib or DLL */ 20 std::string Path_GetThisModulePath(); 21 22 /** Returns the specified path without its filename. 23 * If slash is unspecified the native path separator of the current platform 24 * will be used. */ 25 std::string Path_StripFilename( const std::string & sPath, char slash = 0 ); 26 27 /** returns just the filename from the provided full or relative path. */ 28 std::string Path_StripDirectory( const std::string & sPath, char slash = 0 ); 29 30 /** returns just the filename with no extension of the provided filename. 31 * If there is a path the path is left intact. */ 32 std::string Path_StripExtension( const std::string & sPath ); 33 34 /** returns just extension of the provided filename (if any). */ 35 std::string Path_GetExtension( const std::string & sPath ); 36 37 /** Returns true if the path is absolute */ 38 bool Path_IsAbsolute( const std::string & sPath ); 39 40 /** Makes an absolute path from a relative path and a base path */ 41 std::string Path_MakeAbsolute( const std::string & sRelativePath, const std::string & sBasePath ); 42 43 /** Fixes the directory separators for the current platform. 44 * If slash is unspecified the native path separator of the current platform 45 * will be used. */ 46 std::string Path_FixSlashes( const std::string & sPath, char slash = 0 ); 47 48 /** Returns the path separator for the current platform */ 49 char Path_GetSlash(); 50 51 /** Jams two paths together with the right kind of slash */ 52 std::string Path_Join( const std::string & first, const std::string & second, char slash = 0 ); 53 std::string Path_Join( const std::string & first, const std::string & second, const std::string & third, char slash = 0 ); 54 std::string Path_Join( const std::string & first, const std::string & second, const std::string & third, const std::string &fourth, char slash = 0 ); 55 std::string Path_Join( 56 const std::string & first, 57 const std::string & second, 58 const std::string & third, 59 const std::string & fourth, 60 const std::string & fifth, 61 char slash = 0 ); 62 63 64 /** Removes redundant <dir>/.. elements in the path. Returns an empty path if the 65 * specified path has a broken number of directories for its number of ..s. 66 * If slash is unspecified the native path separator of the current platform 67 * will be used. */ 68 std::string Path_Compact( const std::string & sRawPath, char slash = 0 ); 69 70 /** Returns true if these two paths are the same without respect for internal . or .. 71 * sequences, slash type, or case (on case-insensitive platforms). */ 72 bool Path_IsSamePath( const std::string & sPath1, const std::string & sPath2 ); 73 74 //** Removed trailing slashes */ 75 std::string Path_RemoveTrailingSlash( const std::string & sRawPath, char slash = 0 ); 76 77 /** returns true if the specified path exists and is a directory */ 78 bool Path_IsDirectory( const std::string & sPath ); 79 80 /** returns true if the specified path represents an app bundle */ 81 bool Path_IsAppBundle( const std::string & sPath ); 82 83 /** returns true if the the path exists */ 84 bool Path_Exists( const std::string & sPath ); 85 86 /** Helper functions to find parent directories or subdirectories of parent directories */ 87 std::string Path_FindParentDirectoryRecursively( const std::string &strStartDirectory, const std::string &strDirectoryName ); 88 std::string Path_FindParentSubDirectoryRecursively( const std::string &strStartDirectory, const std::string &strDirectoryName ); 89 90 /** Make a text file writable. */ 91 bool Path_MakeWritable( const std::string &strFilename ); 92 93 /** Path operations to read or write text/binary files */ 94 unsigned char * Path_ReadBinaryFile( const std::string &strFilename, int *pSize ); 95 uint32_t Path_ReadBinaryFile( const std::string &strFilename, unsigned char *pBuffer, uint32_t unSize ); 96 bool Path_WriteBinaryFile( const std::string &strFilename, unsigned char *pData, unsigned nSize ); 97 std::string Path_ReadTextFile( const std::string &strFilename ); 98 bool Path_WriteStringToTextFile( const std::string &strFilename, const char *pchData ); 99 bool Path_WriteStringToTextFileAtomic( const std::string &strFilename, const char *pchData ); 100 101 // Mozilla: see mozilla.patch for more details 102 /** Returns a file:// url for paths, or an http or https url if that's what was provided */ 103 // std::string Path_FilePathToUrl( const std::string & sRelativePath, const std::string & sBasePath ); 104 105 /** Strips off file:// off a URL and returns the path. For other kinds of URLs an empty string is returned */ 106 std::string Path_UrlToFilePath( const std::string & sFileUrl ); 107 108 /** Returns the root of the directory the system wants us to store user documents in */ 109 std::string GetUserDocumentsPath(); 110 111 /** deletes / unlinks a single file */ 112 bool Path_UnlinkFile( const std::string &strFilename ); 113 114 #ifndef MAX_UNICODE_PATH 115 #define MAX_UNICODE_PATH 32767 116 #endif 117 118 #ifndef MAX_UNICODE_PATH_IN_UTF8 119 #define MAX_UNICODE_PATH_IN_UTF8 (MAX_UNICODE_PATH * 4) 120 #endif 121 122 //----------------------------------------------------------------------------- 123 #if defined(WIN32) 124 #define DYNAMIC_LIB_EXT ".dll" 125 #define PROGRAM_EXT ".exe" 126 #ifdef _WIN64 127 #define PLATSUBDIR "win64" 128 #else 129 #define PLATSUBDIR "win32" 130 #endif 131 #elif defined(OSX) 132 #define DYNAMIC_LIB_EXT ".dylib" 133 #define PLATSUBDIR "osx32" 134 #define PROGRAM_EXT "" 135 #elif defined(LINUX) 136 #define DYNAMIC_LIB_EXT ".so" 137 #define PROGRAM_EXT "" 138 #if defined( LINUX32 ) 139 #define PLATSUBDIR "linux32" 140 #elif defined( ANDROIDARM64 ) 141 #define PLATSUBDIR "androidarm64" 142 #elif defined( LINUXARM64 ) 143 #define PLATSUBDIR "linuxarm64" 144 #else 145 #define PLATSUBDIR "linux64" 146 #endif 147 #else 148 #warning "Unknown platform for PLATSUBDIR" 149 #define PLATSUBDIR "unknown_platform" 150 #endif