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.

130 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
  19. {
  20. namespace build_tools
  21. {
  22. //==============================================================================
  23. /** Manipulates a cross-platform partial file path. (Needed because File is designed
  24. for absolute paths on the active OS)
  25. */
  26. class RelativePath
  27. {
  28. public:
  29. //==============================================================================
  30. enum RootFolder
  31. {
  32. unknown,
  33. projectFolder,
  34. buildTargetFolder
  35. };
  36. //==============================================================================
  37. RelativePath()
  38. : root (unknown)
  39. {}
  40. RelativePath (const String& relPath, const RootFolder rootType)
  41. : path (unixStylePath (relPath)), root (rootType)
  42. {}
  43. RelativePath (const File& file, const File& rootFolder, const RootFolder rootType)
  44. : path (unixStylePath (getRelativePathFrom (file, rootFolder))), root (rootType)
  45. {}
  46. RootFolder getRoot() const { return root; }
  47. String toUnixStyle() const { return unixStylePath (path); }
  48. String toWindowsStyle() const { return 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 (StringRef extension) const { return getFakeFile().hasFileExtension (extension); }
  53. bool isAbsolute() const { return isAbsolutePath (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 (isAbsolutePath (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 (getRelativePathFrom (originalRoot.getChildFile (toUnixStyle()), newRoot), newRootType);
  79. }
  80. private:
  81. //==============================================================================
  82. String path;
  83. RootFolder root;
  84. File getFakeFile() const
  85. {
  86. #if JUCE_WINDOWS
  87. if (isAbsolutePath (path))
  88. {
  89. // This is a hack to convert unix-style absolute paths into valid absolute Windows paths to avoid hitting
  90. // an assertion in File::parseAbsolutePath().
  91. if (path.startsWithChar (L'/') || path.startsWithChar (L'$') || path.startsWithChar (L'~'))
  92. return File (String ("C:\\") + windowsStylePath (path.substring (1)));
  93. return File (path);
  94. }
  95. #endif
  96. // This method gets called very often, so we'll cache this directory.
  97. static const File currentWorkingDirectory (File::getCurrentWorkingDirectory());
  98. return currentWorkingDirectory.getChildFile (path);
  99. }
  100. };
  101. }
  102. }