| OLD | NEW |
| (Empty) |
| 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) | |
| 12 #include "webrtc/base/win32.h" | |
| 13 #include <shellapi.h> | |
| 14 #include <shlobj.h> | |
| 15 #include <tchar.h> | |
| 16 #endif // WEBRTC_WIN | |
| 17 | |
| 18 #include "webrtc/base/checks.h" | |
| 19 #include "webrtc/base/logging.h" | |
| 20 #include "webrtc/base/pathutils.h" | |
| 21 #include "webrtc/base/stringutils.h" | |
| 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 | |
| 34 #ifdef WEBRTC_WIN | |
| 35 const char DEFAULT_FOLDER_DELIM = '\\'; | |
| 36 #else // !WEBRTC_WIN | |
| 37 const char DEFAULT_FOLDER_DELIM = '/'; | |
| 38 #endif // !WEBRTC_WIN | |
| 39 | |
| 40 /////////////////////////////////////////////////////////////////////////////// | |
| 41 // Pathname - parsing of pathnames into components, and vice versa | |
| 42 /////////////////////////////////////////////////////////////////////////////// | |
| 43 | |
| 44 bool Pathname::IsFolderDelimiter(char ch) { | |
| 45 return (nullptr != ::strchr(FOLDER_DELIMS, ch)); | |
| 46 } | |
| 47 | |
| 48 char Pathname::DefaultFolderDelimiter() { | |
| 49 return DEFAULT_FOLDER_DELIM; | |
| 50 } | |
| 51 | |
| 52 Pathname::Pathname() | |
| 53 : folder_delimiter_(DEFAULT_FOLDER_DELIM) { | |
| 54 } | |
| 55 | |
| 56 Pathname::Pathname(const Pathname&) = default; | |
| 57 Pathname::Pathname(Pathname&&) = default; | |
| 58 | |
| 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 | |
| 69 Pathname& Pathname::operator=(const Pathname&) = default; | |
| 70 Pathname& Pathname::operator=(Pathname&&) = default; | |
| 71 | |
| 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 | |
| 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 | |
| 119 std::string Pathname::folder() const { | |
| 120 return folder_; | |
| 121 } | |
| 122 | |
| 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 | |
| 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 | |
| 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 | |
| 187 /////////////////////////////////////////////////////////////////////////////// | |
| 188 | |
| 189 } // namespace rtc | |
| OLD | NEW |