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.

116 lines
4.2KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. #pragma once
  18. //==============================================================================
  19. /** Manipulates a cross-platform partial file path. (Needed because File is designed
  20. for absolute paths on the active OS)
  21. */
  22. class RelativePath
  23. {
  24. public:
  25. //==============================================================================
  26. enum RootFolder
  27. {
  28. unknown,
  29. projectFolder,
  30. buildTargetFolder
  31. };
  32. //==============================================================================
  33. RelativePath()
  34. : root (unknown)
  35. {}
  36. RelativePath (const String& relPath, const RootFolder rootType)
  37. : path (FileHelpers::unixStylePath (relPath)), root (rootType)
  38. {
  39. }
  40. RelativePath (const File& file, const File& rootFolder, const RootFolder rootType)
  41. : path (FileHelpers::unixStylePath (FileHelpers::getRelativePathFrom (file, rootFolder))), root (rootType)
  42. {
  43. }
  44. RootFolder getRoot() const { return root; }
  45. String toUnixStyle() const { return FileHelpers::unixStylePath (path); }
  46. String toWindowsStyle() const { return FileHelpers::windowsStylePath (path); }
  47. String getFileName() const { return getFakeFile().getFileName(); }
  48. String getFileNameWithoutExtension() const { return getFakeFile().getFileNameWithoutExtension(); }
  49. String getFileExtension() const { return getFakeFile().getFileExtension(); }
  50. bool hasFileExtension (juce::StringRef extension) const { return getFakeFile().hasFileExtension (extension); }
  51. bool isAbsolute() const { return FileHelpers::isAbsolutePath (path); }
  52. RelativePath withFileExtension (const String& extension) const
  53. {
  54. return RelativePath (path.upToLastOccurrenceOf (".", ! extension.startsWithChar ('.'), false) + extension, root);
  55. }
  56. RelativePath getParentDirectory() const
  57. {
  58. String p (path);
  59. if (path.endsWithChar ('/'))
  60. p = p.dropLastCharacters (1);
  61. return RelativePath (p.upToLastOccurrenceOf ("/", false, false), root);
  62. }
  63. RelativePath getChildFile (const String& subpath) const
  64. {
  65. if (FileHelpers::isAbsolutePath (subpath))
  66. return RelativePath (subpath, root);
  67. String p (toUnixStyle());
  68. if (! p.endsWithChar ('/'))
  69. p << '/';
  70. return RelativePath (p + subpath, root);
  71. }
  72. RelativePath rebased (const File& originalRoot, const File& newRoot, const RootFolder newRootType) const
  73. {
  74. if (isAbsolute())
  75. return RelativePath (path, newRootType);
  76. return RelativePath (FileHelpers::getRelativePathFrom (originalRoot.getChildFile (toUnixStyle()), newRoot), newRootType);
  77. }
  78. private:
  79. //==============================================================================
  80. String path;
  81. RootFolder root;
  82. File getFakeFile() const
  83. {
  84. // This method gets called very often, so we'll cache this directory.
  85. static const File currentWorkingDirectory (File::getCurrentWorkingDirectory());
  86. return currentWorkingDirectory.getChildFile (path);
  87. }
  88. };