blob: 594deb7f0fc132fa7c436ec1742e6829892a1a11 [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:261/*
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)
Patrik Höglunda8005cf2017-12-13 15:05:4212#include <windows.h>
henrike@webrtc.orgf0488722014-05-13 18:00:2613#include <shellapi.h>
14#include <shlobj.h>
15#include <tchar.h>
kjellander470dd372016-04-19 10:03:2316#endif // WEBRTC_WIN
henrike@webrtc.orgf0488722014-05-13 18:00:2617
Mirko Bonadei92ea95e2017-09-15 04:47:3118#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:2622
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-29 00:42:0134#ifdef WEBRTC_WIN
henrike@webrtc.orgf0488722014-05-13 18:00:2635const char DEFAULT_FOLDER_DELIM = '\\';
kjellander470dd372016-04-19 10:03:2336#else // !WEBRTC_WIN
henrike@webrtc.orgf0488722014-05-13 18:00:2637const char DEFAULT_FOLDER_DELIM = '/';
kjellander470dd372016-04-19 10:03:2338#endif // !WEBRTC_WIN
henrike@webrtc.orgf0488722014-05-13 18:00:2639
40///////////////////////////////////////////////////////////////////////////////
41// Pathname - parsing of pathnames into components, and vice versa
42///////////////////////////////////////////////////////////////////////////////
43
44bool Pathname::IsFolderDelimiter(char ch) {
deadbeef37f5ecf2017-02-27 22:06:4145 return (nullptr != ::strchr(FOLDER_DELIMS, ch));
henrike@webrtc.orgf0488722014-05-13 18:00:2646}
47
48char Pathname::DefaultFolderDelimiter() {
49 return DEFAULT_FOLDER_DELIM;
50}
51
52Pathname::Pathname()
53 : folder_delimiter_(DEFAULT_FOLDER_DELIM) {
54}
55
kjellander470dd372016-04-19 10:03:2356Pathname::Pathname(const Pathname&) = default;
kwiberg4fb3d2b2016-04-22 11:59:3157Pathname::Pathname(Pathname&&) = default;
kjellander470dd372016-04-19 10:03:2358
henrike@webrtc.orgf0488722014-05-13 18:00:2659Pathname::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 11:59:3169Pathname& Pathname::operator=(const Pathname&) = default;
70Pathname& Pathname::operator=(Pathname&&) = default;
71
henrike@webrtc.orgf0488722014-05-13 18:00:2672std::string Pathname::pathname() const {
73 std::string pathname(folder_);
74 pathname.append(basename_);
75 pathname.append(extension_);
76 if (pathname.empty()) {
77 // Instead of the empty pathname, return the current working directory.
78 pathname.push_back('.');
79 pathname.push_back(folder_delimiter_);
80 }
81 return pathname;
82}
83
henrike@webrtc.orgf0488722014-05-13 18:00:2684void Pathname::SetPathname(const std::string& pathname) {
85 std::string::size_type pos = pathname.find_last_of(FOLDER_DELIMS);
86 if (pos != std::string::npos) {
87 SetFolder(pathname.substr(0, pos + 1));
88 SetFilename(pathname.substr(pos + 1));
89 } else {
90 SetFolder(EMPTY_STR);
91 SetFilename(pathname);
92 }
93}
94
95void Pathname::SetPathname(const std::string& folder,
96 const std::string& filename) {
97 SetFolder(folder);
98 SetFilename(filename);
99}
100
henrike@webrtc.orgf0488722014-05-13 18:00:26101void Pathname::SetFolder(const std::string& folder) {
102 folder_.assign(folder);
103 // Ensure folder ends in a path delimiter
104 if (!folder_.empty() && !IsFolderDelimiter(folder_[folder_.length()-1])) {
105 folder_.push_back(folder_delimiter_);
106 }
107}
108
109void Pathname::AppendFolder(const std::string& folder) {
110 folder_.append(folder);
111 // Ensure folder ends in a path delimiter
112 if (!folder_.empty() && !IsFolderDelimiter(folder_[folder_.length()-1])) {
113 folder_.push_back(folder_delimiter_);
114 }
115}
116
henrike@webrtc.orgf0488722014-05-13 18:00:26117bool Pathname::SetBasename(const std::string& basename) {
118 if(basename.find_first_of(FOLDER_DELIMS) != std::string::npos) {
119 return false;
120 }
121 basename_.assign(basename);
122 return true;
123}
124
henrike@webrtc.orgf0488722014-05-13 18:00:26125bool Pathname::SetExtension(const std::string& extension) {
126 if (extension.find_first_of(FOLDER_DELIMS) != std::string::npos ||
127 extension.find_first_of(EXT_DELIM, 1) != std::string::npos) {
128 return false;
129 }
130 extension_.assign(extension);
131 // Ensure extension begins with the extension delimiter
132 if (!extension_.empty() && (extension_[0] != EXT_DELIM)) {
133 extension_.insert(extension_.begin(), EXT_DELIM);
134 }
135 return true;
136}
137
138std::string Pathname::filename() const {
139 std::string filename(basename_);
140 filename.append(extension_);
141 return filename;
142}
143
144bool Pathname::SetFilename(const std::string& filename) {
145 std::string::size_type pos = filename.rfind(EXT_DELIM);
146 if ((pos == std::string::npos) || (pos == 0)) {
147 return SetExtension(EMPTY_STR) && SetBasename(filename);
148 } else {
149 return SetExtension(filename.substr(pos)) && SetBasename(filename.substr(0, pos));
150 }
151}
152
henrike@webrtc.orgf0488722014-05-13 18:00:26153///////////////////////////////////////////////////////////////////////////////
154
155} // namespace rtc