util.hpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. * Copyright (c) 2013 Hugh 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)
  64. {
  65. other.config = nullptr;
  66. }
  67. inline ~ConfigFile() { config_close(config); }
  68. inline bool Create(const char *file)
  69. {
  70. Close();
  71. config = config_create(file);
  72. return config != NULL;
  73. }
  74. inline void Swap(ConfigFile &other)
  75. {
  76. config_t *newConfig = other.config;
  77. other.config = config;
  78. config = newConfig;
  79. }
  80. inline int OpenString(const char *str)
  81. {
  82. Close();
  83. return config_open_string(&config, str);
  84. }
  85. inline int Open(const char *file, config_open_type openType)
  86. {
  87. Close();
  88. return config_open(&config, file, openType);
  89. }
  90. inline int Save() { return config_save(config); }
  91. inline int SaveSafe(const char *temp_ext,
  92. const char *backup_ext = nullptr)
  93. {
  94. return config_save_safe(config, temp_ext, backup_ext);
  95. }
  96. inline void Close()
  97. {
  98. config_close(config);
  99. config = NULL;
  100. }
  101. inline operator config_t *() const { return config; }
  102. };
  103. class TextLookup {
  104. lookup_t *lookup;
  105. TextLookup(TextLookup const &) = delete;
  106. TextLookup &operator=(TextLookup const &) = delete;
  107. public:
  108. inline TextLookup(lookup_t *lookup = nullptr) : lookup(lookup) {}
  109. inline TextLookup(TextLookup &&other) noexcept : lookup(other.lookup)
  110. {
  111. other.lookup = nullptr;
  112. }
  113. inline ~TextLookup() { text_lookup_destroy(lookup); }
  114. inline TextLookup &operator=(lookup_t *val)
  115. {
  116. text_lookup_destroy(lookup);
  117. lookup = val;
  118. return *this;
  119. }
  120. inline operator lookup_t *() const { return lookup; }
  121. inline const char *GetString(const char *lookupVal) const
  122. {
  123. const char *out;
  124. if (!text_lookup_getstr(lookup, lookupVal, &out))
  125. return lookupVal;
  126. return out;
  127. }
  128. };