blob: 6ab8b243a4563735ec814276843c844cb40a06f6 [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)
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020012#include "rtc_base/win32.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000013#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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "rtc_base/checks.h"
19#include "rtc_base/logging.h"
20#include "rtc_base/pathutils.h"
21#include "rtc_base/stringutils.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000022
23namespace rtc {
24
25static const char EMPTY_STR[] = "";
26
27// EXT_DELIM separates a file basename from extension
28const char EXT_DELIM = '.';
29
30// FOLDER_DELIMS separate folder segments and the filename
31const char* const FOLDER_DELIMS = "/\\";
32
33// DEFAULT_FOLDER_DELIM is the preferred delimiter for this platform
kwiberg77eab702016-09-28 17:42:01 -070034#ifdef WEBRTC_WIN
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000035const char DEFAULT_FOLDER_DELIM = '\\';
kjellander470dd372016-04-19 03:03:23 -070036#else // !WEBRTC_WIN
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000037const char DEFAULT_FOLDER_DELIM = '/';
kjellander470dd372016-04-19 03:03:23 -070038#endif // !WEBRTC_WIN
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000039
40///////////////////////////////////////////////////////////////////////////////
41// Pathname - parsing of pathnames into components, and vice versa
42///////////////////////////////////////////////////////////////////////////////
43
44bool Pathname::IsFolderDelimiter(char ch) {
deadbeef37f5ecf2017-02-27 14:06:41 -080045 return (nullptr != ::strchr(FOLDER_DELIMS, ch));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000046}
47
48char Pathname::DefaultFolderDelimiter() {
49 return DEFAULT_FOLDER_DELIM;
50}
51
52Pathname::Pathname()
53 : folder_delimiter_(DEFAULT_FOLDER_DELIM) {
54}
55
kjellander470dd372016-04-19 03:03:23 -070056Pathname::Pathname(const Pathname&) = default;
kwiberg4fb3d2b2016-04-22 04:59:31 -070057Pathname::Pathname(Pathname&&) = default;
kjellander470dd372016-04-19 03:03:23 -070058
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000059Pathname::Pathname(const std::string& pathname)
60 : folder_delimiter_(DEFAULT_FOLDER_DELIM) {
61 SetPathname(pathname);
62}
63
64Pathname::Pathname(const std::string& folder, const std::string& filename)
65 : folder_delimiter_(DEFAULT_FOLDER_DELIM) {
66 SetPathname(folder, filename);
67}
68
kwiberg4fb3d2b2016-04-22 04:59:31 -070069Pathname& Pathname::operator=(const Pathname&) = default;
70Pathname& Pathname::operator=(Pathname&&) = default;
71
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000072void Pathname::Normalize() {
73 for (size_t i=0; i<folder_.length(); ++i) {
74 if (IsFolderDelimiter(folder_[i])) {
75 folder_[i] = folder_delimiter_;
76 }
77 }
78}
79
80void Pathname::clear() {
81 folder_.clear();
82 basename_.clear();
83 extension_.clear();
84}
85
86bool Pathname::empty() const {
87 return folder_.empty() && basename_.empty() && extension_.empty();
88}
89
90std::string Pathname::pathname() const {
91 std::string pathname(folder_);
92 pathname.append(basename_);
93 pathname.append(extension_);
94 if (pathname.empty()) {
95 // Instead of the empty pathname, return the current working directory.
96 pathname.push_back('.');
97 pathname.push_back(folder_delimiter_);
98 }
99 return pathname;
100}
101
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000102void Pathname::SetPathname(const std::string& pathname) {
103 std::string::size_type pos = pathname.find_last_of(FOLDER_DELIMS);
104 if (pos != std::string::npos) {
105 SetFolder(pathname.substr(0, pos + 1));
106 SetFilename(pathname.substr(pos + 1));
107 } else {
108 SetFolder(EMPTY_STR);
109 SetFilename(pathname);
110 }
111}
112
113void Pathname::SetPathname(const std::string& folder,
114 const std::string& filename) {
115 SetFolder(folder);
116 SetFilename(filename);
117}
118
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000119std::string Pathname::folder() const {
120 return folder_;
121}
122
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000123std::string Pathname::parent_folder() const {
124 std::string::size_type pos = std::string::npos;
125 if (folder_.size() >= 2) {
126 pos = folder_.find_last_of(FOLDER_DELIMS, folder_.length() - 2);
127 }
128 if (pos != std::string::npos) {
129 return folder_.substr(0, pos + 1);
130 } else {
131 return EMPTY_STR;
132 }
133}
134
135void Pathname::SetFolder(const std::string& folder) {
136 folder_.assign(folder);
137 // Ensure folder ends in a path delimiter
138 if (!folder_.empty() && !IsFolderDelimiter(folder_[folder_.length()-1])) {
139 folder_.push_back(folder_delimiter_);
140 }
141}
142
143void Pathname::AppendFolder(const std::string& folder) {
144 folder_.append(folder);
145 // Ensure folder ends in a path delimiter
146 if (!folder_.empty() && !IsFolderDelimiter(folder_[folder_.length()-1])) {
147 folder_.push_back(folder_delimiter_);
148 }
149}
150
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000151bool Pathname::SetBasename(const std::string& basename) {
152 if(basename.find_first_of(FOLDER_DELIMS) != std::string::npos) {
153 return false;
154 }
155 basename_.assign(basename);
156 return true;
157}
158
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000159bool Pathname::SetExtension(const std::string& extension) {
160 if (extension.find_first_of(FOLDER_DELIMS) != std::string::npos ||
161 extension.find_first_of(EXT_DELIM, 1) != std::string::npos) {
162 return false;
163 }
164 extension_.assign(extension);
165 // Ensure extension begins with the extension delimiter
166 if (!extension_.empty() && (extension_[0] != EXT_DELIM)) {
167 extension_.insert(extension_.begin(), EXT_DELIM);
168 }
169 return true;
170}
171
172std::string Pathname::filename() const {
173 std::string filename(basename_);
174 filename.append(extension_);
175 return filename;
176}
177
178bool Pathname::SetFilename(const std::string& filename) {
179 std::string::size_type pos = filename.rfind(EXT_DELIM);
180 if ((pos == std::string::npos) || (pos == 0)) {
181 return SetExtension(EMPTY_STR) && SetBasename(filename);
182 } else {
183 return SetExtension(filename.substr(pos)) && SetBasename(filename.substr(0, pos));
184 }
185}
186
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000187///////////////////////////////////////////////////////////////////////////////
188
189} // namespace rtc