util.hpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 "bmem.h"
  21. #include "config-file.h"
  22. #include "text-lookup.h"
  23. /* RAII wrappers */
  24. template<typename T> class BPtr {
  25. T *ptr;
  26. BPtr(BPtr const&) = delete;
  27. BPtr &operator=(BPtr const&) = delete;
  28. public:
  29. inline BPtr(T *p=nullptr) : ptr(p) {}
  30. inline BPtr(BPtr &&other) : ptr(other.ptr) {other.ptr = nullptr;}
  31. inline ~BPtr() {bfree(ptr);}
  32. inline T *operator=(T *p) {bfree(ptr); ptr = p; return p;}
  33. inline operator T*() {return ptr;}
  34. inline T **operator&() {bfree(ptr); ptr = nullptr; return &ptr;}
  35. inline bool operator!() {return ptr == NULL;}
  36. inline bool operator==(T p) {return ptr == p;}
  37. inline bool operator!=(T p) {return ptr != p;}
  38. inline T *Get() const {return ptr;}
  39. };
  40. class ConfigFile {
  41. config_t *config;
  42. ConfigFile(ConfigFile const&) = delete;
  43. ConfigFile &operator=(ConfigFile const&) = delete;
  44. public:
  45. inline ConfigFile() : config(NULL) {}
  46. inline ConfigFile(ConfigFile &&other) : config(other.config)
  47. {
  48. other.config = nullptr;
  49. }
  50. inline ~ConfigFile()
  51. {
  52. config_close(config);
  53. }
  54. inline bool Create(const char *file)
  55. {
  56. Close();
  57. config = config_create(file);
  58. return config != NULL;
  59. }
  60. inline void Swap(ConfigFile &other)
  61. {
  62. config_t *newConfig = other.config;
  63. other.config = config;
  64. config = newConfig;
  65. }
  66. inline int Open(const char *file, config_open_type openType)
  67. {
  68. Close();
  69. return config_open(&config, file, openType);
  70. }
  71. inline int Save()
  72. {
  73. return config_save(config);
  74. }
  75. inline int SaveSafe(const char *temp_ext,
  76. const char *backup_ext = nullptr)
  77. {
  78. return config_save_safe(config, temp_ext, backup_ext);
  79. }
  80. inline void Close()
  81. {
  82. config_close(config);
  83. config = NULL;
  84. }
  85. inline operator config_t*() const {return config;}
  86. };
  87. class TextLookup {
  88. lookup_t *lookup;
  89. TextLookup(TextLookup const&) = delete;
  90. TextLookup &operator=(TextLookup const&) = delete;
  91. public:
  92. inline TextLookup(lookup_t *lookup=nullptr) : lookup(lookup) {}
  93. inline TextLookup(TextLookup &&other) : lookup(other.lookup)
  94. {
  95. other.lookup = nullptr;
  96. }
  97. inline ~TextLookup() {text_lookup_destroy(lookup);}
  98. inline TextLookup& operator=(lookup_t *val)
  99. {
  100. text_lookup_destroy(lookup);
  101. lookup = val;
  102. return *this;
  103. }
  104. inline operator lookup_t*() const {return lookup;}
  105. inline const char *GetString(const char *lookupVal) const
  106. {
  107. const char *out;
  108. if (!text_lookup_getstr(lookup, lookupVal, &out))
  109. return lookupVal;
  110. return out;
  111. }
  112. };