File: Path.h

package info (click to toggle)
pcsx2 1.6.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 28,464 kB
  • sloc: cpp: 299,796; ansic: 23,973; lisp: 2,689; asm: 908; perl: 852; sh: 789; xml: 116; makefile: 60
file content (230 lines) | stat: -rw-r--r-- 8,682 bytes parent folder | download | duplicates (4)
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
/*  PCSX2 - PS2 Emulator for PCs
 *  Copyright (C) 2002-2010  PCSX2 Dev Team
 *
 *  PCSX2 is free software: you can redistribute it and/or modify it under the terms
 *  of the GNU Lesser General Public License as published by the Free Software Found-
 *  ation, either version 3 of the License, or (at your option) any later version.
 *
 *  PCSX2 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 PCSX2.
 *  If not, see <http://www.gnu.org/licenses/>.
 */

#pragma once

#include <wx/filename.h>
#include "StringHelpers.h"

#define g_MaxPath 255 // 255 is safer with antiquated Win32 ASCII APIs.

// --------------------------------------------------------------------------------------
//  wxDirName
// --------------------------------------------------------------------------------------
class wxDirName : protected wxFileName
{
public:
    explicit wxDirName(const wxFileName &src)
    {
        Assign(src.GetPath(), wxEmptyString);
    }

    wxDirName()
        : wxFileName()
    {
    }
    wxDirName(const wxDirName &src)
        : wxFileName(src)
    {
    }
    explicit wxDirName(const char *src) { Assign(fromUTF8(src)); }
    explicit wxDirName(const wxString &src) { Assign(src); }

    // ------------------------------------------------------------------------
    void Assign(const wxString &volume, const wxString &path)
    {
        wxFileName::Assign(volume, path, wxEmptyString);
    }

    void Assign(const wxString &path)
    {
        wxFileName::Assign(path, wxEmptyString);
    }

    void Assign(const wxDirName &path)
    {
        wxFileName::Assign(path);
    }

    void Clear() { wxFileName::Clear(); }

    wxCharBuffer ToUTF8() const { return GetPath().ToUTF8(); }
    wxCharBuffer ToAscii() const { return GetPath().ToAscii(); }
    wxString ToString() const { return GetPath(); }

    // ------------------------------------------------------------------------
    bool IsWritable() const { return IsDirWritable(); }
    bool IsReadable() const { return IsDirReadable(); }
    bool Exists() const { return DirExists(); }
    bool FileExists() const { return wxFileName::FileExists(); }
    bool IsOk() const { return wxFileName::IsOk(); }
    bool IsRelative() const { return wxFileName::IsRelative(); }
    bool IsAbsolute() const { return wxFileName::IsAbsolute(); }

    bool SameAs(const wxDirName &filepath) const
    {
        return wxFileName::SameAs(filepath);
    }

    //Returns true if the file is somewhere inside this directory (and both file and directory are not relative).
    bool IsContains(const wxFileName &file) const
    {
        if (this->IsRelative() || file.IsRelative())
            return false;

        wxFileName f(file);

        while (1) {
            if (this->SameAs(wxDirName(f.GetPath())))
                return true;

            if (f.GetDirCount() == 0)
                return false;

            f.RemoveLastDir();
        }

        return false;
    }

    bool IsContains(const wxDirName &dir) const
    {
        return IsContains((wxFileName)dir);
    }


    //Auto relative works as follows:
    // 1. if either base or subject are relative, return subject (should never be used with relative paths).
    // 2. else if subject is somewhere inside base folder, then result is subject relative to base.
    // 3. (windows only, implicitly) else if subject is on the same driveletter as base, result is absolute path of subject without the driveletter.
    // 4. else, result is absolute path of subject.
    //
    // returns ok if both this and base are absolute paths.
    static wxString MakeAutoRelativeTo(const wxFileName _subject, const wxString &pathbase)
    {
        wxFileName subject(_subject);
        wxDirName base(pathbase);
        if (base.IsRelative() || subject.IsRelative())
            return subject.GetFullPath();

        wxString bv(base.GetVolume());
        bv.MakeUpper();
        wxString sv(subject.GetVolume());
        sv.MakeUpper();

        if (base.IsContains(subject)) {
            subject.MakeRelativeTo(base.GetFullPath());
        } else if (base.HasVolume() && subject.HasVolume() && bv == sv) {
            wxString unusedVolume;
            wxString pathSansVolume;
            subject.SplitVolume(subject.GetFullPath(), &unusedVolume, &pathSansVolume);
            subject = pathSansVolume;
        }
        //implicit else: this stays fully absolute

        return subject.GetFullPath();
    }

