ResValue.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. * ResValue.cpp
  3. *
  4. * Created on: May 26, 2018
  5. * Author: hp
  6. */
  7. #include <stdlib.h>
  8. #include <stdio.h>
  9. #include "ResValue.h"
  10. #include "ResStringPool.h"
  11. ResValue::ResValue() {
  12. }
  13. ResValue::ResValue(const ResStringPool& string_pool, Res_value value) {
  14. if (value.dataType == Res_value::TYPE_STRING)
  15. set_string(string_pool.get(value.data));
  16. else
  17. _value = value;
  18. }
  19. ResValue& ResValue::operator=(const ResValue& val) {
  20. _value = val._value;
  21. if (val._value.dataType == Res_value::TYPE_STRING)
  22. _string = strdup(val._string);
  23. return *this;
  24. }
  25. ResValue::~ResValue() {
  26. free((void*) _string);
  27. }
  28. void ResValue::set_bool(bool val) {
  29. _value.dataType = Res_value::TYPE_INT_BOOLEAN;
  30. _value.data = val;
  31. }
  32. void ResValue::set_string(const char* val) {
  33. _value.dataType = Res_value::TYPE_STRING;
  34. // We don't know the stringpool value of this string yet.
  35. _string = strdup(val);
  36. }
  37. void ResValue::set_int(int32_t val) {
  38. _value.dataType = Res_value::TYPE_INT_DEC;
  39. _value.data = val;
  40. }
  41. void ResValue::set_float(float val) {
  42. _value.dataType = Res_value::TYPE_FLOAT;
  43. *(float*)&_value.data = val;
  44. }
  45. void ResValue::set_hex(int32_t val) {
  46. _value.dataType = Res_value::TYPE_INT_HEX;
  47. _value.data = val;
  48. }
  49. void ResValue::set_reference(uint32_t val) {
  50. _value.dataType = Res_value::TYPE_REFERENCE;
  51. _value.data = val;
  52. }
  53. const char* ResValue::to_string() {
  54. char* data = nullptr;
  55. uint32_t size;
  56. switch (_value.dataType) {
  57. case Res_value::TYPE_REFERENCE:
  58. size = snprintf(nullptr, 0, "Reference: 0x%08X", _value.data);
  59. ++size;
  60. data = (char*) malloc(size);
  61. snprintf(data, size, "Reference: 0x%08X", _value.data);
  62. break;
  63. case Res_value::TYPE_INT_DEC:
  64. size = snprintf(nullptr, 0, "%i", _value.data);
  65. ++size;
  66. data = (char*) malloc(size);
  67. snprintf(data, size, "%i", _value.data);
  68. break;
  69. case Res_value::TYPE_INT_HEX:
  70. size = snprintf(nullptr, 0, "0x%02X", _value.data);
  71. ++size;
  72. data = (char*) malloc(size);
  73. snprintf(data, size, "0x%02X", _value.data);
  74. break;
  75. case Res_value::TYPE_FLOAT:
  76. size = snprintf(nullptr, 0, "%g", *(const float*)&_value.data);
  77. ++size;
  78. data = (char*) malloc(size);
  79. snprintf(data, size, "%g", *(const float*)&_value.data);
  80. break;
  81. case Res_value::TYPE_STRING:
  82. data = strdup(_string);
  83. break;
  84. case Res_value::TYPE_INT_BOOLEAN:
  85. if (_value.data) {
  86. data = (char*) malloc(6);
  87. snprintf(data, 6, "true");
  88. } else {
  89. data = (char*) malloc(7);
  90. snprintf(data, 7, "false");
  91. }
  92. break;
  93. default:
  94. data = (char*) malloc(20);
  95. snprintf(data, 20, "Unknown type %i", _value.dataType);
  96. }
  97. return data;
  98. }
  99. Res_value ResValue::serialize(ResStringPool& stringpool) {
  100. if (_value.dataType == Res_value::TYPE_STRING)
  101. _value.data = stringpool.put(_string).index;
  102. return _value;
  103. }