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 | |
Yves Gerey | 2e00abc | 2018-10-05 15:39:24 +0200 | [diff] [blame] | 18 | #include <string.h> // for strchr |
| 19 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 20 | #include "rtc_base/pathutils.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 21 | |
| 22 | namespace rtc { |
| 23 | |
| 24 | static const char EMPTY_STR[] = ""; |
| 25 | |
| 26 | // EXT_DELIM separates a file basename from extension |
| 27 | const char EXT_DELIM = '.'; |
| 28 | |
| 29 | // FOLDER_DELIMS separate folder segments and the filename |
| 30 | const char* const FOLDER_DELIMS = "/\\"; |
| 31 | |
| 32 | // DEFAULT_FOLDER_DELIM is the preferred delimiter for this platform |
kwiberg | 77eab70 | 2016-09-28 17:42:01 -0700 | [diff] [blame] | 33 | #ifdef WEBRTC_WIN |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 34 | const char DEFAULT_FOLDER_DELIM = '\\'; |
kjellander | 470dd37 | 2016-04-19 03:03:23 -0700 | [diff] [blame] | 35 | #else // !WEBRTC_WIN |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 36 | const char DEFAULT_FOLDER_DELIM = '/'; |
kjellander | 470dd37 | 2016-04-19 03:03:23 -0700 | [diff] [blame] | 37 | #endif // !WEBRTC_WIN |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 38 | |
| 39 | /////////////////////////////////////////////////////////////////////////////// |
| 40 | // Pathname - parsing of pathnames into components, and vice versa |
| 41 | /////////////////////////////////////////////////////////////////////////////// |
| 42 | |
| 43 | bool Pathname::IsFolderDelimiter(char ch) { |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 44 | return (nullptr != ::strchr(FOLDER_DELIMS, ch)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | char Pathname::DefaultFolderDelimiter() { |
| 48 | return DEFAULT_FOLDER_DELIM; |
| 49 | } |
| 50 | |
| 51 | Pathname::Pathname() |
| 52 | : folder_delimiter_(DEFAULT_FOLDER_DELIM) { |
| 53 | } |
| 54 | |
kjellander | 470dd37 | 2016-04-19 03:03:23 -0700 | [diff] [blame] | 55 | Pathname::Pathname(const Pathname&) = default; |
kwiberg | 4fb3d2b | 2016-04-22 04:59:31 -0700 | [diff] [blame] | 56 | Pathname::Pathname(Pathname&&) = default; |
kjellander | 470dd37 | 2016-04-19 03:03:23 -0700 | [diff] [blame] | 57 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 58 | Pathname::Pathname(const std::string& pathname) |
| 59 | : folder_delimiter_(DEFAULT_FOLDER_DELIM) { |
| 60 | SetPathname(pathname); |
| 61 | } |
| 62 | |
| 63 | Pathname::Pathname(const std::string& folder, const std::string& filename) |
| 64 | : folder_delimiter_(DEFAULT_FOLDER_DELIM) { |
| 65 | SetPathname(folder, filename); |
| 66 | } |
| 67 | |
kwiberg | 4fb3d2b | 2016-04-22 04:59:31 -0700 | [diff] [blame] | 68 | Pathname& Pathname::operator=(const Pathname&) = default; |
| 69 | Pathname& Pathname::operator=(Pathname&&) = default; |
| 70 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 71 | std::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.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 83 | void 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 | |
| 94 | void Pathname::SetPathname(const std::string& folder, |
| 95 | const std::string& filename) { |
| 96 | SetFolder(folder); |
| 97 | SetFilename(filename); |
| 98 | } |
| 99 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 100 | void 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 | |
| 108 | void 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.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 116 | bool 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.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 124 | bool 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 | |
| 137 | std::string Pathname::filename() const { |
| 138 | std::string filename(basename_); |
| 139 | filename.append(extension_); |
| 140 | return filename; |
| 141 | } |
| 142 | |
| 143 | bool 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.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 152 | /////////////////////////////////////////////////////////////////////////////// |
| 153 | |
| 154 | } // namespace rtc |