-2

What is the best way to convert a void* to stringstream? I need to convert incoming curl data to be able to parse it.

I have done the following and it appears to work but Im sure there is a better way

void ProcessData(void* data, size_t datalength)
{
    if( data != nullptr )
    {
        std::stringstream ssdata;
        ssdata << (char*)data;
    }
}

EDIT My question is part of a larger challenge that I have. I have a curl call back that constantly is providing me MIME data where on each call back I have to parse the data and look for

  1. A boundary string e.g "MYBOUNADRY" and then

  2. Parse for a content length string e.g. "Content Length=1400"

  3. And then copy the data that starts right after the Content Length string for 1400 bytes to another data buffer

But because of the chunking nature of the data callbacks the image data could be in one callback or spread over 2 callbacks so I have to constantly parse each incoming chunk of data.

Harry Boy
  • 3,283
  • 12
  • 57
  • 102
  • 1
    This will not work if data contains 0 in the middle. So, you want to convert data to string, why? It is much better to parse it as is, without conversion. – Alex F Sep 21 '14 at 11:25
  • The answer depends on the content of your `void*` (ie. where it points to, charset etc.etc.) – deviantfan Sep 21 '14 at 11:26
  • the void* data will contain text at the beginning of and then image data as its MIME data. So I want to parse out the text at the start – Harry Boy Sep 21 '14 at 11:28
  • So, if text has 0 in the end, just cast void* to char*. Your code will work as well. – Alex F Sep 21 '14 at 11:30
  • 1
    Do you want to JUST extract the text, or do you want to actually convert the text to smaller pieces? If you are just converting the whole thing to text, then just cast the `void *` to `char *`, and be done with it. – Mats Petersson Sep 21 '14 at 11:35
  • See my edit to my original question @MatsPetersson – Harry Boy Sep 21 '14 at 11:48

1 Answers1

2

You can use stringstream::write(const char* s, size_t count):

std::stringstream ssdata;
ssdata.write((const char*) data, datalength);
…

But doesn't curl invoke the callback repeatedly for each chunk? Then you would have to add more logic.

kay
  • 23,543
  • 10
  • 89
  • 128
  • Yes it does repeatedly call back for each chunk. Yes I will have to add more logic :( – Harry Boy Sep 21 '14 at 11:31
  • Please also try this answer: http://stackoverflow.com/a/7782037/. It seems far superior, but also does not fix the chunking problem. – kay Sep 21 '14 at 11:36