Welcome to mirror list, hosted at ThFree Co, Russian Federation.

PathUtils.cpp « DSUtil « src - github.com/mpc-hc/mpc-hc.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: b46341a87d5c85dea99f2ff8302421f28708a31a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/*
 * (C) 2013-2015 see Authors.txt
 *
 * This file is part of MPC-HC.
 *
 * MPC-HC is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * MPC-HC is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include "stdafx.h"
#include "PathUtils.h"

namespace PathUtils
{
    CString BaseName(LPCTSTR path)
    {
        CPath cp(path);
        cp.RemoveBackslash();
        cp.StripPath();
        return cp;
    }

    CString DirName(LPCTSTR path)
    {
        CPath cp(path);
        cp.RemoveBackslash();
        cp.RemoveFileSpec();
        return cp;
    }

    CString FileName(LPCTSTR path)
    {
        CPath cp(path);
        cp.StripPath();
        cp.RemoveExtension();
        cp.RemoveBackslash();
        return cp;
    }

    CString FileExt(LPCTSTR path)
    {
        return CPath(path).GetExtension();
    }

    CString GetModulePath(HMODULE hModule)
    {
        CString ret;
        int pos, len = MAX_PATH - 1;
        for (;;) {
            pos = GetModuleFileName(hModule, ret.GetBuffer(len), len);
            if (pos == len) {
                // buffer was too small, enlarge it and try again
                len *= 2;
                ret.ReleaseBuffer(0);
                continue;
            }
            ret.ReleaseBuffer(pos);
            break;
        }
        ASSERT(!ret.IsEmpty());
        return ret;
    }

    CString GetAfxModulePath(bool bWithModuleName/* = false*/)
    {
        CString ret = GetModulePath(AfxGetInstanceHandle());
        if (!bWithModuleName) {
            ret = DirName(ret);
        }
        return ret;
    }

    CString GetProgramPath(bool bWithExeName/* = false*/)
    {
        CString ret = GetModulePath(nullptr);
        if (!bWithExeName) {
            ret = DirName(ret);
        }
        return ret;
    }

    CString CombinePaths(LPCTSTR dir, LPCTSTR path)
    {
        CPath cp;
        cp.Combine(dir, path);
        return cp;
    }

    CString FilterInvalidCharsFromFileName(LPCTSTR fn, TCHAR replacementChar /*= _T('_')*/)
    {
        CString ret = fn;
        int iLength = ret.GetLength();
        LPTSTR buff = ret.GetBuffer();

        for (int i = 0; i < iLength; i++) {
            switch (buff[i]) {
                case _T('<'):
                case _T('>'):
                case _T(':'):
                case _T('"'):
                case _T('/'):
                case _T('\\'):
                case _T('|'):
                case _T('?'):
                case _T('*'):
                    buff[i] = replacementChar;
                    break;
                default:
                    // Do nothing
                    break;
            }
        }

        ret.ReleaseBuffer();

        return ret;
    }

    CString Unquote(LPCTSTR path)
    {
        return CString(path).Trim(_T("\""));
    }

    CString StripPathOrUrl(LPCTSTR path)
    {
        // Replacement for CPath::StripPath which works fine also for URLs
        CString p = path;
        p.Replace('\\', '/');
        p.TrimRight('/');
        p = p.Mid(p.ReverseFind('/') + 1);
        return (p.IsEmpty() ? path : p);
    }

    bool IsInDir(LPCTSTR path, LPCTSTR dir)
    {
        return !!CPath(path).IsPrefix(dir);
    }

    CString ToRelative(LPCTSTR dir, const LPCTSTR path, bool* pbRelative/* = nullptr*/)
    {
        CPath cp;
        BOOL rel = cp.RelativePathTo(dir, FILE_ATTRIBUTE_DIRECTORY, path, 0);
        if (pbRelative) {
            *pbRelative = !!rel;
        }
        return cp;
    }

    bool IsRelative(LPCTSTR path)
    {
        return !!CPath(path).IsRelative();
    }

    bool Exists(LPCTSTR path)
    {
        return GetFileAttributes(path) != INVALID_FILE_ATTRIBUTES;
    }

    bool IsFile(LPCTSTR path)
    {
        DWORD attr = GetFileAttributes(path);
        return (attr != INVALID_FILE_ATTRIBUTES) && !(attr & FILE_ATTRIBUTE_DIRECTORY);
    }

    bool IsDir(LPCTSTR path)
    {
        DWORD attr = GetFileAttributes(path);
        return (attr != INVALID_FILE_ATTRIBUTES) && (attr & FILE_ATTRIBUTE_DIRECTORY);
    }

    bool IsLinkFile(LPCTSTR path)
    {
        return !FileExt(path).CompareNoCase(_T(".lnk"));
    }

    bool CreateDirRecursive(LPCTSTR path)
    {
        bool ret = IsDir(path) || CreateDirectory(path, nullptr);
        if (!ret) {
            ret = CreateDirRecursive(DirName(path)) && CreateDirectory(path, nullptr);
        }
        return ret;
    }

    CString ResolveLinkFile(LPCTSTR path)
    {
        TCHAR buff[MAX_PATH];
        CComPtr<IShellLink> pSL;
        pSL.CoCreateInstance(CLSID_ShellLink);
        CComQIPtr<IPersistFile> pPF = pSL;

        if (pSL && pPF
                && SUCCEEDED(pPF->Load(path, STGM_READ))
                && SUCCEEDED(pSL->Resolve(nullptr, SLR_ANY_MATCH | SLR_NO_UI))
                && SUCCEEDED(pSL->GetPath(buff, _countof(buff), nullptr, 0))) {
            return buff;
        }

        return _T("");
    }

    void RecurseAddDir(LPCTSTR path, CAtlList<CString>& paths)
    {
        CFileFind finder;

        BOOL bFound = finder.FindFile(PathUtils::CombinePaths(path, _T("*.*")));
        while (bFound) {
            bFound = finder.FindNextFile();

            if (!finder.IsDots() && finder.IsDirectory()) {
                CString folderPath = finder.GetFilePath();
                paths.AddTail(folderPath);
                RecurseAddDir(folderPath, paths);
            }
        }
    }

    void ParseDirs(CAtlList<CString>& paths)
    {
        POSITION pos = paths.GetHeadPosition();
        while (pos) {
            POSITION prevPos = pos;
            CString fn = paths.GetNext(pos);
            // Try to follow link files that point to a directory
            if (IsLinkFile(fn)) {
                fn = ResolveLinkFile(fn);
            }

            if (IsDir(fn)) {
                CAtlList<CString> subDirs;
                RecurseAddDir(fn, subDirs);
                // Add the subdirectories just after their parent
                // so that the tree is not parsed multiple times
                while (!subDirs.IsEmpty()) {
                    paths.InsertAfter(prevPos, subDirs.RemoveTail());
                }
            }
        }
    }
}