WinInet InternetReadFile get lines out of buffer (\n) and insert to vector

76 views Asked by At

I'm looking for a good solution to split buffer content into lines (searching for \n) and insert line by line into a Vector. I tried it with the str.find and str.substr.

I'm afraid that once it reads again new stuff from the web (because buffer was filled out and now it reads again content from the web) it might loose the last bit of content of the last read. Somehow the incomplete line of the last read must be stick at the front of the next read.

I tried something like this, however, it doesn't actually work as well as I would need it to. I'm looking forward to anyone having a good solution to split the buffer content into lines to insert the lines into a vector.

std::vector<string> szLines;
dwKeepReading = true;
dwBytesRead = -1;
while(dwKeepReading && dwBytesRead != 0)
{
    dwKeepReading = InternetReadFile(_RequestFunction_Here_, _Buffer_Here_, 16384, &dwBytesRead);

    if(dwBytesRead)
    {
        size_t szPos = 0;
        size_t szPrev = 0;
        while((szPos = string(_Buffer_Here_).find("\n", szPrev)) != string::npos)
        {
            szLines.push_back(string(_Buffer_Here_).substr(szPrev, szPos));
            szPrev = szPos + 1;
        }
        //Save somehow remaining content of buffer (if there ist any?) -> Add if at the front of the next Read to complete the line.
        szLines.push_back(string(_Buffer_Here_).substr(szPrev)); //This line might be incomplete -> Save and set on front of next read.
    }
    else
    {
        //Somehow check if there was anything remaining from the last Read to insert into the Vector.
    }
}

At the moment I got a dirty workaround by simply writing the buffer to a cache file and afterwards reading the file line per line into the Vector. However, I wish to directly split the buffer into lines and insert into the Vector without need to write a file.

Thanks in advance!

0

There are 0 answers