util.hpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. * Copyright (c) 2023 Lain Bailey <[email protected]>
  3. *
  4. * Permission to use, copy, modify, and distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. /* Useful C++ classes/bindings for util data and pointers */
  17. #pragma once
  18. #include <string.h>
  19. #include <stdarg.h>
  20. #include <utility>
  21. #include "bmem.h"
  22. #include "config-file.h"
  23. #include "text-lookup.h"
  24. /* RAII wrappers */
  25. template<typename T> class BPtr {
  26. T *ptr;
  27. BPtr(BPtr const &) = delete;
  28. BPtr &operator=(BPtr const &) = delete;
  29. public:
  30. inline BPtr(T *p = nullptr) : ptr(p) {}
  31. inline BPtr(BPtr &&other) { *this = std::move(other); }
  32. inline ~BPtr() { bfree(ptr); }
  33. inline T *operator=(T *p)
  34. {
  35. bfree(ptr);
  36. ptr = p;
  37. return p;
  38. }
  39. inline BPtr &operator=(BPtr &&other)
  40. {
  41. ptr = other.ptr;
  42. other.ptr = nullptr;
  43. return *this;
  44. }
  45. inline operator T *() { return ptr; }
  46. inline T **operator&()
  47. {
  48. bfree(ptr);
  49. ptr = nullptr;
  50. return &ptr;
  51. }
  52. inline bool operator!() { return ptr == NULL; }
  53. inline bool operator==(T p) { return ptr == p; }
  54. inline bool operator!=(T p) { return ptr != p; }
  55. inline T *Get() const { return ptr; }
  56. };
  57. class ConfigFile {
  58. config_t *config;
  59. ConfigFile(ConfigFile const &) = delete;
  60. ConfigFile &operator=(ConfigFile const &) = delete;
  61. public:
  62. inline ConfigFile() : config(NULL) {}
  63. inline ConfigFile(ConfigFile &&other) noexcept : config(other.config) { other.config = nullptr; }
  64. inline ~ConfigFile() { config_close(config); }
  65. inline bool Create(const char *file)
  66. {
  67. Close();
  68. config = config_create(file);
  69. return config != NULL;
  70. }
  71. inline void Swap(ConfigFile &other)
  72. {
  73. config_t *newConfig = other.config;
  74. other.config = config;
  75. config = newConfig;
  76. }
  77. inline int OpenString(const char *str)
  78. {
  79. Close();
  80. return config_open_string(&config, str);
  81. }
  82. inline int Open(const char *file, config_open_type openType)
  83. {
  84. Close();
  85. return config_open(&config, file, openType);
  86. }
  87. inline int Save() { return config_save(config); }
  88. inline int SaveSafe(const char *temp_ext, const char *backup_ext = nullptr)
  89. {
  90. return config_save_safe(config, temp_ext, backup_ext);
  91. }
  92. inline void Close()
  93. {
  94. config_close(config);
  95. config = NULL;
  96. }
  97. inline operator config_t *() const { return config; }
  98. };
  99. class TextLookup {
  100. lookup_t *lookup;
  101. TextLookup(TextLookup const &) = delete;
  102. TextLookup &operator=(TextLookup const &) = delete;
  103. public:
  104. inline TextLookup(lookup_t *lookup = nullptr) : lookup(lookup) {}
  105. inline TextLookup(TextLookup &&other) noexcept : lookup(other.lookup) { other.lookup = nullptr; }
  106. inline ~TextLookup() { text_lookup_destroy(lookup); }
  107. inline TextLookup &operator=(lookup_t *val)
  108. {
  109. text_lookup_destroy(lookup);
  110. lookup = val;
  111. return *this;
  112. }
  113. inline operator lookup_t *() const { return lookup; }
  114. inline const char *GetString(const char *lookupVal) const
  115. {
  116. const char *out;
  117. if (!text_lookup_getstr(lookup, lookupVal, &out))
  118. return lookupVal;
  119. return out;
  120. }
  121. };