1
0

misc_util.h 4.5 KB

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