THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED
On 19/10/2007 at 12:44, xxxxxxxx wrote:
You can always check the character representation: '\n'. But as noted, I wouldn't completely trust that. A Windows build will expect the two character end-of-line and if the file was created on a Mac, it will be interpreted incorrectly (as a Carriage Return CR). Simply load any Mac text file into NotePad and note the result. This is because Windows isn't finding CR+LF, only CR.
As I explained, the best way around this is to ReadChar() and check for 0x0D (CR). That will definitively denote an end-of-line (except for Unix where LF is used - but that should be rare if ever). To be certain though, you have to go one more character to see if there is a 0x0A (LF) for potential Windows files - otherwise it will be added to the next line/token read and wreak havoc.
Here's my FileReader::ReadLine() method. Note that I check for either CR or LF (this supports Windows, MacOS, Unix). If there are two characters representing the end-of-line, note that leading 'whitespace' is always skipped. That includes CR and LF as their numerical values are less than 32 (Space). So the next call to ReadLine() skips any spurious end-of-line characters remaining - including empty lines.
Note that I use a file buffer. This is a large buffer that holds as much of the file as possible - FillBuffer() uses ReadBytes() to bring the file in clumps. It is done this way since I'm dealing with text files of many megabytes (hundreds even). Note that it also simplifies the line reading process to avoid ReadChar() continuously.
// Read a line from file (up to EOL or EOF),
// skipping leading whitespace, even blank lines
//*---------------------------------------------------------------------------*
CHAR* FileReader::ReadLine()
//*---------------------------------------------------------------------------*
{
// Check for ESC key (abort load)
if (GetInputEvent(BFM_INPUT_KEYBOARD, keyinput) && (keyinput.GetLong(BFM_INPUT_CHANNEL) == KEY_ESC))
{
abort = TRUE;
return NULL;
}
// Step 1: Skip leading whitespace
fbuf = fbufptr;
do {
// Reached end of file buffer, read more
if ((fbufptr == fbufend) && !FillBuffer()) return NULL;
c = *fbufptr;
++fbufptr;
} while (c <= UNICODE_SPACE);
bytesRead += (fbufptr-fbuf);
// Step 2: Read line into lbuffer until EOL (or EOF)
fbuf = fbufptr;
lbufptr = lbuffer;
do {
// Buffer overflow - line equal to or longer than BUFFER_SIZE
if (lbufptr == lbufend) return (CHAR* )ErrorException::NullThrow(EE_DIALOG, GeLoadString(IPPERR_LINETOOLONG_TEXT), filename.GetString(), GetLineString());
// Store character into line buffer
*lbufptr = c;
++lbufptr;
// Reached end of file buffer, read more
if ((fbufptr == fbufend) && !FillBuffer()) return NULL;
c = *fbufptr;
++fbufptr;
// - PC uses CR+LF (0x000D+0x000A), Mac uses CR (0x000D)
} while ((c != UNICODE_CR) && (c != UNICODE_LF));
bytesRead += (fbufptr-fbuf);
// Set Status Bar Progression
StatusSetBar(bytesRead / statusConstant);
*lbufptr = 0;
return lbuffer;
}