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.

123 lines
4.6KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE 6 technical preview.
  4. Copyright (c) 2020 - Raw Material Software Limited
  5. You may use this code under the terms of the GPL v3
  6. (see www.gnu.org/licenses).
  7. For this technical preview, this file is not subject to commercial licensing.
  8. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  9. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  10. DISCLAIMED.
  11. ==============================================================================
  12. */
  13. namespace juce
  14. {
  15. namespace build_tools
  16. {
  17. //==============================================================================
  18. /** Manipulates a cross-platform partial file path. (Needed because File is designed
  19. for absolute paths on the active OS)
  20. */
  21. class RelativePath
  22. {
  23. public:
  24. //==============================================================================
  25. enum RootFolder
  26. {
  27. unknown,
  28. projectFolder,
  29. buildTargetFolder
  30. };
  31. //==============================================================================
  32. RelativePath()
  33. : root (unknown)
  34. {}
  35. RelativePath (const String& relPath, const RootFolder rootType)
  36. : path (unixStylePath (relPath)), root (rootType)
  37. {}
  38. RelativePath (const File& file, const File& rootFolder, const RootFolder rootType)
  39. : path (unixStylePath (getRelativePathFrom (file, rootFolder))), root (rootType)
  40. {}
  41. RootFolder getRoot() const { return root; }
  42. String toUnixStyle() const { return unixStylePath (path); }
  43. String toWindowsStyle() const { return windowsStylePath (path); }
  44. String getFileName() const { return getFakeFile().getFileName(); }
  45. String getFileNameWithoutExtension() const { return getFakeFile().getFileNameWithoutExtension(); }
  46. String getFileExtension() const { return getFakeFile().getFileExtension(); }
  47. bool hasFileExtension (juce::StringRef extension) const { return getFakeFile().hasFileExtension (extension); }
  48. bool isAbsolute() const { return isAbsolutePath (path); }
  49. RelativePath withFileExtension (const String& extension) const
  50. {
  51. return RelativePath (path.upToLastOccurrenceOf (".", ! extension.startsWithChar ('.'), false) + extension, root);
  52. }
  53. RelativePath getParentDirectory() const
  54. {
  55. String p (path);
  56. if (path.endsWithChar ('/'))
  57. p = p.dropLastCharacters (1);
  58. return RelativePath (p.upToLastOccurrenceOf ("/", false, false), root);
  59. }
  60. RelativePath getChildFile (const String& subpath) const
  61. {
  62. if (isAbsolutePath (subpath))
  63. return RelativePath (subpath, root);
  64. String p (toUnixStyle());
  65. if (! p.endsWithChar ('/'))
  66. p << '/';
  67. return RelativePath (p + subpath, root);
  68. }
  69. RelativePath rebased (const File& originalRoot, const File& newRoot, const RootFolder newRootType) const
  70. {
  71. if (isAbsolute())
  72. return RelativePath (path, newRootType);
  73. return RelativePath (getRelativePathFrom (originalRoot.getChildFile (toUnixStyle()), newRoot), newRootType);
  74. }
  75. private:
  76. //==============================================================================
  77. String path;
  78. RootFolder root;
  79. File getFakeFile() const
  80. {
  81. #if JUCE_WINDOWS
  82. if (isAbsolutePath (path))
  83. {
  84. // This is a hack to convert unix-style absolute paths into valid absolute Windows paths to avoid hitting
  85. // an assertion in File::parseAbsolutePath().
  86. if (path.startsWithChar (L'/') || path.startsWithChar (L'$') || path.startsWithChar (L'~'))
  87. return File (String ("C:\\") + windowsStylePath (path.substring (1)));
  88. return File (path);
  89. }
  90. #endif
  91. // This method gets called very often, so we'll cache this directory.
  92. static const File currentWorkingDirectory (File::getCurrentWorkingDirectory());
  93. return currentWorkingDirectory.getChildFile (path);
  94. }
  95. };
  96. }
  97. }