blob: 0aebcef1fb1e286465afa79819191aa52e28b42c [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:26 +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#ifndef WEBRTC_BASE_PATHUTILS_H__
12#define WEBRTC_BASE_PATHUTILS_H__
13
14#include <string>
zijiehedd87d582016-12-06 15:04:02 -080015
16#include "webrtc/base/checks.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000017
18namespace rtc {
19
20///////////////////////////////////////////////////////////////////////////////
21// Pathname - parsing of pathnames into components, and vice versa.
22//
23// To establish consistent terminology, a filename never contains a folder
24// component. A folder never contains a filename. A pathname may include
25// a folder and/or filename component. Here are some examples:
26//
27// pathname() /home/john/example.txt
28// folder() /home/john/
29// filename() example.txt
30// parent_folder() /home/
31// folder_name() john/
32// basename() example
33// extension() .txt
34//
35// Basename may begin, end, and/or include periods, but no folder delimiters.
36// If extension exists, it consists of a period followed by zero or more
37// non-period/non-delimiter characters, and basename is non-empty.
38///////////////////////////////////////////////////////////////////////////////
39
40class Pathname {
41public:
42 // Folder delimiters are slash and backslash
43 static bool IsFolderDelimiter(char ch);
44 static char DefaultFolderDelimiter();
45
46 Pathname();
kjellander470dd372016-04-19 03:03:23 -070047 Pathname(const Pathname&);
kwiberg4fb3d2b2016-04-22 04:59:31 -070048 Pathname(Pathname&&);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000049 Pathname(const std::string& pathname);
50 Pathname(const std::string& folder, const std::string& filename);
51
kwiberg4fb3d2b2016-04-22 04:59:31 -070052 Pathname& operator=(const Pathname&);
53 Pathname& operator=(Pathname&&);
54
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000055 // Set's the default folder delimiter for this Pathname
56 char folder_delimiter() const { return folder_delimiter_; }
57 void SetFolderDelimiter(char delimiter);
58
59 // Normalize changes all folder delimiters to folder_delimiter()
60 void Normalize();
61
62 // Reset to the empty pathname
63 void clear();
64
65 // Returns true if the pathname is empty. Note: this->pathname().empty()
66 // is always false.
67 bool empty() const;
68
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000069 // Returns the folder and filename components. If the pathname is empty,
70 // returns a string representing the current directory (as a relative path,
71 // i.e., ".").
72 std::string pathname() const;
73 void SetPathname(const std::string& pathname);
74 void SetPathname(const std::string& folder, const std::string& filename);
75
76 // Append pathname to the current folder (if any). Any existing filename
77 // will be discarded.
78 void AppendPathname(const std::string& pathname);
79
80 std::string folder() const;
81 std::string folder_name() const;
82 std::string parent_folder() const;
83 // SetFolder and AppendFolder will append a folder delimiter, if needed.
84 void SetFolder(const std::string& folder);
85 void AppendFolder(const std::string& folder);
86
87 std::string basename() const;
88 bool SetBasename(const std::string& basename);
89
90 std::string extension() const;
91 // SetExtension will prefix a period, if needed.
92 bool SetExtension(const std::string& extension);
93
94 std::string filename() const;
95 bool SetFilename(const std::string& filename);
96
97#if defined(WEBRTC_WIN)
Peter Boström0c4e06b2015-10-07 12:23:21 +020098 bool GetDrive(char* drive, uint32_t bytes) const;
99 static bool GetDrive(char* drive,
100 uint32_t bytes,
101 const std::string& pathname);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000102#endif
103
104private:
105 std::string folder_, basename_, extension_;
106 char folder_delimiter_;
107};
108
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000109} // namespace rtc
110
111#endif // WEBRTC_BASE_PATHUTILS_H__