Debian Codegraph @

common/mptPathString.cpp

raw
1 /*
2 * mptPathString.cpp
3 * -----------------
4 * Purpose: Wrapper class around the platform-native representation of path names. Should be the only type that is used to store path names.
5 * Notes : Currently none.
6 * Authors: OpenMPT Devs
7 * The OpenMPT source code is released under the BSD license. Read LICENSE for more details.
8 */
9
10 #include "stdafx.h"
11 #include "mptPathString.h"
12
13 #include <vector>
14
15 #if defined(MODPLUG_TRACKER) && MPT_OS_WINDOWS
16 #include <tchar.h>
17 #endif
18
19 OPENMPT_NAMESPACE_BEGIN
20
21
22
23 namespace mpt
24 {
25
26
27
28 #if defined(MODPLUG_TRACKER) && MPT_OS_WINDOWS
29
30
31
32 // Convert an absolute path to a path that's relative to "&relativeTo".
33 mpt::PathString AbsolutePathToRelative(const mpt::PathString &path, const mpt::PathString &relativeTo)
34 {
35 using namespace path_literals;
36 using char_type = RawPathString::value_type;
37 mpt::PathString result = path;
38 if(path.empty() || relativeTo.empty())
39 {
40 return result;
41 }
42 if(!_tcsncicmp(relativeTo.AsNative().c_str(), path.AsNative().c_str(), relativeTo.AsNative().length()))
43 {
44 // Path is OpenMPT's directory or a sub directory ("C:\OpenMPT\Somepath" => ".\Somepath")
45 result = mpt::PathString::FromNative(L<char_type>(".\\")); // ".\"
46 result += mpt::PathString::FromNative(path.AsNative().substr(relativeTo.AsNative().length()));
47 } else if(!_tcsncicmp(relativeTo.AsNative().c_str(), path.AsNative().c_str(), 2))
48 {
49 // Path is on the same drive as OpenMPT ("C:\Somepath" => "\Somepath")
50 result = mpt::PathString::FromNative(path.AsNative().substr(2));
51 }
52 return result;
53 }
54
55
56 // Convert a path that is relative to "&relativeTo" to an absolute path.
57 mpt::PathString RelativePathToAbsolute(const mpt::PathString &path, const mpt::PathString &relativeTo)
58 {
59 using namespace path_literals;
60 using char_type = RawPathString::value_type;
61 mpt::PathString result = path;
62 if(path.empty() || relativeTo.empty())
63 {
64 return result;
65 }
66 if(path.length() >= 2 && path.AsNative()[0] == L<char_type>('\\') && path.AsNative()[1] == L<char_type>('\\'))
67 {
68 // Network / UNC paths
69 return result;
70 } if(path.length() >= 1 && path.AsNative()[0] == L<char_type>('\\'))
71 {
72 // Path is on the same drive as relativeTo ("\Somepath\" => "C:\Somepath\")
73 result = mpt::PathString::FromNative(relativeTo.AsNative().substr(0, 2));
74 result += mpt::PathString(path);
75 } else if(path.length() >= 2 && path.AsNative().substr(0, 2) == L<char_type>(".\\"))
76 {
77 // Path is in relativeTo or a sub directory (".\Somepath\" => "C:\OpenMPT\Somepath\")
78 result = relativeTo; // "C:\OpenMPT\"
79 result += mpt::PathString::FromNative(path.AsNative().substr(2));
80 } else if(path.length() < 3 || path.AsNative()[1] != L<char_type>(':') || path.AsNative()[2] != L<char_type>('\\'))
81 {
82 // Any other path not starting with drive letter
83 result = relativeTo; // "C:\OpenMPT\"
84 result += mpt::PathString(path);
85 }
86 return result;
87 }
88
89
90
91 #endif // MODPLUG_TRACKER && MPT_OS_WINDOWS
92
93
94
95 } // namespace mpt
96
97
98
99 OPENMPT_NAMESPACE_END