[Release] wrt-commons_0.2.135
[framework/web/wrt-commons.git] / modules / utils / include / dpl / utils / path.h
1 /*
2  * Copyright (c) 2013 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 /**
17  * @file    path.h
18  * @author  Tomasz Iwanek (t.iwanek@samsung.com)
19  * @version 1.0
20  */
21 #ifndef PATH_H
22 #define PATH_H
23
24 #include <dirent.h>
25 #include <sys/stat.h>
26
27 #include <string>
28 #include <sstream>
29 #include <iterator>
30 #include <memory>
31 #include <vector>
32
33 #include <dpl/exception.h>
34 #include <dpl/string.h>
35
36 namespace DPL {
37 namespace Utils {
38 class Path;
39 }
40 }
41
42 std::ostream & operator<<(std::ostream & str, const DPL::Utils::Path & path);
43
44 namespace DPL {
45 namespace Utils {
46 /**
47  * @brief The Path class path abstraction
48  *
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
51  *
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())
58  *
59              * Check tests for details of usage.
60  */
61 class Path
62 {
63 public:
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
74
75     class Iterator : public std::iterator<std::input_iterator_tag, Path>
76     {
77     public:
78         Iterator();
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->();
86     private:
87         void ReadNext();
88
89         std::shared_ptr<DIR> m_dir;
90         std::shared_ptr<Path> m_path;
91         std::shared_ptr<Path> m_root;
92     };
93
94     explicit Path(const DPL::String & str);
95     explicit Path(const std::string & str);
96     explicit Path(const char * str);
97     Path();
98
99     /**
100      * @brief DirectoryPath shell's dirname equivalent as path
101      * @return directory path
102      */
103     Path DirectoryPath() const;
104     /**
105      * @brief DirectoryName shell's dirname equivalent
106      * @return directory name of given path
107      */
108     std::string DirectoryName() const;
109     /**
110      * @brief Basename shell's basename equivalent
111      * @return base name of given path
112      */
113     std::string Filename() const;
114     /**
115      * @brief Fullpath fullpath based on current working diretory
116      * @return full path
117      */
118     std::string Fullpath() const;
119
120     bool Exists() const;
121     bool IsDir() const;
122     bool IsFile() const;
123     bool IsSymlink() const;
124     std::size_t Size() const;
125     /**
126      * @brief isSubPath Returns relative path to given base
127      * @param prefix base path
128      * @return reltive path
129      *
130      * @throws If prefix does not match to this path object
131      */
132     bool isSubPath(const Path & other) const;
133     bool hasExtension(const std::string& extension) const;
134
135     bool operator==(const Path & other) const;
136     bool operator!=(const Path & other) const;
137
138     //appending to path
139     Path operator/(const DPL::String& part) const;
140     Path operator/(const std::string& part) const;
141     Path operator/(const char * part) const;
142
143     Path & operator/=(const DPL::String& part);
144     Path & operator/=(const std::string& part);
145     Path & operator/=(const char * part);
146
147     //foreach
148     Iterator begin() const;
149     Iterator end() const;
150
151     //root error - throws error on root directory
152     void RootGuard() const;
153
154 private:
155
156     void Append(const std::string& part);
157     void Construct(const std::string & src);
158
159     std::vector<std::string> m_parts;
160
161     friend std::ostream & ::operator<<(std::ostream & str, const DPL::Utils::Path & path);
162 };
163
164 /**
165  * @brief MkDir creates 'current path' as directory
166  * @param path path
167  * @param mode mode
168  */
169 void MakeDir(const Path & path, mode_t mode = 0755);
170
171 /**
172  * @brief MkFile creates 'current path' as empty file
173  * @param path path
174  */
175 void MakeEmptyFile(const Path & path);
176
177 /**
178  * @brief Remove removes 'current path'
179  * @param path path to remove
180  */
181 void Remove(const Path & path);
182
183 /**
184  * @brief TryRemove tries to remvoe path
185  * @param path returns status of removal
186  */
187 bool TryRemove(const Path & path);
188
189 /**
190  * @brief Rename renames(moves) current path
191  *
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
196  */
197 void Rename(const Path & from, const Path & to);
198
199 /**
200  * @brief Exists Checks if given path exists
201  * @param path path
202  * @return true if path exists
203  */
204 bool Exists(const Path & path);
205
206 /**
207  * @brief Copy file
208  *
209  * @param from source path
210  * @param to target path
211  */
212 void CopyFile(const Path & from, const Path & to);
213
214 /**
215  * @brief Copy directory recursively
216  *
217  * @param from source directory path
218  * @param to target directory path
219  */
220 void CopyDir(const Path & from, const Path & to);
221
222 }
223
224 }
225
226 //TODO: uncomment when user defiend literals are supported
227 ///Path operator"" p(const char * str);
228
229 #endif // PATH_H