Subversion Repositories Games.Descent

Rev

Blame | Last modification | View Log | Download | RSS feed

  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. #pragma once
  8.  
  9. #ifdef __cplusplus
  10. #include <cstddef>
  11. #include <cstdint>
  12. #include <cstring>
  13. #include "dxxsconf.h"
  14. #include "dsx-ns.h"
  15. #include <array>
  16. #include <vector>
  17.  
  18. namespace dcx {
  19.  
  20. #if defined(macintosh)
  21. #define snprintf macintosh_snprintf
  22. extern void snprintf(char *out_string, int size, const char * format, ... );
  23. #endif
  24.  
  25. #ifdef DXX_HAVE_STRCASECMP
  26. #define d_stricmp strcasecmp
  27. static inline int d_strnicmp(const char *s1, const char *s2, size_t n)
  28. {
  29.         return strncasecmp(s1, s2, n);
  30. }
  31. #else
  32. __attribute_nonnull()
  33. extern int d_stricmp( const char *s1, const char *s2 );
  34. __attribute_nonnull()
  35. int d_strnicmp(const char *s1, const char *s2, uint_fast32_t n);
  36. #endif
  37. extern void d_strlwr( char *s1 );
  38. extern void d_strupr( char *s1 );
  39. extern void d_strrev( char *s1 );
  40. #ifdef DEBUG_MEMORY_ALLOCATIONS
  41. char *d_strdup(const char *str, const char *, const char *, unsigned) __attribute_malloc();
  42. #define d_strdup(str)   (d_strdup(str, #str, __FILE__,__LINE__))
  43. #else
  44. #include <cstring>
  45. #define d_strdup strdup
  46. #endif
  47.  
  48. template <std::size_t N>
  49. static inline int d_strnicmp(const char *s1, const char (&s2)[N])
  50. {
  51.         return d_strnicmp(s1, s2, N - 1);
  52. }
  53.  
  54. struct splitpath_t
  55. {
  56.         const char *drive_start, *drive_end, *path_start, *path_end, *base_start, *base_end, *ext_start;
  57. };
  58.  
  59. // remove extension from filename, doesn't work with paths.
  60. void removeext(const char *filename, std::array<char, 20> &out);
  61.  
  62. //give a filename a new extension, doesn't work with paths with no extension already there
  63. extern void change_filename_extension( char *dest, const char *src, const char *new_ext );
  64.  
  65. // split an MS-DOS path into drive, directory path, filename without the extension (base) and extension.
  66. // if it's just a filename with no directory specified, this function will get 'base' and 'ext'
  67. void d_splitpath(const char *name, struct splitpath_t *path);
  68.  
  69. class string_array_t
  70. {
  71.         typedef std::vector<const char *> ptr_t;
  72.         std::vector<char> buffer;
  73.         ptr_t ptr;
  74. public:
  75.         ptr_t &pointer() { return ptr; }
  76.         void clear()
  77.         {
  78.                 ptr.clear();
  79.                 buffer.clear();
  80.         }
  81.         void add(const char *);
  82.         void tidy(std::size_t offset, int (*comp)( const char *, const char * ));
  83. };
  84.  
  85. int string_array_sort_func(const void *v0, const void *v1);
  86.  
  87. /* Compute the number of digits required to represent MaximumValue in
  88.  * the given Base.  The result does not account for any characters other
  89.  * than the required digits.
  90.  */
  91. template <std::size_t MaximumValue, std::size_t Base = 10>
  92. constexpr std::size_t number_to_text_length = (MaximumValue < Base) ? 1 : 1 + number_to_text_length<MaximumValue / Base, Base>;
  93. static_assert(number_to_text_length<1> == 1, "");
  94. static_assert(number_to_text_length<9> == 1, "");
  95. static_assert(number_to_text_length<10> == 2, "");
  96. static_assert(number_to_text_length<255, 16> == 2, "");
  97. static_assert(number_to_text_length<256, 16> == 3, "");
  98.  
  99. }
  100.  
  101. #endif
  102.