blob: 59f2a4ac29d91548e642bcf0c771522286cb1c25 [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef RTC_BASE_PATHUTILS_H_
12#define RTC_BASE_PATHUTILS_H_
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000013
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020014#include <string>
zijiehedd87d582016-12-06 15:04:02 -080015
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020016namespace rtc {
17
18///////////////////////////////////////////////////////////////////////////////
19// Pathname - parsing of pathnames into components, and vice versa.
20//
21// To establish consistent terminology, a filename never contains a folder
22// component. A folder never contains a filename. A pathname may include
23// a folder and/or filename component. Here are some examples:
24//
25// pathname() /home/john/example.txt
26// folder() /home/john/
27// filename() example.txt
28// parent_folder() /home/
29// folder_name() john/
30// basename() example
31// extension() .txt
32//
33// Basename may begin, end, and/or include periods, but no folder delimiters.
34// If extension exists, it consists of a period followed by zero or more
35// non-period/non-delimiter characters, and basename is non-empty.
36///////////////////////////////////////////////////////////////////////////////
37
38class Pathname {
Yves Gerey665174f2018-06-19 15:03:05 +020039 public:
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020040 // Folder delimiters are slash and backslash
41 static bool IsFolderDelimiter(char ch);
42 static char DefaultFolderDelimiter();
43
44 Pathname();
45 Pathname(const Pathname&);
46 Pathname(Pathname&&);
47 Pathname(const std::string& pathname);
48 Pathname(const std::string& folder, const std::string& filename);
49
50 Pathname& operator=(const Pathname&);
51 Pathname& operator=(Pathname&&);
52
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020053 // Returns the folder and filename components. If the pathname is empty,
54 // returns a string representing the current directory (as a relative path,
55 // i.e., ".").
56 std::string pathname() const;
57 void SetPathname(const std::string& pathname);
58 void SetPathname(const std::string& folder, const std::string& filename);
59
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020060 // SetFolder and AppendFolder will append a folder delimiter, if needed.
61 void SetFolder(const std::string& folder);
62 void AppendFolder(const std::string& folder);
63
64 bool SetBasename(const std::string& basename);
65
66 // SetExtension will prefix a period, if needed.
67 bool SetExtension(const std::string& extension);
68
69 std::string filename() const;
70 bool SetFilename(const std::string& filename);
71
Yves Gerey665174f2018-06-19 15:03:05 +020072 private:
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020073 std::string folder_, basename_, extension_;
74 char folder_delimiter_;
75};
76
77} // namespace rtc
78
Yves Gerey665174f2018-06-19 15:03:05 +020079#endif // RTC_BASE_PATHUTILS_H_