2 ==============================================================================
4 This file is part of the JUCE library - "Jules' Utility Class Extensions"
5 Copyright 2004-10 by Raw Material Software Ltd.
7 ------------------------------------------------------------------------------
9 JUCE can be redistributed and/or modified under the terms of the GNU General
10 Public License (Version 2), as published by the Free Software Foundation.
11 A copy of the license is included in the JUCE distribution, or can be found
12 online at www.gnu.org/licenses.
14 JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
18 ------------------------------------------------------------------------------
20 To release a closed-source product which uses JUCE, commercial licenses are
21 available: visit www.rawmaterialsoftware.com/juce for more information.
23 ==============================================================================
26 #ifndef __JUCER_RELATIVEPATH_JUCEHEADER__
27 #define __JUCER_RELATIVEPATH_JUCEHEADER__
30 //==============================================================================
31 /** Manipulates a cross-platform partial file path. (Needed because File is designed
32 for absolute paths on the active OS)
37 //==============================================================================
45 //==============================================================================
50 RelativePath (const String
& path_
, const RootFolder root_
)
51 : path (path_
.replaceCharacter ('\\', '/')), root (root_
)
55 RelativePath (const File
& file
, const File
& rootFolder
, const RootFolder root_
)
56 : path (file
.getRelativePathFrom (rootFolder
).replaceCharacter ('\\', '/')), root (root_
)
60 RootFolder
getRoot() const { return root
; }
62 const String
toUnixStyle() const { return FileHelpers::unixStylePath (path
); }
63 const String
toWindowsStyle() const { return FileHelpers::windowsStylePath (path
); }
65 const String
getFileName() const { return getFakeFile().getFileName(); }
66 const String
getFileNameWithoutExtension() const { return getFakeFile().getFileNameWithoutExtension(); }
68 const String
getFileExtension() const { return getFakeFile().getFileExtension(); }
69 bool hasFileExtension (const String
& extension
) const { return getFakeFile().hasFileExtension (extension
); }
70 bool isAbsolute() const { return isAbsolute (path
); }
72 const RelativePath
withFileExtension (const String
& extension
) const
74 return RelativePath (path
.upToLastOccurrenceOf (".", ! extension
.startsWithChar ('.'), false) + extension
, root
);
77 const RelativePath
getParentDirectory() const
80 if (path
.endsWithChar ('/'))
81 p
= p
.dropLastCharacters (1);
83 return RelativePath (p
.upToLastOccurrenceOf ("/", false, false), root
);
86 const RelativePath
getChildFile (const String
& subpath
) const
88 if (isAbsolute (subpath
))
89 return RelativePath (subpath
, root
);
91 String
p (toUnixStyle());
92 if (! p
.endsWithChar ('/'))
95 return RelativePath (p
+ subpath
, root
);
98 const RelativePath
rebased (const File
& originalRoot
, const File
& newRoot
, const RootFolder newRootType
) const
101 return RelativePath (path
, newRootType
);
103 return RelativePath (originalRoot
.getChildFile (toUnixStyle()).getRelativePathFrom (newRoot
), newRootType
);
107 //==============================================================================
111 const File
getFakeFile() const
113 return File::getCurrentWorkingDirectory().getChildFile (path
);
116 static bool isAbsolute (const String
& path
)
118 return File::isAbsolutePath (path
)
119 || path
.startsWithChar ('/') // (needed because File::isAbsolutePath will ignore forward-slashes on win32)
120 || path
.startsWithChar ('$')
121 || path
.startsWithChar ('~')
122 || (CharacterFunctions::isLetter (path
[0]) && path
[1] == ':')
123 || path
.startsWithIgnoreCase ("smb:");
128 #endif // __JUCER_RELATIVEPATH_JUCEHEADER__