2 * Copyright (c) 2013 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 * @author Tomasz Iwanek (t.iwanek@samsung.com)
34 #include <dpl/availability.h>
35 #include <dpl/exception.h>
36 #include <dpl/string.h>
44 std::ostream & operator<<(std::ostream & str, const DPL::Utils::Path & path);
49 * @brief The Path class path abstraction
51 * Class for expressing paths not limited not existing ones.
52 * It's possible to check if path exists, remove it or iterate it if it's directory
54 * Created Path object allways contains absolute path, never relative path.
55 * Simplifies common usage cases:
56 * - path construction (with /= and / operators)
57 * - directory iterator (begin(), end(), iterator construction)
58 * - receiving filenames and directory names of given paths
59 * - checking what is pointed by path (Exists(), IsFile(), IsDir())
61 * Check tests for details of usage.
66 DECLARE_EXCEPTION_TYPE(DPL::Exception, BaseException)
67 DECLARE_EXCEPTION_TYPE(BaseException, AlreadyExists) //path already exists
68 DECLARE_EXCEPTION_TYPE(BaseException, NotPrefix) //given path is not prefix of this path
69 DECLARE_EXCEPTION_TYPE(BaseException, NotExists) //file not exists
70 DECLARE_EXCEPTION_TYPE(BaseException, NotDirectory) //directory nto exists
71 DECLARE_EXCEPTION_TYPE(BaseException, OperationFailed) //operation failed due to system error(permission etc..)
72 DECLARE_EXCEPTION_TYPE(BaseException, EmptyPath) //object cannot be constructed with empty path
73 DECLARE_EXCEPTION_TYPE(BaseException, InternalError) //internal error / wrong path usage
74 DECLARE_EXCEPTION_TYPE(BaseException, CannotCopy) //cannot make copy
75 DECLARE_EXCEPTION_TYPE(BaseException, RootDirectoryError) //operation cannot be done with root diretory
77 class Iterator : public std::iterator<std::input_iterator_tag, Path>
81 Iterator(const char *);
82 Iterator& operator++();
83 Iterator operator++(int);
84 bool operator==(const Iterator& rhs) const;
85 bool operator!=(const Iterator& rhs) const;
86 const Path & operator*();
87 const Path * operator->();
91 std::shared_ptr<DIR> m_dir;
92 std::shared_ptr<Path> m_path;
93 std::shared_ptr<Path> m_root;
96 explicit Path(const DPL::String & str);
97 explicit Path(const std::string & str);
98 explicit Path(const char * str);
102 * @brief DirectoryPath shell's dirname equivalent as path
103 * @return directory path
105 Path DirectoryPath() const;
107 * @brief DirectoryName shell's dirname equivalent
108 * @return directory name of given path
110 std::string DirectoryName() const;
112 * @brief Basename shell's basename equivalent
113 * @return base name of given path
115 std::string Filename() const;
117 * @brief Fullpath fullpath based on current working diretory
120 std::string Fullpath() const;
125 std::string Extension() const;
130 bool ExistsAndIsFile() const;
131 bool ExistsAndIsDir() const;
132 bool IsSymlink() const;
133 std::size_t Size() const;
134 std::size_t LastWriteTime() const;
137 * @brief isSubPath Returns relative path to given base
138 * @param prefix base path
139 * @return reltive path
141 * @throws If prefix does not match to this path object
143 bool isSubPath(const Path & other) const;
144 bool hasExtension(const std::string& extension) const;
146 bool operator==(const Path & other) const;
147 bool operator!=(const Path & other) const;
150 Path operator/(const DPL::String& part) const;
151 Path operator/(const std::string& part) const;
152 Path operator/(const char * part) const;
154 Path & operator/=(const DPL::String& part);
155 Path & operator/=(const std::string& part);
156 Path & operator/=(const char * part);
159 Iterator begin() const;
160 Iterator end() const;
162 //root error - throws error on root directory
163 void RootGuard() const;
167 void Append(const std::string& part);
168 void Construct(const std::string & src);
170 std::vector<std::string> m_parts;
172 friend std::ostream & ::operator<<(std::ostream & str, const DPL::Utils::Path & path);
173 } DPL_DEPRECATED_WITH_MESSAGE("Use boost::filesystem::path instead");
176 * @brief MkDir creates 'current path' as directory
180 void MakeDir(const Path & path, mode_t mode = 0755);
183 * @brief MkFile creates 'current path' as empty file
186 void MakeEmptyFile(const Path & path);
189 * @brief Remove removes 'current path'
190 * @param path path to remove
192 void Remove(const Path & path);
195 * @brief TryRemove tries to remvoe path
196 * @param path returns status of removal
198 bool TryRemove(const Path & path);
201 * @brief Rename renames(moves) current path
203 * If you uses this method string to path is internally change
204 * and this object will store new path not only anymore
205 * @param from source path
206 * @param to target path
208 void Rename(const Path & from, const Path & to);
211 * @brief Exists Checks if given path exists
213 * @return true if path exists
215 bool Exists(const Path & path);
220 * @param from source path
221 * @param to target path
223 void CopyFile(const Path & from, const Path & to);
226 * @brief Copy directory recursively
228 * @param from source directory path
229 * @param to target directory path
231 void CopyDir(const Path & from, const Path & to);
233 Path CreateTempPath(const Path & path);
238 //TODO: uncomment when user defiend literals are supported
239 ///Path operator"" p(const char * str);