aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBoris Kolpackov <boris@codesynthesis.com>2017-04-22 10:35:59 +0200
committerBoris Kolpackov <boris@codesynthesis.com>2017-04-22 10:35:59 +0200
commitc1d5be099ecd2a0d4e120360bae24582723f1153 (patch)
tree19a59c6ea56d7031bf6d89ae6f3a77e934a91555
parent5a36f357e174d002722122d2408c57fb43da6e59 (diff)
Handle Windows CR/LF style line-ending in char_scanner
-rw-r--r--butl/char-scanner8
-rw-r--r--butl/char-scanner.cxx11
2 files changed, 18 insertions, 1 deletions
diff --git a/butl/char-scanner b/butl/char-scanner
index e070e45..7dc56fa 100644
--- a/butl/char-scanner
+++ b/butl/char-scanner
@@ -19,7 +19,12 @@ namespace butl
class LIBBUTL_EXPORT char_scanner
{
public:
- char_scanner (std::istream& is): is_ (is) {}
+ // If the crlf argument is true, then recognize Windows newlines (0x0D
+ // 0x0A) and convert them to just '\n' (0x0A). Note that standalone 0x0D
+ // that are not followed by 0x0A are returned as is.
+ //
+ char_scanner (std::istream& is, bool crlf = true)
+ : is_ (is), crlf_ (crlf) {}
char_scanner (const char_scanner&) = delete;
char_scanner& operator= (const char_scanner&) = delete;
@@ -74,6 +79,7 @@ namespace butl
protected:
std::istream& is_;
+ bool crlf_;
bool unget_ {false};
xchar buf_ = '\0';
diff --git a/butl/char-scanner.cxx b/butl/char-scanner.cxx
index a54c20d..b923aa6 100644
--- a/butl/char-scanner.cxx
+++ b/butl/char-scanner.cxx
@@ -53,6 +53,17 @@ namespace butl
{
is_.get ();
+ if (crlf_ && c == 0x0D)
+ {
+ xchar c1 (peek ());
+
+ if (c1 == '\n')
+ {
+ is_.get ();
+ c = c1;
+ }
+ }
+
if (c == '\n')
{
line++;