1 /*
2 * libjingle
3 * Copyright 2004--2005, Google Inc.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
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.
15 *
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.
26 */
27
28 #ifndef TALK_BASE_PATHUTILS_H__
29 #define TALK_BASE_PATHUTILS_H__
30
31 #include <string>
32 // Temporary, until deprecated helpers are removed.
33 #include "talk/base/fileutils.h"
34
35 namespace talk_base {
36
37 ///////////////////////////////////////////////////////////////////////////////
38 // Pathname - parsing of pathnames into components, and vice versa.
39 //
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:
43 //
44 // pathname() /home/john/example.txt
45 // folder() /home/john/
46 // filename() example.txt
47 // parent_folder() /home/
48 // folder_name() john/
49 // basename() example
50 // extension() .txt
51 //
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 ///////////////////////////////////////////////////////////////////////////////
56
57 class Pathname {
58 public:
59 // Folder delimiters are slash and backslash
60 static bool IsFolderDelimiter(char ch);
61 static char DefaultFolderDelimiter();
62
63 Pathname();
64 Pathname(const std::string& pathname);
65 Pathname(const std::string& folder, const std::string& filename);
66
67 // Set's the default folder delimiter for this Pathname
folder_delimiter()68 char folder_delimiter() const { return folder_delimiter_; }
69 void SetFolderDelimiter(char delimiter);
70
71 // Normalize changes all folder delimiters to folder_delimiter()
72 void Normalize();
73
74 // Reset to the empty pathname
75 void clear();
76
77 // Returns true if the pathname is empty. Note: this->pathname().empty()
78 // is always false.
79 bool empty() const;
80
81 std::string url() const;
82
83 // Returns the folder and filename components. If the pathname is empty,
84 // returns a string representing the current directory (as a relative path,
85 // i.e., ".").
86 std::string pathname() const;
87 void SetPathname(const std::string& pathname);
88 void SetPathname(const std::string& folder, const std::string& filename);
89
90 // Append pathname to the current folder (if any). Any existing filename
91 // will be discarded.
92 void AppendPathname(const std::string& pathname);
93
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);
100
101 std::string basename() const;
102 bool SetBasename(const std::string& basename);
103
104 std::string extension() const;
105 // SetExtension will prefix a period, if needed.
106 bool SetExtension(const std::string& extension);
107
108 std::string filename() const;
109 bool SetFilename(const std::string& filename);
110
111 #ifdef WIN32
112 bool GetDrive(char *drive, uint32 bytes) const;
113 static bool GetDrive(char *drive, uint32 bytes,const std::string& pathname);
114 #endif
115
116 private:
117 std::string folder_, basename_, extension_;
118 char folder_delimiter_;
119 };
120
121 ///////////////////////////////////////////////////////////////////////////////
122 // Global Helpers (deprecated)
123 ///////////////////////////////////////////////////////////////////////////////
124
SetOrganizationName(const std::string & organization)125 inline void SetOrganizationName(const std::string& organization) {
126 Filesystem::SetOrganizationName(organization);
127 }
SetApplicationName(const std::string & application)128 inline void SetApplicationName(const std::string& application) {
129 Filesystem::SetApplicationName(application);
130 }
GetOrganizationName(std::string * organization)131 inline void GetOrganizationName(std::string* organization) {
132 Filesystem::GetOrganizationName(organization);
133 }
GetApplicationName(std::string * application)134 inline void GetApplicationName(std::string* application) {
135 Filesystem::GetApplicationName(application);
136 }
CreateFolder(const Pathname & path)137 inline bool CreateFolder(const Pathname& path) {
138 return Filesystem::CreateFolder(path);
139 }
FinishPath(Pathname & path,bool create,const std::string & append)140 inline bool FinishPath(Pathname& path, bool create, const std::string& append) {
141 if (!append.empty())
142 path.AppendFolder(append);
143 return !create || CreateFolder(path);
144 }
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.
GetTemporaryFolder(Pathname & path,bool create,const std::string & append)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);
156 }
GetAppDataFolder(Pathname & path,bool create,const std::string & 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);
162 }
CleanupTemporaryFolder()163 inline bool CleanupTemporaryFolder() {
164 Pathname path;
165 if (!GetTemporaryFolder(path, false, ""))
166 return false;
167 if (Filesystem::IsAbsent(path))
168 return true;
169 if (!Filesystem::IsTemporaryPath(path)) {
170 ASSERT(false);
171 return false;
172 }
173 return Filesystem::DeleteFolderContents(path);
174 }
175
176 ///////////////////////////////////////////////////////////////////////////////
177
178 } // namespace talk_base
179
180 #endif // TALK_BASE_PATHUTILS_H__
181