From: Mikko Rasa Date: Sat, 16 Sep 2023 09:16:39 +0000 (+0300) Subject: Recognize CRLF as line terminator in getline X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=355104d002bbe132cc213f274bfc3e3e2ec2eb35;p=libs%2Fcore.git Recognize CRLF as line terminator in getline --- diff --git a/source/io/base.cpp b/source/io/base.cpp index b8c13d2..bf23ea2 100644 --- a/source/io/base.cpp +++ b/source/io/base.cpp @@ -31,6 +31,13 @@ bool Base::getline(string &line) while(!eof()) { int c = get(); + if(c=='\r') + { + c = get(); + if(c=='\n') + break; + line += '\r'; + } if(c==-1 || c=='\n') break; line += c; diff --git a/source/io/buffered.cpp b/source/io/buffered.cpp index 9f001fb..7ba3c33 100644 --- a/source/io/buffered.cpp +++ b/source/io/buffered.cpp @@ -156,7 +156,10 @@ bool Buffered::getline(string &line) for(char *i=begin; i!=end; ++i) if(*i=='\n') { - line.assign(begin, i-begin); + size_t length = i-begin; + if(i>begin && *(i-1)=='\r') + --length; + line.assign(begin, length); begin = i+1; return true; } diff --git a/source/io/memory.cpp b/source/io/memory.cpp index 0539fdb..2dc94e0 100644 --- a/source/io/memory.cpp +++ b/source/io/memory.cpp @@ -87,7 +87,10 @@ bool Memory::getline(string &line) } char *nl = find(pos, end, '\n'); - line.assign(pos, nl); + size_t len = nl-pos; + if(nl!=end && nl>pos && *(nl-1)=='\r') + --len; + line.assign(pos, len); pos = (nl==end ? end : nl+1); return true; }