|
3 | 3 | #include "rapidjson/document.h" |
4 | 4 | #include "rapidjson/writer.h" |
5 | 5 | #include "rapidjson/stringbuffer.h" |
6 | | -#include "bson.h" |
| 6 | +#include <bson.h> |
7 | 7 |
|
8 | 8 | using json = rapidjson::Document; |
9 | | -namespace rosbridge2cpp{ |
10 | | - class Helper { |
11 | | - public: |
12 | | - Helper() = default; |
13 | | - ~Helper () = default; |
14 | | - |
15 | | - std::string static get_string_from_rapidjson(json &d){ |
16 | | - rapidjson::StringBuffer buffer; |
17 | | - rapidjson::Writer<rapidjson::StringBuffer> writer(buffer); |
18 | | - d.Accept(writer); |
19 | | - return buffer.GetString(); |
20 | | - } |
21 | | - |
22 | | - std::string static get_string_from_rapidjson(const json &d){ |
23 | | - rapidjson::StringBuffer buffer; |
24 | | - rapidjson::Writer<rapidjson::StringBuffer> writer(buffer); |
25 | | - d.Accept(writer); |
26 | | - return buffer.GetString(); |
27 | | - } |
28 | | - |
29 | | - // dot_notation refers to MongoDB dot notation¬ |
30 | | - // returns "" and sets success to true if suitable data can't be found via the dot notation¬ |
31 | | - std::string static get_utf8_by_key(const char *dot_notation, bson_t &b, bool &success){ |
32 | | - bson_iter_t iter; |
33 | | - bson_iter_t val; |
34 | | - |
35 | | - if (bson_iter_init (&iter, &b) && |
36 | | - bson_iter_find_descendant (&iter, dot_notation, &val) && |
37 | | - BSON_ITER_HOLDS_UTF8 (&val)) { |
38 | | - success = true; |
39 | | - return std::string(bson_iter_utf8 (&val,NULL)); |
40 | | - } |
41 | | - |
42 | | - success = false; |
43 | | - return ""; |
44 | | - } |
45 | | - |
46 | | - // dot_notation refers to MongoDB dot notation¬ |
47 | | - // returns INT32_MAX and sets success to 'false' if suitable data can't be found via the dot notation¬ |
48 | | - int32_t static get_int32_by_key(const char *dot_notation, bson_t &b, bool &success){ |
49 | | - bson_iter_t iter; |
50 | | - bson_iter_t val; |
51 | | - |
52 | | - if (bson_iter_init (&iter, &b) && |
53 | | - bson_iter_find_descendant (&iter, dot_notation, &val) && |
54 | | - BSON_ITER_HOLDS_INT32 (&val)) { |
55 | | - success = true; |
56 | | - return bson_iter_int32 (&val); |
57 | | - } |
58 | | - success = false; |
59 | | - return INT32_MAX; |
60 | | - } |
61 | | - |
62 | | - // dot_notation refers to MongoDB dot notation¬ |
63 | | - // returns -1 and sets success to 'false' if suitable data can't be found via the dot notation¬ |
64 | | - double static get_double_by_key(const char *dot_notation, bson_t &b, bool &success){ |
65 | | - bson_iter_t iter; |
66 | | - bson_iter_t val; |
67 | | - |
68 | | - // if (bson_iter_init (&iter, &b) && |
69 | | - // bson_iter_find_descendant (&iter, dot_notation, &val) && |
70 | | - // BSON_ITER_HOLDS_DOUBLE (&val)) { |
71 | | - if (bson_iter_init (&iter, &b) && |
72 | | - bson_iter_find_descendant (&iter, dot_notation, &val)){ |
73 | | - if(!BSON_ITER_HOLDS_DOUBLE (&val)){ |
74 | | - std::cout << "Key found, but not double typed" << std::endl; |
75 | | - }else{ |
76 | | -// std::cout << "Key " << dot_notation <<" found. success is = " << success << std::endl; |
77 | | - std::cout << "Key " << dot_notation <<" found."<< std::endl; |
78 | | - success = true; |
79 | | -// std::cout << " success is now = " << success << std::endl; |
80 | | - return bson_iter_double (&val); |
81 | | - } |
82 | | - } |
83 | | - std::cout << "Couldn't find descendant" << std::endl; |
84 | | - success = false; |
85 | | - return -1.0; |
86 | | - } |
87 | | - |
88 | | - // dot_notation refers to MongoDB dot notation¬ |
89 | | - // returns false and sets success to 'false' if suitable data can't be found via the dot notation¬ |
90 | | - bool static get_bool_by_key(const char *dot_notation, bson_t &b, bool &success){ |
91 | | - bson_iter_t iter; |
92 | | - bson_iter_t val; |
93 | | - |
94 | | - if (bson_iter_init (&iter, &b) && |
95 | | - bson_iter_find_descendant (&iter, dot_notation, &val) && |
96 | | - BSON_ITER_HOLDS_BOOL (&val)) { |
97 | | - success = true; |
98 | | - return bson_iter_bool (&val); |
99 | | - } |
100 | | - success = false; |
101 | | - return false; |
102 | | - } |
103 | | - |
104 | | - // dot_notation refers to MongoDB dot notation¬ |
105 | | - // returns nullptr and sets success to 'false' if suitable data can't be found via the dot notation |
106 | | - // |
107 | | - // binary_data_length holds the size of the buffer where the returned pointer points to. |
108 | | - static const uint8_t * get_binary_by_key(const char *dot_notation, bson_t &b, uint32_t &binary_data_length, bool &success){ |
109 | | - bson_iter_t iter; |
110 | | - bson_iter_t val; |
111 | | - |
112 | | - if (bson_iter_init (&iter, &b) && |
113 | | - bson_iter_find_descendant (&iter, dot_notation, &val) && |
114 | | - BSON_ITER_HOLDS_BINARY (&val)) { |
115 | | - bson_subtype_t subtype; |
116 | | - const uint8_t *binary; |
117 | | - |
118 | | - bson_iter_binary(&val, &subtype, &binary_data_length, &binary); |
119 | | - assert( binary ); |
120 | | - success = true; |
121 | | - return binary; |
122 | | - } |
123 | | - success = false; |
124 | | - return nullptr; |
125 | | - } |
126 | | - |
127 | | - bool static bson_has_key(bson_t &b, const char *key){ |
128 | | - return bson_has_field(&b,key); |
129 | | - } |
130 | | - |
131 | | - }; |
| 9 | +namespace rosbridge2cpp { |
| 10 | + class Helper { |
| 11 | + public: |
| 12 | + Helper() = default; |
| 13 | + ~Helper() = default; |
| 14 | + |
| 15 | + std::string static get_string_from_rapidjson(json &d) |
| 16 | + { |
| 17 | + rapidjson::StringBuffer buffer; |
| 18 | + rapidjson::Writer<rapidjson::StringBuffer> writer(buffer); |
| 19 | + d.Accept(writer); |
| 20 | + return buffer.GetString(); |
| 21 | + } |
| 22 | + |
| 23 | + std::string static get_string_from_rapidjson(const json &d) |
| 24 | + { |
| 25 | + rapidjson::StringBuffer buffer; |
| 26 | + rapidjson::Writer<rapidjson::StringBuffer> writer(buffer); |
| 27 | + d.Accept(writer); |
| 28 | + return buffer.GetString(); |
| 29 | + } |
| 30 | + |
| 31 | + // dot_notation refers to MongoDB dot notation |
| 32 | + // returns "" and sets success to true if suitable data can't be found via the dot notation |
| 33 | + std::string static get_utf8_by_key(const char *dot_notation, bson_t &b, bool &success) |
| 34 | + { |
| 35 | + bson_iter_t iter; |
| 36 | + bson_iter_t val; |
| 37 | + |
| 38 | + if (bson_iter_init(&iter, &b) && |
| 39 | + bson_iter_find_descendant(&iter, dot_notation, &val) && |
| 40 | + BSON_ITER_HOLDS_UTF8(&val)) { |
| 41 | + success = true; |
| 42 | + return std::string(bson_iter_utf8(&val, NULL)); |
| 43 | + } |
| 44 | + |
| 45 | + success = false; |
| 46 | + return ""; |
| 47 | + } |
| 48 | + |
| 49 | + // dot_notation refers to MongoDB dot notation |
| 50 | + // returns INT32_MAX and sets success to 'false' if suitable data can't be found via the dot notation |
| 51 | + int32_t static get_int32_by_key(const char *dot_notation, bson_t &b, bool &success) |
| 52 | + { |
| 53 | + bson_iter_t iter; |
| 54 | + bson_iter_t val; |
| 55 | + |
| 56 | + if (bson_iter_init(&iter, &b) && |
| 57 | + bson_iter_find_descendant(&iter, dot_notation, &val) && |
| 58 | + BSON_ITER_HOLDS_INT32(&val)) { |
| 59 | + success = true; |
| 60 | + return bson_iter_int32(&val); |
| 61 | + } |
| 62 | + success = false; |
| 63 | + return INT32_MAX; |
| 64 | + } |
| 65 | + |
| 66 | + // dot_notation refers to MongoDB dot notation |
| 67 | + // returns -1 and sets success to 'false' if suitable data can't be found via the dot notation |
| 68 | + double static get_double_by_key(const char *dot_notation, bson_t &b, bool &success) |
| 69 | + { |
| 70 | + bson_iter_t iter; |
| 71 | + bson_iter_t val; |
| 72 | + |
| 73 | + // if (bson_iter_init (&iter, &b) && |
| 74 | + // bson_iter_find_descendant (&iter, dot_notation, &val) && |
| 75 | + // BSON_ITER_HOLDS_DOUBLE (&val)) { |
| 76 | + if (bson_iter_init(&iter, &b) && |
| 77 | + bson_iter_find_descendant(&iter, dot_notation, &val)) { |
| 78 | + if (!BSON_ITER_HOLDS_DOUBLE(&val)) { |
| 79 | + std::cout << "Key found, but not double typed" << std::endl; |
| 80 | + } |
| 81 | + else { |
| 82 | + //std::cout << "Key " << dot_notation << " found. success is = " << success << std::endl; |
| 83 | + //std::cout << "Key " << dot_notation << " found." << std::endl; |
| 84 | + success = true; |
| 85 | + //std::cout << " success is now = " << success << std::endl; |
| 86 | + return bson_iter_double(&val); |
| 87 | + } |
| 88 | + } |
| 89 | + std::cout << "Couldn't find descendant" << std::endl; |
| 90 | + success = false; |
| 91 | + return -1.0; |
| 92 | + } |
| 93 | + |
| 94 | + // dot_notation refers to MongoDB dot notation |
| 95 | + // returns false and sets success to 'false' if suitable data can't be found via the dot notation |
| 96 | + bool static get_bool_by_key(const char *dot_notation, bson_t &b, bool &success) |
| 97 | + { |
| 98 | + bson_iter_t iter; |
| 99 | + bson_iter_t val; |
| 100 | + |
| 101 | + if (bson_iter_init(&iter, &b) && |
| 102 | + bson_iter_find_descendant(&iter, dot_notation, &val) && |
| 103 | + BSON_ITER_HOLDS_BOOL(&val)) { |
| 104 | + success = true; |
| 105 | + return bson_iter_bool(&val); |
| 106 | + } |
| 107 | + success = false; |
| 108 | + return false; |
| 109 | + } |
| 110 | + |
| 111 | + // dot_notation refers to MongoDB dot notation |
| 112 | + // returns nullptr and sets success to 'false' if suitable data can't be found via the dot notation |
| 113 | + // |
| 114 | + // binary_data_length holds the size of the buffer where the returned pointer points to. |
| 115 | + static const uint8_t * get_binary_by_key(const char *dot_notation, bson_t &b, uint32_t &binary_data_length, bool &success) |
| 116 | + { |
| 117 | + bson_iter_t iter; |
| 118 | + bson_iter_t val; |
| 119 | + |
| 120 | + if (bson_iter_init(&iter, &b) && |
| 121 | + bson_iter_find_descendant(&iter, dot_notation, &val) && |
| 122 | + BSON_ITER_HOLDS_BINARY(&val)) { |
| 123 | + bson_subtype_t subtype; |
| 124 | + const uint8_t *binary; |
| 125 | + |
| 126 | + bson_iter_binary(&val, &subtype, &binary_data_length, &binary); |
| 127 | + assert(binary); |
| 128 | + success = true; |
| 129 | + return binary; |
| 130 | + } |
| 131 | + success = false; |
| 132 | + return nullptr; |
| 133 | + } |
| 134 | + |
| 135 | + // dot_notation refers to MongoDB dot notation |
| 136 | + // returns nullptr and sets success to 'false' if suitable data can't be found via the dot notation |
| 137 | + // |
| 138 | + // array_size holds the size in byte of the buffer where the returned pointer points to. |
| 139 | + static const uint8_t * get_array_by_key(const char *dot_notation, bson_t &b, uint32_t &array_size, bool &success) |
| 140 | + { |
| 141 | + bson_iter_t iter; |
| 142 | + bson_iter_t val; |
| 143 | + |
| 144 | + if (bson_iter_init(&iter, &b) && |
| 145 | + bson_iter_find_descendant(&iter, dot_notation, &val) && |
| 146 | + BSON_ITER_HOLDS_ARRAY(&val)) { |
| 147 | + const uint8_t *binary; |
| 148 | + |
| 149 | + bson_iter_array(&val, &array_size, &binary); |
| 150 | + assert(binary); |
| 151 | + success = true; |
| 152 | + return binary; |
| 153 | + } |
| 154 | + success = false; |
| 155 | + return nullptr; |
| 156 | + } |
| 157 | + |
| 158 | + bool static bson_has_key(bson_t &b, const char *key) |
| 159 | + { |
| 160 | + return bson_has_field(&b, key); |
| 161 | + } |
| 162 | + }; |
132 | 163 | } |
0 commit comments