2
0

path.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #ifndef _PATH_H_
  23. #define _PATH_H_
  24. #ifndef _TORQUE_STRING_H_
  25. #include "core/util/str.h"
  26. #endif
  27. namespace Torque
  28. {
  29. //-----------------------------------------------------------------------------
  30. /// FileSystem filename representation.
  31. /// Filenames has the following form: "root:path/file.ext"
  32. /// @ingroup UtilString
  33. class Path
  34. {
  35. public:
  36. enum Separator
  37. {
  38. #if defined(TORQUE_OS_WIN)
  39. OsSeparator = '\\'
  40. #else
  41. OsSeparator = '/'
  42. #endif
  43. };
  44. Path()
  45. : mIsDirtyFileName( true ),
  46. mIsDirtyPath( true )
  47. {
  48. }
  49. Path( const char *file )
  50. : mIsDirtyFileName( true ),
  51. mIsDirtyPath( true )
  52. {
  53. _split(file);
  54. }
  55. Path( const String &file )
  56. : mIsDirtyFileName( true ),
  57. mIsDirtyPath( true )
  58. {
  59. _split(file);
  60. }
  61. Path& operator = ( const String &file ) { _split(file); mIsDirtyPath = mIsDirtyFileName = true; return *this; }
  62. operator String() const { return getFullPath(); }
  63. bool operator == (const Path& path) const { return getFullPath().equal(path.getFullPath()); }
  64. bool operator != (const Path& path) const { return !(*this == path); }
  65. bool operator != (const String& path) const { return !(getFullPath().equal(path)); }
  66. bool isEmpty() const { return getFullPath().isEmpty(); }
  67. /// Join two path or file name components together.
  68. static String Join(const String&,String::ValueType,const String&);
  69. /// Replace all '\' with '/'
  70. static String CleanSeparators( String path );
  71. /// Remove "." and ".." relative paths.
  72. static String CompressPath( String path );
  73. /// Take two paths and return the relative path between them.
  74. static Path MakeRelativePath( const Path &makeRelative, const Path &relativeTo, U32 mode = String::NoCase );
  75. const String& getRoot() const { return mRoot; }
  76. const String& getPath() const { return mPath; }
  77. const String& getFileName() const { return mFile; }
  78. const String& getExtension() const { return mExt; }
  79. const String& getFullFileName() const;
  80. const String& getFullPath() const;
  81. /// Returns the full file path without the volume root.
  82. String getFullPathWithoutRoot() const;
  83. /// Returns the root and path.
  84. String getRootAndPath() const;
  85. const String& setRoot(const String &s);
  86. const String& setPath(const String &s);
  87. const String& setFileName(const String &s);
  88. const String& setExtension(const String &s);
  89. U32 getDirectoryCount() const;
  90. String getDirectory(U32) const;
  91. bool isDirectory() const;
  92. bool isRelative() const;
  93. bool isAbsolute() const;
  94. /// Appends the argument's path component to the object's
  95. /// path component. The object's root, filename and
  96. /// extension are unaffected.
  97. bool appendPath(const Path &path);
  98. private:
  99. String mRoot;
  100. String mPath;
  101. String mFile;
  102. String mExt;
  103. mutable String mFullFileName;
  104. mutable String mFullPath;
  105. mutable bool mIsDirtyFileName;
  106. mutable bool mIsDirtyPath;
  107. void _split(String name);
  108. String _join() const;
  109. };
  110. /// Convert file/path name to use platform standard path separator
  111. ///@ingroup VolumeSystem
  112. String PathToPlatform(String file);
  113. /// Convert file/path name to use OS standard path separator
  114. ///@ingroup VolumeSystem
  115. String PathToOS(String file);
  116. } // Namespace
  117. #endif