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 bool IsSymlink() const;
124 std::size_t Size() const;
126 * @brief isSubPath Returns relative path to given base
127 * @param prefix base path
128 * @return reltive path
130 * @throws If prefix does not match to this path object
132 bool isSubPath(const Path & other) const;
133 bool hasExtension(const std::string& extension) const;
135 bool operator==(const Path & other) const;
136 bool operator!=(const Path & other) const;
139 Path operator/(const DPL::String& part) const;
140 Path operator/(const std::string& part) const;
141 Path operator/(const char * part) const;
143 Path & operator/=(const DPL::String& part);
144 Path & operator/=(const std::string& part);
145 Path & operator/=(const char * part);
148 Iterator begin() const;
149 Iterator end() const;
151 //root error - throws error on root directory
152 void RootGuard() const;
156 void Append(const std::string& part);
157 void Construct(const std::string & src);
159 std::vector<std::string> m_parts;
161 friend std::ostream & ::operator<<(std::ostream & str, const DPL::Utils::Path & path);
165 * @brief MkDir creates 'current path' as directory
169 void MakeDir(const Path & path, mode_t mode = 0755);
172 * @brief MkFile creates 'current path' as empty file
175 void MakeEmptyFile(const Path & path);
178 * @brief Remove removes 'current path'
179 * @param path path to remove
181 void Remove(const Path & path);
184 * @brief TryRemove tries to remvoe path
185 * @param path returns status of removal
187 bool TryRemove(const Path & path);
190 * @brief Rename renames(moves) current path
192 * If you uses this method string to path is internally change
193 * and this object will store new path not only anymore
194 * @param from source path
195 * @param to target path
197 void Rename(const Path & from, const Path & to);
200 * @brief Exists Checks if given path exists
202 * @return true if path exists
204 bool Exists(const Path & path);
209 * @param from source path
210 * @param to target path
212 void CopyFile(const Path & from, const Path & to);
215 * @brief Copy directory recursively
217 * @param from source directory path
218 * @param to target directory path
220 void CopyDir(const Path & from, const Path & to);
226 //TODO: uncomment when user defiend literals are supported
227 ///Path operator"" p(const char * str);