GCC Code Coverage Report


Directory: src/gate/
File: src/gate/cxx_files.cpp
Date: 2026-12-03 19:36:00
Exec Total Coverage
Lines: 420 434 96.8%
Functions: 109 109 100.0%
Branches: 124 274 45.3%

Line Branch Exec Source
1 /* GATE PROJECT LICENSE:
2 +----------------------------------------------------------------------------+
3 | Copyright (c) 2018-2026, 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 20 static gate_controlstream_t* open_filestream(String const& filepath, enumint_t flags)
51 {
52 20 gate_controlstream_t* ret = NULL;
53
1/2
✓ Branch 2 taken 20 times.
✗ Branch 3 not taken.
20 result_t res = gate_file_openstream(filepath.c_impl(), flags, &ret);
54
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 20 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
20 GATEXX_CHECK_EXCEPTION(res);
55 20 return ret;
56 }
57
58 20 FileStream::FileStream(String const& filepath, enumint_t flags)
59 20 : ControlStream(open_filestream(filepath, flags))
60 {
61 20 }
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 21 FileStream::~FileStream()
73 {
74
75 21 }
76
77
78
79 1 void FileStreamLock::lock(FileStream& stream)
80 {
81 1 uintptr_t const handle = stream.getResource();
82 1 gate_file_t const file = reinterpret_cast<gate_file_t>(handle);
83 1 result_t const result = gate_file_lock(file, true);
84
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 GATEXX_CHECK_EXCEPTION(result);
85 1 }
86
87 2 bool_t FileStreamLock::tryLock(FileStream& stream) noexcept
88 {
89 2 uintptr_t const handle = stream.getResource();
90 2 gate_file_t const file = reinterpret_cast<gate_file_t>(handle);
91 2 result_t const result = gate_file_lock(file, false);
92 2 return GATE_SUCCEEDED(result);
93 }
94
95 2 result_t FileStreamLock::unlock(FileStream& stream) noexcept
96 {
97 2 uintptr_t const handle = stream.getResource();
98 2 gate_file_t const file = reinterpret_cast<gate_file_t>(handle);
99 2 result_t const result = gate_file_unlock(file);
100 2 return result;
101 }
102
103 1 FileStreamLock::FileStreamLock(FileStream& fileStream)
104 1 : stream(fileStream)
105 {
106 1 FileStreamLock::lock(this->stream);
107 1 }
108
109 2 FileStreamLock::~FileStreamLock() noexcept
110 {
111 1 FileStreamLock::unlock(this->stream);
112 1 }
113
114
115
116 16 String File::buildPath(String const& parent, String const& subitem)
117 {
118 gate_string_t tmp;
119
2/4
✓ Branch 3 taken 16 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✓ Branch 6 taken 16 times.
16 if (NULL == gate_file_build_path_string(&tmp, parent.c_impl(), subitem.c_impl()))
120 {
121 ✗ GATEXX_RAISE_ERROR(results::OutOfMemory);
122 }
123 16 String ret(String::duplicate(tmp));
124
1/2
✓ Branch 1 taken 16 times.
✗ Branch 2 not taken.
16 gate_string_release(&tmp);
125 32 return ret;
126 }
127 3 bool File::splitPath(String const& path, String& parent, String& subitem)
128 {
129 3 gate_string_t parent_str = GATE_STRING_INIT_EMPTY;
130 3 gate_string_t subitem_str = GATE_STRING_INIT_EMPTY;
131
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);
132
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
3 if (GATE_SUCCEEDED(result))
133 {
134 2 parent = String::createFrom(parent_str);
135 2 subitem = String::createFrom(subitem_str);
136 2 return true;
137 }
138 else
139 {
140 1 parent = String::createFrom(parent_str);
141
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 subitem = path.clone();
142 1 return false;
143 }
144 }
145 1 void File::extractPath(String const& path, char* name, size_t nameCapacity, char* parent, size_t parentCapacity)
146 {
147 1 result_t result = gate_file_extract_path(path.c_str(), path.length(), name, nameCapacity, parent, parentCapacity);
148
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 GATEXX_CHECK_EXCEPTION(result);
149 1 }
150 1 void File::extractPath(String const& path, String* name, String* parent)
151 {
152 gate_string_t str_name;
153 gate_string_t str_parent;
154
3/6
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 6 taken 1 times.
✗ Branch 7 not taken.
1 result_t result = gate_file_extract_path_string(
155 path.c_impl(),
156 name ? &str_name : NULL,
157 parent ? &str_parent : NULL);
158
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
1 GATEXX_CHECK_EXCEPTION(result);
159
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (name)
160 {
161 1 *name = String::createFrom(str_name);
162 }
163
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (parent)
164 {
165 1 *parent = String::createFrom(str_parent);
166 }
167 1 }
168
169
170 2 bool_t File::getParent(String const& path, String& parent)
171 {
172 2 gate_string_t parent_str = GATE_STRING_INIT_EMPTY;
173
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);
174
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (GATE_SUCCEEDED(result))
175 {
176 2 parent = String::createFrom(parent_str);
177 2 return true;
178 }
179 ✗ return false;
180 }
181 3 bool_t File::getName(String const& path, String& name)
182 {
183 3 gate_string_t name_str = GATE_STRING_INIT_EMPTY;
184
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);
185
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 if (GATE_SUCCEEDED(result))
186 {
187 3 name = String::createFrom(name_str);
188 3 return true;
189 }
190 ✗ return false;
191 }
192
193 2 String File::baseName(String const& path)
194 {
195 2 String ret;
196
1/2
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
2 File::getName(path, ret);
197 2 return ret;
198 }
199
200 1 String File::dirName(String const& path)
201 {
202 1 String ret;
203
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 File::getParent(path, ret);
204 1 return ret;
205 }
206
207 1 String File::realPath(String const& path)
208 {
209 1 gate_string_t ret = GATE_STRING_INIT_EMPTY;
210
1/2
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 result_t result = gate_file_real_path(path.c_impl(), &ret);
211
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
1 GATEXX_CHECK_EXCEPTION(result);
212 2 return String::createFrom(ret);
213 }
214
215 1 ControlStream File::open(String const& path, enumint_t openFlags)
216 {
217 1 gate_controlstream_t* stream = NULL;
218
1/2
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 result_t result = gate_file_openstream(path.c_impl(), openFlags, &stream);
219
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
1 GATEXX_CHECK_EXCEPTION(result);
220
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
2 return ControlStream(stream);
221 }
222
223
224 29 bool File::exists(String const& filepath)
225 {
226 29 return GATE_SUCCEEDED(gate_file_exists(filepath.c_impl()));
227 }
228 2 void File::copy(String const& srcfilepath, String const& dstfilepath, CopyEnum flags)
229 {
230 2 result_t const res = gate_file_copy(srcfilepath.c_impl(), dstfilepath.c_impl(), (gate_uint32_t)flags);
231
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 GATEXX_CHECK_EXCEPTION(res);
232 2 }
233
234 1 static gate_bool_t GATE_CALL File_copyList_callback(char const* source_path, char const* dest_path, gate_result_t result, void* user_param)
235 {
236 1 File::CopyListCallback const* const ptrCb = static_cast<File::CopyListCallback const*>(user_param);
237
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
2 String const source(source_path);
238
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 String const dest(dest_path);
239
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 ptrCb->invoke(source, dest, result);
240 2 return true;
241 }
242
243 1 void File::copyList(Map<String, String> const& fileMapping, CopyListCallback const& callback, enumint_t flags)
244 {
245 1 result_t const result = gate_file_copy_list(
246 fileMapping.c_impl(), flags,
247 &File_copyList_callback, const_cast<File::CopyListCallback*>(&callback));
248
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 GATEXX_CHECK_EXCEPTION(result);
249 1 }
250
251
252 1 void File::move(String const& srcfilepath, String const& dstfilepath)
253 {
254 1 result_t res = gate_file_move(srcfilepath.c_impl(), dstfilepath.c_impl());
255
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 GATEXX_CHECK_EXCEPTION(res);
256 1 }
257 6 void File::removeFile(String const& filepath)
258 {
259 6 result_t res = gate_file_delete(filepath.c_impl());
260
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 GATEXX_CHECK_EXCEPTION(res);
261 6 }
262 1 void File::createLink(String const& targetpath, String const& linkfile)
263 {
264 1 result_t res = gate_file_create_link(targetpath.c_impl(), linkfile.c_impl());
265
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 GATEXX_CHECK_EXCEPTION(res);
266 1 }
267 1 void File::createHardLink(String const& targetpath, String const& newlinkpath)
268 {
269 1 result_t res = gate_file_create_hardlink(targetpath.c_impl(), newlinkpath.c_impl());
270
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 GATEXX_CHECK_EXCEPTION(res);
271 1 }
272 1 String File::readLink(String const& linkfilepath)
273 {
274 gate_string_t realpath;
275
1/2
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 result_t res = gate_file_read_link(linkfilepath.c_impl(), &realpath);
276
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
1 GATEXX_CHECK_EXCEPTION(res);
277 2 return String::createFrom(realpath);
278 }
279
280 struct GATE_API_LOCAL gate_directory_list_entries_struct
281 {
282 File::callback_t delegate_cb;
283 void* user_param;
284
285 2 gate_directory_list_entries_struct(File::callback_t const& cb, void* param)
286 2 : delegate_cb(cb), user_param(param)
287 {
288 2 }
289 };
290
291 20 File::Entry::Entry()
292 {
293 20 gate_mem_clear(static_cast<gate_file_entry_t*>(this), sizeof(gate_file_entry_t));
294 20 }
295
296 39 File::Entry::Entry(gate_file_entry_t const& entry)
297 {
298 39 gate_mem_copy(static_cast<gate_file_entry_t*>(this), &entry, sizeof(gate_file_entry_t));
299 39 }
300 1 File::Entry::Entry(Entry const& src)
301 {
302 1 gate_mem_copy(static_cast<gate_file_entry_t*>(this),
303 static_cast<gate_file_entry_t const*>(&src),
304 sizeof(gate_file_entry_t));
305 1 }
306 1 File::Entry& File::Entry::operator=(Entry const& src)
307 {
308
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (this != &src)
309 {
310 1 gate_mem_copy(static_cast<gate_file_entry_t*>(this),
311 static_cast<gate_file_entry_t const*>(&src),
312 sizeof(gate_file_entry_t));
313 }
314 1 return *this;
315 }
316
317 60 File::Entry::~Entry() noexcept
318 {
319 60 }
320
321 19 String File::Entry::getName() const
322 {
323 19 return String(this->name, gate_str_length(this->name));
324 }
325 25 String File::Entry::getPath() const
326 {
327 25 return String(this->path, gate_str_length(this->path));
328 }
329 2 uint64_t File::Entry::getSize() const
330 {
331 2 return this->props.size;
332 }
333 1 enumint_t File::Entry::getAttributes() const
334 {
335 1 return this->props.attribs;
336 }
337 1 enumint_t File::Entry::getAccessBits() const
338 {
339 1 return this->props.access;
340 }
341
342 1 DateTime File::Entry::getTimeCreated(bool_t addSystemBias) const
343 {
344 1 return DateTime(this->props.time_created, addSystemBias);
345 }
346 1 DateTime File::Entry::getTimeModified(bool_t addSystemBias) const
347 {
348 1 return DateTime(this->props.time_modified, addSystemBias);
349 }
350 1 DateTime File::Entry::getTimeAccessed(bool_t addSystemBias) const
351 {
352 1 return DateTime(this->props.time_accessed, addSystemBias);
353 }
354
355
356 2 bool_t File::Entry::isDirectory() const
357 {
358 2 return GATE_FLAG_ENABLED(this->props.attribs, GATE_FILEENTRY_ATTRIB_DIRECTORY);
359 }
360 2 bool_t File::Entry::isVolume() const
361 {
362 2 return GATE_FLAG_ENABLED(this->props.attribs, GATE_FILEENTRY_ATTRIB_VOLUME);
363 }
364 2 bool_t File::Entry::isExecutable() const
365 {
366 2 return 0 != (this->props.access & (GATE_FILEENTRY_ACCESS_OWNEREXECUTE | GATE_FILEENTRY_ACCESS_GROUPEXECUTE | GATE_FILEENTRY_ACCESS_ALLEXECUTE));
367 }
368
369
370 19 File::Properties::Properties(gate_file_properties_t const& props)
371 {
372 19 gate_mem_copy(static_cast<gate_file_properties_t*>(this),
373 static_cast<gate_file_properties_t const*>(&props),
374 sizeof(gate_file_properties_t));
375 19 }
376 18 File::Properties::Properties(Properties const& src)
377 {
378 18 gate_mem_copy(static_cast<gate_file_properties_t*>(this),
379 static_cast<gate_file_properties_t const*>(&src),
380 sizeof(gate_file_properties_t));
381
382 18 }
383 2 File::Properties& File::Properties::operator=(Properties const& src)
384 {
385 2 gate_mem_copy(static_cast<gate_file_properties_t*>(this),
386 static_cast<gate_file_properties_t const*>(&src),
387 sizeof(gate_file_properties_t));
388 2 return *this;
389 }
390 37 File::Properties::~Properties()
391 {
392
393 37 }
394
395 3 uint64_t File::Properties::getSize() const
396 {
397 3 return this->size;
398 }
399 1 enumint_t File::Properties::getAttributes() const
400 {
401 1 return this->attribs;
402 }
403 1 enumint_t File::Properties::getAccessBits() const
404 {
405 1 return this->access;
406 }
407 1 DateTime File::Properties::getTimeCreated(bool_t addSystemBias) const
408 {
409 1 return DateTime(this->time_created, addSystemBias);
410 }
411 1 DateTime File::Properties::getTimeModified(bool_t addSystemBias) const
412 {
413 1 return DateTime(this->time_modified, addSystemBias);
414 }
415 1 DateTime File::Properties::getTimeAccessed(bool_t addSystemBias) const
416 {
417 1 return DateTime(this->time_accessed, addSystemBias);
418 }
419
420 1 bool_t File::Properties::isDirectory() const
421 {
422 1 return GATE_FLAG_ENABLED(this->attribs, GATE_FILEENTRY_ATTRIB_DIRECTORY);
423 }
424 1 bool_t File::Properties::isVolume() const
425 {
426 1 return GATE_FLAG_ENABLED(this->attribs, GATE_FILEENTRY_ATTRIB_VOLUME);
427 }
428 1 bool_t File::Properties::isExecutable() const
429 {
430 1 return 0 != (this->access &
431 1 (GATE_FILEENTRY_ACCESS_ALLEXECUTE | GATE_FILEENTRY_ACCESS_GROUPEXECUTE | GATE_FILEENTRY_ACCESS_OWNEREXECUTE));
432 }
433
434
435
436
437
438 2 static bool_t GATE_CALL gate_file_list_entries(gate_file_entry_t const* ptr, void* userparam)
439 {
440 2 gate_directory_list_entries_struct* param = (gate_directory_list_entries_struct*)userparam;
441
1/2
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
2 File::Entry entry(*ptr);
442 2 bool_t continue_listing = true;
443
1/2
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
2 param->delegate_cb(&entry, param->user_param, &continue_listing);
444 4 return continue_listing;
445 }
446
447 1 void File::listEntries(String const& dirpath, callback_t const& callback, void* userparam)
448 {
449 2 gate_directory_list_entries_struct param(callback, userparam);
450
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, &param);
451
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
1 GATEXX_CHECK_EXCEPTION(result);
452 1 }
453 2 static bool_t GATE_CALL gate_file_list_entries_in_map(gate_file_entry_t const* ptr, void* userparam)
454 {
455 2 File::pathmap_t* mapping = static_cast<File::pathmap_t*>(userparam);
456
1/2
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
4 String path(ptr->path);
457
1/2
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
2 String name(ptr->name);
458 2 mapping->tryAdd(path, name);
459 4 return true;
460 }
461
462 1 File::pathmap_t File::listEntryPaths(String const& dirpath)
463 {
464 1 File::pathmap_t paths;
465
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);
466
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
1 GATEXX_CHECK_EXCEPTION(result);
467 1 return paths;
468 }
469
470 1 void File::listRootEntries(callback_t const& callback, void* userparam)
471 {
472 2 gate_directory_list_entries_struct param(callback, userparam);
473
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 gate_result_t result = gate_file_root_list(&gate_file_list_entries, &param);
474
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
1 GATEXX_CHECK_EXCEPTION(result);
475 1 }
476
477
478 1 File::pathmap_t File::listRootEntryPaths()
479 {
480 1 File::pathmap_t paths;
481
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);
482
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
1 GATEXX_CHECK_EXCEPTION(result);
483 1 return paths;
484 }
485
486
487 1 File::Entry File::getEntry(String const& filepath)
488 {
489 gate_file_entry_t direntry;
490
1/2
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 gate_result_t res = gate_file_get_entry(filepath.c_impl(), &direntry);
491
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
1 GATEXX_CHECK_EXCEPTION(res);
492
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
2 return File::Entry(direntry);
493 }
494 4 void File::createDirectory(String const& dirpath, CreateDirEnum mode)
495 {
496 4 result_t res = gate_file_dir_create(dirpath.c_impl(), static_cast<gate_enumint_t>(mode));
497
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 GATEXX_CHECK_EXCEPTION(res);
498 4 }
499 3 void File::createAllDirectories(String const& dirpath, CreateDirEnum mode)
500 {
501 3 result_t res = gate_file_dir_create_all(dirpath.c_impl(), static_cast<gate_enumint_t>(mode));
502
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 GATEXX_CHECK_EXCEPTION(res);
503 3 }
504
505 1 void File::removeDirectory(String const& dirpath)
506 {
507 1 result_t res = gate_file_dir_delete(dirpath.c_impl());
508
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 GATEXX_CHECK_EXCEPTION(res);
509 1 }
510 5 void File::removeDirectoryRecursive(String const& dirpath)
511 {
512 5 result_t res = gate_file_dir_delete_recursive(dirpath.c_impl());
513
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5 GATEXX_CHECK_EXCEPTION(res);
514 5 }
515
516
517 2 bool_t File::isDirectory(String const& dirpath)
518 {
519 2 result_t res = gate_file_dir_exists(dirpath.c_impl());
520 2 return GATE_SUCCEEDED(res);
521 }
522 1 bool_t File::isHidden(String const& path)
523 {
524 1 bool_t ret = false;
525 1 gate_enumint_t attrib = 0;
526 1 gate_enumint_t accessbits = 0;
527
1/2
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 result_t result = gate_file_get_attributes(path.c_impl(), &attrib, &accessbits);
528
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (GATE_SUCCEEDED(result))
529 {
530 1 ret = GATE_FLAG_ENABLED(attrib, GATE_FILEENTRY_ATTRIB_HIDDEN);
531 }
532 1 return ret;
533 }
534 1 bool_t File::isExecutable(String const& filepath)
535 {
536 1 bool_t ret = false;
537 1 gate_enumint_t attrib = 0;
538 1 gate_enumint_t accessbits = 0;
539
1/2
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 result_t result = gate_file_get_attributes(filepath.c_impl(), &attrib, &accessbits);
540
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (GATE_SUCCEEDED(result))
541 {
542
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (!GATE_FLAG_ENABLED(attrib, GATE_FILEENTRY_ATTRIB_DIRECTORY))
543 {
544 1 ret = (0 != (accessbits &
545 (GATE_FILEENTRY_ACCESS_OWNEREXECUTE
546 | GATE_FILEENTRY_ACCESS_GROUPEXECUTE
547 | GATE_FILEENTRY_ACCESS_ALLEXECUTE))
548 );
549 }
550 }
551 1 return ret;
552 }
553
554
555 2 String File::getContent(String const& filePath)
556 {
557 2 gate_string_t buffer = GATE_STRING_INIT_EMPTY;
558
1/2
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
2 result_t result = gate_file_get_content(filePath.c_impl(), &buffer);
559
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
2 GATEXX_CHECK_EXCEPTION(result);
560 4 return String::createFrom(buffer);
561 }
562 1 size_t File::getContent(String const& filePath, char* buffer, size_t buffer_len)
563 {
564 1 result_t result = gate_file_get_content_buffer(filePath.c_impl(), buffer, &buffer_len);
565
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 GATEXX_CHECK_EXCEPTION(result);
566 1 return buffer_len;
567 }
568 1 ArrayList<String> File::getContentLines(String const& filePath)
569 {
570
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 ArrayList<String> ret;
571
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 gate_arraylist_t rawList = gate_util_stringarray_create();
572
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
2 GenericArrayList list = GenericArrayList::createFrom(rawList);
573
3/6
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 1 times.
1 if (gate_arraylist_is_null(list.c_impl()))
574 {
575 ✗ GATEXX_RAISE_ERROR(results::OutOfMemory);
576 }
577
2/4
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 5 taken 1 times.
✗ Branch 6 not taken.
1 result_t result = gate_file_get_content_lines(filePath.c_impl(), list.c_impl());
578
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (GATE_SUCCEEDED(result))
579 {
580
2/4
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
1 ret = util::convertStringArray(list.c_impl());
581 }
582 2 return ret;
583 }
584 1 void File::setContent(String const& filePath, String const& content)
585 {
586 1 result_t result = gate_file_set_content(filePath.c_impl(), content.c_impl());
587
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 GATEXX_CHECK_EXCEPTION(result);
588 1 }
589 1 void File::setContent(String const& filePath, char const* buffer, size_t buffer_len)
590 {
591 1 result_t result = gate_file_set_content_buffer(filePath.c_impl(), buffer, buffer_len);
592
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 GATEXX_CHECK_EXCEPTION(result);
593 1 }
594 1 void File::setContentLines(String const& filePath, ArrayList<String> const& lines)
595 {
596
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 gate_arraylist_t list = util::convertStringArray(lines);
597
2/4
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
1 if (gate_arraylist_is_null(list))
598 {
599 ✗ GATEXX_RAISE_ERROR(results::OutOfMemory);
600 }
601
1/2
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 result_t result = gate_file_set_content_lines(filePath.c_impl(), list);
602
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 gate_arraylist_release(&list);
603
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
1 GATEXX_CHECK_EXCEPTION(result);
604 1 }
605
606 1 File::SearchFilter::SearchFilter(String const& pattern, bool_t recursive,
607 uint64_t fromSize, uint64_t toSize,
608 1 timestamp_t fromModified, timestamp_t toModified)
609 {
610 1 gate_string_duplicate(&this->pattern, pattern.c_impl());
611 1 this->recursive = recursive;
612 1 this->from_size = fromSize;
613 1 this->to_size = toSize;
614 1 this->from_modified = fromModified;
615 1 this->to_modified = toModified;
616 1 }
617 2 File::SearchFilter::~SearchFilter() noexcept
618 {
619 1 gate_string_release(&this->pattern);
620 1 }
621
622 1 void File::SearchFilter::setPattern(String const& pattern)
623 {
624 1 String::assign(this->pattern, pattern);
625 1 }
626 2 String File::SearchFilter::getPattern() const
627 {
628 2 return String::clone(this->pattern);
629 }
630
631 struct GATE_API_LOCAL File_find_dispatcher_param
632 {
633 File::callback_t const* ptr_cb;
634 void* user_param;
635 Exception user_error;
636
637 1 File_find_dispatcher_param()
638 1 : ptr_cb(NULL),
639 user_param(NULL),
640 1 user_error(results::Ok)
641 {
642 1 }
643 };
644
645 36 static gate_bool_t GATE_CALL File_find_dispatcher(gate_file_entry_t const* ptr_entry, void* userparam)
646 {
647 36 File_find_dispatcher_param* ptr_param = static_cast<File_find_dispatcher_param*>(userparam);
648 36 bool_t continueSearch = true;
649
1/2
✓ Branch 0 taken 36 times.
✗ Branch 1 not taken.
36 if (ptr_entry)
650 {
651
1/2
✓ Branch 1 taken 36 times.
✗ Branch 2 not taken.
72 File::Entry entry(*ptr_entry);
652 #if defined(GATEXX_NO_EXCEPTIONS)
653 VoidResult result = ptr_param->ptr_cb->tryInvoke(&entry, ptr_param->user_param, &continueSearch);
654 if (result.hasError())
655 {
656 ptr_param->user_error = Exception(result.error(), NULL, "File_find_dispatcher");
657 continueSearch = false;
658 }
659 #else
660 try
661 {
662
1/2
✓ Branch 1 taken 36 times.
✗ Branch 2 not taken.
36 ptr_param->ptr_cb->invoke(&entry, ptr_param->user_param, &continueSearch);
663 }
664 ✗ catch(Throwable const& xcpt)
665 {
666 ✗ ptr_param->user_error = Exception(xcpt.getResult(), xcpt.getMessage(), xcpt.getSource(), xcpt.getErrorCode());
667 ✗ continueSearch = false;
668 }
669 ✗ catch(...)
670 {
671 ✗ ptr_param->user_error = Exception(results::UnknownException, NULL, "File::find_dispatcher");
672 ✗ continueSearch = false;
673 }
674 #endif
675 }
676 36 return continueSearch;
677 }
678
679 1 void File::find(String const& path, SearchFilter const& filter, callback_t const& callback, void* userparam)
680 {
681 2 File_find_dispatcher_param param;
682 1 param.ptr_cb = &callback;
683 1 param.user_param = userparam;
684
1/2
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 result_t result = gate_file_find(path.c_impl(), &filter, &File_find_dispatcher, &param);
685
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
1 GATEXX_CHECK_EXCEPTION(result);
686
687 1 Exception const& xcpt = param.user_error;
688
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 if (GATE_FAILED(xcpt.getResult()))
689 {
690 ✗ gate::raiseException(
691 xcpt.getResult(), xcpt.getMessage(),
692 xcpt.getSource(), xcpt.getErrorCode());
693 }
694 1 }
695
696
697
698 char const FilePath::SeparatorChar = gate_file_path_separator_character();;
699
700 4 String const& FilePath::Separator()
701 {
702 static char const chrs[2] = {
703 2 gate_file_path_separator_character(),
704 0
705
4/8
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 3 times.
✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 1 times.
✗ Branch 7 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
4 };
706 #if defined(GATE_COMPILER_MSVC) && !defined(GATE_COMPILER_MSVC03)
707 static StaticString const sep(&chrs[0], gate_size_t(1));
708 #else
709
3/4
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 3 times.
✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
4 static StaticString const sep = StaticString(&chrs[0], gate_size_t(1));
710 #endif
711 4 return sep;
712 }
713
714 1 FilePath::FilePath()
715 {
716 1 }
717 2 FilePath::FilePath(FilePath const& src)
718 2 : value(src.value)
719 {
720 2 }
721 26 FilePath::FilePath(String const& path)
722 26 : value(path)
723 {
724 26 }
725 1 FilePath& FilePath::operator=(FilePath const& src)
726 {
727 1 this->value = src.value;
728 1 return *this;
729 }
730 29 FilePath::~FilePath()
731 {
732 29 }
733
734 13 FilePath FilePath::build(String const& parent, String const& child)
735 {
736 13 return FilePath(File::buildPath(parent, child));
737 }
738
739 1 ArrayList<String> FilePath::split(String const& path, char pathSeparator)
740 {
741 gate_string_t components[512];
742
743
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 gate_mem_clear(&components[0], sizeof(components));
744
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (pathSeparator == 0)
745 {
746
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 pathSeparator = gate_file_path_separator_character();
747 }
748
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
2 String basePath = path.clone();
749
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]));
750
751
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 ArrayList<String> ret(used);
752
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1 times.
4 for (size_t ndx = 0; ndx != used; ++ndx)
753 {
754
1/2
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
3 ret.add(String::createFrom(components[ndx]));
755 }
756
757 2 return ret;
758 }
759
760 1 ArrayList<String> FilePath::split(String const& path)
761 {
762 static char const defaultPathSeparator = 0;
763 1 return FilePath::split(path, defaultPathSeparator);
764 }
765
766
767 3 bool_t FilePath::empty() const
768 {
769 3 return this->value.empty();
770 }
771
772 1 FilePath FilePath::getParent() const
773 {
774 2 String parent;
775
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))
776 {
777 1 return FilePath(parent);
778 }
779 else
780 {
781 ✗ return FilePath();
782 }
783 }
784 1 FilePath FilePath::getName() const
785 {
786 2 String name;
787
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))
788 {
789 1 return FilePath(name);
790 }
791 else
792 {
793 ✗ return FilePath();
794 }
795 }
796 25 String FilePath::toString() const
797 {
798 25 return this->value;
799 }
800
801 2 FilePath& FilePath::append(FilePath const& path)
802 {
803
1/2
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
2 String newPath = File::buildPath(this->value, path.toString());
804 2 this->value = newPath;
805 4 return *this;
806 }
807
808 1 FilePath& FilePath::operator/=(FilePath const& path)
809 {
810 1 return this->append(path);
811 }
812 1 FilePath& FilePath::operator/=(char const* path)
813 {
814
1/2
✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
1 return this->append(FilePath(String::createStatic(path)));
815 }
816
817 4 FilePath FilePath::operator/(FilePath const& path) const
818 {
819 4 return FilePath::build(this->value, path.value);
820 }
821 9 FilePath FilePath::operator/(char const* path) const
822 {
823
1/2
✓ Branch 2 taken 9 times.
✗ Branch 3 not taken.
9 return FilePath::build(this->value, String::createStatic(path));
824 }
825
826
827 9 FilePath::operator String() const
828 {
829 9 return this->toString();
830 }
831
832
833 2 FileDirReader::FileDirReader(String const& dirpath)
834 {
835 2 result_t result = gate_file_dir_open(dirpath.c_impl(), &this->impl);
836
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 GATEXX_CHECK_EXCEPTION(result);
837 2 }
838
839 2 String FileDirReader::nextFilename()
840 {
841 char pathBuffer[GATE_MAX_FILEPATH_LENGTH];
842
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);
843
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 if (pathLength)
844 {
845
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 return String(pathBuffer, pathLength);
846 }
847 1 return String();
848 }
849 2 String FileDirReader::nextPath()
850 {
851 char pathBuffer[GATE_MAX_FILEPATH_LENGTH];
852
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);
853
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 if (pathLength)
854 {
855
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 return String(pathBuffer, pathLength);
856 }
857 1 return String();
858 }
859
860 4 FileDirReader::~FileDirReader()
861 {
862 2 gate_file_dir_close(&this->impl);
863 2 }
864
865
866
867 } // end of namespace gate
868