Debian Codegraph @

common/misc_util.h

raw
1 /*
2 * misc_util.h
3 * -----------
4 * Purpose: Various useful utility functions.
5 * Notes : (currently none)
6 * Authors: OpenMPT Devs
7 * The OpenMPT source code is released under the BSD license. Read LICENSE for more details.
8 */
9
10
11 #pragma once
12
13 #include "openmpt/all/BuildSettings.hpp"
14
15 #include "mpt/base/span.hpp"
16 #include "mpt/exception/exception_text.hpp"
17
18 #include "mptAssert.h"
19 #include "mptBaseMacros.h"
20 #include "mptBaseTypes.h"
21 #include "mptBaseUtils.h"
22 #include "mptString.h"
23
24 // old
25 #include "mptBaseUtils.h"
26 #include "mptStringFormat.h"
27 #include "mptTime.h"
28
29 #include <stdexcept>
30 #include <optional>
31 #include <vector>
32
33 #include <cstdlib>
34
35 #include <stdlib.h>
36
37
38
39 OPENMPT_NAMESPACE_BEGIN
40
41
42
43 namespace Util
44 {
45
46
47
48 // Insert a range of items [insStart, insEnd], and possibly shift item fix to the left.
49 template<typename T>
50 void InsertItem(const T insStart, const T insEnd, T &fix)
51 {
52 MPT_ASSERT(insEnd >= insStart);
53 if(fix >= insStart)
54 {
55 fix += (insEnd - insStart + 1);
56 }
57 }
58
59 // Insert a range of items [insStart, insEnd], and possibly shift items in range [fixStart, fixEnd] to the right.
60 template<typename T>
61 void InsertRange(const T insStart, const T insEnd, T &fixStart, T &fixEnd)
62 {
63 MPT_ASSERT(insEnd >= insStart);
64 const T insLength = insEnd - insStart + 1;
65 if(fixStart >= insEnd)
66 {
67 fixStart += insLength;
68 }
69 if(fixEnd >= insEnd)
70 {
71 fixEnd += insLength;
72 }
73 }
74
75 // Delete a range of items [delStart, delEnd], and possibly shift item fix to the left.
76 template<typename T>
77 void DeleteItem(const T delStart, const T delEnd, T &fix)
78 {
79 MPT_ASSERT(delEnd >= delStart);
80 if(fix > delEnd)
81 {
82 fix -= (delEnd - delStart + 1);
83 }
84 }
85
86 // Delete a range of items [delStart, delEnd], and possibly shift items in range [fixStart, fixEnd] to the left.
87 template<typename T>
88 void DeleteRange(const T delStart, const T delEnd, T &fixStart, T &fixEnd)
89 {
90 MPT_ASSERT(delEnd >= delStart);
91 const T delLength = delEnd - delStart + 1;
92 if(delStart < fixStart && delEnd < fixStart)
93 {
94 // cut part is before loop start
95 fixStart -= delLength;
96 fixEnd -= delLength;
97 } else if(delStart < fixStart && delEnd < fixEnd)
98 {
99 // cut part is partly before loop start
100 fixStart = delStart;
101 fixEnd -= delLength;
102 } else if(delStart >= fixStart && delEnd < fixEnd)
103 {
104 // cut part is in the loop
105 fixEnd -= delLength;
106 } else if(delStart >= fixStart && delStart < fixEnd && delEnd > fixEnd)
107 {
108 // cut part is partly before loop end
109 fixEnd = delStart;
110 }
111 }
112
113
114
115 template<typename T, std::size_t n>
116 class fixed_size_queue
117 {
118 private:
119 T buffer[n+1];
120 std::size_t read_position;
121 std::size_t write_position;
122 public:
123 fixed_size_queue() : read_position(0), write_position(0)
124 {
125 return;
126 }
127 void clear()
128 {
129 read_position = 0;
130 write_position = 0;
131 }
132 std::size_t read_size() const
133 {
134 if ( write_position > read_position )
135 {
136 return write_position - read_position;
137 } else if ( write_position < read_position )
138 {
139 return write_position - read_position + n + 1;
140 } else
141 {
142 return 0;
143 }
144 }
145 std::size_t write_size() const
146 {
147 if ( write_position > read_position )
148 {
149 return read_position - write_position + n;
150 } else if ( write_position < read_position )
151 {
152 return read_position - write_position - 1;
153 } else
154 {
155 return n;
156 }
157 }
158 bool push( const T & v )
159 {
160 if ( !write_size() )
161 {
162 return false;
163 }
164 buffer[write_position] = v;
165 write_position = ( write_position + 1 ) % ( n + 1 );
166 return true;
167 }
168 bool pop() {
169 if ( !read_size() )
170 {
171 return false;
172 }
173 read_position = ( read_position + 1 ) % ( n + 1 );
174 return true;
175 }
176 T peek() {
177 if ( !read_size() )
178 {
179 return T();
180 }
181 return buffer[read_position];
182 }
183 const T * peek_p()
184 {
185 if ( !read_size() )
186 {
187 return nullptr;
188 }
189 return &(buffer[read_position]);
190 }
191 const T * peek_next_p()
192 {
193 if ( read_size() < 2 )
194 {
195 return nullptr;
196 }
197 return &(buffer[(read_position+1)%(n+1)]);
198 }
199 };
200
201
202
203 } // namespace Util
204
205
206
207 #if MPT_OS_WINDOWS
208
209 template <typename Tstring, typename Tbuf, typename Tsize>
210 Tstring ParseMaybeNullTerminatedStringFromBufferWithSizeInBytes(const Tbuf *buf, Tsize sizeBytes)
211 {
212 // REG_SZ may contain a single NUL terminator, multiple NUL terminators, or no NUL terminator at all
213 return Tstring(reinterpret_cast<const typename Tstring::value_type*>(buf), reinterpret_cast<const typename Tstring::value_type*>(buf) + (sizeBytes / sizeof(typename Tstring::value_type))).c_str();
214 }
215
216
217 #endif // MPT_OS_WINDOWS
218
219
220 OPENMPT_NAMESPACE_END