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.

129 lines
4.9KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - Raw Material Software Limited
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 7 End-User License
  8. Agreement and JUCE Privacy Policy.
  9. End User License Agreement: www.juce.com/juce-7-licence
  10. Privacy Policy: www.juce.com/juce-privacy-policy
  11. Or: You may also use this code under the terms of the GPL v3 (see
  12. www.gnu.org/licenses).
  13. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  14. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  15. DISCLAIMED.
  16. ==============================================================================
  17. */
  18. namespace juce::build_tools
  19. {
  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& relPath, const RootFolder rootType)
  39. : path (unixStylePath (relPath)), root (rootType)
  40. {}
  41. RelativePath (const File& file, const File& rootFolder, const RootFolder rootType)
  42. : path (unixStylePath (getRelativePathFrom (file, rootFolder))), root (rootType)
  43. {}
  44. RootFolder getRoot() const { return root; }
  45. String toUnixStyle() const { return unixStylePath (path); }
  46. String toWindowsStyle() const { return 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 (StringRef extension) const { return getFakeFile().hasFileExtension (extension); }
  51. bool isAbsolute() const { return 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 (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 (getRelativePathFrom (originalRoot.getChildFile (toUnixStyle()), newRoot), newRootType);
  77. }
  78. private:
  79. //==============================================================================
  80. String path;
  81. RootFolder root;
  82. File getFakeFile() const
  83. {
  84. #if JUCE_WINDOWS
  85. if (isAbsolutePath (path))
  86. {
  87. // This is a hack to convert unix-style absolute paths into valid absolute Windows paths to avoid hitting
  88. // an assertion in File::parseAbsolutePath().
  89. if (path.startsWithChar (L'/') || path.startsWithChar (L'$') || path.startsWithChar (L'~'))
  90. return File (String ("C:\\") + windowsStylePath (path.substring (1)));
  91. return File (path);
  92. }
  93. #endif
  94. // This method gets called very often, so we'll cache this directory.
  95. static const File currentWorkingDirectory (File::getCurrentWorkingDirectory());
  96. return currentWorkingDirectory.getChildFile (path);
  97. }
  98. };
  99. } // namespace juce::build_tools