<pre lang="c++">
std::wstring FDIWTextStream:: ReadLineAndGetTrailingBlankLines(int& trailingBlankLines) { std::wstring result; wchar_t currentChar; const int bufSize=100; wchar_t buffer[bufSize]; int savedPosition = mStreamIMP->GetPositionIMP(); int blankLines = 0; wchar_t newBuff[bufSize]; for (;;) { int bytesRead = std::min(bufSize, GetLength() - GetPosition()); int i; ReadBytes(buffer, bytesRead); // scan buffer for a return or linefeed for (i=0; i<(bytesRead/sizeof(wchar_t)); i++) { wchar_t character= buffer[i]; if(character == L'\n' || character == L'\r') break; } // if no return or linefeed in this buffer then continue looking if (i == (bytesRead/sizeof(wchar_t))) { //wcsncpy(const_cast<wchar_t*>(result.c_str()),buffer, i); result += std::wstring(buffer,i); // if we are at the end then return what we got so far if (IsPositionAtEnd()) break; } else { result += std::wstring(buffer,i); break; } } // set the position to the end of the string mStreamIMP->SetPositionIMP(savedPosition + result.length()); // go past all returns and linefeeds while (!IsPositionAtEnd()) { *this >> currentChar; if (currentChar != L'\n' && currentChar != L'\r') { // went to far, back up mStreamIMP->SetPositionIMP(mStreamIMP->GetPositionIMP()-1); break; } else if(currentChar == L'\n') blankLines++; } if(blankLines > 0) trailingBlankLines = blankLines - 1; return result; }
void FDTextStreamTester:: testReadLineAndGetTrailingBlankLines() { std::wstring data = L"\nA\n\r\n\nTest.\n\nHere\nB"; FDMemObject memFile; FDOStream dataWriter(memFile); dataWriter.Reset(); dataWriter << data; std::wstring result; int blankLines = 0; FDIWTextStream reader(memFile); result = reader.ReadLineAndGetTrailingBlankLines(blankLines); assert(blankLines == 0); result = reader.ReadLineAndGetTrailingBlankLines(blankLines); assert(result == L"A"); assert(blankLines == 2); result = reader.ReadLineAndGetTrailingBlankLines(blankLines); assert(result == L"Test."); assert(blankLines == 1); result = reader.ReadLineAndGetTrailingBlankLines(blankLines); assert(result == L"Here"); assert(blankLines == 0); result = reader.ReadLineAndGetTrailingBlankLines(blankLines); assert(result == L"B"); assert(blankLines == 0); }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)