1 // Created on: 1992-02-18 2 // Created by: Stephan GARNAUD (ARM) 3 // Copyright (c) 1992-1999 Matra Datavision 4 // Copyright (c) 1999-2014 OPEN CASCADE SAS 5 // 6 // This file is part of Open CASCADE Technology software library. 7 // 8 // This library is free software; you can redistribute it and/or modify it under 9 // the terms of the GNU Lesser General Public License version 2.1 as published 10 // by the Free Software Foundation, with special exception defined in the file 11 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT 12 // distribution for complete text of the license and disclaimer of any warranty. 13 // 14 // Alternatively, this file may be used under the terms of Open CASCADE 15 // commercial license or contractual agreement. 16 17 #ifndef _OSD_Path_HeaderFile 18 #define _OSD_Path_HeaderFile 19 20 #include <Standard.hxx> 21 #include <Standard_DefineAlloc.hxx> 22 #include <Standard_Handle.hxx> 23 #include <TCollection_AsciiString.hxx> 24 #include <OSD_SysType.hxx> 25 26 class OSD_Path 27 { 28 public: 29 DEFINE_STANDARD_ALLOC 30 31 //! Creates a Path object initialized to an empty string. 32 //! i.e. current directory. 33 Standard_EXPORT OSD_Path(); 34 35 //! Creates a Path object initialized by dependant path. 36 //! ex: OSD_Path me ("/usr/bin/myprog.sh",OSD_UnixBSD); 37 //! 38 //! OSD_Path me ("sys$common:[syslib]cc.exe",OSD_OSF) will 39 //! raise a ProgramError due to invalid name for this 40 //! type of system. 41 //! In order to avoid a 'ProgramError' , use IsValid(...) 42 //! to ensure you the validity of <aDependentName>. 43 //! Raises ConstructionError when the path is either null 44 //! or contains characters not in range of ' '...'~'. 45 Standard_EXPORT OSD_Path(const TCollection_AsciiString& aDependentName, const OSD_SysType aSysType = OSD_Default); 46 47 //! Initializes a system independent path. 48 //! By default , the Path conversion will be assumed using 49 //! currently used system. 50 //! A special syntax is used to specify a "aTrek" in an 51 //! independent manner : 52 //! a "|" represents directory separator 53 //! a "^" means directory above (father) 54 //! examples: 55 //! "|usr|bin" - On UNIX -> "/usr/bin" 56 //! - On VMS -> "[usr.bin]" 57 //! - On MSDOS-> "\usr\bin" 58 //! - On MacOs-> ": usr : bin" 59 //! 60 //! "^|rep" - On UNIX -> "../rep" 61 //! - On VMS -> "[-.rep]" 62 //! - On MSDOS -> "..\rep" 63 //! - On MacOS-> ":: rep" 64 //! 65 //! "subdir|" - On UNIX -> "subdir/" 66 //! - On VMS -> "[.subdir.]" 67 Standard_EXPORT OSD_Path(const TCollection_AsciiString& aNode, const TCollection_AsciiString& aUsername, const TCollection_AsciiString& aPassword, const TCollection_AsciiString& aDisk, const TCollection_AsciiString& aTrek, const TCollection_AsciiString& aName, const TCollection_AsciiString& anExtension); 68 69 //! Gets each component of a path. 70 Standard_EXPORT void Values (TCollection_AsciiString& aNode, TCollection_AsciiString& aUsername, TCollection_AsciiString& aPassword, TCollection_AsciiString& aDisk, TCollection_AsciiString& aTrek, TCollection_AsciiString& aName, TCollection_AsciiString& anExtension) const; 71 72 //! Sets each component of a path. 73 Standard_EXPORT void SetValues (const TCollection_AsciiString& aNode, const TCollection_AsciiString& aUsername, const TCollection_AsciiString& aPassword, const TCollection_AsciiString& aDisk, const TCollection_AsciiString& aTrek, const TCollection_AsciiString& aName, const TCollection_AsciiString& anExtension); 74 75 //! Returns system dependent path 76 //! <aType> is one among Unix,VMS ... 77 //! This function is not private because you may need to 78 //! display system dependent path on a front-end. 79 //! It can be useful when communicating with another system. 80 //! For instance when you want to communicate between VMS and Unix 81 //! to transfer files, or to do a remote procedure call 82 //! using files. 83 //! example : 84 //! OSD_Path myPath ("sparc4", "sga", "secret_passwd", 85 //! "$5$dkb100","|users|examples"); 86 //! Internal ( Dependent_name ); 87 //! On UNIX sga"secret_passwd"@sparc4:/users/examples 88 //! On VMS sparc4"sga secret_passwd"::$5$dkb100:[users.examples] 89 //! Sets each component of a Path giving its system dependent name. 90 Standard_EXPORT void SystemName (TCollection_AsciiString& FullName, const OSD_SysType aType = OSD_Default) const; 91 92 //! Returns system dependent path resolving logical symbols. 93 Standard_EXPORT void ExpandedName (TCollection_AsciiString& aName); 94 95 //! Returns TRUE if <theDependentName> is valid for this SysType. 96 Standard_EXPORT static Standard_Boolean IsValid (const TCollection_AsciiString& theDependentName, const OSD_SysType theSysType = OSD_Default); 97 98 //! This removes the last directory name in <aTrek> 99 //! and returns result. 100 //! ex: me = "|usr|bin|todo.sh" 101 //! me.UpTrek() gives me = "|usr|todo.sh" 102 //! if <me> contains "|", me.UpTrek() will give again "|" 103 //! without any error. 104 Standard_EXPORT void UpTrek(); 105 106 //! This appends a directory name into the Trek. 107 //! ex: me = "|usr|todo.sh" 108 //! me.DownTrek("bin") gives me = "|usr|bin|todo.sh". 109 Standard_EXPORT void DownTrek (const TCollection_AsciiString& aName); 110 111 //! Returns number of components in Trek of <me>. 112 //! ex: me = "|usr|sys|etc|bin" 113 //! me.TrekLength() returns 4. 114 Standard_EXPORT Standard_Integer TrekLength() const; 115 116 //! This removes a component of Trek in <me> at position <where>. 117 //! The first component of Trek is numbered 1. 118 //! ex: me = "|usr|bin|" 119 //! me.RemoveATrek(1) gives me = "|bin|" 120 //! To avoid a 'NumericError' because of a bad <where>, use 121 //! TrekLength() to know number of components of Trek in <me>. 122 Standard_EXPORT void RemoveATrek (const Standard_Integer where); 123 124 //! This removes <aName> from <me> in Trek. 125 //! No error is raised if <aName> is not in <me>. 126 //! ex: me = "|usr|sys|etc|doc" 127 //! me.RemoveATrek("sys") gives me = "|usr|etc|doc". 128 Standard_EXPORT void RemoveATrek (const TCollection_AsciiString& aName); 129 130 //! Returns component of Trek in <me> at position <where>. 131 //! ex: me = "|usr|bin|sys|" 132 //! me.TrekValue(2) returns "bin" 133 Standard_EXPORT TCollection_AsciiString TrekValue (const Standard_Integer where) const; 134 135 //! This inserts <aName> at position <where> into Trek of <me>. 136 //! ex: me = "|usr|etc|" 137 //! me.InsertATrek("sys",2) gives me = "|usr|sys|etc" 138 Standard_EXPORT void InsertATrek (const TCollection_AsciiString& aName, const Standard_Integer where); 139 140 //! Returns Node of <me>. 141 Standard_EXPORT TCollection_AsciiString Node() const; 142 143 //! Returns UserName of <me>. 144 Standard_EXPORT TCollection_AsciiString UserName() const; 145 146 //! Returns Password of <me>. 147 Standard_EXPORT TCollection_AsciiString Password() const; 148 149 //! Returns Disk of <me>. 150 Standard_EXPORT TCollection_AsciiString Disk() const; 151 152 //! Returns Trek of <me>. 153 Standard_EXPORT TCollection_AsciiString Trek() const; 154 155 //! Returns file name of <me>. 156 //! If <me> hasn't been initialized, it returns an empty AsciiString. 157 Standard_EXPORT TCollection_AsciiString Name() const; 158 159 //! Returns my extension name. 160 //! This returns an empty string if path contains no file name. 161 Standard_EXPORT TCollection_AsciiString Extension() const; 162 163 //! Sets Node of <me>. 164 Standard_EXPORT void SetNode (const TCollection_AsciiString& aName); 165 166 //! Sets UserName of <me>. 167 Standard_EXPORT void SetUserName (const TCollection_AsciiString& aName); 168 169 //! Sets Password of <me>. 170 Standard_EXPORT void SetPassword (const TCollection_AsciiString& aName); 171 172 //! Sets Disk of <me>. 173 Standard_EXPORT void SetDisk (const TCollection_AsciiString& aName); 174 175 //! Sets Trek of <me>. 176 Standard_EXPORT void SetTrek (const TCollection_AsciiString& aName); 177 178 //! Sets file name of <me>. 179 //! If <me> hasn't been initialized, it returns an empty AsciiString. 180 Standard_EXPORT void SetName (const TCollection_AsciiString& aName); 181 182 //! Sets my extension name. 183 Standard_EXPORT void SetExtension (const TCollection_AsciiString& aName); 184 185 //! Finds the full path of an executable file, like the 186 //! "which" Unix utility. Uses the path environment variable. 187 //! Returns False if executable file not found. 188 Standard_EXPORT Standard_Boolean LocateExecFile (OSD_Path& aPath); 189 190 public: 191 192 //! Returns the relative file path between the absolute directory 193 //! path <DirPath> and the absolute file path <AbsFilePath>. 194 //! If <DirPath> starts with "/", paths are handled as 195 //! on Unix, if it starts with a letter followed by ":", as on 196 //! WNT. In particular on WNT directory names are not key sensitive. 197 //! If handling fails, an empty string is returned. 198 Standard_EXPORT static TCollection_AsciiString RelativePath (const TCollection_AsciiString& DirPath, const TCollection_AsciiString& AbsFilePath); 199 200 //! Returns the absolute file path from the absolute directory path 201 //! <DirPath> and the relative file path returned by RelativePath(). 202 //! If the RelFilePath is an absolute path, it is returned and the 203 //! directory path is ignored. 204 //! If handling fails, an empty string is returned. 205 Standard_EXPORT static TCollection_AsciiString AbsolutePath (const TCollection_AsciiString& DirPath, const TCollection_AsciiString& RelFilePath); 206 207 //! Split absolute filepath into folder path and file name. 208 //! Example: IN theFilePath ='/media/cdrom/image.jpg' 209 //! OUT theFolder ='/media/cdrom/' 210 //! OUT theFileName ='image.jpg' 211 //! @param theFilePath [in] file path 212 //! @param theFolder [out] folder path (with trailing separator) 213 //! @param theFileName [out] file name 214 Standard_EXPORT static void FolderAndFileFromPath (const TCollection_AsciiString& theFilePath, 215 TCollection_AsciiString& theFolder, 216 TCollection_AsciiString& theFileName); 217 218 //! Detect absolute DOS-path also used in Windows. 219 //! The total path length is limited to 256 characters. 220 //! Sample path: 221 //! C:\folder\file 222 //! @return true if DOS path syntax detected. IsDosPath(const char * thePath)223 static Standard_Boolean IsDosPath (const char* thePath) { return thePath[0] != '\0' && thePath[1] == ':'; } 224 225 //! Detect extended-length NT path (can be only absolute). 226 //! Approximate maximum path is 32767 characters. 227 //! Sample path: 228 //! \\?\D:\very long path 229 //! File I/O functions in the Windows API convert "/" to "\" as part of converting the name to an NT-style name, except when using the "\\?\" prefix. 230 //! @return true if extended-length NT path syntax detected. IsNtExtendedPath(const char * thePath)231 static Standard_Boolean IsNtExtendedPath (const char* thePath) { return ::memcmp (thePath, "\\\\?\\", 4) == 0; } 232 233 //! UNC is a naming convention used primarily to specify and map network drives in Microsoft Windows. 234 //! Sample path: 235 //! \\server\share\file 236 //! @return true if UNC path syntax detected. IsUncPath(const char * thePath)237 static Standard_Boolean IsUncPath (const char* thePath) 238 { 239 if (::memcmp (thePath, "\\\\", 2) == 0) 240 { 241 return thePath[2] != '?' 242 || IsUncExtendedPath (thePath); 243 } 244 return ::memcmp (thePath, "//", 2) == 0; 245 } 246 247 //! Detect extended-length UNC path. 248 //! Sample path: 249 //! \\?\UNC\server\share 250 //! @return true if extended-length UNC path syntax detected. IsUncExtendedPath(const char * thePath)251 static Standard_Boolean IsUncExtendedPath (const char* thePath) { return ::memcmp (thePath, "\\\\?\\UNC\\", 8) == 0; } 252 253 //! Detect absolute UNIX-path. 254 //! Sample path: 255 //! /media/cdrom/file 256 //! @return true if UNIX path syntax detected. IsUnixPath(const char * thePath)257 static Standard_Boolean IsUnixPath (const char* thePath) { return thePath[0] == '/' && thePath[1] != '/'; } 258 259 //! Detect special URLs on Android platform. 260 //! Sample path: 261 //! content://filename 262 //! @return true if content path syntax detected IsContentProtocolPath(const char * thePath)263 static Standard_Boolean IsContentProtocolPath (const char* thePath) { return ::memcmp (thePath, "content://", 10) == 0; } 264 265 //! Detect remote protocol path (http / ftp / ...). 266 //! Actually shouldn't be remote... 267 //! Sample path: 268 //! http://domain/path/file 269 //! @return true if remote protocol path syntax detected. IsRemoteProtocolPath(const char * thePath)270 static Standard_Boolean IsRemoteProtocolPath (const char* thePath) 271 { 272 const char* anIter = thePath; 273 if (*anIter == ':') 274 { 275 return false; 276 } 277 for (; *anIter != '\0'; ++anIter) 278 { 279 if (*anIter == ':') 280 { 281 return *(++anIter) == '/' 282 && *(++anIter) == '/'; 283 } 284 } 285 return false; 286 } 287 288 //! Method to recognize path is absolute or not. 289 //! Detection is based on path syntax - no any filesystem / network access performed. 290 //! @return true if path is incomplete (relative). IsRelativePath(const char * thePath)291 static Standard_Boolean IsRelativePath (const char* thePath) 292 { 293 return !IsUncPath (thePath) 294 && !IsDosPath (thePath) 295 && !IsNtExtendedPath (thePath) 296 && !IsUnixPath (thePath) 297 && !IsRemoteProtocolPath (thePath); 298 } 299 300 //! Method to recognize path is absolute or not. 301 //! Detection is based on path syntax - no any filesystem / network access performed. 302 //! @return true if path is complete (absolute) IsAbsolutePath(const char * thePath)303 static Standard_Boolean IsAbsolutePath (const char* thePath) 304 { 305 return !IsRelativePath (thePath); 306 } 307 308 private: 309 310 TCollection_AsciiString myNode; 311 TCollection_AsciiString myUserName; 312 TCollection_AsciiString myPassword; 313 TCollection_AsciiString myDisk; 314 TCollection_AsciiString myTrek; 315 TCollection_AsciiString myName; 316 TCollection_AsciiString myExtension; 317 Standard_Boolean myUNCFlag; 318 OSD_SysType mySysDep; 319 320 }; 321 322 #endif // _OSD_Path_HeaderFile 323