The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

131 lines
4.9KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. #ifndef __JUCER_RELATIVEPATH_JUCEHEADER__
  19. #define __JUCER_RELATIVEPATH_JUCEHEADER__
  20. //==============================================================================
  21. /** Manipulates a cross-platform partial file path. (Needed because File is designed
  22. for absolute paths on the active OS)
  23. */
  24. class RelativePath
  25. {
  26. public:
  27. //==============================================================================
  28. enum RootFolder
  29. {
  30. unknown,
  31. projectFolder,
  32. buildTargetFolder
  33. };
  34. //==============================================================================
  35. RelativePath()
  36. : root (unknown)
  37. {}
  38. RelativePath (const String& path_, const RootFolder root_)
  39. : path (path_.replaceCharacter ('\\', '/')), root (root_)
  40. {
  41. }
  42. RelativePath (const File& file, const File& rootFolder, const RootFolder root_)
  43. : path (file.getRelativePathFrom (rootFolder).replaceCharacter ('\\', '/')), root (root_)
  44. {
  45. }
  46. RootFolder getRoot() const { return root; }
  47. String toUnixStyle() const { return FileHelpers::unixStylePath (path); }
  48. String toWindowsStyle() const { return FileHelpers::windowsStylePath (path); }
  49. String getFileName() const { return getFakeFile().getFileName(); }
  50. String getFileNameWithoutExtension() const { return getFakeFile().getFileNameWithoutExtension(); }
  51. String getFileExtension() const { return getFakeFile().getFileExtension(); }
  52. bool hasFileExtension (const String& extension) const { return getFakeFile().hasFileExtension (extension); }
  53. bool isAbsolute() const { return isAbsolute (path); }
  54. RelativePath withFileExtension (const String& extension) const
  55. {
  56. return RelativePath (path.upToLastOccurrenceOf (".", ! extension.startsWithChar ('.'), false) + extension, root);
  57. }
  58. RelativePath getParentDirectory() const
  59. {
  60. String p (path);
  61. if (path.endsWithChar ('/'))
  62. p = p.dropLastCharacters (1);
  63. return RelativePath (p.upToLastOccurrenceOf ("/", false, false), root);
  64. }
  65. RelativePath getChildFile (const String& subpath) const
  66. {
  67. if (isAbsolute (subpath))
  68. return RelativePath (subpath, root);
  69. String p (toUnixStyle());
  70. if (! p.endsWithChar ('/'))
  71. p << '/';
  72. return RelativePath (p + subpath, root);
  73. }
  74. RelativePath rebased (const File& originalRoot, const File& newRoot, const RootFolder newRootType) const
  75. {
  76. if (isAbsolute())
  77. return RelativePath (path, newRootType);
  78. return RelativePath (originalRoot.getChildFile (toUnixStyle()).getRelativePathFrom (newRoot), newRootType);
  79. }
  80. private:
  81. //==============================================================================
  82. String path;
  83. RootFolder root;
  84. File getFakeFile() const
  85. {
  86. // This method gets called very often, so we'll cache this directory.
  87. static const File currentWorkingDirectory (File::getCurrentWorkingDirectory());
  88. return currentWorkingDirectory.getChildFile (path);
  89. }
  90. static bool isAbsolute (const String& path)
  91. {
  92. return File::isAbsolutePath (path)
  93. || path.startsWithChar ('/') // (needed because File::isAbsolutePath will ignore forward-slashes on Windows)
  94. || path.startsWithChar ('$')
  95. || path.startsWithChar ('~')
  96. || (CharacterFunctions::isLetter (path[0]) && path[1] == ':')
  97. || path.startsWithIgnoreCase ("smb:");
  98. }
  99. };
  100. #endif // __JUCER_RELATIVEPATH_JUCEHEADER__