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)
33 #include <dpl/exception.h>
34 #include <dpl/string.h>
42 std::ostream & operator<<(std::ostream & str, const DPL::Utils::Path & path);
47 * @brief The Path class path abstraction
49 * Class for expressing paths not limited not existing ones.
50 * It's possible to check if path exists, remove it or iterate it if it's directory
52 * Created Path object allways contains absolute path, never relative path.
53 * Simplifies common usage cases:
54 * - path construction (with /= and / operators)
55 * - directory iterator (begin(), end(), iterator construction)
56 * - receiving filenames and directory names of given paths
57 * - checking what is pointed by path (Exists(), IsFile(), IsDir())
59 * Check tests for details of usage.
64 DECLARE_EXCEPTION_TYPE(DPL::Exception, BaseException)
65 DECLARE_EXCEPTION_TYPE(BaseException, AlreadyExists) //path already exists
66 DECLARE_EXCEPTION_TYPE(BaseException, NotPrefix) //given path is not prefix of this path
67 DECLARE_EXCEPTION_TYPE(BaseException, NotExists) //file not exists
68 DECLARE_EXCEPTION_TYPE(BaseException, NotDirectory) //directory nto exists
69 DECLARE_EXCEPTION_TYPE(BaseException, OperationFailed) //operation failed due to system error(permission etc..)
70 DECLARE_EXCEPTION_TYPE(BaseException, EmptyPath) //object cannot be constructed with empty path
71 DECLARE_EXCEPTION_TYPE(BaseException, InternalError) //internal error / wrong path usage
72 DECLARE_EXCEPTION_TYPE(BaseException, CannotCopy) //cannot make copy
73 DECLARE_EXCEPTION_TYPE(BaseException, RootDirectoryError) //operation cannot be done with root diretory
75 class Iterator : public std::iterator<std::input_iterator_tag, Path>
79 Iterator(const char *);
80 Iterator& operator++();
81 Iterator operator++(int);
82 bool operator==(const Iterator& rhs) const;
83 bool operator!=(const Iterator& rhs) const;
84 const Path & operator*();
85 const Path * operator->();
89 std::shared_ptr<DIR> m_dir;
90 std::shared_ptr<Path> m_path;
91 std::shared_ptr<Path> m_root;
94 explicit Path(const DPL::String & str);
95 explicit Path(const std::string & str);
96 explicit Path(const char * str);
100 * @brief DirectoryPath shell's dirname equivalent as path
101 * @return directory path
103 Path DirectoryPath() const;
105 * @brief DirectoryName shell's dirname equivalent
106 * @return directory name of given path
108 std::string DirectoryName() const;
110 * @brief Basename shell's basename equivalent
111 * @return base name of given path
113 std::string Filename() const;
115 * @brief Fullpath fullpath based on current working diretory
118 std::string Fullpath() const;
123 std::string Extension() const;
128 bool ExistsAndIsFile() const;
129 bool ExistsAndIsDir() const;
130 bool IsSymlink() const;
131 std::size_t Size() const;
133 * @brief isSubPath Returns relative path to given base
134 * @param prefix base path
135 * @return reltive path
137 * @throws If prefix does not match to this path object
139 bool isSubPath(const Path & other) const;
140 bool hasExtension(const std::string& extension) const;
142 bool operator==(const Path & other) const;
143 bool operator!=(const Path & other) const;
146 Path operator/(const DPL::String& part) const;
147 Path operator/(const std::string& part) const;
148 Path operator/(const char * part) const;
150 Path & operator/=(const DPL::String& part);
151 Path & operator/=(const std::string& part);
152 Path & operator/=(const char * part);
155 Iterator begin() const;
156 Iterator end() const;
158 //root error - throws error on root directory
159 void RootGuard() const;
163 void Append(const std::string& part);
164 void Construct(const std::string & src);
166 std::vector<std::string> m_parts;
168 friend std::ostream & ::operator<<(std::ostream & str, const DPL::Utils::Path & path);
172 * @brief MkDir creates 'current path' as directory
176 void MakeDir(const Path & path, mode_t mode = 0755);
179 * @brief MkFile creates 'current path' as empty file
182 void MakeEmptyFile(const Path & path);
185 * @brief Remove removes 'current path'
186 * @param path path to remove
188 void Remove(const Path & path);
191 * @brief TryRemove tries to remvoe path
192 * @param path returns status of removal
194 bool TryRemove(const Path & path);
197 * @brief Rename renames(moves) current path
199 * If you uses this method string to path is internally change
200 * and this object will store new path not only anymore
201 * @param from source path
202 * @param to target path
204 void Rename(const Path & from, const Path & to);
207 * @brief Exists Checks if given path exists
209 * @return true if path exists
211 bool Exists(const Path & path);
216 * @param from source path
217 * @param to target path
219 void CopyFile(const Path & from, const Path & to);
222 * @brief Copy directory recursively
224 * @param from source directory path
225 * @param to target directory path
227 void CopyDir(const Path & from, const Path & to);
229 Path CreateTempPath(const Path & path);
234 //TODO: uncomment when user defiend literals are supported
235 ///Path operator"" p(const char * str);