Apr 12, 2024 04:05 PM
(This post was last modified: Apr 12, 2024 04:36 PM by nucleo_type09.)
c++ Payload Blaster, take a raw Buffer, output a JSON like block...
you can find the code here: https://github.com/nucleoType98/Payload_Blaster.git
----------------------------------- Some Demo Here -----------------------------------
Without all those crappy comments...
you can find the code here: https://github.com/nucleoType98/Payload_Blaster.git
----------------------------------- Some Demo Here -----------------------------------
Code
#include "../include/blaster.cpp"
using namespace Payload; // Namespace Payload, or use directly Payload::Blaster
int main() {
// declare a key for the block, this will be the key that represents a block
Payload::tDataBlockKey key = 42323;
/**
* declare a value which in real world will be the actual raw buffer data as character array(string) format.
**/
Payload::tDataBlockValue value = "{key_1: \"some value\", key_2: \"some value 2\", key_3: \"some value 3\"}";
// creating unit, intersect key with values
tKeyValueUnit u{key, value};
/**
* create a Payload instance with an initial payload block, the initial block is optional, but if you do not set it here,
* you must execute payload_allocate(...) to allocate one before last operation (Blast())
**/
Payload::Blaster B1(u);
/**
* Required Step: register operation scheme.
* Note that im setting one of the keys(key_1) to a wrong value(XXXkey_1) on purpose, to show how the operation fails if some keys go missing.
**/
B1.register_scheme("logs", {"XXXkey_1", "key_2", "key_3"}); // <-- Intersects with "u"
/**
* default Blast mode is set to "STRICT", meaning if even only one key is missing from the target, the operation brutally fails.
* If you want to allow partial results, then set the argument value to SCAN_MODE::LOOSE, which allows for loosed values, but returns misplaced record position.
**/
B1.Blast();
/**
* Collect Final block, collector will return a structure containing;
* - Data -> an unordered_map type containing field/value pairs of final result
* - Size -> the number of entries returned (key-value) pair block size
* - State -> the state of the operation, true if success, false if failure
*/
Payload::CollectType collector = B1.collect();
std::cout << "----------- SCHEME LOGS ------------" << std::endl;
/**
* Display the result.
* NOTE: this operation performs a direct failure, as we provided a wrong key within the register_scheme() call
* and did NOT set Blast() call SCAN_MODE argument to SCAN_MODE::LOOSE in order to allow loosed result, if want to
* return partial result even if key "x" is missing, use Blast(SCAN_MODE::LOOSE).
* The Operation Will Choke!
*/
if (collector.State)
for (auto &frame : collector.Data)
std::cout << frame.first << " = " << frame.second << std::endl;
/**
* Reallocate "logs" scheme, this time using the right field ids.
* NOTE: since "logs" is already allocated, the operation requires flag(SCHEME_ALLOC_MODE) set to OVERWRITE_IF_FOUND to overwrite the
* current "logs" scheme, otherwise will not have any effect.
*/
B1.register_scheme("logs", {"key_1", "key_2", "key_3"}, SCHEME_ALLOC_MODE::OVERWRITE_IF_FOUND);
B1.Blast();
collector = B1.collect();
/**
* Display the result.
* This Operation will return Success as i provided the right key/value pair.
*/
if (collector.State)
for (auto &frame : collector.Data)
std::cout << frame.first << " = " << frame.second << std::endl;
// lock the data
B1.set_lock();
// remove the lock, extract data
B1.drop_lock();
return 0;
}Without all those crappy comments...
Code
#include "../include/blaster.cpp"
using namespace Payload; // Namespace Payload, or use directly Payload::Blaster
int main() {
Payload::tDataBlockKey key = 42323;
Payload::tDataBlockValue value = "{key_1: \"some value\", key_2: \"some value 2\", key_3: \"some value 3\"}";
tKeyValueUnit u{key, value};
Payload::Blaster B1;
B1.register_scheme("logs", {"XXXkey_1", "key_2", "key_3"});
B1.payload_allocate(u);
B1.Blast();
Payload::CollectType collector = B1.collect();
B1.set_lock();
B1.drop_lock();
return 0;
}
