blob: 07646716c28b5c2a845d57cf99831c012e10a3dd [file] [log] [blame]
Qingsi Wang2039ee72018-11-02 16:30:10 +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)
12#include <windows.h>
13#include <shellapi.h>
14#include <shlobj.h>
15#include <tchar.h>
16#endif // WEBRTC_WIN
17
18#include <string.h> // for strchr
19
20#include "rtc_base/pathutils.h"
21
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
33#ifdef WEBRTC_WIN
34const char DEFAULT_FOLDER_DELIM = '\\';
35#else // !WEBRTC_WIN
36const char DEFAULT_FOLDER_DELIM = '/';
37#endif // !WEBRTC_WIN
38
39///////////////////////////////////////////////////////////////////////////////
40// Pathname - parsing of pathnames into components, and vice versa
41///////////////////////////////////////////////////////////////////////////////
42
43bool Pathname::IsFolderDelimiter(char ch) {
44 return (nullptr != ::strchr(FOLDER_DELIMS, ch));
45}
46
47char Pathname::DefaultFolderDelimiter() {
48 return DEFAULT_FOLDER_DELIM;
49}
50
51Pathname::Pathname()
52 : folder_delimiter_(DEFAULT_FOLDER_DELIM) {
53}
54
55Pathname::Pathname(const Pathname&) = default;
56Pathname::Pathname(Pathname&&) = default;
57
58Pathname::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
68Pathname& Pathname::operator=(const Pathname&) = default;
69Pathname& Pathname::operator=(Pathname&&) = default;
70
71std::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
83void 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
100void 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
116bool 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
124bool 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
152///////////////////////////////////////////////////////////////////////////////
153
154} // namespace rtc