blob: c155e8cd1dc20281f6d547c274c4e18b9c559b32 [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>
15// Temporary, until deprecated helpers are removed.
16#include "webrtc/base/fileutils.h"
17
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&);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000048 Pathname(const std::string& pathname);
49 Pathname(const std::string& folder, const std::string& filename);
50
51 // Set's the default folder delimiter for this Pathname
52 char folder_delimiter() const { return folder_delimiter_; }
53 void SetFolderDelimiter(char delimiter);
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 std::string url() const;
66
67 // Returns the folder and filename components. If the pathname is empty,
68 // returns a string representing the current directory (as a relative path,
69 // i.e., ".").
70 std::string pathname() const;
71 void SetPathname(const std::string& pathname);
72 void SetPathname(const std::string& folder, const std::string& filename);
73
74 // Append pathname to the current folder (if any). Any existing filename
75 // will be discarded.
76 void AppendPathname(const std::string& pathname);
77
78 std::string folder() const;
79 std::string folder_name() const;
80 std::string parent_folder() const;
81 // SetFolder and AppendFolder will append a folder delimiter, if needed.
82 void SetFolder(const std::string& folder);
83 void AppendFolder(const std::string& folder);
84
85 std::string basename() const;
86 bool SetBasename(const std::string& basename);
87
88 std::string extension() const;
89 // SetExtension will prefix a period, if needed.
90 bool SetExtension(const std::string& extension);
91
92 std::string filename() const;
93 bool SetFilename(const std::string& filename);
94
95#if defined(WEBRTC_WIN)
Peter Boström0c4e06b2015-10-07 12:23:21 +020096 bool GetDrive(char* drive, uint32_t bytes) const;
97 static bool GetDrive(char* drive,
98 uint32_t bytes,
99 const std::string& pathname);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000100#endif
101
102private:
103 std::string folder_, basename_, extension_;
104 char folder_delimiter_;
105};
106
107///////////////////////////////////////////////////////////////////////////////
108// Global Helpers (deprecated)
109///////////////////////////////////////////////////////////////////////////////
110
111inline void SetOrganizationName(const std::string& organization) {
112 Filesystem::SetOrganizationName(organization);
113}
114inline void SetApplicationName(const std::string& application) {
115 Filesystem::SetApplicationName(application);
116}
117inline void GetOrganizationName(std::string* organization) {
118 Filesystem::GetOrganizationName(organization);
119}
120inline void GetApplicationName(std::string* application) {
121 Filesystem::GetApplicationName(application);
122}
123inline bool CreateFolder(const Pathname& path) {
124 return Filesystem::CreateFolder(path);
125}
126inline bool FinishPath(Pathname& path, bool create, const std::string& append) {
127 if (!append.empty())
128 path.AppendFolder(append);
129 return !create || CreateFolder(path);
130}
131// Note: this method uses the convention of <temp>/<appname> for the temporary
132// folder. Filesystem uses <temp>/<exename>. We will be migrating exclusively
133// to <temp>/<orgname>/<appname> eventually. Since these are temp folders,
134// it's probably ok to orphan them during the transition.
135inline bool GetTemporaryFolder(Pathname& path, bool create,
136 const std::string& append) {
137 std::string application_name;
138 Filesystem::GetApplicationName(&application_name);
139 ASSERT(!application_name.empty());
140 return Filesystem::GetTemporaryFolder(path, create, &application_name)
141 && FinishPath(path, create, append);
142}
143inline bool GetAppDataFolder(Pathname& path, bool create,
144 const std::string& append) {
145 ASSERT(!create); // TODO: Support create flag on Filesystem::GetAppDataFolder.
146 return Filesystem::GetAppDataFolder(&path, true)
147 && FinishPath(path, create, append);
148}
149inline bool CleanupTemporaryFolder() {
150 Pathname path;
151 if (!GetTemporaryFolder(path, false, ""))
152 return false;
153 if (Filesystem::IsAbsent(path))
154 return true;
155 if (!Filesystem::IsTemporaryPath(path)) {
156 ASSERT(false);
157 return false;
158 }
159 return Filesystem::DeleteFolderContents(path);
160}
161
162///////////////////////////////////////////////////////////////////////////////
163
164} // namespace rtc
165
166#endif // WEBRTC_BASE_PATHUTILS_H__