3
* Copyright 2004--2005, Google Inc.
5
* Redistribution and use in source and binary forms, with or without
6
* modification, are permitted provided that the following conditions are met:
8
* 1. Redistributions of source code must retain the above copyright notice,
9
* this list of conditions and the following disclaimer.
10
* 2. Redistributions in binary form must reproduce the above copyright notice,
11
* this list of conditions and the following disclaimer in the documentation
12
* and/or other materials provided with the distribution.
13
* 3. The name of the author may not be used to endorse or promote products
14
* derived from this software without specific prior written permission.
16
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19
* EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
#ifndef TALK_BASE_PATHUTILS_H__
29
#define TALK_BASE_PATHUTILS_H__
32
// Temporary, until deprecated helpers are removed.
33
#include "talk/base/fileutils.h"
37
///////////////////////////////////////////////////////////////////////////////
38
// Pathname - parsing of pathnames into components, and vice versa.
40
// To establish consistent terminology, a filename never contains a folder
41
// component. A folder never contains a filename. A pathname may include
42
// a folder and/or filename component. Here are some examples:
44
// pathname() /home/john/example.txt
45
// folder() /home/john/
46
// filename() example.txt
47
// parent_folder() /home/
48
// folder_name() john/
52
// Basename may begin, end, and/or include periods, but no folder delimiters.
53
// If extension exists, it consists of a period followed by zero or more
54
// non-period/non-delimiter characters, and basename is non-empty.
55
///////////////////////////////////////////////////////////////////////////////
59
// Folder delimiters are slash and backslash
60
static bool IsFolderDelimiter(char ch);
61
static char DefaultFolderDelimiter();
64
Pathname(const std::string& pathname);
65
Pathname(const std::string& folder, const std::string& filename);
67
// Set's the default folder delimiter for this Pathname
68
char folder_delimiter() const { return folder_delimiter_; }
69
void SetFolderDelimiter(char delimiter);
71
// Normalize changes all folder delimiters to folder_delimiter()
74
// Reset to the empty pathname
77
// Returns true if the pathname is empty. Note: this->pathname().empty()
81
std::string url() const;
83
// Returns the folder and filename components. If the pathname is empty,
84
// returns a string representing the current directory (as a relative path,
86
std::string pathname() const;
87
void SetPathname(const std::string& pathname);
88
void SetPathname(const std::string& folder, const std::string& filename);
90
// Append pathname to the current folder (if any). Any existing filename
92
void AppendPathname(const std::string& pathname);
94
std::string folder() const;
95
std::string folder_name() const;
96
std::string parent_folder() const;
97
// SetFolder and AppendFolder will append a folder delimiter, if needed.
98
void SetFolder(const std::string& folder);
99
void AppendFolder(const std::string& folder);
101
std::string basename() const;
102
bool SetBasename(const std::string& basename);
104
std::string extension() const;
105
// SetExtension will prefix a period, if needed.
106
bool SetExtension(const std::string& extension);
108
std::string filename() const;
109
bool SetFilename(const std::string& filename);
112
bool GetDrive(char *drive, uint32 bytes) const;
113
static bool GetDrive(char *drive, uint32 bytes,const std::string& pathname);
117
std::string folder_, basename_, extension_;
118
char folder_delimiter_;
121
///////////////////////////////////////////////////////////////////////////////
122
// Global Helpers (deprecated)
123
///////////////////////////////////////////////////////////////////////////////
125
inline void SetOrganizationName(const std::string& organization) {
126
Filesystem::SetOrganizationName(organization);
128
inline void SetApplicationName(const std::string& application) {
129
Filesystem::SetApplicationName(application);
131
inline void GetOrganizationName(std::string* organization) {
132
Filesystem::GetOrganizationName(organization);
134
inline void GetApplicationName(std::string* application) {
135
Filesystem::GetApplicationName(application);
137
inline bool CreateFolder(const Pathname& path) {
138
return Filesystem::CreateFolder(path);
140
inline bool FinishPath(Pathname& path, bool create, const std::string& append) {
142
path.AppendFolder(append);
143
return !create || CreateFolder(path);
145
// Note: this method uses the convention of <temp>/<appname> for the temporary
146
// folder. Filesystem uses <temp>/<exename>. We will be migrating exclusively
147
// to <temp>/<orgname>/<appname> eventually. Since these are temp folders,
148
// it's probably ok to orphan them during the transition.
149
inline bool GetTemporaryFolder(Pathname& path, bool create,
150
const std::string& append) {
151
std::string application_name;
152
Filesystem::GetApplicationName(&application_name);
153
ASSERT(!application_name.empty());
154
return Filesystem::GetTemporaryFolder(path, create, &application_name)
155
&& FinishPath(path, create, append);
157
inline bool GetAppDataFolder(Pathname& path, bool create,
158
const std::string& append) {
159
ASSERT(!create); // TODO: Support create flag on Filesystem::GetAppDataFolder.
160
return Filesystem::GetAppDataFolder(&path, true)
161
&& FinishPath(path, create, append);
163
inline bool CleanupTemporaryFolder() {
165
if (!GetTemporaryFolder(path, false, ""))
167
if (Filesystem::IsAbsent(path))
169
if (!Filesystem::IsTemporaryPath(path)) {
173
return Filesystem::DeleteFolderContents(path);
176
///////////////////////////////////////////////////////////////////////////////
178
} // namespace talk_base
180
#endif // TALK_BASE_PATHUTILS_H__