Click here to Skip to main content
15,891,828 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
<pre lang="c++">
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;
}


C++
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);
}


In buffer when i read bytes i read a junk character at the start of the buffer along with the string like as shown ....*\nA\n\r\n\nTest.\n\nHere\nB
but when i read a string instead of wide string buffer has \nA\n\r\n\nTest.\n\nHere\nB with no junk character at the start .
Please let me know why am i getting that added at the start of the string.
Posted
Updated 9-Mar-15 18:08pm
v2
Comments
Member 11500796 10-Mar-15 1:28am    
FDOStream&
FDOStream::
operator<<(const wchar_t* data)
{
std::wstring newData(data);
*this << newData;

return *this;
}
FDIStream&
FDIStream::
operator>>(wchar_t& data)
{
if (CanReadData())
{
ReadBytes(&data, (sizeof(data)));
}

return *this;
}
[no name] 10-Mar-15 1:29am    
Your debugger will tell you everything.

1 solution

Since position was being read for character position and if we got odd number position then byte count was getting messed .
Bytecount should always be even and now position and length is manipulated to match even numbers as follows:-



// set the position to the end of the string
*mStreamIMP->SetPositionIMP(savedPosition + result.length()*sizeof(wchar_t));*

// 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() - sizeof(wchar_t));*
break;
}
else if(currentChar == L'\n')
blankLines++;
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900