Line | Branch | Exec | Source |
---|---|---|---|
1 | /* GATE PROJECT LICENSE: | ||
2 | +----------------------------------------------------------------------------+ | ||
3 | | Copyright(c) 2018-2025, Stefan Meislinger <sm@opengate.at> | | ||
4 | | All rights reserved. | | ||
5 | | | | ||
6 | | Redistribution and use in source and binary forms, with or without | | ||
7 | | modification, are permitted provided that the following conditions are met:| | ||
8 | | | | ||
9 | | 1. Redistributions of source code must retain the above copyright notice, | | ||
10 | | this list of conditions and the following disclaimer. | | ||
11 | | 2. Redistributions in binary form must reproduce the above copyright | | ||
12 | | notice, this list of conditions and the following disclaimer in the | | ||
13 | | documentation and/or other materials provided with the distribution. | | ||
14 | | | | ||
15 | | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"| | ||
16 | | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | | ||
17 | | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | | ||
18 | | ARE DISCLAIMED.IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | | ||
19 | | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | | ||
20 | | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | | ||
21 | | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | | ||
22 | | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | | ||
23 | | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | | ||
24 | | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF | | ||
25 | | THE POSSIBILITY OF SUCH DAMAGE. | | ||
26 | +----------------------------------------------------------------------------+ | ||
27 | */ | ||
28 | |||
29 | #include "gate/files.hpp" | ||
30 | #include "gate/results.h" | ||
31 | #include "gate/utilities.h" | ||
32 | #include "gate/utilities.hpp" | ||
33 | #include "gate/exceptions.hpp" | ||
34 | #include "gate/strings.hpp" | ||
35 | |||
36 | namespace gate | ||
37 | { | ||
38 | enumint_t const File::Open_Read = GATE_STREAM_OPEN_READ; | ||
39 | enumint_t const File::Open_Write = GATE_STREAM_OPEN_WRITE; | ||
40 | enumint_t const File::Open_ReadWrite = GATE_STREAM_OPEN_READWRITE; | ||
41 | enumint_t const File::Open_Append = GATE_STREAM_OPEN_APPEND; | ||
42 | enumint_t const File::Open_AppendWrite = GATE_STREAM_OPEN_APPENDWRITE; | ||
43 | enumint_t const File::Open_AppendReadWrite = GATE_STREAM_OPEN_APPENDREADWRITE; | ||
44 | enumint_t const File::Open_CreateOwnerRestricted = GATE_FILE_OPEN_CREATEOWNERRESTRICTED; | ||
45 | enumint_t const File::Open_CreateGroupRestricted = GATE_FILE_OPEN_CREATEGROUPRESTRICTED; | ||
46 | enumint_t const File::Open_CreateUnrestricted = GATE_FILE_OPEN_CREATEUNRESTRICTED; | ||
47 | enumint_t const File::Open_CreateExecutable = GATE_FILE_OPEN_CREATEEXECUTABLE; | ||
48 | enumint_t const File::Open_Shared = GATE_FILE_OPEN_SHARED; | ||
49 | |||
50 | 2 | static gate_controlstream_t* open_filestream(String const& filepath, enumint_t flags) | |
51 | { | ||
52 | 2 | gate_controlstream_t* ret = NULL; | |
53 |
1/2✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
|
2 | result_t res = gate_file_openstream(filepath.c_impl(), flags, &ret); |
54 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
2 | GATEXX_CHECK_EXCEPTION(res); |
55 | 2 | return ret; | |
56 | } | ||
57 | |||
58 | 2 | FileStream::FileStream(String const& filepath, enumint_t flags) | |
59 | 2 | : ControlStream(open_filestream(filepath, flags)) | |
60 | { | ||
61 | 2 | } | |
62 | 1 | FileStream::FileStream(FileStream const& src) | |
63 | 1 | : ControlStream(src) | |
64 | { | ||
65 | |||
66 | 1 | } | |
67 | 1 | FileStream& FileStream::operator=(FileStream const& src) | |
68 | { | ||
69 | 1 | Stream::operator=(src); | |
70 | 1 | return *this; | |
71 | } | ||
72 | 3 | FileStream::~FileStream() | |
73 | { | ||
74 | |||
75 | 3 | } | |
76 | |||
77 | |||
78 | |||
79 | |||
80 | |||
81 | |||
82 | 9 | String File::buildPath(String const& parent, String const& subitem) | |
83 | { | ||
84 | gate_string_t tmp; | ||
85 |
2/4✓ Branch 3 taken 9 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✓ Branch 6 taken 9 times.
|
9 | if (NULL == gate_file_build_path_string(&tmp, parent.c_impl(), subitem.c_impl())) |
86 | { | ||
87 | ✗ | GATEXX_RAISE_ERROR(results::OutOfMemory); | |
88 | } | ||
89 | 9 | String ret(String::duplicate(tmp)); | |
90 |
1/2✓ Branch 1 taken 9 times.
✗ Branch 2 not taken.
|
9 | gate_string_release(&tmp); |
91 | 18 | return ret; | |
92 | } | ||
93 | 3 | bool File::splitPath(String const& path, String& parent, String& subitem) | |
94 | { | ||
95 | 3 | gate_string_t parent_str = GATE_STRING_INIT_EMPTY; | |
96 | 3 | gate_string_t subitem_str = GATE_STRING_INIT_EMPTY; | |
97 |
1/2✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
|
3 | gate_result_t result = gate_file_split_path(path.c_impl(), &parent_str, &subitem_str); |
98 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
|
3 | if (GATE_SUCCEEDED(result)) |
99 | { | ||
100 | 2 | parent = String::createFrom(parent_str); | |
101 | 2 | subitem = String::createFrom(subitem_str); | |
102 | 2 | return true; | |
103 | } | ||
104 | else | ||
105 | { | ||
106 | 1 | parent = String::createFrom(parent_str); | |
107 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | subitem = path.clone(); |
108 | 1 | return false; | |
109 | } | ||
110 | } | ||
111 | 2 | bool_t File::getParent(String const& path, String& parent) | |
112 | { | ||
113 | 2 | gate_string_t parent_str = GATE_STRING_INIT_EMPTY; | |
114 |
1/2✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
|
2 | gate_result_t result = gate_file_split_path(path.c_impl(), &parent_str, NULL); |
115 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 | if (GATE_SUCCEEDED(result)) |
116 | { | ||
117 | 2 | parent = String::createFrom(parent_str); | |
118 | 2 | return true; | |
119 | } | ||
120 | ✗ | return false; | |
121 | } | ||
122 | 3 | bool_t File::getName(String const& path, String& name) | |
123 | { | ||
124 | 3 | gate_string_t name_str = GATE_STRING_INIT_EMPTY; | |
125 |
1/2✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
|
3 | gate_result_t result = gate_file_split_path(path.c_impl(), NULL, &name_str); |
126 |
1/2✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
|
3 | if (GATE_SUCCEEDED(result)) |
127 | { | ||
128 | 3 | name = String::createFrom(name_str); | |
129 | 3 | return true; | |
130 | } | ||
131 | ✗ | return false; | |
132 | } | ||
133 | |||
134 | 2 | String File::baseName(String const& path) | |
135 | { | ||
136 | 2 | String ret; | |
137 |
1/2✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
|
2 | File::getName(path, ret); |
138 | 2 | return ret; | |
139 | } | ||
140 | |||
141 | 1 | String File::dirName(String const& path) | |
142 | { | ||
143 | 1 | String ret; | |
144 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | File::getParent(path, ret); |
145 | 1 | return ret; | |
146 | } | ||
147 | |||
148 | 1 | String File::realPath(String const& path) | |
149 | { | ||
150 | 1 | gate_string_t ret = GATE_STRING_INIT_EMPTY; | |
151 |
1/2✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
1 | result_t result = gate_file_real_path(path.c_impl(), &ret); |
152 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
1 | GATEXX_CHECK_EXCEPTION(result); |
153 | 2 | return String::createFrom(ret); | |
154 | } | ||
155 | |||
156 | |||
157 | 1 | ControlStream File::open(String const& path, enumint_t openFlags) | |
158 | { | ||
159 | 1 | gate_controlstream_t* stream = NULL; | |
160 |
1/2✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
1 | result_t result = gate_file_openstream(path.c_impl(), openFlags, &stream); |
161 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
1 | GATEXX_CHECK_EXCEPTION(result); |
162 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
2 | return ControlStream(stream); |
163 | } | ||
164 | |||
165 | |||
166 | 21 | bool File::exists(String const& filepath) | |
167 | { | ||
168 | 21 | return GATE_SUCCEEDED(gate_file_exists(filepath.c_impl())); | |
169 | } | ||
170 | 1 | void File::copy(String const& srcfilepath, String const& dstfilepath, CopyEnum flags) | |
171 | { | ||
172 | 1 | result_t res = gate_file_copy(srcfilepath.c_impl(), dstfilepath.c_impl(), (gate_uint32_t)flags); | |
173 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | GATEXX_CHECK_EXCEPTION(res); |
174 | 1 | } | |
175 | 1 | void File::move(String const& srcfilepath, String const& dstfilepath) | |
176 | { | ||
177 | 1 | result_t res = gate_file_move(srcfilepath.c_impl(), dstfilepath.c_impl()); | |
178 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | GATEXX_CHECK_EXCEPTION(res); |
179 | 1 | } | |
180 | 3 | void File::removeFile(String const& filepath) | |
181 | { | ||
182 | 3 | result_t res = gate_file_delete(filepath.c_impl()); | |
183 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | GATEXX_CHECK_EXCEPTION(res); |
184 | 3 | } | |
185 | 1 | void File::createLink(String const& targetpath, String const& linkfile) | |
186 | { | ||
187 | 1 | result_t res = gate_file_create_link(targetpath.c_impl(), linkfile.c_impl()); | |
188 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | GATEXX_CHECK_EXCEPTION(res); |
189 | 1 | } | |
190 | 1 | void File::createHardLink(String const& targetpath, String const& newlinkpath) | |
191 | { | ||
192 | 1 | result_t res = gate_file_create_hardlink(targetpath.c_impl(), newlinkpath.c_impl()); | |
193 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | GATEXX_CHECK_EXCEPTION(res); |
194 | 1 | } | |
195 | 1 | String File::readLink(String const& linkfilepath) | |
196 | { | ||
197 | gate_string_t realpath; | ||
198 |
1/2✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
1 | result_t res = gate_file_read_link(linkfilepath.c_impl(), &realpath); |
199 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
1 | GATEXX_CHECK_EXCEPTION(res); |
200 | 2 | return String::createFrom(realpath); | |
201 | } | ||
202 | |||
203 | struct GATE_API_LOCAL gate_directory_list_entries_struct | ||
204 | { | ||
205 | File::callback_t delegate_cb; | ||
206 | void* user_param; | ||
207 | |||
208 | 2 | gate_directory_list_entries_struct(File::callback_t const& cb, void* param) | |
209 | 2 | : delegate_cb(cb), user_param(param) | |
210 | { | ||
211 | 2 | } | |
212 | }; | ||
213 | |||
214 | 20 | File::Entry::Entry() | |
215 | { | ||
216 | 20 | gate_mem_clear(static_cast<gate_file_entry_t*>(this), sizeof(gate_file_entry_t)); | |
217 | 20 | } | |
218 | |||
219 | 3 | File::Entry::Entry(gate_file_entry_t const& entry) | |
220 | { | ||
221 | 3 | gate_mem_copy(static_cast<gate_file_entry_t*>(this), &entry, sizeof(gate_file_entry_t)); | |
222 | 3 | } | |
223 | 1 | File::Entry::Entry(Entry const& src) | |
224 | { | ||
225 | 1 | gate_mem_copy(static_cast<gate_file_entry_t*>(this), | |
226 | static_cast<gate_file_entry_t const*>(&src), | ||
227 | sizeof(gate_file_entry_t)); | ||
228 | 1 | } | |
229 | 1 | File::Entry& File::Entry::operator=(Entry const& src) | |
230 | { | ||
231 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | if (this != &src) |
232 | { | ||
233 | 1 | gate_mem_copy(static_cast<gate_file_entry_t*>(this), | |
234 | static_cast<gate_file_entry_t const*>(&src), | ||
235 | sizeof(gate_file_entry_t)); | ||
236 | } | ||
237 | 1 | return *this; | |
238 | } | ||
239 | |||
240 | 24 | File::Entry::~Entry() noexcept | |
241 | { | ||
242 | 24 | } | |
243 | |||
244 | 19 | String File::Entry::getName() const | |
245 | { | ||
246 | 19 | return String(this->name, gate_str_length(this->name)); | |
247 | } | ||
248 | 25 | String File::Entry::getPath() const | |
249 | { | ||
250 | 25 | return String(this->path, gate_str_length(this->path)); | |
251 | } | ||
252 | 2 | uint64_t File::Entry::getSize() const | |
253 | { | ||
254 | 2 | return this->props.size; | |
255 | } | ||
256 | 1 | enumint_t File::Entry::getAttributes() const | |
257 | { | ||
258 | 1 | return this->props.attribs; | |
259 | } | ||
260 | 1 | enumint_t File::Entry::getAccessBits() const | |
261 | { | ||
262 | 1 | return this->props.access; | |
263 | } | ||
264 | |||
265 | 1 | DateTime File::Entry::getTimeCreated(bool_t addSystemBias) const | |
266 | { | ||
267 | 1 | return DateTime(this->props.time_created, addSystemBias); | |
268 | } | ||
269 | 1 | DateTime File::Entry::getTimeModified(bool_t addSystemBias) const | |
270 | { | ||
271 | 1 | return DateTime(this->props.time_modified, addSystemBias); | |
272 | } | ||
273 | 1 | DateTime File::Entry::getTimeAccessed(bool_t addSystemBias) const | |
274 | { | ||
275 | 1 | return DateTime(this->props.time_accessed, addSystemBias); | |
276 | } | ||
277 | |||
278 | |||
279 | 2 | bool_t File::Entry::isDirectory() const | |
280 | { | ||
281 | 2 | return GATE_FLAG_ENABLED(this->props.attribs, GATE_FILEENTRY_ATTRIB_DIRECTORY); | |
282 | } | ||
283 | 2 | bool_t File::Entry::isVolume() const | |
284 | { | ||
285 | 2 | return GATE_FLAG_ENABLED(this->props.attribs, GATE_FILEENTRY_ATTRIB_VOLUME); | |
286 | } | ||
287 | 2 | bool_t File::Entry::isExecutable() const | |
288 | { | ||
289 | 2 | return 0 != (this->props.access & (GATE_FILEENTRY_ACCESS_OWNEREXECUTE | GATE_FILEENTRY_ACCESS_GROUPEXECUTE | GATE_FILEENTRY_ACCESS_ALLEXECUTE)); | |
290 | } | ||
291 | |||
292 | |||
293 | 19 | File::Properties::Properties(gate_file_properties_t const& props) | |
294 | { | ||
295 | 19 | gate_mem_copy(static_cast<gate_file_properties_t*>(this), | |
296 | static_cast<gate_file_properties_t const*>(&props), | ||
297 | sizeof(gate_file_properties_t)); | ||
298 | 19 | } | |
299 | 18 | File::Properties::Properties(Properties const& src) | |
300 | { | ||
301 | 18 | gate_mem_copy(static_cast<gate_file_properties_t*>(this), | |
302 | static_cast<gate_file_properties_t const*>(&src), | ||
303 | sizeof(gate_file_properties_t)); | ||
304 | |||
305 | 18 | } | |
306 | 2 | File::Properties& File::Properties::operator=(Properties const& src) | |
307 | { | ||
308 | 2 | gate_mem_copy(static_cast<gate_file_properties_t*>(this), | |
309 | static_cast<gate_file_properties_t const*>(&src), | ||
310 | sizeof(gate_file_properties_t)); | ||
311 | 2 | return *this; | |
312 | } | ||
313 | 37 | File::Properties::~Properties() | |
314 | { | ||
315 | |||
316 | 37 | } | |
317 | |||
318 | 3 | uint64_t File::Properties::getSize() const | |
319 | { | ||
320 | 3 | return this->size; | |
321 | } | ||
322 | 1 | enumint_t File::Properties::getAttributes() const | |
323 | { | ||
324 | 1 | return this->attribs; | |
325 | } | ||
326 | 1 | enumint_t File::Properties::getAccessBits() const | |
327 | { | ||
328 | 1 | return this->access; | |
329 | } | ||
330 | 1 | DateTime File::Properties::getTimeCreated(bool_t addSystemBias) const | |
331 | { | ||
332 | 1 | return DateTime(this->time_created, addSystemBias); | |
333 | } | ||
334 | 1 | DateTime File::Properties::getTimeModified(bool_t addSystemBias) const | |
335 | { | ||
336 | 1 | return DateTime(this->time_modified, addSystemBias); | |
337 | } | ||
338 | 1 | DateTime File::Properties::getTimeAccessed(bool_t addSystemBias) const | |
339 | { | ||
340 | 1 | return DateTime(this->time_accessed, addSystemBias); | |
341 | } | ||
342 | |||
343 | 1 | bool_t File::Properties::isDirectory() const | |
344 | { | ||
345 | 1 | return GATE_FLAG_ENABLED(this->attribs, GATE_FILEENTRY_ATTRIB_DIRECTORY); | |
346 | } | ||
347 | 1 | bool_t File::Properties::isVolume() const | |
348 | { | ||
349 | 1 | return GATE_FLAG_ENABLED(this->attribs, GATE_FILEENTRY_ATTRIB_VOLUME); | |
350 | } | ||
351 | 1 | bool_t File::Properties::isExecutable() const | |
352 | { | ||
353 | 1 | return 0 != (this->access & | |
354 | 1 | (GATE_FILEENTRY_ACCESS_ALLEXECUTE | GATE_FILEENTRY_ACCESS_GROUPEXECUTE | GATE_FILEENTRY_ACCESS_OWNEREXECUTE)); | |
355 | } | ||
356 | |||
357 | |||
358 | |||
359 | |||
360 | |||
361 | 2 | static bool_t gate_file_list_entries(gate_file_entry_t const* ptr, void* userparam) | |
362 | { | ||
363 | 2 | gate_directory_list_entries_struct* param = (gate_directory_list_entries_struct*)userparam; | |
364 |
1/2✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
|
2 | File::Entry entry(*ptr); |
365 | 2 | bool_t continue_listing = true; | |
366 |
1/2✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
|
2 | param->delegate_cb(&entry, param->user_param, &continue_listing); |
367 | 4 | return continue_listing; | |
368 | } | ||
369 | |||
370 | 1 | void File::listEntries(String const& dirpath, callback_t const& callback, void* userparam) | |
371 | { | ||
372 | 2 | gate_directory_list_entries_struct param(callback, userparam); | |
373 |
1/2✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
1 | gate_result_t result = gate_file_list(dirpath.c_impl(), &gate_file_list_entries, ¶m); |
374 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
1 | GATEXX_CHECK_EXCEPTION(result); |
375 | 1 | } | |
376 | 2 | static bool_t gate_file_list_entries_in_map(gate_file_entry_t const* ptr, void* userparam) | |
377 | { | ||
378 | 2 | File::pathmap_t* mapping = static_cast<File::pathmap_t*>(userparam); | |
379 | try | ||
380 | { | ||
381 |
1/2✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
|
4 | String path(ptr->path); |
382 |
1/2✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
|
4 | String name(ptr->name); |
383 |
1/2✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
|
2 | mapping->add(path, name); |
384 | } | ||
385 | ✗ | catch (...) {} | |
386 | 2 | return true; | |
387 | } | ||
388 | |||
389 | 1 | File::pathmap_t File::listEntryPaths(String const& dirpath) | |
390 | { | ||
391 | 1 | File::pathmap_t paths; | |
392 |
1/2✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
1 | result_t result = gate_file_list(dirpath.c_impl(), &gate_file_list_entries_in_map, &paths); |
393 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
1 | GATEXX_CHECK_EXCEPTION(result); |
394 | 1 | return paths; | |
395 | } | ||
396 | |||
397 | 1 | void File::listRootEntries(callback_t const& callback, void* userparam) | |
398 | { | ||
399 | 2 | gate_directory_list_entries_struct param(callback, userparam); | |
400 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | gate_result_t result = gate_file_root_list(&gate_file_list_entries, ¶m); |
401 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
1 | GATEXX_CHECK_EXCEPTION(result); |
402 | 1 | } | |
403 | |||
404 | |||
405 | 1 | File::pathmap_t File::listRootEntryPaths() | |
406 | { | ||
407 | 1 | File::pathmap_t paths; | |
408 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | result_t result = gate_file_root_list(&gate_file_list_entries_in_map, &paths); |
409 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
1 | GATEXX_CHECK_EXCEPTION(result); |
410 | 1 | return paths; | |
411 | } | ||
412 | |||
413 | |||
414 | 1 | File::Entry File::getEntry(String const& filepath) | |
415 | { | ||
416 | gate_file_entry_t direntry; | ||
417 |
1/2✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
1 | gate_result_t res = gate_file_get_entry(filepath.c_impl(), &direntry); |
418 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
1 | GATEXX_CHECK_EXCEPTION(res); |
419 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
2 | return File::Entry(direntry); |
420 | } | ||
421 | 3 | void File::createDirectory(String const& dirpath, CreateDirEnum mode) | |
422 | { | ||
423 | 3 | result_t res = gate_file_dir_create(dirpath.c_impl(), static_cast<gate_enumint_t>(mode)); | |
424 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | GATEXX_CHECK_EXCEPTION(res); |
425 | 3 | } | |
426 | 1 | void File::createAllDirectories(String const& dirpath, CreateDirEnum mode) | |
427 | { | ||
428 | 1 | result_t res = gate_file_dir_create_all(dirpath.c_impl(), static_cast<gate_enumint_t>(mode)); | |
429 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | GATEXX_CHECK_EXCEPTION(res); |
430 | 1 | } | |
431 | |||
432 | 1 | void File::removeDirectory(String const& dirpath) | |
433 | { | ||
434 | 1 | result_t res = gate_file_dir_delete(dirpath.c_impl()); | |
435 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | GATEXX_CHECK_EXCEPTION(res); |
436 | 1 | } | |
437 | 2 | void File::removeDirectoryRecursive(String const& dirpath) | |
438 | { | ||
439 | 2 | result_t res = gate_file_dir_delete_recursive(dirpath.c_impl()); | |
440 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | GATEXX_CHECK_EXCEPTION(res); |
441 | 2 | } | |
442 | |||
443 | |||
444 | 2 | bool_t File::isDirectory(String const& dirpath) | |
445 | { | ||
446 | 2 | result_t res = gate_file_dir_exists(dirpath.c_impl()); | |
447 | 2 | return GATE_SUCCEEDED(res); | |
448 | } | ||
449 | 1 | bool_t File::isHidden(String const& path) | |
450 | { | ||
451 | 1 | bool_t ret = false; | |
452 | 1 | gate_enumint_t attrib = 0; | |
453 | 1 | gate_enumint_t accessbits = 0; | |
454 |
1/2✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
1 | result_t result = gate_file_get_attributes(path.c_impl(), &attrib, &accessbits); |
455 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | if (GATE_SUCCEEDED(result)) |
456 | { | ||
457 | 1 | ret = GATE_FLAG_ENABLED(attrib, GATE_FILEENTRY_ATTRIB_HIDDEN); | |
458 | } | ||
459 | 1 | return ret; | |
460 | } | ||
461 | 1 | bool_t File::isExecutable(String const& filepath) | |
462 | { | ||
463 | 1 | bool_t ret = false; | |
464 | 1 | gate_enumint_t attrib = 0; | |
465 | 1 | gate_enumint_t accessbits = 0; | |
466 |
1/2✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
1 | result_t result = gate_file_get_attributes(filepath.c_impl(), &attrib, &accessbits); |
467 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | if (GATE_SUCCEEDED(result)) |
468 | { | ||
469 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | if (!GATE_FLAG_ENABLED(attrib, GATE_FILEENTRY_ATTRIB_DIRECTORY)) |
470 | { | ||
471 | 1 | ret = (0 != (accessbits & | |
472 | (GATE_FILEENTRY_ACCESS_OWNEREXECUTE | ||
473 | | GATE_FILEENTRY_ACCESS_GROUPEXECUTE | ||
474 | | GATE_FILEENTRY_ACCESS_ALLEXECUTE)) | ||
475 | ); | ||
476 | } | ||
477 | } | ||
478 | 1 | return ret; | |
479 | } | ||
480 | |||
481 | |||
482 | 2 | String File::getContent(String const& filePath) | |
483 | { | ||
484 | 2 | gate_string_t buffer = GATE_STRING_INIT_EMPTY; | |
485 |
1/2✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
|
2 | result_t result = gate_file_get_content(filePath.c_impl(), &buffer); |
486 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
2 | GATEXX_CHECK_EXCEPTION(result); |
487 | 4 | return String::createFrom(buffer); | |
488 | } | ||
489 | 1 | size_t File::getContent(String const& filePath, char* buffer, size_t buffer_len) | |
490 | { | ||
491 | 1 | result_t result = gate_file_get_content_buffer(filePath.c_impl(), buffer, &buffer_len); | |
492 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | GATEXX_CHECK_EXCEPTION(result); |
493 | 1 | return buffer_len; | |
494 | } | ||
495 | 1 | ArrayList<String> File::getContentLines(String const& filePath) | |
496 | { | ||
497 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | ArrayList<String> ret; |
498 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | gate_arraylist_t list = gate_util_stringarray_create(); |
499 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (!list) |
500 | { | ||
501 | ✗ | GATEXX_RAISE_ERROR(results::OutOfMemory); | |
502 | } | ||
503 |
1/2✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
1 | result_t result = gate_file_get_content_lines(filePath.c_impl(), list); |
504 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | if (GATE_SUCCEEDED(result)) |
505 | { | ||
506 | try | ||
507 | { | ||
508 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | ret = util::convertStringArray(list); |
509 | } | ||
510 | ✗ | catch (...) | |
511 | { | ||
512 | ✗ | gate_arraylist_release(list); | |
513 | ✗ | throw; | |
514 | } | ||
515 | } | ||
516 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | gate_arraylist_release(list); |
517 | 2 | return ret; | |
518 | } | ||
519 | 1 | void File::setContent(String const& filePath, String const& content) | |
520 | { | ||
521 | 1 | result_t result = gate_file_set_content(filePath.c_impl(), content.c_impl()); | |
522 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | GATEXX_CHECK_EXCEPTION(result); |
523 | 1 | } | |
524 | 1 | void File::setContent(String const& filePath, char const* buffer, size_t buffer_len) | |
525 | { | ||
526 | 1 | result_t result = gate_file_set_content_buffer(filePath.c_impl(), buffer, buffer_len); | |
527 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | GATEXX_CHECK_EXCEPTION(result); |
528 | 1 | } | |
529 | 1 | void File::setContentLines(String const& filePath, ArrayList<String> const& lines) | |
530 | { | ||
531 | 1 | gate_arraylist_t list = util::convertStringArray(lines); | |
532 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (!list) |
533 | { | ||
534 | ✗ | GATEXX_RAISE_ERROR(results::OutOfMemory); | |
535 | } | ||
536 | 1 | result_t result = gate_file_set_content_lines(filePath.c_impl(), list); | |
537 | 1 | gate_arraylist_release(list); | |
538 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | GATEXX_CHECK_EXCEPTION(result); |
539 | 1 | } | |
540 | |||
541 | |||
542 | String const FilePath::Separator = String::createStatic(gate_file_path_separator); | ||
543 | |||
544 | ✗ | FilePath::FilePath() | |
545 | { | ||
546 | ✗ | } | |
547 | 2 | FilePath::FilePath(FilePath const& src) | |
548 | 2 | : value(src.value) | |
549 | { | ||
550 | 2 | } | |
551 | 12 | FilePath::FilePath(String const& path) | |
552 | 12 | : value(path) | |
553 | { | ||
554 | 12 | } | |
555 | ✗ | FilePath& FilePath::operator=(FilePath const& src) | |
556 | { | ||
557 | ✗ | this->value = src.value; | |
558 | ✗ | return *this; | |
559 | } | ||
560 | 14 | FilePath::~FilePath() | |
561 | { | ||
562 | 14 | } | |
563 | |||
564 | 6 | FilePath FilePath::build(String const& parent, String const& child) | |
565 | { | ||
566 | 6 | return FilePath(File::buildPath(parent, child)); | |
567 | } | ||
568 | |||
569 | 1 | ArrayList<String> FilePath::split(String const& path, char pathSeparator) | |
570 | { | ||
571 | gate_string_t components[512]; | ||
572 | |||
573 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | gate_mem_clear(&components[0], sizeof(components)); |
574 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | if (pathSeparator == 0) |
575 | { | ||
576 | 1 | pathSeparator = gate_file_path_separator_char; | |
577 | } | ||
578 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
2 | String basePath = path.clone(); |
579 |
1/2✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
1 | size_t used = gate_file_split_path_components(basePath.c_impl(), pathSeparator, components, sizeof(components) / sizeof(components[0])); |
580 | |||
581 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | ArrayList<String> ret(used); |
582 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1 times.
|
4 | for (size_t ndx = 0; ndx != used; ++ndx) |
583 | { | ||
584 |
1/2✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
|
3 | ret.add(String::createFrom(components[ndx])); |
585 | } | ||
586 | |||
587 | 2 | return ret; | |
588 | } | ||
589 | |||
590 | |||
591 | 1 | FilePath FilePath::getParent() const | |
592 | { | ||
593 | 2 | String parent; | |
594 |
2/4✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
|
1 | if (File::getParent(this->value, parent)) |
595 | { | ||
596 | 1 | return FilePath(parent); | |
597 | } | ||
598 | else | ||
599 | { | ||
600 | ✗ | return FilePath(); | |
601 | } | ||
602 | } | ||
603 | 1 | FilePath FilePath::getName() const | |
604 | { | ||
605 | 2 | String name; | |
606 |
2/4✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
|
1 | if (File::getName(this->value, name)) |
607 | { | ||
608 | 1 | return FilePath(name); | |
609 | } | ||
610 | else | ||
611 | { | ||
612 | ✗ | return FilePath(); | |
613 | } | ||
614 | } | ||
615 | 12 | String FilePath::toString() const | |
616 | { | ||
617 | 12 | return this->value; | |
618 | } | ||
619 | |||
620 | 2 | FilePath& FilePath::append(FilePath const& path) | |
621 | { | ||
622 |
1/2✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
|
2 | String newPath = File::buildPath(this->value, path.toString()); |
623 | 2 | this->value = newPath; | |
624 | 4 | return *this; | |
625 | } | ||
626 | |||
627 | 1 | FilePath& FilePath::operator/=(FilePath const& path) | |
628 | { | ||
629 | 1 | return this->append(path); | |
630 | } | ||
631 | 1 | FilePath& FilePath::operator/=(char const* path) | |
632 | { | ||
633 |
1/2✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
|
1 | return this->append(FilePath(String::createStatic(path))); |
634 | } | ||
635 | |||
636 | 1 | FilePath FilePath::operator/(FilePath const& path) const | |
637 | { | ||
638 | 1 | return FilePath::build(this->value, path.value); | |
639 | } | ||
640 | 5 | FilePath FilePath::operator/(char const* path) const | |
641 | { | ||
642 |
1/2✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
|
5 | return FilePath::build(this->value, String::createStatic(path)); |
643 | } | ||
644 | |||
645 | |||
646 | ✗ | FilePath::operator String() const | |
647 | { | ||
648 | ✗ | return this->toString(); | |
649 | } | ||
650 | |||
651 | |||
652 | 2 | FileDirReader::FileDirReader(String const& dirpath) | |
653 | { | ||
654 | 2 | result_t result = gate_file_dir_open(dirpath.c_impl(), &this->impl); | |
655 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | GATEXX_CHECK_EXCEPTION(result); |
656 | 2 | } | |
657 | |||
658 | 2 | String FileDirReader::nextFilename() | |
659 | { | ||
660 | char pathBuffer[GATE_MAX_FILEPATH_LENGTH]; | ||
661 |
1/2✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
|
2 | size_t pathLength = gate_file_dir_read(&this->impl, pathBuffer, sizeof(pathBuffer), true); |
662 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
|
2 | if (pathLength) |
663 | { | ||
664 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | return String(pathBuffer, pathLength); |
665 | } | ||
666 | 1 | return String(); | |
667 | } | ||
668 | 2 | String FileDirReader::nextPath() | |
669 | { | ||
670 | char pathBuffer[GATE_MAX_FILEPATH_LENGTH]; | ||
671 |
1/2✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
|
2 | size_t pathLength = gate_file_dir_read(&this->impl, pathBuffer, sizeof(pathBuffer), false); |
672 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
|
2 | if (pathLength) |
673 | { | ||
674 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | return String(pathBuffer, pathLength); |
675 | } | ||
676 | 1 | return String(); | |
677 | } | ||
678 | |||
679 | 4 | FileDirReader::~FileDirReader() | |
680 | { | ||
681 | 2 | gate_file_dir_close(&this->impl); | |
682 | 2 | } | |
683 | |||
684 | |||
685 | |||
686 | } // end of namespace gate | ||
687 |