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.

120 lines
4.3KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software 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. #ifndef __JUCER_RELATIVEPATH_JUCEHEADER__
  18. #define __JUCER_RELATIVEPATH_JUCEHEADER__
  19. //==============================================================================
  20. /** Manipulates a cross-platform partial file path. (Needed because File is designed
  21. for absolute paths on the active OS)
  22. */
  23. class RelativePath
  24. {
  25. public:
  26. //==============================================================================
  27. enum RootFolder
  28. {
  29. unknown,
  30. projectFolder,
  31. buildTargetFolder
  32. };
  33. //==============================================================================
  34. RelativePath()
  35. : root (unknown)
  36. {}
  37. RelativePath (const String& relPath, const RootFolder rootType)
  38. : path (FileHelpers::unixStylePath (relPath)), root (rootType)
  39. {
  40. }
  41. RelativePath (const File& file, const File& rootFolder, const RootFolder rootType)
  42. : path (FileHelpers::unixStylePath (FileHelpers::getRelativePathFrom (file, rootFolder))), root (rootType)
  43. {
  44. }
  45. RootFolder getRoot() const { return root; }
  46. String toUnixStyle() const { return FileHelpers::unixStylePath (path); }
  47. String toWindowsStyle() const { return FileHelpers::windowsStylePath (path); }
  48. String getFileName() const { return getFakeFile().getFileName(); }
  49. String getFileNameWithoutExtension() const { return getFakeFile().getFileNameWithoutExtension(); }
  50. String getFileExtension() const { return getFakeFile().getFileExtension(); }
  51. bool hasFileExtension (const String& extension) const { return getFakeFile().hasFileExtension (extension); }
  52. bool isAbsolute() const { return FileHelpers::isAbsolutePath (path); }
  53. RelativePath withFileExtension (const String& extension) const
  54. {
  55. return RelativePath (path.upToLastOccurrenceOf (".", ! extension.startsWithChar ('.'), false) + extension, root);
  56. }
  57. RelativePath getParentDirectory() const
  58. {
  59. String p (path);
  60. if (path.endsWithChar ('/'))
  61. p = p.dropLastCharacters (1);
  62. return RelativePath (p.upToLastOccurrenceOf ("/", false, false), root);
  63. }
  64. RelativePath getChildFile (const String& subpath) const
  65. {
  66. if (FileHelpers::isAbsolutePath (subpath))
  67. return RelativePath (subpath, root);
  68. String p (toUnixStyle());
  69. if (! p.endsWithChar ('/'))
  70. p << '/';
  71. return RelativePath (p + subpath, root);
  72. }
  73. RelativePath rebased (const File& originalRoot, const File& newRoot, const RootFolder newRootType) const
  74. {
  75. if (isAbsolute())
  76. return RelativePath (path, newRootType);
  77. return RelativePath (FileHelpers::getRelativePathFrom (originalRoot.getChildFile (toUnixStyle()), newRoot), newRootType);
  78. }
  79. private:
  80. //==============================================================================
  81. String path;
  82. RootFolder root;
  83. File getFakeFile() const
  84. {
  85. // This method gets called very often, so we'll cache this directory.
  86. static const File currentWorkingDirectory (File::getCurrentWorkingDirectory());
  87. return currentWorkingDirectory.getChildFile (path);
  88. }
  89. };
  90. #endif // __JUCER_RELATIVEPATH_JUCEHEADER__