]> granicus.if.org Git - re2c/commitdiff
Convert all line endings to LF before writing the generated code to file.
authorUlya Trofimovich <skvadrik@gmail.com>
Fri, 11 Nov 2016 13:54:33 +0000 (13:54 +0000)
committerUlya Trofimovich <skvadrik@gmail.com>
Fri, 11 Nov 2016 13:54:33 +0000 (13:54 +0000)
This commit should fix bug #163 reported by pauloscustodio:
    Reading files with "rb" causes issues in Windows

re2c reads input files in binary mode and writes the generated output in
text mode. This caused CR LF conversion to CR CR LF on Windows: first CR
comes from reading input in binary mode, second CR is added when writing
output in text mode. This only happened to those parts of input which are
not transformed by re2c: we used to copy-paste verbatim, now we patch line
endings.

re2c/src/codegen/output.cc

index 8d4f3945d61a0eb1982edf9fa01145cb8498d64f..ef57ad6615280b1155b08c451ab7b714237bf509 100644 (file)
@@ -95,7 +95,20 @@ std::ostream & OutputFile::stream ()
 
 OutputFile & OutputFile::wraw (const char * s, size_t n)
 {
-       stream ().write (s, static_cast<std::streamsize> (n));
+       std::ostream &o = stream();
+       const char *const e = s + n;
+
+       // convert CR LF to LF
+       for (const char *p = s; p < e; ++p) {
+               if (*p == '\n') continue;
+
+               std::streamsize l = p - s;
+               if (p > s && p[-1] == '\r') --l;
+               o.write(s, l);
+               s = p;
+       }
+       o.write(s, e - s);
+
        return *this;
 }