BOOL CStdioFileEx::ReadString(CString& rString) { #ifdef _UNICODE return ReadWideString(rString); #else return ReadAnsiString(rString); #endif }
BOOL CStdioFileEx::ReadAnsiString(CStringA& rString) { _ASSERTE(m_pStream); rString = ""; // empty string without deallocating if(!m_bIsUnicodeText) { const int nMaxSize = 128; LPSTR lpsz = rString.GetBuffer(nMaxSize); LPSTR lpszResult; int nLen = 0; for (;;) { lpszResult = fgets(lpsz, nMaxSize+1, m_pStream); rString.ReleaseBuffer(); // handle error/eof case if (lpszResult == NULL && !feof(m_pStream)) { Afx_clearerr_s(m_pStream); AfxThrowFileException(CFileException::genericException, _doserrno, m_strFileName); } // if string is read completely or EOF if (lpszResult == NULL || (nLen = (int)lstrlenA(lpsz)) < nMaxSize || lpsz[nLen-1] == '\n') break; nLen = rString.GetLength(); lpsz = rString.GetBuffer(nMaxSize + nLen) + nLen; } //remove crlf if exist. nLen = rString.GetLength(); if (nLen > 1 && rString.Mid(nLen-2) == "\r\n") { rString.GetBufferSetLength(nLen-2); } return rString.GetLength() > 0; } else { CStringW wideString; BOOL bRetval = ReadWideString(wideString); //setlocale(LC_ALL, "chs_chn.936");//no need rString = wideString; return bRetval; } }
CStdStringW DocReader::GetDocumentText(int iCharsToCollect) { PieceTable piece_table = GetPieceTable(); piece_table.DetectDeletedPieces(GetCLXGrpprls()); InitialiseDeletionList(); CStdStringW sDoc; for (unsigned int i=0; i<piece_table.ObjectCount(); i++) { if (iCharsToCollect<=0) break; int iChunkSize = piece_table.HighBound(i)-piece_table.LowBound(i); PCD& piece = piece_table.ObjectAt(i); if (iChunkSize > iCharsToCollect) iChunkSize = iCharsToCollect; iCharsToCollect-=iChunkSize; if (piece_table.IsDeletedPiece(i)) continue; if (piece.IsUnicode()) { sDoc+=ReadWideString(piece.GetFC(), iChunkSize); } else { sDoc+=ReadASCIIString(piece.GetFC(), iChunkSize); } } _ASSERT(iCharsToCollect>=0); return sDoc; }