diff options
author | Brad King <brad.king@kitware.com> | 2013-10-14 17:54:19 (GMT) |
---|---|---|
committer | Brad King <brad.king@kitware.com> | 2013-10-17 13:07:00 (GMT) |
commit | a8c652342f0e4dcaf933ecb0ce164b44d4997ae4 (patch) | |
tree | f95ad82b3bbadfbfde14b1642fa88b385c322373 /Source/cmListFileLexer.in.l | |
parent | dbd933365ec780d27ab7c0dfba30dc1af1094607 (diff) | |
download | CMake-a8c652342f0e4dcaf933ecb0ce164b44d4997ae4.zip CMake-a8c652342f0e4dcaf933ecb0ce164b44d4997ae4.tar.gz CMake-a8c652342f0e4dcaf933ecb0ce164b44d4997ae4.tar.bz2 |
cmListFileLexer: Convert CRLF -> LF newlines explicitly
Read input files in binary mode instead of text mode and convert CRLF
newlines to LF newlines explicitly in our own buffer. This is necessary
to read CMake source files with CRLF newlines on platforms whose C
runtime libraries do not transform newlines in text mode. For example,
a Cygwin or Linux binary may not transform CRLF -> LF in files read from
a Windows filesystem. Perform the conversion ourselves to ensure that
multi-line string literals in CMake source files have LF newlines
everywhere.
Diffstat (limited to 'Source/cmListFileLexer.in.l')
-rw-r--r-- | Source/cmListFileLexer.in.l | 36 |
1 files changed, 34 insertions, 2 deletions
diff --git a/Source/cmListFileLexer.in.l b/Source/cmListFileLexer.in.l index a660d37..e2ad0f5 100644 --- a/Source/cmListFileLexer.in.l +++ b/Source/cmListFileLexer.in.l @@ -46,6 +46,7 @@ struct cmListFileLexer_s int column; int size; FILE* file; + size_t cr; char* string_buffer; char* string_position; int string_left; @@ -264,7 +265,38 @@ static int cmListFileLexerInput(cmListFileLexer* lexer, char* buffer, { if(lexer->file) { - return (int)fread(buffer, 1, bufferSize, lexer->file); + /* Convert CRLF -> LF explicitly. The C FILE "t"ext mode + does not convert newlines on all platforms. Move any + trailing CR to the start of the buffer for the next read. */ + size_t cr = lexer->cr; + size_t n; + buffer[0] = '\r'; + n = fread(buffer+cr, 1, bufferSize-cr, lexer->file); + if(n) + { + char* o = buffer; + const char* i = buffer; + const char* e; + n += cr; + cr = (buffer[n-1] == '\r')? 1:0; + e = buffer + n - cr; + while(i != e) + { + if(i[0] == '\r' && i[1] == '\n') + { + ++i; + } + *o++ = *i++; + } + n = o - buffer; + } + else + { + n = cr; + cr = 0; + } + lexer->cr = cr; + return n; } else if(lexer->string_left) { @@ -381,7 +413,7 @@ int cmListFileLexer_SetFileName(cmListFileLexer* lexer, const char* name, cmListFileLexerDestroy(lexer); if(name) { - lexer->file = fopen(name, "r"); + lexer->file = fopen(name, "rb"); if(lexer->file) { if(bom) |