GCC Code Coverage Report


Directory: src/gate/
File: src/gate/cxx_files.cpp
Date: 2026-09-21 06:26:40
Exec Total Coverage
Lines: 407 431 94.4%
Functions: 104 108 96.3%
Branches: 119 260 45.8%

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 18 static gate_controlstream_t* open_filestream(String const& filepath, enumint_t flags)
51 {
52 18 gate_controlstream_t* ret = NULL;
53
1/2
✓ Branch 2 taken 18 times.
✗ Branch 3 not taken.
18 result_t res = gate_file_openstream(filepath.c_impl(), flags, &ret);
54
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 18 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
18 GATEXX_CHECK_EXCEPTION(res);
55 18 return ret;
56 }
57
58 18 FileStream::FileStream(String const& filepath, enumint_t flags)
59 18 : ControlStream(open_filestream(filepath, flags))
60 {
61 18 }
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 19 FileStream::~FileStream()
73 {
74
75 19 }
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 12 String File::buildPath(String const& parent, String const& subitem)
117 {
118 gate_string_t tmp;
119
2/4
✓ Branch 3 taken 12 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✓ Branch 6 taken 12 times.
12 if (NULL == gate_file_build_path_string(&tmp, parent.c_impl(), subitem.c_impl()))
120 {
121 GATEXX_RAISE_ERROR(results::OutOfMemory);
122 }
123 12 String ret(String::duplicate(tmp));
124
1/2
✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
12 gate_string_release(&tmp);
125 24 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 27 bool File::exists(String const& filepath)
225 {
226 27 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 35 File::Entry::Entry(gate_file_entry_t const& entry)
297 {
298 35 gate_mem_copy(static_cast<gate_file_entry_t*>(this), &entry, sizeof(gate_file_entry_t));
299 35 }
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 56 File::Entry::~Entry() noexcept
318 {
319 56 }
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 1 void File::createAllDirectories(String const& dirpath, CreateDirEnum mode)
500 {
501 1 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 1 times.
1 GATEXX_CHECK_EXCEPTION(res);
503 1 }
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 3 void File::removeDirectoryRecursive(String const& dirpath)
511 {
512 3 result_t res = gate_file_dir_delete_recursive(dirpath.c_impl());
513
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 GATEXX_CHECK_EXCEPTION(res);
514 3 }
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
2/4
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
2 GenericArrayList list(gate_util_stringarray_create());
572
2/4
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
1 if (!list.c_impl())
573 {
574 GATEXX_RAISE_ERROR(results::OutOfMemory);
575 }
576
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());
577
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (GATE_SUCCEEDED(result))
578 {
579
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());
580 }
581 2 return ret;
582 }
583 1 void File::setContent(String const& filePath, String const& content)
584 {
585 1 result_t result = gate_file_set_content(filePath.c_impl(), content.c_impl());
586
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 GATEXX_CHECK_EXCEPTION(result);
587 1 }
588 1 void File::setContent(String const& filePath, char const* buffer, size_t buffer_len)
589 {
590 1 result_t result = gate_file_set_content_buffer(filePath.c_impl(), buffer, buffer_len);
591
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 GATEXX_CHECK_EXCEPTION(result);
592 1 }
593 1 void File::setContentLines(String const& filePath, ArrayList<String> const& lines)
594 {
595 1 gate_arraylist_t list = util::convertStringArray(lines);
596
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!list)
597 {
598 GATEXX_RAISE_ERROR(results::OutOfMemory);
599 }
600 1 result_t result = gate_file_set_content_lines(filePath.c_impl(), list);
601 1 gate_arraylist_release(list);
602
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 GATEXX_CHECK_EXCEPTION(result);
603 1 }
604
605 1 File::SearchFilter::SearchFilter(String const& pattern, bool_t recursive,
606 uint64_t fromSize, uint64_t toSize,
607 1 timestamp_t fromModified, timestamp_t toModified)
608 {
609 1 gate_string_duplicate(&this->pattern, pattern.c_impl());
610 1 this->recursive = recursive;
611 1 this->from_size = fromSize;
612 1 this->to_size = toSize;
613 1 this->from_modified = fromModified;
614 1 this->to_modified = toModified;
615 1 }
616 2 File::SearchFilter::~SearchFilter() noexcept
617 {
618 1 gate_string_release(&this->pattern);
619 1 }
620
621 void File::SearchFilter::setPattern(String const& pattern)
622 {
623 String::assign(this->pattern, pattern);
624 }
625 String File::SearchFilter::getPattern() const
626 {
627 return String::clone(this->pattern);
628 }
629
630 struct GATE_API_LOCAL File_find_dispatcher_param
631 {
632 File::callback_t const* ptr_cb;
633 void* user_param;
634 Exception user_error;
635
636 1 File_find_dispatcher_param()
637 1 : ptr_cb(NULL),
638 user_param(NULL),
639 1 user_error(results::Ok)
640 {
641 1 }
642 };
643
644 32 static gate_bool_t GATE_CALL File_find_dispatcher(gate_file_entry_t const* ptr_entry, void* userparam)
645 {
646 32 File_find_dispatcher_param* ptr_param = static_cast<File_find_dispatcher_param*>(userparam);
647 32 bool_t continueSearch = true;
648
1/2
✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
32 if (ptr_entry)
649 {
650
1/2
✓ Branch 1 taken 32 times.
✗ Branch 2 not taken.
64 File::Entry entry(*ptr_entry);
651 #if defined(GATEXX_NO_EXCEPTIONS)
652 VoidResult result = ptr_param->ptr_cb->tryInvoke(&entry, ptr_param->user_param, &continueSearch);
653 if (result.hasError())
654 {
655 ptr_param->user_error = Exception(result.error(), NULL, "File_find_dispatcher");
656 continueSearch = false;
657 }
658 #else
659 try
660 {
661
1/2
✓ Branch 1 taken 32 times.
✗ Branch 2 not taken.
32 ptr_param->ptr_cb->invoke(&entry, ptr_param->user_param, &continueSearch);
662 }
663 catch(Throwable const& xcpt)
664 {
665 ptr_param->user_error = Exception(xcpt.getResult(), xcpt.getMessage(), xcpt.getSource(), xcpt.getErrorCode());
666 continueSearch = false;
667 }
668 catch(...)
669 {
670 ptr_param->user_error = Exception(results::UnknownException, NULL, "File::find_dispatcher");
671 continueSearch = false;
672 }
673 #endif
674 }
675 32 return continueSearch;
676 }
677
678 1 void File::find(String const& path, SearchFilter const& filter, callback_t const& callback, void* userparam)
679 {
680 2 File_find_dispatcher_param param;
681 1 param.ptr_cb = &callback;
682 1 param.user_param = userparam;
683
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);
684
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
1 GATEXX_CHECK_EXCEPTION(result);
685
686 1 Exception const& xcpt = param.user_error;
687
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 if (GATE_FAILED(xcpt.getResult()))
688 {
689 gate::raiseException(
690 xcpt.getResult(), xcpt.getMessage(),
691 xcpt.getSource(), xcpt.getErrorCode());
692 }
693 1 }
694
695
696
697 char const FilePath::SeparatorChar = gate_file_path_separator_character();;
698
699 4 String const& FilePath::Separator()
700 {
701 static char const chrs[2] = {
702 2 gate_file_path_separator_character(),
703 0
704
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 };
705 #if defined(GATE_COMPILER_MSVC) && !defined(GATE_COMPILER_MSVC03)
706 static StaticString const sep(&chrs[0], gate_size_t(1));
707 #else
708
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));
709 #endif
710 4 return sep;
711 }
712
713 FilePath::FilePath()
714 {
715 }
716 2 FilePath::FilePath(FilePath const& src)
717 2 : value(src.value)
718 {
719 2 }
720 20 FilePath::FilePath(String const& path)
721 20 : value(path)
722 {
723 20 }
724 FilePath& FilePath::operator=(FilePath const& src)
725 {
726 this->value = src.value;
727 return *this;
728 }
729 22 FilePath::~FilePath()
730 {
731 22 }
732
733 9 FilePath FilePath::build(String const& parent, String const& child)
734 {
735 9 return FilePath(File::buildPath(parent, child));
736 }
737
738 1 ArrayList<String> FilePath::split(String const& path, char pathSeparator)
739 {
740 gate_string_t components[512];
741
742
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 gate_mem_clear(&components[0], sizeof(components));
743
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (pathSeparator == 0)
744 {
745
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 pathSeparator = gate_file_path_separator_character();
746 }
747
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
2 String basePath = path.clone();
748
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]));
749
750
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 ArrayList<String> ret(used);
751
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1 times.
4 for (size_t ndx = 0; ndx != used; ++ndx)
752 {
753
1/2
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
3 ret.add(String::createFrom(components[ndx]));
754 }
755
756 2 return ret;
757 }
758
759 1 ArrayList<String> FilePath::split(String const& path)
760 {
761 static char const defaultPathSeparator = 0;
762 1 return FilePath::split(path, defaultPathSeparator);
763 }
764
765
766
767 1 FilePath FilePath::getParent() const
768 {
769 2 String parent;
770
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))
771 {
772 1 return FilePath(parent);
773 }
774 else
775 {
776 return FilePath();
777 }
778 }
779 1 FilePath FilePath::getName() const
780 {
781 2 String name;
782
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))
783 {
784 1 return FilePath(name);
785 }
786 else
787 {
788 return FilePath();
789 }
790 }
791 17 String FilePath::toString() const
792 {
793 17 return this->value;
794 }
795
796 2 FilePath& FilePath::append(FilePath const& path)
797 {
798
1/2
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
2 String newPath = File::buildPath(this->value, path.toString());
799 2 this->value = newPath;
800 4 return *this;
801 }
802
803 1 FilePath& FilePath::operator/=(FilePath const& path)
804 {
805 1 return this->append(path);
806 }
807 1 FilePath& FilePath::operator/=(char const* path)
808 {
809
1/2
✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
1 return this->append(FilePath(String::createStatic(path)));
810 }
811
812 4 FilePath FilePath::operator/(FilePath const& path) const
813 {
814 4 return FilePath::build(this->value, path.value);
815 }
816 5 FilePath FilePath::operator/(char const* path) const
817 {
818
1/2
✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
5 return FilePath::build(this->value, String::createStatic(path));
819 }
820
821
822 3 FilePath::operator String() const
823 {
824 3 return this->toString();
825 }
826
827
828 2 FileDirReader::FileDirReader(String const& dirpath)
829 {
830 2 result_t result = gate_file_dir_open(dirpath.c_impl(), &this->impl);
831
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 GATEXX_CHECK_EXCEPTION(result);
832 2 }
833
834 2 String FileDirReader::nextFilename()
835 {
836 char pathBuffer[GATE_MAX_FILEPATH_LENGTH];
837
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);
838
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 if (pathLength)
839 {
840
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 return String(pathBuffer, pathLength);
841 }
842 1 return String();
843 }
844 2 String FileDirReader::nextPath()
845 {
846 char pathBuffer[GATE_MAX_FILEPATH_LENGTH];
847
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);
848
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 if (pathLength)
849 {
850
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 return String(pathBuffer, pathLength);
851 }
852 1 return String();
853 }
854
855 4 FileDirReader::~FileDirReader()
856 {
857 2 gate_file_dir_close(&this->impl);
858 2 }
859
860
861
862 } // end of namespace gate
863