blob: 07646716c28b5c2a845d57cf99831c012e10a3dd [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)
Patrik Höglunda8005cf2017-12-13 16:05:42 +010012#include <windows.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
Yves Gereybe8b5342018-10-05 12:37:29 +020018#include <string.h> // for strchr
19
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#include "rtc_base/pathutils.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000021
22namespace rtc {
23
24static const char EMPTY_STR[] = "";
25
26// EXT_DELIM separates a file basename from extension
27const char EXT_DELIM = '.';
28
29// FOLDER_DELIMS separate folder segments and the filename
30const char* const FOLDER_DELIMS = "/\\";
31
32// DEFAULT_FOLDER_DELIM is the preferred delimiter for this platform
kwiberg77eab702016-09-28 17:42:01 -070033#ifdef WEBRTC_WIN
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000034const char DEFAULT_FOLDER_DELIM = '\\';
kjellander470dd372016-04-19 03:03:23 -070035#else // !WEBRTC_WIN
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000036const char DEFAULT_FOLDER_DELIM = '/';
kjellander470dd372016-04-19 03:03:23 -070037#endif // !WEBRTC_WIN
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000038
39///////////////////////////////////////////////////////////////////////////////
40// Pathname - parsing of pathnames into components, and vice versa
41///////////////////////////////////////////////////////////////////////////////
42
43bool Pathname::IsFolderDelimiter(char ch) {
deadbeef37f5ecf2017-02-27 14:06:41 -080044 return (nullptr != ::strchr(FOLDER_DELIMS, ch));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000045}
46
47char Pathname::DefaultFolderDelimiter() {
48 return DEFAULT_FOLDER_DELIM;
49}
50
51Pathname::Pathname()
52 : folder_delimiter_(DEFAULT_FOLDER_DELIM) {
53}
54
kjellander470dd372016-04-19 03:03:23 -070055Pathname::Pathname(const Pathname&) = default;
kwiberg4fb3d2b2016-04-22 04:59:31 -070056Pathname::Pathname(Pathname&&) = default;
kjellander470dd372016-04-19 03:03:23 -070057
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000058Pathname::Pathname(const std::string& pathname)
59 : folder_delimiter_(DEFAULT_FOLDER_DELIM) {
60 SetPathname(pathname);
61}
62
63Pathname::Pathname(const std::string& folder, const std::string& filename)
64 : folder_delimiter_(DEFAULT_FOLDER_DELIM) {
65 SetPathname(folder, filename);
66}
67
kwiberg4fb3d2b2016-04-22 04:59:31 -070068Pathname& Pathname::operator=(const Pathname&) = default;
69Pathname& Pathname::operator=(Pathname&&) = default;
70
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000071std::string Pathname::pathname() const {
72 std::string pathname(folder_);
73 pathname.append(basename_);
74 pathname.append(extension_);
75 if (pathname.empty()) {
76 // Instead of the empty pathname, return the current working directory.
77 pathname.push_back('.');
78 pathname.push_back(folder_delimiter_);
79 }
80 return pathname;
81}
82
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000083void Pathname::SetPathname(const std::string& pathname) {
84 std::string::size_type pos = pathname.find_last_of(FOLDER_DELIMS);
85 if (pos != std::string::npos) {
86 SetFolder(pathname.substr(0, pos + 1));
87 SetFilename(pathname.substr(pos + 1));
88 } else {
89 SetFolder(EMPTY_STR);
90 SetFilename(pathname);
91 }
92}
93
94void Pathname::SetPathname(const std::string& folder,
95 const std::string& filename) {
96 SetFolder(folder);
97 SetFilename(filename);
98}
99
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000100void Pathname::SetFolder(const std::string& folder) {
101 folder_.assign(folder);
102 // Ensure folder ends in a path delimiter
103 if (!folder_.empty() && !IsFolderDelimiter(folder_[folder_.length()-1])) {
104 folder_.push_back(folder_delimiter_);
105 }
106}
107
108void Pathname::AppendFolder(const std::string& folder) {
109 folder_.append(folder);
110 // Ensure folder ends in a path delimiter
111 if (!folder_.empty() && !IsFolderDelimiter(folder_[folder_.length()-1])) {
112 folder_.push_back(folder_delimiter_);
113 }
114}
115
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000116bool Pathname::SetBasename(const std::string& basename) {
117 if(basename.find_first_of(FOLDER_DELIMS) != std::string::npos) {
118 return false;
119 }
120 basename_.assign(basename);
121 return true;
122}
123
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000124bool Pathname::SetExtension(const std::string& extension) {
125 if (extension.find_first_of(FOLDER_DELIMS) != std::string::npos ||
126 extension.find_first_of(EXT_DELIM, 1) != std::string::npos) {
127 return false;
128 }
129 extension_.assign(extension);
130 // Ensure extension begins with the extension delimiter
131 if (!extension_.empty() && (extension_[0] != EXT_DELIM)) {
132 extension_.insert(extension_.begin(), EXT_DELIM);
133 }
134 return true;
135}
136
137std::string Pathname::filename() const {
138 std::string filename(basename_);
139 filename.append(extension_);
140 return filename;
141}
142
143bool Pathname::SetFilename(const std::string& filename) {
144 std::string::size_type pos = filename.rfind(EXT_DELIM);
145 if ((pos == std::string::npos) || (pos == 0)) {
146 return SetExtension(EMPTY_STR) && SetBasename(filename);
147 } else {
148 return SetExtension(filename.substr(pos)) && SetBasename(filename.substr(0, pos));
149 }
150}
151
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000152///////////////////////////////////////////////////////////////////////////////
153
154} // namespace rtc