blob: 75dabb53e4e351674150823e6d77d98b8bb268b8 [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::Normalize() {
74 for (size_t i=0; i<folder_.length(); ++i) {
75 if (IsFolderDelimiter(folder_[i])) {
76 folder_[i] = folder_delimiter_;
77 }
78 }
79}
80
81void Pathname::clear() {
82 folder_.clear();
83 basename_.clear();
84 extension_.clear();
85}
86
87bool Pathname::empty() const {
88 return folder_.empty() && basename_.empty() && extension_.empty();
89}
90
91std::string Pathname::pathname() const {
92 std::string pathname(folder_);
93 pathname.append(basename_);
94 pathname.append(extension_);
95 if (pathname.empty()) {
96 // Instead of the empty pathname, return the current working directory.
97 pathname.push_back('.');
98 pathname.push_back(folder_delimiter_);
99 }
100 return pathname;
101}
102
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000103void Pathname::SetPathname(const std::string& pathname) {
104 std::string::size_type pos = pathname.find_last_of(FOLDER_DELIMS);
105 if (pos != std::string::npos) {
106 SetFolder(pathname.substr(0, pos + 1));
107 SetFilename(pathname.substr(pos + 1));
108 } else {
109 SetFolder(EMPTY_STR);
110 SetFilename(pathname);
111 }
112}
113
114void Pathname::SetPathname(const std::string& folder,
115 const std::string& filename) {
116 SetFolder(folder);
117 SetFilename(filename);
118}
119
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000120std::string Pathname::folder() const {
121 return folder_;
122}
123
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000124std::string Pathname::parent_folder() const {
125 std::string::size_type pos = std::string::npos;
126 if (folder_.size() >= 2) {
127 pos = folder_.find_last_of(FOLDER_DELIMS, folder_.length() - 2);
128 }
129 if (pos != std::string::npos) {
130 return folder_.substr(0, pos + 1);
131 } else {
132 return EMPTY_STR;
133 }
134}
135
136void Pathname::SetFolder(const std::string& folder) {
137 folder_.assign(folder);
138 // Ensure folder ends in a path delimiter
139 if (!folder_.empty() && !IsFolderDelimiter(folder_[folder_.length()-1])) {
140 folder_.push_back(folder_delimiter_);
141 }
142}
143
144void Pathname::AppendFolder(const std::string& folder) {
145 folder_.append(folder);
146 // Ensure folder ends in a path delimiter
147 if (!folder_.empty() && !IsFolderDelimiter(folder_[folder_.length()-1])) {
148 folder_.push_back(folder_delimiter_);
149 }
150}
151
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000152bool Pathname::SetBasename(const std::string& basename) {
153 if(basename.find_first_of(FOLDER_DELIMS) != std::string::npos) {
154 return false;
155 }
156 basename_.assign(basename);
157 return true;
158}
159
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000160bool Pathname::SetExtension(const std::string& extension) {
161 if (extension.find_first_of(FOLDER_DELIMS) != std::string::npos ||
162 extension.find_first_of(EXT_DELIM, 1) != std::string::npos) {
163 return false;
164 }
165 extension_.assign(extension);
166 // Ensure extension begins with the extension delimiter
167 if (!extension_.empty() && (extension_[0] != EXT_DELIM)) {
168 extension_.insert(extension_.begin(), EXT_DELIM);
169 }
170 return true;
171}
172
173std::string Pathname::filename() const {
174 std::string filename(basename_);
175 filename.append(extension_);
176 return filename;
177}
178
179bool Pathname::SetFilename(const std::string& filename) {
180 std::string::size_type pos = filename.rfind(EXT_DELIM);
181 if ((pos == std::string::npos) || (pos == 0)) {
182 return SetExtension(EMPTY_STR) && SetBasename(filename);
183 } else {
184 return SetExtension(filename.substr(pos)) && SetBasename(filename.substr(0, pos));
185 }
186}
187
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000188///////////////////////////////////////////////////////////////////////////////
189
190} // namespace rtc