henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1 | /* |
| 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öglund | a8005cf | 2017-12-13 16:05:42 +0100 | [diff] [blame] | 12 | #include <windows.h> |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 13 | #include <shellapi.h> |
| 14 | #include <shlobj.h> |
| 15 | #include <tchar.h> |
kjellander | 470dd37 | 2016-04-19 03:03:23 -0700 | [diff] [blame] | 16 | #endif // WEBRTC_WIN |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 17 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 18 | #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.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 22 | |
| 23 | namespace rtc { |
| 24 | |
| 25 | static const char EMPTY_STR[] = ""; |
| 26 | |
| 27 | // EXT_DELIM separates a file basename from extension |
| 28 | const char EXT_DELIM = '.'; |
| 29 | |
| 30 | // FOLDER_DELIMS separate folder segments and the filename |
| 31 | const char* const FOLDER_DELIMS = "/\\"; |
| 32 | |
| 33 | // DEFAULT_FOLDER_DELIM is the preferred delimiter for this platform |
kwiberg | 77eab70 | 2016-09-28 17:42:01 -0700 | [diff] [blame] | 34 | #ifdef WEBRTC_WIN |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 35 | const char DEFAULT_FOLDER_DELIM = '\\'; |
kjellander | 470dd37 | 2016-04-19 03:03:23 -0700 | [diff] [blame] | 36 | #else // !WEBRTC_WIN |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 37 | const char DEFAULT_FOLDER_DELIM = '/'; |
kjellander | 470dd37 | 2016-04-19 03:03:23 -0700 | [diff] [blame] | 38 | #endif // !WEBRTC_WIN |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 39 | |
| 40 | /////////////////////////////////////////////////////////////////////////////// |
| 41 | // Pathname - parsing of pathnames into components, and vice versa |
| 42 | /////////////////////////////////////////////////////////////////////////////// |
| 43 | |
| 44 | bool Pathname::IsFolderDelimiter(char ch) { |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 45 | return (nullptr != ::strchr(FOLDER_DELIMS, ch)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | char Pathname::DefaultFolderDelimiter() { |
| 49 | return DEFAULT_FOLDER_DELIM; |
| 50 | } |
| 51 | |
| 52 | Pathname::Pathname() |
| 53 | : folder_delimiter_(DEFAULT_FOLDER_DELIM) { |
| 54 | } |
| 55 | |
kjellander | 470dd37 | 2016-04-19 03:03:23 -0700 | [diff] [blame] | 56 | Pathname::Pathname(const Pathname&) = default; |
kwiberg | 4fb3d2b | 2016-04-22 04:59:31 -0700 | [diff] [blame] | 57 | Pathname::Pathname(Pathname&&) = default; |
kjellander | 470dd37 | 2016-04-19 03:03:23 -0700 | [diff] [blame] | 58 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 59 | Pathname::Pathname(const std::string& pathname) |
| 60 | : folder_delimiter_(DEFAULT_FOLDER_DELIM) { |
| 61 | SetPathname(pathname); |
| 62 | } |
| 63 | |
| 64 | Pathname::Pathname(const std::string& folder, const std::string& filename) |
| 65 | : folder_delimiter_(DEFAULT_FOLDER_DELIM) { |
| 66 | SetPathname(folder, filename); |
| 67 | } |
| 68 | |
kwiberg | 4fb3d2b | 2016-04-22 04:59:31 -0700 | [diff] [blame] | 69 | Pathname& Pathname::operator=(const Pathname&) = default; |
| 70 | Pathname& Pathname::operator=(Pathname&&) = default; |
| 71 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 72 | void 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 | |
| 80 | void Pathname::clear() { |
| 81 | folder_.clear(); |
| 82 | basename_.clear(); |
| 83 | extension_.clear(); |
| 84 | } |
| 85 | |
| 86 | bool Pathname::empty() const { |
| 87 | return folder_.empty() && basename_.empty() && extension_.empty(); |
| 88 | } |
| 89 | |
| 90 | std::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.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 102 | void 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 | |
| 113 | void Pathname::SetPathname(const std::string& folder, |
| 114 | const std::string& filename) { |
| 115 | SetFolder(folder); |
| 116 | SetFilename(filename); |
| 117 | } |
| 118 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 119 | std::string Pathname::folder() const { |
| 120 | return folder_; |
| 121 | } |
| 122 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 123 | std::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 | |
| 135 | void 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 | |
| 143 | void 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.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 151 | bool 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.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 159 | bool 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 | |
| 172 | std::string Pathname::filename() const { |
| 173 | std::string filename(basename_); |
| 174 | filename.append(extension_); |
| 175 | return filename; |
| 176 | } |
| 177 | |
| 178 | bool 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.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 187 | /////////////////////////////////////////////////////////////////////////////// |
| 188 | |
| 189 | } // namespace rtc |