Int128Field.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /* ---------------------------------------------------------------------------
  2. Nullsoft Database Engine
  3. --------------------
  4. codename: Near Death Experience
  5. --------------------------------------------------------------------------- */
  6. /* ---------------------------------------------------------------------------
  7. Int128Field Class
  8. Field data layout:
  9. [16 bytes] value
  10. --------------------------------------------------------------------------- */
  11. #include "nde.h"
  12. #include "Int128Field.h"
  13. #include <time.h>
  14. //---------------------------------------------------------------------------
  15. Int128Field::Int128Field(void *data)
  16. {
  17. InitField();
  18. Type = FIELD_INT128;
  19. memcpy(Value, data, 16);
  20. }
  21. //---------------------------------------------------------------------------
  22. void Int128Field::InitField(void)
  23. {
  24. Type = FIELD_INT128;
  25. memset(Value, 0, 16);
  26. }
  27. //---------------------------------------------------------------------------
  28. Int128Field::Int128Field()
  29. {
  30. InitField();
  31. }
  32. //---------------------------------------------------------------------------
  33. Int128Field::~Int128Field()
  34. {
  35. }
  36. //---------------------------------------------------------------------------
  37. void Int128Field::ReadTypedData(const uint8_t *data, size_t len)
  38. {
  39. if (len < 16) return;
  40. memcpy(Value, data, 16);
  41. }
  42. //---------------------------------------------------------------------------
  43. void Int128Field::WriteTypedData(uint8_t *data, size_t len)
  44. {
  45. if (len < 16) return;
  46. memcpy(data, Value, 16);
  47. }
  48. //---------------------------------------------------------------------------
  49. void *Int128Field::GetValue(void)
  50. {
  51. return Value;
  52. }
  53. //---------------------------------------------------------------------------
  54. void Int128Field::SetValue(const void *Val)
  55. {
  56. memcpy(Value, Val, 16);
  57. }
  58. //---------------------------------------------------------------------------
  59. size_t Int128Field::GetDataSize(void)
  60. {
  61. return 16;
  62. }
  63. //---------------------------------------------------------------------------
  64. int Int128Field::Compare(Field *Entry)
  65. {
  66. if (!Entry) return -1;
  67. return memcmp(Value, ((Int128Field*)Entry)->GetValue(), 16);
  68. }
  69. static char zerobuf[16]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  70. //---------------------------------------------------------------------------
  71. bool Int128Field::ApplyFilter(Field *Data, int op)
  72. {
  73. void *p = ((Int128Field *)Data)->GetValue();
  74. void *d = Value;
  75. if (!p)
  76. p = zerobuf;
  77. if (!d)
  78. d = zerobuf;
  79. bool r;
  80. switch (op)
  81. {
  82. case FILTER_EQUALS:
  83. r = !memcmp(d, p, 16);
  84. break;
  85. case FILTER_CONTAINS:
  86. r = !memcmp(d, p, 16);
  87. break;
  88. case FILTER_ABOVE:
  89. r = (memcmp(d, p, 16) > 0);
  90. break;
  91. case FILTER_BELOW:
  92. r = (memcmp(d, p, 16) < 0);
  93. break;
  94. case FILTER_BELOWOREQUAL:
  95. r = (memcmp(d, p, 16) <= 0);
  96. break;
  97. case FILTER_ABOVEOREQUAL:
  98. r = (memcmp(d, p, 16) >= 0);
  99. break;
  100. case FILTER_ISEMPTY:
  101. r = !d || (!memcmp(d, zerobuf, 16));
  102. break;
  103. case FILTER_ISNOTEMPTY:
  104. r = d && (memcmp(d, zerobuf, 16));
  105. break;
  106. default:
  107. r = true;
  108. break;
  109. }
  110. return r;
  111. }