0030953: Data Exchange - implement export of mesh data into glTF 2.0 format
[occt.git] / src / OSD / OSD_Path.hxx
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   //! Return file extension from the name in lower case.
219   //! Extension is expected to be within 20-symbols length, and determined as file name tail after last dot.
220   //! Example: IN  theFilePath ='Image.sbs.JPG'
221   //!          OUT theName     ='Image.sbs'
222   //!          OUT theFileName ='jpg'
223   //! @param theFilePath  [in]  file path
224   //! @param theName      [out] file name without extension
225   //! @param theExtension [out] file extension in lower case and without dot
226   Standard_EXPORT static void FileNameAndExtension (const TCollection_AsciiString& theFilePath,
227                                                     TCollection_AsciiString&       theName,
228                                                     TCollection_AsciiString&       theExtension);
229
230   //! Detect absolute DOS-path also used in Windows.
231   //! The total path length is limited to 256 characters.
232   //! Sample path:
233   //!   C:\folder\file
234   //! @return true if DOS path syntax detected.
235   static Standard_Boolean IsDosPath (const char* thePath) { return thePath[0] != '\0' && thePath[1] == ':'; }
236
237   //! Detect extended-length NT path (can be only absolute).
238   //! Approximate maximum path is 32767 characters.
239   //! Sample path:
240   //!   \\?\D:\very long path
241   //! 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.
242   //! @return true if extended-length NT path syntax detected.
243   static Standard_Boolean IsNtExtendedPath (const char* thePath) 
244   { 
245     return ::strncmp (thePath, "\\\\?\\", 4) == 0;
246   }
247
248   //! UNC is a naming convention used primarily to specify and map network drives in Microsoft Windows.
249   //! Sample path:
250   //!   \\server\share\file
251   //! @return true if UNC path syntax detected.
252   static Standard_Boolean IsUncPath (const char* thePath)
253   {
254     if (::strncmp (thePath, "\\\\", 2) == 0)
255     {
256       return thePath[2] != '?'
257           || IsUncExtendedPath (thePath);
258     }
259     return ::strncmp (thePath, "//", 2) == 0;
260   }
261
262   //! Detect extended-length UNC path.
263   //! Sample path:
264   //!   \\?\UNC\server\share
265   //! @return true if extended-length UNC path syntax detected.
266   static Standard_Boolean IsUncExtendedPath (const char* thePath) 
267   { 
268     return ::strncmp (thePath, "\\\\?\\UNC\\", 8) == 0;
269   }
270
271   //! Detect absolute UNIX-path.
272   //! Sample path:
273   //!   /media/cdrom/file
274   //! @return true if UNIX path syntax detected.
275   static Standard_Boolean IsUnixPath (const char* thePath)
276   {
277     return thePath[0] == '/' && thePath[1] != '/';
278   }
279
280   //! Detect special URLs on Android platform.
281   //! Sample path:
282   //!   content://filename
283   //! @return true if content path syntax detected
284   static Standard_Boolean IsContentProtocolPath (const char* thePath)
285   {
286     return ::strncmp (thePath, "content://", 10) == 0;
287   }
288
289   //! Detect remote protocol path (http / ftp / ...).
290   //! Actually shouldn't be remote...
291   //! Sample path:
292   //!   http://domain/path/file
293   //! @return true if remote protocol path syntax detected.
294   static Standard_Boolean IsRemoteProtocolPath (const char* thePath)
295   {
296     const char* anIter = thePath;
297     if (*anIter == ':')
298     {
299       return false;
300     }
301     for (; *anIter != '\0'; ++anIter)
302     {
303       if (*anIter == ':')
304       {
305         return *(++anIter) == '/'
306             && *(++anIter) == '/';
307       }
308     }
309     return false;
310   }
311
312   //! Method to recognize path is absolute or not.
313   //! Detection is based on path syntax - no any filesystem / network access performed.
314   //! @return true if path is incomplete (relative).
315   static Standard_Boolean IsRelativePath (const char* thePath)
316   {
317     return !IsUncPath  (thePath)
318         && !IsDosPath  (thePath)
319         && !IsNtExtendedPath (thePath)
320         && !IsUnixPath (thePath)
321         && !IsRemoteProtocolPath (thePath);
322   }
323
324   //! Method to recognize path is absolute or not.
325   //! Detection is based on path syntax - no any filesystem / network access performed.
326   //! @return true if path is complete (absolute)
327   static Standard_Boolean IsAbsolutePath (const char* thePath)
328   {
329     return !IsRelativePath (thePath);
330   }
331
332 private:
333
334   TCollection_AsciiString myNode;
335   TCollection_AsciiString myUserName;
336   TCollection_AsciiString myPassword;
337   TCollection_AsciiString myDisk;
338   TCollection_AsciiString myTrek;
339   TCollection_AsciiString myName;
340   TCollection_AsciiString myExtension;
341   Standard_Boolean myUNCFlag;
342   OSD_SysType mySysDep;
343
344 };
345
346 #endif // _OSD_Path_HeaderFile