    static wxString MakeAutoRelativeTo(const wxDirName subject, const wxString &pathbase)
    {
        return MakeAutoRelativeTo(wxFileName(subject), pathbase);
    }

    // Returns the number of sub folders in this directory path
    size_t GetCount() const { return GetDirCount(); }

    // ------------------------------------------------------------------------
    wxFileName Combine(const wxFileName &right) const;
    wxDirName Combine(const wxDirName &right) const;

    // removes the lastmost directory from the path
    void RemoveLast() { wxFileName::RemoveDir(GetCount() - 1); }

    wxDirName &Normalize(int flags = wxPATH_NORM_ALL, const wxString &cwd = wxEmptyString);
    wxDirName &MakeRelativeTo(const wxString &pathBase = wxEmptyString);
    wxDirName &MakeAbsolute(const wxString &cwd = wxEmptyString);

    // ------------------------------------------------------------------------

    void AssignCwd(const wxString &volume = wxEmptyString) { wxFileName::AssignCwd(volume); }
    bool SetCwd() { return wxFileName::SetCwd(); }

    // wxWidgets is missing the const qualifier for this one!  Shame!
    void Rmdir();
    bool Mkdir();

    // ------------------------------------------------------------------------

    wxDirName &operator=(const wxDirName &dirname)
    {
        Assign(dirname);
        return *this;
    }
    wxDirName &operator=(const wxString &dirname)
    {
        Assign(dirname);
        return *this;
    }
    wxDirName &operator=(const char *dirname)
    {
        Assign(fromUTF8(dirname));
        return *this;
    }

    wxFileName operator+(const wxFileName &right) const { return Combine(right); }
    wxDirName operator+(const wxDirName &right) const { return Combine(right); }
    wxFileName operator+(const wxString &right) const { return Combine(wxFileName(right)); }
    wxFileName operator+(const char *right) const { return Combine(wxFileName(fromUTF8(right))); }

    bool operator==(const wxDirName &filename) const { return SameAs(filename); }
    bool operator!=(const wxDirName &filename) const { return !SameAs(filename); }

    bool operator==(const wxFileName &filename) const { return SameAs(wxDirName(filename)); }
    bool operator!=(const wxFileName &filename) const { return !SameAs(wxDirName(filename)); }

    // compare with a filename string interpreted as a native file name
    bool operator==(const wxString &filename) const { return SameAs(wxDirName(filename)); }
    bool operator!=(const wxString &filename) const { return !SameAs(wxDirName(filename)); }

    const wxFileName &GetFilename() const { return *this; }
    wxFileName &GetFilename() { return *this; }
};

// --------------------------------------------------------------------------------------
//  Path Namespace
// --------------------------------------------------------------------------------------
// Cross-platform utilities for manipulation of paths and filenames.  Mostly these fall
// back on wxWidgets APIs internally, but are still helpful because some of wx's file stuff
// has minor glitches, or requires sloppy wxFileName typecasting.
//
namespace Path
{
extern bool IsRelative(const wxString &path);
extern s64 GetFileSize(const wxString &path);

extern wxString Normalize(const wxString &srcpath);
extern wxString Normalize(const wxDirName &srcpath);
extern wxString MakeAbsolute(const wxString &srcpath);

extern wxString Combine(const wxString &srcPath, const wxString &srcFile);
extern wxString Combine(const wxDirName &srcPath, const wxFileName &srcFile);
extern wxString Combine(const wxString &srcPath, const wxDirName &srcFile);
extern wxString ReplaceExtension(const wxString &src, const wxString &ext);
extern wxString ReplaceFilename(const wxString &src, const wxString &newfilename);
extern wxString GetFilename(const wxString &src);
extern wxString GetDirectory(const wxString &src);
extern wxString GetFilenameWithoutExt(const wxString &src);
extern wxString GetRootDirectory(const wxString &src);
}