- Added maximization features, with corresponding plugins. Added painting feature...
[mldemos:mldemos.git] / _3rdParty / dlib / dir_nav / dir_nav_kernel_2.cpp
1 // Copyright (C) 2003  Davis E. King (davis@dlib.net)\r
2 // License: Boost Software License   See LICENSE.txt for the full license.\r
3 #ifndef DLIB_DIR_NAV_KERNEL_2_CPp_\r
4 #define DLIB_DIR_NAV_KERNEL_2_CPp_\r
5 \r
6 #include "../platform.h"\r
7 \r
8 #ifdef POSIX\r
9 \r
10 \r
11 #include "dir_nav_kernel_2.h"\r
12 \r
13 \r
14 \r
15 \r
16 \r
17 namespace dlib\r
18 {\r
19 \r
20 // ----------------------------------------------------------------------------------------\r
21 // ----------------------------------------------------------------------------------------\r
22     // file object implementation\r
23 // ----------------------------------------------------------------------------------------\r
24 // ----------------------------------------------------------------------------------------\r
25 \r
26     void file::\r
27     init (\r
28         const std::string& name\r
29     )\r
30     {\r
31         using namespace std;\r
32         state = new data;\r
33         state->count = 1;\r
34 \r
35 \r
36 \r
37         char buf[PATH_MAX];\r
38         if (realpath(name.c_str(),buf) == 0)\r
39         {\r
40             // the file was not found\r
41             throw file_not_found("Unable to find file " + name);\r
42         }\r
43         state->full_name = buf;\r
44 \r
45 \r
46         string::size_type pos = state->full_name.find_last_of(directory::get_separator());\r
47         if (pos == string::npos)\r
48         {\r
49             // no valid full path has no separtor characters.  \r
50             throw file_not_found("Unable to find file " + name);\r
51         }\r
52         state->name = state->full_name.substr(pos+1);\r
53 \r
54 \r
55         // now find the size of this file\r
56         struct stat64 buffer;\r
57         if (::stat64(state->full_name.c_str(), &buffer) ||\r
58             S_ISDIR(buffer.st_mode))\r
59         {\r
60             // there was an error during the call to stat64 or\r
61             // name is actually a directory\r
62             throw file_not_found("Unable to find file " + name);\r
63         }\r
64         else\r
65         {\r
66             state->file_size = static_cast<uint64>(buffer.st_size);\r
67         }\r
68 \r
69     }\r
70 \r
71 // ----------------------------------------------------------------------------------------\r
72 \r
73     bool file::\r
74     operator == (\r
75         const file& rhs\r
76     ) const \r
77     { \r
78         using namespace std;\r
79         if (state->full_name.size() == 0 && rhs.state->full_name.size() == 0)\r
80             return true;\r
81 \r
82         // These files might have different names but actually represent the same\r
83         // file due to the presence of symbolic links.\r
84         char buf[PATH_MAX];\r
85         string left, right;\r
86         if (realpath(state->full_name.c_str(),buf) == 0)\r
87             return false;    \r
88         left = buf;\r
89 \r
90         if (realpath(rhs.state->full_name.c_str(),buf) == 0)\r
91             return false;    \r
92         right = buf;\r
93 \r
94         return (left == right);\r
95     }\r
96 \r
97 // ----------------------------------------------------------------------------------------\r
98 // ----------------------------------------------------------------------------------------\r
99     // directory object implementation\r
100 // ----------------------------------------------------------------------------------------\r
101 // ----------------------------------------------------------------------------------------\r
102 \r
103     void directory::\r
104     init (\r
105         const std::string& name\r
106     )\r
107     {\r
108         using namespace std;\r
109 \r
110         state = new data;\r
111         state->count = 1;        \r
112         \r
113         char buf[PATH_MAX];\r
114         if (realpath(name.c_str(),buf) == 0)\r
115         {\r
116             // the directory was not found\r
117             throw dir_not_found("Unable to find directory " + name);\r
118         }\r
119         state->full_name = buf;\r
120   \r
121         \r
122         const char sep = get_separator();\r
123         if (is_root_path(state->full_name) == false)\r
124         {\r
125             // ensure that thre is not a trialing separator\r
126             if (state->full_name[state->full_name.size()-1] == sep)\r
127                 state->full_name.erase(state->full_name.size()-1);\r
128 \r
129             // pick out the directory name\r
130             string::size_type pos = state->full_name.find_last_of(sep);\r
131             state->name = state->full_name.substr(pos+1);\r
132         }\r
133         else\r
134         {\r
135             // ensure that there is a trailing separator\r
136             if (state->full_name[state->full_name.size()-1] != sep)\r
137                 state->full_name += sep;\r
138         }\r
139 \r
140 \r
141         struct stat64 buffer;\r
142         // now check that this is actually a valid directory\r
143         if (::stat64(state->full_name.c_str(),&buffer))\r
144         {\r
145             // the directory was not found\r
146             throw dir_not_found("Unable to find directory " + name);\r
147         }\r
148         else if (S_ISDIR(buffer.st_mode) == 0)\r
149         {\r
150             // It is not a directory\r
151             throw dir_not_found("Unable to find directory " + name);\r
152         }\r
153     }\r
154 \r
155 // ----------------------------------------------------------------------------------------\r
156 \r
157     char directory::\r
158     get_separator (\r
159     ) \r
160     {\r
161         return '/';\r
162     }\r
163 \r
164 // ----------------------------------------------------------------------------------------\r
165 \r
166     bool directory::\r
167     operator == (\r
168         const directory& rhs\r
169     ) const \r
170     { \r
171         using namespace std;\r
172         if (state->full_name.size() == 0 && rhs.state->full_name.size() == 0)\r
173             return true;\r
174 \r
175         // These directories might have different names but actually represent the same\r
176         // directory due to the presence of symbolic links.\r
177         char buf[PATH_MAX];\r
178         string left, right;\r
179         if (realpath(state->full_name.c_str(),buf) == 0)\r
180             return false;    \r
181         left = buf;\r
182 \r
183         if (realpath(rhs.state->full_name.c_str(),buf) == 0)\r
184             return false;    \r
185         right = buf;\r
186 \r
187         return (left == right);\r
188     }\r
189 \r
190 // ----------------------------------------------------------------------------------------\r
191 \r
192     const directory directory::\r
193     get_parent (\r
194     ) const\r
195     {\r
196         using namespace std;\r
197         // if *this is the root then just return *this\r
198         if (is_root())\r
199         {\r
200             return *this;\r
201         }\r
202         else\r
203         {\r
204             directory temp;\r
205 \r
206             const char sep = get_separator();\r
207 \r
208             string::size_type pos = state->full_name.find_last_of(sep);\r
209             temp.state->full_name = state->full_name.substr(0,pos);\r
210 \r
211             if ( is_root_path(temp.state->full_name))\r
212             {\r
213                 temp.state->full_name += sep;\r
214             }\r
215             else\r
216             {\r
217                 pos = temp.state->full_name.find_last_of(sep);\r
218                 if (pos != string::npos)\r
219                 {\r
220                     temp.state->name = temp.state->full_name.substr(pos+1);\r
221                 }\r
222                 else\r
223                 {\r
224                     temp.state->full_name += sep;\r
225                 }\r
226             }\r
227             return temp;\r
228         }\r
229     }\r
230 \r
231 // ----------------------------------------------------------------------------------------\r
232 \r
233     bool directory::\r
234     is_root_path (\r
235         const std::string& path\r
236     ) const\r
237     {\r
238         const char sep = get_separator();\r
239         if (path.size() == 1 && path[0] == sep)\r
240             return true;\r
241         else\r
242             return false;   \r
243     }\r
244 \r
245 // ----------------------------------------------------------------------------------------\r
246 \r
247 }\r
248 \r
249 #endif // POSIX\r
250 \r
251 #endif // DLIB_DIR_NAV_KERNEL_2_CPp_\r
252 \r