henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1 | /* |
| 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 Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #ifndef RTC_BASE_PATHUTILS_H_ |
| 12 | #define RTC_BASE_PATHUTILS_H_ |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 13 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 14 | #include <string> |
zijiehe | dd87d58 | 2016-12-06 15:04:02 -0800 | [diff] [blame] | 15 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 16 | #include "rtc_base/checks.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 17 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 18 | namespace 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 | |
| 40 | class Pathname { |
| 41 | public: |
| 42 | // Folder delimiters are slash and backslash |
| 43 | static bool IsFolderDelimiter(char ch); |
| 44 | static char DefaultFolderDelimiter(); |
| 45 | |
| 46 | Pathname(); |
| 47 | Pathname(const Pathname&); |
| 48 | Pathname(Pathname&&); |
| 49 | Pathname(const std::string& pathname); |
| 50 | Pathname(const std::string& folder, const std::string& filename); |
| 51 | |
| 52 | Pathname& operator=(const Pathname&); |
| 53 | Pathname& operator=(Pathname&&); |
| 54 | |
| 55 | // Normalize changes all folder delimiters to folder_delimiter() |
| 56 | void Normalize(); |
| 57 | |
| 58 | // Reset to the empty pathname |
| 59 | void clear(); |
| 60 | |
| 61 | // Returns true if the pathname is empty. Note: this->pathname().empty() |
| 62 | // is always false. |
| 63 | bool empty() const; |
| 64 | |
| 65 | // Returns the folder and filename components. If the pathname is empty, |
| 66 | // returns a string representing the current directory (as a relative path, |
| 67 | // i.e., "."). |
| 68 | std::string pathname() const; |
| 69 | void SetPathname(const std::string& pathname); |
| 70 | void SetPathname(const std::string& folder, const std::string& filename); |
| 71 | |
| 72 | std::string folder() const; |
| 73 | std::string parent_folder() const; |
| 74 | // SetFolder and AppendFolder will append a folder delimiter, if needed. |
| 75 | void SetFolder(const std::string& folder); |
| 76 | void AppendFolder(const std::string& folder); |
| 77 | |
| 78 | bool SetBasename(const std::string& basename); |
| 79 | |
| 80 | // SetExtension will prefix a period, if needed. |
| 81 | bool SetExtension(const std::string& extension); |
| 82 | |
| 83 | std::string filename() const; |
| 84 | bool SetFilename(const std::string& filename); |
| 85 | |
| 86 | private: |
| 87 | std::string folder_, basename_, extension_; |
| 88 | char folder_delimiter_; |
| 89 | }; |
| 90 | |
| 91 | } // namespace rtc |
| 92 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 93 | #endif // RTC_BASE_PATHUTILS_H_ |