Initialize Tizen 2.3
[framework/web/wrt-commons.git] / modules_wearable / 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      * @brief Extension
121      * @return extension
122      */
123     std::string Extension() const;
124
125     bool Exists() const;
126     bool IsDir() const;
127     bool IsFile() const;
128     bool ExistsAndIsFile() const;
129     bool ExistsAndIsDir() const;
130     bool IsSymlink() const;
131     std::size_t Size() const;
132     /**
133      * @brief isSubPath Returns relative path to given base
134      * @param prefix base path
135      * @return reltive path
136      *
137      * @throws If prefix does not match to this path object
138      */
139     bool isSubPath(const Path & other) const;
140     bool hasExtension(const std::string& extension) const;
141
142     bool operator==(const Path & other) const;
143     bool operator!=(const Path & other) const;
144
145     //appending to path
146     Path operator/(const DPL::String& part) const;
147     Path operator/(const std::string& part) const;
148     Path operator/(const char * part) const;
149
150     Path & operator/=(const DPL::String& part);
151     Path & operator/=(const std::string& part);
152     Path & operator/=(const char * part);
153
154     //foreach
155     Iterator begin() const;
156     Iterator end() const;
157
158     //root error - throws error on root directory
159     void RootGuard() const;
160
161 private:
162
163     void Append(const std::string& part);
164     void Construct(const std::string & src);
165
166     std::vector<std::string> m_parts;
167
168     friend std::ostream & ::operator<<(std::ostream & str, const DPL::Utils::Path & path);
169 };
170
171 /**
172  * @brief MkDir creates 'current path' as directory
173  * @param path path
174  * @param mode mode
175  */
176 void MakeDir(const Path & path, mode_t mode = 0755);
177
178 /**
179  * @brief MkFile creates 'current path' as empty file
180  * @param path path
181  */
182 void MakeEmptyFile(const Path & path);
183
184 /**
185  * @brief Remove removes 'current path'
186  * @param path path to remove
187  */
188 void Remove(const Path & path);
189
190 /**
191  * @brief TryRemove tries to remvoe path
192  * @param path returns status of removal
193  */
194 bool TryRemove(const Path & path);
195
196 /**
197  * @brief Rename renames(moves) current path
198  *
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
203  */
204 void Rename(const Path & from, const Path & to);
205
206 /**
207  * @brief Exists Checks if given path exists
208  * @param path path
209  * @return true if path exists
210  */
211 bool Exists(const Path & path);
212
213 /**
214  * @brief Copy file
215  *
216  * @param from source path
217  * @param to target path
218  */
219 void CopyFile(const Path & from, const Path & to);
220
221 /**
222  * @brief Copy directory recursively
223  *
224  * @param from source directory path
225  * @param to target directory path
226  */
227 void CopyDir(const Path & from, const Path & to);
228
229 Path CreateTempPath(const Path & path);
230 }
231
232 }
233
234 //TODO: uncomment when user defiend literals are supported
235 ///Path operator"" p(const char * str);
236
237 #endif // PATH_H