147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org/*
247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org *  Copyright 2004 The WebRTC Project Authors. All rights reserved.
347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org *
447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org *  Use of this source code is governed by a BSD-style license
547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org *  that can be found in the LICENSE file in the root of the source
647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org *  tree. An additional intellectual property rights grant can be found
747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org *  in the file PATENTS.  All contributing project authors may
847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org *  be found in the AUTHORS file in the root of the source tree.
947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org */
1047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
1147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org#if defined(WEBRTC_WIN)
1247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org#include "webrtc/base/win32.h"
1347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org#include <shellapi.h>
1447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org#include <shlobj.h>
1547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org#include <tchar.h>
1647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org#endif  // WEBRTC_WIN
1747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
1847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org#include "webrtc/base/common.h"
1947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org#include "webrtc/base/fileutils.h"
2047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org#include "webrtc/base/logging.h"
2147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org#include "webrtc/base/pathutils.h"
2247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org#include "webrtc/base/stringutils.h"
2347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org#include "webrtc/base/urlencode.h"
2447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
2547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.orgnamespace rtc {
2647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
2747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.orgstatic const char EMPTY_STR[] = "";
2847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
2947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org// EXT_DELIM separates a file basename from extension
3047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.orgconst char EXT_DELIM = '.';
3147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
3247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org// FOLDER_DELIMS separate folder segments and the filename
3347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.orgconst char* const FOLDER_DELIMS = "/\\";
3447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
3547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org// DEFAULT_FOLDER_DELIM is the preferred delimiter for this platform
3647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org#if WEBRTC_WIN
3747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.orgconst char DEFAULT_FOLDER_DELIM = '\\';
3847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org#else  // !WEBRTC_WIN
3947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.orgconst char DEFAULT_FOLDER_DELIM = '/';
4047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org#endif  // !WEBRTC_WIN
4147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
4247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org///////////////////////////////////////////////////////////////////////////////
4347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org// Pathname - parsing of pathnames into components, and vice versa
4447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org///////////////////////////////////////////////////////////////////////////////
4547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
4647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.orgbool Pathname::IsFolderDelimiter(char ch) {
4747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  return (NULL != ::strchr(FOLDER_DELIMS, ch));
4847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org}
4947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
5047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.orgchar Pathname::DefaultFolderDelimiter() {
5147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  return DEFAULT_FOLDER_DELIM;
5247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org}
5347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
5447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.orgPathname::Pathname()
5547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    : folder_delimiter_(DEFAULT_FOLDER_DELIM) {
5647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org}
5747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
5847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.orgPathname::Pathname(const std::string& pathname)
5947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    : folder_delimiter_(DEFAULT_FOLDER_DELIM) {
6047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  SetPathname(pathname);
6147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org}
6247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
6347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.orgPathname::Pathname(const std::string& folder, const std::string& filename)
6447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    : folder_delimiter_(DEFAULT_FOLDER_DELIM) {
6547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  SetPathname(folder, filename);
6647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org}
6747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
6847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.orgvoid Pathname::SetFolderDelimiter(char delimiter) {
6947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  ASSERT(IsFolderDelimiter(delimiter));
7047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  folder_delimiter_ = delimiter;
7147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org}
7247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
7347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.orgvoid Pathname::Normalize() {
7447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  for (size_t i=0; i<folder_.length(); ++i) {
7547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    if (IsFolderDelimiter(folder_[i])) {
7647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org      folder_[i] = folder_delimiter_;
7747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    }
7847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  }
7947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org}
8047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
8147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.orgvoid Pathname::clear() {
8247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  folder_.clear();
8347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  basename_.clear();
8447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  extension_.clear();
8547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org}
8647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
8747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.orgbool Pathname::empty() const {
8847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  return folder_.empty() && basename_.empty() && extension_.empty();
8947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org}
9047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
9147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.orgstd::string Pathname::pathname() const {
9247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  std::string pathname(folder_);
9347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  pathname.append(basename_);
9447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  pathname.append(extension_);
9547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  if (pathname.empty()) {
9647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    // Instead of the empty pathname, return the current working directory.
9747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    pathname.push_back('.');
9847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    pathname.push_back(folder_delimiter_);
9947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  }
10047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  return pathname;
10147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org}
10247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
10347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.orgstd::string Pathname::url() const {
10447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  std::string s = "file:///";
10547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  for (size_t i=0; i<folder_.length(); ++i) {
10647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    if (IsFolderDelimiter(folder_[i]))
10747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org      s += '/';
10847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    else
10947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org      s += folder_[i];
11047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  }
11147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  s += basename_;
11247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  s += extension_;
11347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  return UrlEncodeStringForOnlyUnsafeChars(s);
11447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org}
11547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
11647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.orgvoid Pathname::SetPathname(const std::string& pathname) {
11747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  std::string::size_type pos = pathname.find_last_of(FOLDER_DELIMS);
11847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  if (pos != std::string::npos) {
11947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    SetFolder(pathname.substr(0, pos + 1));
12047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    SetFilename(pathname.substr(pos + 1));
12147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  } else {
12247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    SetFolder(EMPTY_STR);
12347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    SetFilename(pathname);
12447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  }
12547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org}
12647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
12747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.orgvoid Pathname::SetPathname(const std::string& folder,
12847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org                           const std::string& filename) {
12947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  SetFolder(folder);
13047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  SetFilename(filename);
13147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org}
13247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
13347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.orgvoid Pathname::AppendPathname(const std::string& pathname) {
13447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  std::string full_pathname(folder_);
13547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  full_pathname.append(pathname);
13647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  SetPathname(full_pathname);
13747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org}
13847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
13947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.orgstd::string Pathname::folder() const {
14047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  return folder_;
14147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org}
14247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
14347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.orgstd::string Pathname::folder_name() const {
14447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  std::string::size_type pos = std::string::npos;
14547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  if (folder_.size() >= 2) {
14647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    pos = folder_.find_last_of(FOLDER_DELIMS, folder_.length() - 2);
14747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  }
14847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  if (pos != std::string::npos) {
14947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    return folder_.substr(pos + 1);
15047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  } else {
15147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    return folder_;
15247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  }
15347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org}
15447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
15547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.orgstd::string Pathname::parent_folder() const {
15647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  std::string::size_type pos = std::string::npos;
15747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  if (folder_.size() >= 2) {
15847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    pos = folder_.find_last_of(FOLDER_DELIMS, folder_.length() - 2);
15947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  }
16047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  if (pos != std::string::npos) {
16147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    return folder_.substr(0, pos + 1);
16247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  } else {
16347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    return EMPTY_STR;
16447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  }
16547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org}
16647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
16747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.orgvoid Pathname::SetFolder(const std::string& folder) {
16847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  folder_.assign(folder);
16947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  // Ensure folder ends in a path delimiter
17047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  if (!folder_.empty() && !IsFolderDelimiter(folder_[folder_.length()-1])) {
17147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    folder_.push_back(folder_delimiter_);
17247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  }
17347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org}
17447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
17547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.orgvoid Pathname::AppendFolder(const std::string& folder) {
17647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  folder_.append(folder);
17747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  // Ensure folder ends in a path delimiter
17847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  if (!folder_.empty() && !IsFolderDelimiter(folder_[folder_.length()-1])) {
17947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    folder_.push_back(folder_delimiter_);
18047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  }
18147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org}
18247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
18347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.orgstd::string Pathname::basename() const {
18447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  return basename_;
18547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org}
18647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
18747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.orgbool Pathname::SetBasename(const std::string& basename) {
18847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  if(basename.find_first_of(FOLDER_DELIMS) != std::string::npos) {
18947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    return false;
19047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  }
19147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  basename_.assign(basename);
19247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  return true;
19347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org}
19447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
19547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.orgstd::string Pathname::extension() const {
19647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  return extension_;
19747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org}
19847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
19947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.orgbool Pathname::SetExtension(const std::string& extension) {
20047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  if (extension.find_first_of(FOLDER_DELIMS) != std::string::npos ||
20147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    extension.find_first_of(EXT_DELIM, 1) != std::string::npos) {
20247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org      return false;
20347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  }
20447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  extension_.assign(extension);
20547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  // Ensure extension begins with the extension delimiter
20647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  if (!extension_.empty() && (extension_[0] != EXT_DELIM)) {
20747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    extension_.insert(extension_.begin(), EXT_DELIM);
20847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  }
20947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  return true;
21047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org}
21147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
21247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.orgstd::string Pathname::filename() const {
21347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  std::string filename(basename_);
21447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  filename.append(extension_);
21547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  return filename;
21647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org}
21747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
21847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.orgbool Pathname::SetFilename(const std::string& filename) {
21947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  std::string::size_type pos = filename.rfind(EXT_DELIM);
22047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  if ((pos == std::string::npos) || (pos == 0)) {
22147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    return SetExtension(EMPTY_STR) && SetBasename(filename);
22247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  } else {
22347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    return SetExtension(filename.substr(pos)) && SetBasename(filename.substr(0, pos));
22447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  }
22547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org}
22647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
22747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org#if defined(WEBRTC_WIN)
22847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.orgbool Pathname::GetDrive(char *drive, uint32 bytes) const {
22947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  return GetDrive(drive, bytes, folder_);
23047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org}
23147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
23247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org// static
23347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.orgbool Pathname::GetDrive(char *drive, uint32 bytes,
23447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org                        const std::string& pathname) {
23547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  // need at lease 4 bytes to save c:
23647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  if (bytes < 4 || pathname.size() < 3) {
23747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org    return false;
23847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  }
23947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
24047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  memcpy(drive, pathname.c_str(), 3);
24147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  drive[3] = 0;
24247be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  // sanity checking
24347be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org  return (isalpha(drive[0]) &&
24447be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org          drive[1] == ':' &&
24547be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org          drive[2] == '\\');
24647be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org}
24747be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org#endif
24847be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
24947be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org///////////////////////////////////////////////////////////////////////////////
25047be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org
25147be73b8629244d6bb63a28198f97f040ce53d21henrike@webrtc.org} // namespace rtc
252