Back to home page

EIC code displayed by LXR

 
 

    


Warning, file /include/opencascade/OSD_Path.hxx was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 // Created on: 1992-02-18
0002 // Created by: Stephan GARNAUD (ARM)
0003 // Copyright (c) 1992-1999 Matra Datavision
0004 // Copyright (c) 1999-2014 OPEN CASCADE SAS
0005 //
0006 // This file is part of Open CASCADE Technology software library.
0007 //
0008 // This library is free software; you can redistribute it and/or modify it under
0009 // the terms of the GNU Lesser General Public License version 2.1 as published
0010 // by the Free Software Foundation, with special exception defined in the file
0011 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
0012 // distribution for complete text of the license and disclaimer of any warranty.
0013 //
0014 // Alternatively, this file may be used under the terms of Open CASCADE
0015 // commercial license or contractual agreement.
0016 
0017 #ifndef _OSD_Path_HeaderFile
0018 #define _OSD_Path_HeaderFile
0019 
0020 #include <Standard.hxx>
0021 #include <Standard_DefineAlloc.hxx>
0022 #include <Standard_Handle.hxx>
0023 #include <TCollection_AsciiString.hxx>
0024 #include <OSD_SysType.hxx>
0025 
0026 class OSD_Path 
0027 {
0028 public:
0029   DEFINE_STANDARD_ALLOC
0030 
0031   //! Creates a Path object initialized to an empty string.
0032   //! i.e. current directory.
0033   Standard_EXPORT OSD_Path();
0034   
0035   //! Creates a Path object initialized by dependent path.
0036   //! ex: OSD_Path me ("/usr/bin/myprog.sh",OSD_UnixBSD);
0037   //!
0038   //! OSD_Path me ("sys$common:[syslib]cc.exe",OSD_OSF) will
0039   //! raise a ProgramError due to invalid name for this
0040   //! type of system.
0041   //! In order to avoid a 'ProgramError' , use IsValid(...)
0042   //! to ensure you the validity of <aDependentName>.
0043   //! Raises ConstructionError when the path is either null
0044   //! or contains characters not in range of ' '...'~'.
0045   Standard_EXPORT OSD_Path(const TCollection_AsciiString& aDependentName, const OSD_SysType aSysType = OSD_Default);
0046   
0047   //! Initializes a system independent path.
0048   //! By default , the Path conversion will be assumed using
0049   //! currently used system.
0050   //! A special syntax is used to specify a "aTrek" in an
0051   //! independent manner :
0052   //! a "|" represents directory separator
0053   //! a "^" means directory above (father)
0054   //! examples:
0055   //! "|usr|bin" - On UNIX -> "/usr/bin"
0056   //! - On VMS  -> "[usr.bin]"
0057   //! - On MSDOS-> "\usr\bin"
0058   //! - On MacOs-> ": usr : bin"
0059   //!
0060   //! "^|rep"    - On UNIX -> "../rep"
0061   //! - On VMS  -> "[-.rep]"
0062   //! - On MSDOS -> "..\rep"
0063   //! - On MacOS->  ":: rep"
0064   //!
0065   //! "subdir|" - On UNIX -> "subdir/"
0066   //! - On VMS  -> "[.subdir.]"
0067   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);
0068   
0069   //! Gets each component of a path.
0070   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;
0071   
0072   //! Sets each component of a path.
0073   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);
0074   
0075   //! Returns system dependent path
0076   //! <aType> is one among Unix,VMS ...
0077   //! This function is not private because you may need to
0078   //! display system dependent path on a front-end.
0079   //! It can be useful when communicating with another system.
0080   //! For instance when you want to communicate between VMS and Unix
0081   //! to transfer files, or to do a remote procedure call
0082   //! using files.
0083   //! example :
0084   //! OSD_Path myPath ("sparc4", "sga", "secret_passwd",
0085   //! "$5$dkb100","|users|examples");
0086   //! Internal ( Dependent_name );
0087   //! On UNIX  sga"secret_passwd"@sparc4:/users/examples
0088   //! On VMS   sparc4"sga secret_passwd"::$5$dkb100:[users.examples]
0089   //! Sets each component of a Path giving its system dependent name.
0090   Standard_EXPORT void SystemName (TCollection_AsciiString& FullName, const OSD_SysType aType = OSD_Default) const;
0091   
0092   //! Returns system dependent path resolving logical symbols.
0093   Standard_EXPORT void ExpandedName (TCollection_AsciiString& aName);
0094   
0095   //! Returns TRUE if <theDependentName> is valid for this SysType.
0096   Standard_EXPORT static Standard_Boolean IsValid (const TCollection_AsciiString& theDependentName, const OSD_SysType theSysType = OSD_Default);
0097   
0098   //! This removes the last directory name in <aTrek>
0099   //! and returns result.
0100   //! ex:  me = "|usr|bin|todo.sh"
0101   //! me.UpTrek() gives me = "|usr|todo.sh"
0102   //! if <me> contains "|", me.UpTrek() will give again "|"
0103   //! without any error.
0104   Standard_EXPORT void UpTrek();
0105   
0106   //! This appends a directory name into the Trek.
0107   //! ex: me = "|usr|todo.sh"
0108   //! me.DownTrek("bin") gives me = "|usr|bin|todo.sh".
0109   Standard_EXPORT void DownTrek (const TCollection_AsciiString& aName);
0110   
0111   //! Returns number of components in Trek of <me>.
0112   //! ex: me = "|usr|sys|etc|bin"
0113   //! me.TrekLength() returns 4.
0114   Standard_EXPORT Standard_Integer TrekLength() const;
0115   
0116   //! This removes a component of Trek in <me> at position <where>.
0117   //! The first component of Trek is numbered 1.
0118   //! ex:   me = "|usr|bin|"
0119   //! me.RemoveATrek(1) gives me = "|bin|"
0120   //! To avoid a 'NumericError' because of a bad <where>, use
0121   //! TrekLength() to know number of components of Trek in <me>.
0122   Standard_EXPORT void RemoveATrek (const Standard_Integer where);
0123   
0124   //! This removes <aName> from <me> in Trek.
0125   //! No error is raised if <aName> is not in <me>.
0126   //! ex:  me = "|usr|sys|etc|doc"
0127   //! me.RemoveATrek("sys") gives me = "|usr|etc|doc".
0128   Standard_EXPORT void RemoveATrek (const TCollection_AsciiString& aName);
0129   
0130   //! Returns component of Trek in <me> at position <where>.
0131   //! ex:  me = "|usr|bin|sys|"
0132   //! me.TrekValue(2) returns "bin"
0133   Standard_EXPORT TCollection_AsciiString TrekValue (const Standard_Integer where) const;
0134   
0135   //! This inserts <aName> at position <where> into Trek of <me>.
0136   //! ex:  me = "|usr|etc|"
0137   //! me.InsertATrek("sys",2) gives me = "|usr|sys|etc"
0138   Standard_EXPORT void InsertATrek (const TCollection_AsciiString& aName, const Standard_Integer where);
0139   
0140   //! Returns Node of <me>.
0141   Standard_EXPORT TCollection_AsciiString Node() const;
0142   
0143   //! Returns UserName of <me>.
0144   Standard_EXPORT TCollection_AsciiString UserName() const;
0145   
0146   //! Returns Password of <me>.
0147   Standard_EXPORT TCollection_AsciiString Password() const;
0148   
0149   //! Returns Disk of <me>.
0150   Standard_EXPORT TCollection_AsciiString Disk() const;
0151   
0152   //! Returns Trek of <me>.
0153   Standard_EXPORT TCollection_AsciiString Trek() const;
0154   
0155   //! Returns file name of <me>.
0156   //! If <me> hasn't been initialized, it returns an empty AsciiString.
0157   Standard_EXPORT TCollection_AsciiString Name() const;
0158   
0159   //! Returns my extension name.
0160   //! This returns an empty string if path contains no file name.
0161   Standard_EXPORT TCollection_AsciiString Extension() const;
0162   
0163   //! Sets Node of <me>.
0164   Standard_EXPORT void SetNode (const TCollection_AsciiString& aName);
0165   
0166   //! Sets UserName of <me>.
0167   Standard_EXPORT void SetUserName (const TCollection_AsciiString& aName);
0168   
0169   //! Sets Password of <me>.
0170   Standard_EXPORT void SetPassword (const TCollection_AsciiString& aName);
0171   
0172   //! Sets Disk of <me>.
0173   Standard_EXPORT void SetDisk (const TCollection_AsciiString& aName);
0174   
0175   //! Sets Trek of <me>.
0176   Standard_EXPORT void SetTrek (const TCollection_AsciiString& aName);
0177   
0178   //! Sets file name of <me>.
0179   //! If <me> hasn't been initialized, it returns an empty AsciiString.
0180   Standard_EXPORT void SetName (const TCollection_AsciiString& aName);
0181   
0182   //! Sets my extension name.
0183   Standard_EXPORT void SetExtension (const TCollection_AsciiString& aName);
0184   
0185   //! Finds the full path of an executable file, like the
0186   //! "which" Unix utility. Uses the path environment variable.
0187   //! Returns False if executable file not found.
0188   Standard_EXPORT Standard_Boolean LocateExecFile (OSD_Path& aPath);
0189 
0190 public:
0191 
0192   //! Returns the relative file path between the absolute directory
0193   //! path <DirPath>  and the absolute file path <AbsFilePath>.
0194   //! If <DirPath> starts with "/", paths are handled as
0195   //! on Unix, if it starts with a letter followed by ":", as on
0196   //! WNT. In particular on WNT directory names are not key sensitive.
0197   //! If handling fails, an empty string is returned.
0198   Standard_EXPORT static TCollection_AsciiString RelativePath (const TCollection_AsciiString& DirPath, const TCollection_AsciiString& AbsFilePath);
0199   
0200   //! Returns the absolute file path from the absolute directory path
0201   //! <DirPath> and the relative file path returned by RelativePath().
0202   //! If the RelFilePath is an absolute path, it is returned and the
0203   //! directory path is ignored.
0204   //! If handling fails, an empty string is returned.
0205   Standard_EXPORT static TCollection_AsciiString AbsolutePath (const TCollection_AsciiString& DirPath, const TCollection_AsciiString& RelFilePath);
0206 
0207   //! Split absolute filepath into folder path and file name.
0208   //! Example: IN  theFilePath ='/media/cdrom/image.jpg'
0209   //!          OUT theFolder   ='/media/cdrom/'
0210   //!          OUT theFileName ='image.jpg'
0211   //! @param theFilePath [in]  file path
0212   //! @param theFolder   [out] folder path (with trailing separator)
0213   //! @param theFileName [out] file name
0214   Standard_EXPORT static void FolderAndFileFromPath (const TCollection_AsciiString& theFilePath,
0215                                                      TCollection_AsciiString&       theFolder,
0216                                                      TCollection_AsciiString&       theFileName);
0217 
0218   //! Return file extension from the name in lower case.
0219   //! Extension is expected to be within 20-symbols length, and determined as file name tail after last dot.
0220   //! Example: IN  theFilePath ='Image.sbs.JPG'
0221   //!          OUT theName     ='Image.sbs'
0222   //!          OUT theFileName ='jpg'
0223   //! @param theFilePath  [in]  file path
0224   //! @param theName      [out] file name without extension
0225   //! @param theExtension [out] file extension in lower case and without dot
0226   Standard_EXPORT static void FileNameAndExtension (const TCollection_AsciiString& theFilePath,
0227                                                     TCollection_AsciiString&       theName,
0228                                                     TCollection_AsciiString&       theExtension);
0229 
0230   //! Detect absolute DOS-path also used in Windows.
0231   //! The total path length is limited to 256 characters.
0232   //! Sample path:
0233   //!   C:\folder\file
0234   //! @return true if DOS path syntax detected.
0235   static Standard_Boolean IsDosPath (const char* thePath) { return thePath[0] != '\0' && thePath[1] == ':'; }
0236 
0237   //! Detect extended-length NT path (can be only absolute).
0238   //! Approximate maximum path is 32767 characters.
0239   //! Sample path:
0240   //!   \\?\D:\very long path
0241   //! 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.
0242   //! @return true if extended-length NT path syntax detected.
0243   static Standard_Boolean IsNtExtendedPath (const char* thePath) 
0244   { 
0245     return ::strncmp (thePath, "\\\\?\\", 4) == 0;
0246   }
0247 
0248   //! UNC is a naming convention used primarily to specify and map network drives in Microsoft Windows.
0249   //! Sample path:
0250   //!   \\server\share\file
0251   //! @return true if UNC path syntax detected.
0252   static Standard_Boolean IsUncPath (const char* thePath)
0253   {
0254     if (::strncmp (thePath, "\\\\", 2) == 0)
0255     {
0256       return thePath[2] != '?'
0257           || IsUncExtendedPath (thePath);
0258     }
0259     return ::strncmp (thePath, "//", 2) == 0;
0260   }
0261 
0262   //! Detect extended-length UNC path.
0263   //! Sample path:
0264   //!   \\?\UNC\server\share
0265   //! @return true if extended-length UNC path syntax detected.
0266   static Standard_Boolean IsUncExtendedPath (const char* thePath) 
0267   { 
0268     return ::strncmp (thePath, "\\\\?\\UNC\\", 8) == 0;
0269   }
0270 
0271   //! Detect absolute UNIX-path.
0272   //! Sample path:
0273   //!   /media/cdrom/file
0274   //! @return true if UNIX path syntax detected.
0275   static Standard_Boolean IsUnixPath (const char* thePath)
0276   {
0277     return thePath[0] == '/' && thePath[1] != '/';
0278   }
0279 
0280   //! Detect special URLs on Android platform.
0281   //! Sample path:
0282   //!   content://filename
0283   //! @return true if content path syntax detected
0284   static Standard_Boolean IsContentProtocolPath (const char* thePath)
0285   {
0286     return ::strncmp (thePath, "content://", 10) == 0;
0287   }
0288 
0289   //! Detect remote protocol path (http / ftp / ...).
0290   //! Actually shouldn't be remote...
0291   //! Sample path:
0292   //!   http://domain/path/file
0293   //! @return true if remote protocol path syntax detected.
0294   static Standard_Boolean IsRemoteProtocolPath (const char* thePath)
0295   {
0296     const char* anIter = thePath;
0297     if (*anIter == ':')
0298     {
0299       return false;
0300     }
0301     for (; *anIter != '\0'; ++anIter)
0302     {
0303       if (*anIter == ':')
0304       {
0305         return *(++anIter) == '/'
0306             && *(++anIter) == '/';
0307       }
0308     }
0309     return false;
0310   }
0311 
0312   //! Method to recognize path is absolute or not.
0313   //! Detection is based on path syntax - no any filesystem / network access performed.
0314   //! @return true if path is incomplete (relative).
0315   static Standard_Boolean IsRelativePath (const char* thePath)
0316   {
0317     return !IsUncPath  (thePath)
0318         && !IsDosPath  (thePath)
0319         && !IsNtExtendedPath (thePath)
0320         && !IsUnixPath (thePath)
0321         && !IsRemoteProtocolPath (thePath);
0322   }
0323 
0324   //! Method to recognize path is absolute or not.
0325   //! Detection is based on path syntax - no any filesystem / network access performed.
0326   //! @return true if path is complete (absolute)
0327   static Standard_Boolean IsAbsolutePath (const char* thePath)
0328   {
0329     return !IsRelativePath (thePath);
0330   }
0331 
0332 private:
0333 
0334   TCollection_AsciiString myNode;
0335   TCollection_AsciiString myUserName;
0336   TCollection_AsciiString myPassword;
0337   TCollection_AsciiString myDisk;
0338   TCollection_AsciiString myTrek;
0339   TCollection_AsciiString myName;
0340   TCollection_AsciiString myExtension;
0341   Standard_Boolean myUNCFlag;
0342   OSD_SysType mySysDep;
0343 
0344 };
0345 
0346 #endif // _OSD_Path_HeaderFile