Initialize Tizen 2.3
[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 <ctime>
28 #include <string>
29 #include <sstream>
30 #include <iterator>
31 #include <memory>
32 #include <vector>
33
34 #include <dpl/availability.h>
35 #include <dpl/exception.h>
36 #include <dpl/string.h>
37
38 namespace DPL {
39 namespace Utils {
40 class Path;
41 }
42 }
43
44 std::ostream & operator<<(std::ostream & str, const DPL::Utils::Path & path);
45
46 namespace DPL {
47 namespace Utils {
48 /**
49  * @brief The Path class path abstraction
50  *
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
53  *
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())
60  *
61              * Check tests for details of usage.
62  */
63 class Path
64 {
65 public:
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
76
77     class Iterator : public std::iterator<std::input_iterator_tag, Path>
78     {
79     public:
80         Iterator();
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->();
88     private:
89         void ReadNext();
90
91         std::shared_ptr<DIR> m_dir;
92         std::shared_ptr<Path> m_path;
93         std::shared_ptr<Path> m_root;
94     };
95
96     explicit Path(const DPL::String & str);
97     explicit Path(const std::string & str);
98     explicit Path(const char * str);
99     Path();
100
101     /**
102      * @brief DirectoryPath shell's dirname equivalent as path
103      * @return directory path
104      */
105     Path DirectoryPath() const;
106     /**
107      * @brief DirectoryName shell's dirname equivalent
108      * @return directory name of given path
109      */
110     std::string DirectoryName() const;
111     /**
112      * @brief Basename shell's basename equivalent
113      * @return base name of given path
114      */
115     std::string Filename() const;
116     /**
117      * @brief Fullpath fullpath based on current working diretory
118      * @return full path
119      */
120     std::string Fullpath() const;
121     /**
122      * @brief Extension
123      * @return extension
124      */
125     std::string Extension() const;
126
127     bool Exists() const;
128     bool IsDir() const;
129     bool IsFile() 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;
135
136     /**
137      * @brief isSubPath Returns relative path to given base
138      * @param prefix base path
139      * @return reltive path
140      *
141      * @throws If prefix does not match to this path object
142      */
143     bool isSubPath(const Path & other) const;
144     bool hasExtension(const std::string& extension) const;
145
146     bool operator==(const Path & other) const;
147     bool operator!=(const Path & other) const;
148
149     //appending to path
150     Path operator/(const DPL::String& part) const;
151     Path operator/(const std::string& part) const;
152     Path operator/(const char * part) const;
153
154     Path & operator/=(const DPL::String& part);
155     Path & operator/=(const std::string& part);
156     Path & operator/=(const char * part);
157
158     //foreach
159     Iterator begin() const;
160     Iterator end() const;
161
162     //root error - throws error on root directory
163     void RootGuard() const;
164
165 private:
166
167     void Append(const std::string& part);
168     void Construct(const std::string & src);
169
170     std::vector<std::string> m_parts;
171
172     friend std::ostream & ::operator<<(std::ostream & str, const DPL::Utils::Path & path);
173 } DPL_DEPRECATED_WITH_MESSAGE("Use boost::filesystem::path instead");
174
175 /**
176  * @brief MkDir creates 'current path' as directory
177  * @param path path
178  * @param mode mode
179  */
180 void MakeDir(const Path & path, mode_t mode = 0755);
181
182 /**
183  * @brief MkFile creates 'current path' as empty file
184  * @param path path
185  */
186 void MakeEmptyFile(const Path & path);
187
188 /**
189  * @brief Remove removes 'current path'
190  * @param path path to remove
191  */
192 void Remove(const Path & path);
193
194 /**
195  * @brief TryRemove tries to remvoe path
196  * @param path returns status of removal
197  */
198 bool TryRemove(const Path & path);
199
200 /**
201  * @brief Rename renames(moves) current path
202  *
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
207  */
208 void Rename(const Path & from, const Path & to);
209
210 /**
211  * @brief Exists Checks if given path exists
212  * @param path path
213  * @return true if path exists
214  */
215 bool Exists(const Path & path);
216
217 /**
218  * @brief Copy file
219  *
220  * @param from source path
221  * @param to target path
222  */
223 void CopyFile(const Path & from, const Path & to);
224
225 /**
226  * @brief Copy directory recursively
227  *
228  * @param from source directory path
229  * @param to target directory path
230  */
231 void CopyDir(const Path & from, const Path & to);
232
233 Path CreateTempPath(const Path & path);
234 }
235
236 }
237
238 //TODO: uncomment when user defiend literals are supported
239 ///Path operator"" p(const char * str);
240
241 #endif // PATH_H