For exaplme, I have some buffer : const char* buf with next content (mysql packet):
72 00 00 00 select * from `db` where (`name` = "Bill's car")
and i need to write to ostream only query with quoting. So, result should be next:
select * from `db` where (`name` = \"Bill\'s car\")
I know, that << quote << will make quoting and ostream.write(buf,len) will write part I need.
But what the best solution for both?
Something like this ought to do:
This copies the contents of the buffer character by character to the output stream (in this case
std::cout). You don’t need to worry about handling quotes then.The only things you need make sure are correct are the two indexes (start and end of chunk of sql).
NOTE: this will print out what is in the buffer, but will not escape the quotes. If you need to escape the quotes, then you’ll need to take a different approach. e.g. use
for_eachand a custom functor to check if character is'or"and escaping as necessary…