Pergunta

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?

Foi útil?

Solução

Something like this ought to do:

std::copy(buffer + index_of_start_of_sql, buffer + index_of_end_of_sql, std::ostream_iterator<char>(std::cout, ""));

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_each and a custom functor to check if character is ' or " and escaping as necessary...

Outras dicas

I think this would be a perfect example of when to use a regular expression.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top