merge with master
[platform/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 class Path
53 {
54 public:
55     DECLARE_EXCEPTION_TYPE(DPL::Exception, BaseException)
56     DECLARE_EXCEPTION_TYPE(BaseException, AlreadyExists)
57     DECLARE_EXCEPTION_TYPE(BaseException, NotExists)
58     DECLARE_EXCEPTION_TYPE(BaseException, NotDirectory)
59     DECLARE_EXCEPTION_TYPE(BaseException, OperationFailed)
60     DECLARE_EXCEPTION_TYPE(BaseException, EmptyPath)
61     DECLARE_EXCEPTION_TYPE(BaseException, InternalError)
62
63     class Iterator : public std::iterator<std::input_iterator_tag, Path>
64     {
65     public:
66         Iterator();
67         Iterator(const char *);
68         Iterator& operator++();
69         Iterator operator++(int);
70         bool operator==(const Iterator& rhs) const;
71         bool operator!=(const Iterator& rhs) const;
72         const Path & operator*();
73         const Path * operator->();
74     private:
75         void ReadNext();
76
77         std::shared_ptr<DIR> m_dir;
78         std::shared_ptr<Path> m_path;
79         std::shared_ptr<Path> m_root;
80     };
81
82     explicit Path(const DPL::String & str);
83     explicit Path(const std::string & str);
84     explicit Path(const char * str);
85
86     /**
87      * @brief DirectoryName shell's dirname equivalent
88      * @return directory name of given path
89      */
90     std::string DirectoryName() const;
91     /**
92      * @brief Basename shell's basename equivalent
93      * @return base name of given path
94      */
95     std::string Basename() const;
96     /**
97      * @brief Fullpath fullpath based on current working diretory
98      * @return full path
99      */
100     std::string Fullpath() const;
101
102     bool Exists() const;
103     bool IsDir() const;
104     bool IsFile() const;
105     bool IsSymlink() const;
106
107     bool operator==(const Path & other) const;
108     bool operator!=(const Path & other) const;
109
110     //appending to path
111     Path operator/(const DPL::String& part) const;
112     Path operator/(const std::string& part) const;
113     Path operator/(const char * part) const;
114
115     Path & operator/=(const DPL::String& part);
116     Path & operator/=(const std::string& part);
117     Path & operator/=(const char * part);
118
119     //foreach
120     Iterator begin() const;
121     Iterator end() const;
122
123 private:
124     Path();
125
126     void Append(const std::string& part);
127     void Construct(const std::string & src);
128
129     std::vector<std::string> m_parts;
130
131     friend std::ostream & ::operator<<(std::ostream & str, const DPL::Utils::Path & path);
132 };
133
134 /**
135  * @brief MkDir creates 'current path' as directory
136  * @param path path
137  * @param mode mode
138  */
139 void MakeDir(const Path & path, mode_t mode = 0755);
140
141 /**
142  * @brief MkFile creates 'current path' as empty file
143  * @param path path
144  */
145 void MakeEmptyFile(const Path & path);
146
147 /**
148  * @brief Remove removes 'current path'
149  * @param path path to remove
150  */
151 void Remove(const Path & path);
152
153 /**
154  * @brief Rename renames(moves) current path
155  *
156  * If you uses this method string to path is internally change
157  * and this object will store new path not only anymore
158  * @param from source path
159  * @param to target path
160  */
161 void Rename(const Path & from, const Path & to);
162
163 /**
164  * @brief Exists Checks if given path exists
165  * @param path path
166  * @return true if path exists
167  */
168 bool Exists(const Path & path);
169
170 }
171
172 }
173
174 //TODO: uncomment when user defiend literals are supported
175 ///Path operator"" p(const char * str);
176
177 #endif // PATH_H