1 /*
2  * This file is part of the DXX-Rebirth project <https://www.dxx-rebirth.com/>.
3  * It is copyright by its individual contributors, as recorded in the
4  * project's Git history.  See COPYING.txt at the top level for license
5  * terms and a link to the Git history.
6  */
7 
8 #pragma once
9 #include <physfs.h>
10 
11 typedef char file_extension_t[5];
12 
13 #ifdef __cplusplus
14 #include <cstdint>
15 #include <memory>
16 #include "null_sentinel_iterator.h"
17 
18 #include "dxxsconf.h"
19 #include "dsx-ns.h"
20 #include "fwd-partial_range.h"
21 #include <array>
22 
23 namespace dcx {
24 
25 class PHYSFS_list_deleter
26 {
27 public:
operator()28 	void operator()(char **list) const
29 	{
30 		PHYSFS_freeList(list);
31 	}
32 };
33 
34 template <typename D>
35 class PHYSFSX_uncounted_list_template : public std::unique_ptr<char *[], D>
36 {
37 public:
38 	typedef null_sentinel_iterator<char *> const_iterator;
39 	DXX_INHERIT_CONSTRUCTORS(PHYSFSX_uncounted_list_template, std::unique_ptr<char *[], D>);
begin()40 	const_iterator begin() const
41 	{
42 		return this->get();
43 	}
end()44 	const_iterator end() const
45 	{
46 		return {};
47 	}
48 };
49 
50 template <typename D>
51 class PHYSFSX_counted_list_template : public PHYSFSX_uncounted_list_template<D>
52 {
53 	typedef PHYSFSX_uncounted_list_template<D> base_ptr;
54 	using typename base_ptr::pointer;
55 	uint_fast32_t count = 0;
56 public:
57 	using base_ptr::base_ptr;
get_count()58 	uint_fast32_t get_count() const
59 	{
60 		return count;
61 	}
set_count(uint_fast32_t c)62 	void set_count(uint_fast32_t c)
63 	{
64 		count = c;
65 	}
reset()66 	void reset()
67 	{
68 		base_ptr::reset();
69 	}
reset(pointer p)70 	void reset(pointer p)
71 	{
72 		count = 0;
73 		base_ptr::reset(p);
74 	}
75 };
76 
77 typedef PHYSFSX_uncounted_list_template<PHYSFS_list_deleter> PHYSFSX_uncounted_list;
78 typedef PHYSFSX_counted_list_template<PHYSFS_list_deleter> PHYSFSX_counted_list;
79 
80 [[nodiscard]]
81 __attribute_nonnull()
82 PHYSFSX_uncounted_list PHYSFSX_findFiles(const char *path, partial_range_t<const file_extension_t *> exts);
83 
84 [[nodiscard]]
85 __attribute_nonnull()
86 PHYSFSX_uncounted_list PHYSFSX_findabsoluteFiles(const char *path, const char *realpath, const partial_range_t<const file_extension_t *> exts);
87 
88 }
89 #endif
90