BUG: GetLineFromStream should remove carriage return characters to make sure newlines do not get duplicates.

This commit is contained in:
Brad King 2006-08-26 16:14:26 -04:00
parent e61eac3f05
commit dc0c5d082b
1 changed files with 31 additions and 21 deletions
Source/kwsys

View File

@ -3416,39 +3416,49 @@ kwsys_stl::string SystemTools::MakeCindentifier(const char* s)
// Due to a buggy stream library on the HP and another on Mac OSX, we // Due to a buggy stream library on the HP and another on Mac OSX, we
// need this very carefully written version of getline. Returns true // need this very carefully written version of getline. Returns true
// if any data were read before the end-of-file was reached. // if any data were read before the end-of-file was reached.
bool SystemTools::GetLineFromStream(kwsys_ios::istream& is, kwsys_stl::string& line, bool SystemTools::GetLineFromStream(kwsys_ios::istream& is,
bool *has_newline /* = 0 */) kwsys_stl::string& line,
bool* has_newline /* = 0 */)
{ {
const int bufferSize = 1024; const int bufferSize = 1024;
char buffer[bufferSize]; char buffer[bufferSize];
line = "";
bool haveData = false; bool haveData = false;
if ( has_newline ) bool haveNewline = false;
{
*has_newline = false; // Start with an empty line.
} line = "";
// If no characters are read from the stream, the end of file has // If no characters are read from the stream, the end of file has
// been reached. // been reached. Clear the fail bit just before reading.
while((is.getline(buffer, bufferSize), is.gcount() > 0)) while(!haveNewline &&
(is.clear(is.rdstate() & ~kwsys_ios::ios::failbit),
is.getline(buffer, bufferSize), is.gcount() > 0))
{ {
// We have read at least one byte.
haveData = true; haveData = true;
line.append(buffer);
// If newline character was read, the gcount includes the // If newline character was read the gcount includes the character
// character, but the buffer does not. The end of line has been // but the buffer does not: the end of line has been reached.
// reached. size_t length = strlen(buffer);
if(strlen(buffer) < static_cast<size_t>(is.gcount())) if(length < static_cast<size_t>(is.gcount()))
{ {
if ( has_newline ) haveNewline = true;
{
*has_newline = true;
}
break;
} }
// The fail bit may be set. Clear it. // Avoid storing a carriage return character.
is.clear(is.rdstate() & ~kwsys_ios::ios::failbit); if(length > 0 && buffer[length-1] == '\r')
{
buffer[length-1] = 0;
}
// Append the data read to the line.
line.append(buffer);
}
// Return the results.
if(has_newline)
{
*has_newline = haveNewline;
} }
return haveData; return haveData;
} }