3 * Copyright 2004--2005, Google Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include "talk/base/win32.h"
35 #include "talk/base/common.h"
36 #include "talk/base/fileutils.h"
37 #include "talk/base/logging.h"
38 #include "talk/base/pathutils.h"
39 #include "talk/base/stringutils.h"
40 #include "talk/base/urlencode.h"
44 static const char EMPTY_STR[] = "";
46 // EXT_DELIM separates a file basename from extension
47 const char EXT_DELIM = '.';
49 // FOLDER_DELIMS separate folder segments and the filename
50 const char* const FOLDER_DELIMS = "/\\";
52 // DEFAULT_FOLDER_DELIM is the preferred delimiter for this platform
54 const char DEFAULT_FOLDER_DELIM = '\\';
56 const char DEFAULT_FOLDER_DELIM = '/';
59 ///////////////////////////////////////////////////////////////////////////////
60 // Pathname - parsing of pathnames into components, and vice versa
61 ///////////////////////////////////////////////////////////////////////////////
63 bool Pathname::IsFolderDelimiter(char ch) {
64 return (NULL != ::strchr(FOLDER_DELIMS, ch));
67 char Pathname::DefaultFolderDelimiter() {
68 return DEFAULT_FOLDER_DELIM;
72 : folder_delimiter_(DEFAULT_FOLDER_DELIM) {
75 Pathname::Pathname(const std::string& pathname)
76 : folder_delimiter_(DEFAULT_FOLDER_DELIM) {
77 SetPathname(pathname);
80 Pathname::Pathname(const std::string& folder, const std::string& filename)
81 : folder_delimiter_(DEFAULT_FOLDER_DELIM) {
82 SetPathname(folder, filename);
85 void Pathname::SetFolderDelimiter(char delimiter) {
86 ASSERT(IsFolderDelimiter(delimiter));
87 folder_delimiter_ = delimiter;
90 void Pathname::Normalize() {
91 for (size_t i=0; i<folder_.length(); ++i) {
92 if (IsFolderDelimiter(folder_[i])) {
93 folder_[i] = folder_delimiter_;
98 void Pathname::clear() {
104 bool Pathname::empty() const {
105 return folder_.empty() && basename_.empty() && extension_.empty();
108 std::string Pathname::pathname() const {
109 std::string pathname(folder_);
110 pathname.append(basename_);
111 pathname.append(extension_);
112 if (pathname.empty()) {
113 // Instead of the empty pathname, return the current working directory.
114 pathname.push_back('.');
115 pathname.push_back(folder_delimiter_);
120 std::string Pathname::url() const {
121 std::string s = "file:///";
122 for (size_t i=0; i<folder_.length(); ++i) {
123 if (IsFolderDelimiter(folder_[i]))
130 return UrlEncodeStringForOnlyUnsafeChars(s);
133 void Pathname::SetPathname(const std::string& pathname) {
134 std::string::size_type pos = pathname.find_last_of(FOLDER_DELIMS);
135 if (pos != std::string::npos) {
136 SetFolder(pathname.substr(0, pos + 1));
137 SetFilename(pathname.substr(pos + 1));
139 SetFolder(EMPTY_STR);
140 SetFilename(pathname);
144 void Pathname::SetPathname(const std::string& folder,
145 const std::string& filename) {
147 SetFilename(filename);
150 void Pathname::AppendPathname(const std::string& pathname) {
151 std::string full_pathname(folder_);
152 full_pathname.append(pathname);
153 SetPathname(full_pathname);
156 std::string Pathname::folder() const {
160 std::string Pathname::folder_name() const {
161 std::string::size_type pos = std::string::npos;
162 if (folder_.size() >= 2) {
163 pos = folder_.find_last_of(FOLDER_DELIMS, folder_.length() - 2);
165 if (pos != std::string::npos) {
166 return folder_.substr(pos + 1);
172 std::string Pathname::parent_folder() const {
173 std::string::size_type pos = std::string::npos;
174 if (folder_.size() >= 2) {
175 pos = folder_.find_last_of(FOLDER_DELIMS, folder_.length() - 2);
177 if (pos != std::string::npos) {
178 return folder_.substr(0, pos + 1);
184 void Pathname::SetFolder(const std::string& folder) {
185 folder_.assign(folder);
186 // Ensure folder ends in a path delimiter
187 if (!folder_.empty() && !IsFolderDelimiter(folder_[folder_.length()-1])) {
188 folder_.push_back(folder_delimiter_);
192 void Pathname::AppendFolder(const std::string& folder) {
193 folder_.append(folder);
194 // Ensure folder ends in a path delimiter
195 if (!folder_.empty() && !IsFolderDelimiter(folder_[folder_.length()-1])) {
196 folder_.push_back(folder_delimiter_);
200 std::string Pathname::basename() const {
204 bool Pathname::SetBasename(const std::string& basename) {
205 if(basename.find_first_of(FOLDER_DELIMS) != std::string::npos) {
208 basename_.assign(basename);
212 std::string Pathname::extension() const {
216 bool Pathname::SetExtension(const std::string& extension) {
217 if (extension.find_first_of(FOLDER_DELIMS) != std::string::npos ||
218 extension.find_first_of(EXT_DELIM, 1) != std::string::npos) {
221 extension_.assign(extension);
222 // Ensure extension begins with the extension delimiter
223 if (!extension_.empty() && (extension_[0] != EXT_DELIM)) {
224 extension_.insert(extension_.begin(), EXT_DELIM);
229 std::string Pathname::filename() const {
230 std::string filename(basename_);
231 filename.append(extension_);
235 bool Pathname::SetFilename(const std::string& filename) {
236 std::string::size_type pos = filename.rfind(EXT_DELIM);
237 if ((pos == std::string::npos) || (pos == 0)) {
238 return SetExtension(EMPTY_STR) && SetBasename(filename);
240 return SetExtension(filename.substr(pos)) && SetBasename(filename.substr(0, pos));
245 bool Pathname::GetDrive(char *drive, uint32 bytes) const {
246 return GetDrive(drive, bytes, folder_);
250 bool Pathname::GetDrive(char *drive, uint32 bytes,
251 const std::string& pathname) {
252 // need at lease 4 bytes to save c:
253 if (bytes < 4 || pathname.size() < 3) {
257 memcpy(drive, pathname.c_str(), 3);
260 return (isalpha(drive[0]) &&
266 ///////////////////////////////////////////////////////////////////////////////
268 } // namespace talk_base