blob: f79c2980a42d04981d662897493842b8ea48815f [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001/*
2 * Copyright 2004 The WebRTC Project Authors. All rights reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11#if defined(WEBRTC_WIN)
12#include "webrtc/base/win32.h"
13#include <shellapi.h>
14#include <shlobj.h>
15#include <tchar.h>
kjellander470dd372016-04-19 03:03:23 -070016#endif // WEBRTC_WIN
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000017
zijiehedd87d582016-12-06 15:04:02 -080018#include "webrtc/base/checks.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000019#include "webrtc/base/fileutils.h"
20#include "webrtc/base/logging.h"
21#include "webrtc/base/pathutils.h"
22#include "webrtc/base/stringutils.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000023
24namespace rtc {
25
26static const char EMPTY_STR[] = "";
27
28// EXT_DELIM separates a file basename from extension
29const char EXT_DELIM = '.';
30
31// FOLDER_DELIMS separate folder segments and the filename
32const char* const FOLDER_DELIMS = "/\\";
33
34// DEFAULT_FOLDER_DELIM is the preferred delimiter for this platform
kwiberg77eab702016-09-28 17:42:01 -070035#ifdef WEBRTC_WIN
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000036const char DEFAULT_FOLDER_DELIM = '\\';
kjellander470dd372016-04-19 03:03:23 -070037#else // !WEBRTC_WIN
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000038const char DEFAULT_FOLDER_DELIM = '/';
kjellander470dd372016-04-19 03:03:23 -070039#endif // !WEBRTC_WIN
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000040
41///////////////////////////////////////////////////////////////////////////////
42// Pathname - parsing of pathnames into components, and vice versa
43///////////////////////////////////////////////////////////////////////////////
44
45bool Pathname::IsFolderDelimiter(char ch) {
deadbeef37f5ecf2017-02-27 14:06:41 -080046 return (nullptr != ::strchr(FOLDER_DELIMS, ch));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000047}
48
49char Pathname::DefaultFolderDelimiter() {
50 return DEFAULT_FOLDER_DELIM;
51}
52
53Pathname::Pathname()
54 : folder_delimiter_(DEFAULT_FOLDER_DELIM) {
55}
56
kjellander470dd372016-04-19 03:03:23 -070057Pathname::Pathname(const Pathname&) = default;
kwiberg4fb3d2b2016-04-22 04:59:31 -070058Pathname::Pathname(Pathname&&) = default;
kjellander470dd372016-04-19 03:03:23 -070059
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000060Pathname::Pathname(const std::string& pathname)
61 : folder_delimiter_(DEFAULT_FOLDER_DELIM) {
62 SetPathname(pathname);
63}
64
65Pathname::Pathname(const std::string& folder, const std::string& filename)
66 : folder_delimiter_(DEFAULT_FOLDER_DELIM) {
67 SetPathname(folder, filename);
68}
69
kwiberg4fb3d2b2016-04-22 04:59:31 -070070Pathname& Pathname::operator=(const Pathname&) = default;
71Pathname& Pathname::operator=(Pathname&&) = default;
72
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000073void Pathname::SetFolderDelimiter(char delimiter) {
zijiehedd87d582016-12-06 15:04:02 -080074 RTC_DCHECK(IsFolderDelimiter(delimiter));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000075 folder_delimiter_ = delimiter;
76}
77
78void Pathname::Normalize() {
79 for (size_t i=0; i<folder_.length(); ++i) {
80 if (IsFolderDelimiter(folder_[i])) {
81 folder_[i] = folder_delimiter_;
82 }
83 }
84}
85
86void Pathname::clear() {
87 folder_.clear();
88 basename_.clear();
89 extension_.clear();
90}
91
92bool Pathname::empty() const {
93 return folder_.empty() && basename_.empty() && extension_.empty();
94}
95
96std::string Pathname::pathname() const {
97 std::string pathname(folder_);
98 pathname.append(basename_);
99 pathname.append(extension_);
100 if (pathname.empty()) {
101 // Instead of the empty pathname, return the current working directory.
102 pathname.push_back('.');
103 pathname.push_back(folder_delimiter_);
104 }
105 return pathname;
106}
107
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000108void Pathname::SetPathname(const std::string& pathname) {
109 std::string::size_type pos = pathname.find_last_of(FOLDER_DELIMS);
110 if (pos != std::string::npos) {
111 SetFolder(pathname.substr(0, pos + 1));
112 SetFilename(pathname.substr(pos + 1));
113 } else {
114 SetFolder(EMPTY_STR);
115 SetFilename(pathname);
116 }
117}
118
119void Pathname::SetPathname(const std::string& folder,
120 const std::string& filename) {
121 SetFolder(folder);
122 SetFilename(filename);
123}
124
125void Pathname::AppendPathname(const std::string& pathname) {
126 std::string full_pathname(folder_);
127 full_pathname.append(pathname);
128 SetPathname(full_pathname);
129}
130
131std::string Pathname::folder() const {
132 return folder_;
133}
134
135std::string Pathname::folder_name() const {
136 std::string::size_type pos = std::string::npos;
137 if (folder_.size() >= 2) {
138 pos = folder_.find_last_of(FOLDER_DELIMS, folder_.length() - 2);
139 }
140 if (pos != std::string::npos) {
141 return folder_.substr(pos + 1);
142 } else {
143 return folder_;
144 }
145}
146
147std::string Pathname::parent_folder() const {
148 std::string::size_type pos = std::string::npos;
149 if (folder_.size() >= 2) {
150 pos = folder_.find_last_of(FOLDER_DELIMS, folder_.length() - 2);
151 }
152 if (pos != std::string::npos) {
153 return folder_.substr(0, pos + 1);
154 } else {
155 return EMPTY_STR;
156 }
157}
158
159void Pathname::SetFolder(const std::string& folder) {
160 folder_.assign(folder);
161 // Ensure folder ends in a path delimiter
162 if (!folder_.empty() && !IsFolderDelimiter(folder_[folder_.length()-1])) {
163 folder_.push_back(folder_delimiter_);
164 }
165}
166
167void Pathname::AppendFolder(const std::string& folder) {
168 folder_.append(folder);
169 // Ensure folder ends in a path delimiter
170 if (!folder_.empty() && !IsFolderDelimiter(folder_[folder_.length()-1])) {
171 folder_.push_back(folder_delimiter_);
172 }
173}
174
175std::string Pathname::basename() const {
176 return basename_;
177}
178
179bool Pathname::SetBasename(const std::string& basename) {
180 if(basename.find_first_of(FOLDER_DELIMS) != std::string::npos) {
181 return false;
182 }
183 basename_.assign(basename);
184 return true;
185}
186
187std::string Pathname::extension() const {
188 return extension_;
189}
190
191bool Pathname::SetExtension(const std::string& extension) {
192 if (extension.find_first_of(FOLDER_DELIMS) != std::string::npos ||
193 extension.find_first_of(EXT_DELIM, 1) != std::string::npos) {
194 return false;
195 }
196 extension_.assign(extension);
197 // Ensure extension begins with the extension delimiter
198 if (!extension_.empty() && (extension_[0] != EXT_DELIM)) {
199 extension_.insert(extension_.begin(), EXT_DELIM);
200 }
201 return true;
202}
203
204std::string Pathname::filename() const {
205 std::string filename(basename_);
206 filename.append(extension_);
207 return filename;
208}
209
210bool Pathname::SetFilename(const std::string& filename) {
211 std::string::size_type pos = filename.rfind(EXT_DELIM);
212 if ((pos == std::string::npos) || (pos == 0)) {
213 return SetExtension(EMPTY_STR) && SetBasename(filename);
214 } else {
215 return SetExtension(filename.substr(pos)) && SetBasename(filename.substr(0, pos));
216 }
217}
218
219#if defined(WEBRTC_WIN)
Peter Boström0c4e06b2015-10-07 12:23:21 +0200220bool Pathname::GetDrive(char* drive, uint32_t bytes) const {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000221 return GetDrive(drive, bytes, folder_);
222}
223
224// static
Peter Boström0c4e06b2015-10-07 12:23:21 +0200225bool Pathname::GetDrive(char* drive,
226 uint32_t bytes,
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000227 const std::string& pathname) {
228 // need at lease 4 bytes to save c:
229 if (bytes < 4 || pathname.size() < 3) {
230 return false;
231 }
232
233 memcpy(drive, pathname.c_str(), 3);
234 drive[3] = 0;
235 // sanity checking
236 return (isalpha(drive[0]) &&
237 drive[1] == ':' &&
238 drive[2] == '\\');
239}
240#endif
241
242///////////////////////////////////////////////////////////////////////////////
243
244} // namespace rtc