hwc_binlog_obj.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. #ifndef HWC_BINLOG_OBJ_H_
  2. #define HWC_BINLOG_OBJ_H_
  3. #include <string.h>
  4. #include "mem_check.h"
  5. #include "log/log.h"
  6. #define INT_SIZE sizeof(int)
  7. struct HwcBinlogCont
  8. {
  9. int i_sql_len; // sql statement length
  10. char* p_sql; // sql statement
  11. int i_check_flag; // check flag, 0:pass ,1:checking
  12. int i_raw_nums; // raw nums, begin at 1
  13. int i_raw_len; // raw value length
  14. char* p_raw_val; // raw value
  15. HwcBinlogCont()
  16. : i_sql_len(0)
  17. , p_sql(NULL)
  18. , i_check_flag(0)
  19. , i_raw_nums(0)
  20. , i_raw_len(0)
  21. , p_raw_val(NULL)
  22. { }
  23. ~HwcBinlogCont()
  24. { }
  25. void Clear() {
  26. FREE_IF(p_sql);
  27. FREE_IF(p_raw_val);
  28. }
  29. bool ParseFromString(
  30. const char* c_buf,
  31. int i_len)
  32. {
  33. log4cplus_info("total len:%d , len:%d" , total_length() , i_len);
  34. if (total_length() > i_len) {
  35. return false;
  36. }
  37. this->i_sql_len = parse_int(c_buf);
  38. this->p_sql = parse_str(c_buf , i_sql_len);
  39. this->i_check_flag = parse_int(c_buf);
  40. if (i_check_flag) {
  41. this->i_raw_nums = parse_int(c_buf);
  42. if (this->i_raw_nums) {
  43. this->i_raw_len = parse_int(c_buf);
  44. this->p_raw_val = parse_str(c_buf , i_raw_len);
  45. }
  46. }
  47. return true;
  48. };
  49. bool SerializeToString(char* p_buf)
  50. {
  51. log4cplus_info("line:%d " , __LINE__);
  52. if (!p_buf) { return false; }
  53. append_int_to_buf(p_buf , this->i_sql_len);
  54. append_char_to_buf(p_buf , this->p_sql , this->i_sql_len);
  55. append_int_to_buf(p_buf , this->i_check_flag);
  56. log4cplus_info("line:%d " , __LINE__);
  57. if (i_check_flag) {
  58. append_int_to_buf(p_buf , this->i_raw_nums);
  59. if (this->i_raw_nums) {
  60. append_int_to_buf(p_buf , this->i_raw_len);
  61. append_char_to_buf(p_buf , this->p_raw_val , this->i_raw_len);
  62. }
  63. }
  64. log4cplus_info("line:%d " , __LINE__);
  65. return true;
  66. };
  67. int total_length() {
  68. int i_total_len = 2*INT_SIZE + i_sql_len;
  69. if (i_check_flag) {
  70. i_total_len += (2*INT_SIZE + i_raw_len);
  71. }
  72. return i_total_len;
  73. }
  74. private:
  75. void append_int_to_buf(
  76. char*& c_buf ,
  77. const int& i_val)
  78. {
  79. memcpy((void*)(c_buf) , (void*)&i_val , INT_SIZE);
  80. c_buf += INT_SIZE;
  81. }
  82. void append_char_to_buf(
  83. char*& c_buf ,
  84. const char* src_buf,
  85. const int& i_len)
  86. {
  87. memcpy((void*)(c_buf) , (void*)src_buf , i_len);
  88. c_buf += i_len;
  89. }
  90. int parse_int(const char*& c_buf) {
  91. int i_value = *(int*)c_buf;
  92. c_buf += INT_SIZE;
  93. return i_value;
  94. }
  95. char* parse_str(const char*& c_buf , int i_len) {
  96. char* p_value = (char*)MALLOC(i_len + 1);
  97. memcpy((void*)p_value , (void*)c_buf , i_len);
  98. c_buf += i_len;
  99. p_value[i_len] = '\0';
  100. return p_value;
  101. }
  102. };
  103. #endif