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