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
|
/*
* Copyright 2004 The WebRTC Project Authors. All rights reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#if defined(WEBRTC_WIN)
#include "webrtc/base/win32.h"
#include <shellapi.h>
#include <shlobj.h>
#include <tchar.h>
#endif // WEBRTC_WIN
#include "webrtc/base/common.h"
#include "webrtc/base/fileutils.h"
#include "webrtc/base/logging.h"
#include "webrtc/base/pathutils.h"
#include "webrtc/base/stringutils.h"
#include "webrtc/base/urlencode.h"
namespace rtc {
static const char EMPTY_STR[] = "";
// EXT_DELIM separates a file basename from extension
const char EXT_DELIM = '.';
// FOLDER_DELIMS separate folder segments and the filename
const char* const FOLDER_DELIMS = "/\\";
// DEFAULT_FOLDER_DELIM is the preferred delimiter for this platform
#if WEBRTC_WIN
const char DEFAULT_FOLDER_DELIM = '\\';
#else // !WEBRTC_WIN
const char DEFAULT_FOLDER_DELIM = '/';
#endif // !WEBRTC_WIN
///////////////////////////////////////////////////////////////////////////////
// Pathname - parsing of pathnames into components, and vice versa
///////////////////////////////////////////////////////////////////////////////
bool Pathname::IsFolderDelimiter(char ch) {
return (NULL != ::strchr(FOLDER_DELIMS, ch));
}
char Pathname::DefaultFolderDelimiter() {
return DEFAULT_FOLDER_DELIM;
}
Pathname::Pathname()
: folder_delimiter_(DEFAULT_FOLDER_DELIM) {
}
Pathname::Pathname(const std::string& pathname)
: folder_delimiter_(DEFAULT_FOLDER_DELIM) {
SetPathname(pathname);
}
Pathname::Pathname(const std::string& folder, const std::string& filename)
: folder_delimiter_(DEFAULT_FOLDER_DELIM) {
SetPathname(folder, filename);
}
void Pathname::SetFolderDelimiter(char delimiter) {
ASSERT(IsFolderDelimiter(delimiter));
folder_delimiter_ = delimiter;
}
void Pathname::Normalize() {
for (size_t i=0; i<folder_.length(); ++i) {
if (IsFolderDelimiter(folder_[i])) {
folder_[i] = folder_delimiter_;
}
}
}
void Pathname::clear() {
folder_.clear();
basename_.clear();
extension_.clear();
}
bool Pathname::empty() const {
return folder_.empty() && basename_.empty() && extension_.empty();
}
std::string Pathname::pathname() const {
std::string pathname(folder_);
pathname.append(basename_);
pathname.append(extension_);
if (pathname.empty()) {
// Instead of the empty pathname, return the current working directory.
pathname.push_back('.');
pathname.push_back(folder_delimiter_);
}
return pathname;
}
std::string Pathname::url() const {
std::string s = "file:///";
for (size_t i=0; i<folder_.length(); ++i) {
if (IsFolderDelimiter(folder_[i]))
s += '/';
else
s += folder_[i];
}
s += basename_;
s += extension_;
return UrlEncodeStringForOnlyUnsafeChars(s);
}
void Pathname::SetPathname(const std::string& pathname) {
std::string::size_type pos = pathname.find_last_of(FOLDER_DELIMS);
if (pos != std::string::npos) {
SetFolder(pathname.substr(0, pos + 1));
SetFilename(pathname.substr(pos + 1));
} else {
SetFolder(EMPTY_STR);
SetFilename(pathname);
}
}
void Pathname::SetPathname(const std::string& folder,
const std::string& filename) {
SetFolder(folder);
SetFilename(filename);
}
void Pathname::AppendPathname(const std::string& pathname) {
std::string full_pathname(folder_);
full_pathname.append(pathname);
SetPathname(full_pathname);
}
std::string Pathname::folder() const {
return folder_;
}
std::string Pathname::folder_name() const {
std::string::size_type pos = std::string::npos;
if (folder_.size() >= 2) {
pos = folder_.find_last_of(FOLDER_DELIMS, folder_.length() - 2);
}
if (pos != std::string::npos) {
return folder_.substr(pos + 1);
} else {
return folder_;
}
}
std::string Pathname::parent_folder() const {
std::string::size_type pos = std::string::npos;
if (folder_.size() >= 2) {
pos = folder_.find_last_of(FOLDER_DELIMS, folder_.length() - 2);
}
if (pos != std::string::npos) {
return folder_.substr(0, pos + 1);
} else {
return EMPTY_STR;
}
}
void Pathname::SetFolder(const std::string& folder) {
folder_.assign(folder);
// Ensure folder ends in a path delimiter
if (!folder_.empty() && !IsFolderDelimiter(folder_[folder_.length()-1])) {
folder_.push_back(folder_delimiter_);
}
}
void Pathname::AppendFolder(const std::string& folder) {
folder_.append(folder);
// Ensure folder ends in a path delimiter
if (!folder_.empty() && !IsFolderDelimiter(folder_[folder_.length()-1])) {
folder_.push_back(folder_delimiter_);
}
}
std::string Pathname::basename() const {
return basename_;
}
bool Pathname::SetBasename(const std::string& basename) {
if(basename.find_first_of(FOLDER_DELIMS) != std::string::npos) {
return false;
}
basename_.assign(basename);
return true;
}
std::string Pathname::extension() const {
return extension_;
}
bool Pathname::SetExtension(const std::string& extension) {
if (extension.find_first_of(FOLDER_DELIMS) != std::string::npos ||
extension.find_first_of(EXT_DELIM, 1) != std::string::npos) {
return false;
}
extension_.assign(extension);
// Ensure extension begins with the extension delimiter
if (!extension_.empty() && (extension_[0] != EXT_DELIM)) {
extension_.insert(extension_.begin(), EXT_DELIM);
}
return true;
}
std::string Pathname::filename() const {
std::string filename(basename_);
filename.append(extension_);
return filename;
}
bool Pathname::SetFilename(const std::string& filename) {
std::string::size_type pos = filename.rfind(EXT_DELIM);
if ((pos == std::string::npos) || (pos == 0)) {
return SetExtension(EMPTY_STR) && SetBasename(filename);
} else {
return SetExtension(filename.substr(pos)) && SetBasename(filename.substr(0, pos));
}
}
#if defined(WEBRTC_WIN)
bool Pathname::GetDrive(char *drive, uint32 bytes) const {
return GetDrive(drive, bytes, folder_);
}
// static
bool Pathname::GetDrive(char *drive, uint32 bytes,
const std::string& pathname) {
// need at lease 4 bytes to save c:
if (bytes < 4 || pathname.size() < 3) {
return false;
}
memcpy(drive, pathname.c_str(), 3);
drive[3] = 0;
// sanity checking
return (isalpha(drive[0]) &&
drive[1] == ':' &&
drive[2] == '\\');
}
#endif
///////////////////////////////////////////////////////////////////////////////
} // namespace rtc
|