From 675497020e6839543510706ddcd50ae5a479fac9 Mon Sep 17 00:00:00 2001 From: Denis Tikhomirov Date: Thu, 26 Nov 2015 20:00:50 +0300 Subject: [PATCH] Fix file read --- src/file_backward_reader.cpp | 19 ++++++++++++------- src/file_forward_reader.cpp | 18 ++++++++---------- 2 files changed, 20 insertions(+), 17 deletions(-) diff --git a/src/file_backward_reader.cpp b/src/file_backward_reader.cpp index 7bc7f3e..084c24a 100644 --- a/src/file_backward_reader.cpp +++ b/src/file_backward_reader.cpp @@ -87,12 +87,20 @@ bool BackwardReader::processCache() { && (cache[cache_end - 1] == '\n')) { ++copy_len; } + size_t line_len = copy_len; memcpy(lines[current_line].data(), cache.data() + start, copy_len * mbchar_size); - line_lens[current_line] = copy_len; - lines[current_line][copy_len] = '\0'; - if (longest_line_len < copy_len) { - longest_line_len = copy_len; + + if (lines[current_line][copy_len - 1] != '\n') { + lines[current_line][copy_len] = '\n'; + lines[current_line][copy_len + 1] = '\0'; + } else { + --line_len; + lines[current_line][copy_len] = '\0'; + } + line_lens[current_line] = line_len; + if (longest_line_len < line_len) { + longest_line_len = line_len; } current_out_line_id = current_line; ++current_line; @@ -192,9 +200,6 @@ std::wstring BackwardReader::getLine() const { } str.append(lines[current_out_line_id].data()); - if (lines[current_out_line_id][line_lens[current_out_line_id]] != '\n') { - str.append(L"\n"); - } ++current_out_line_id; return str; diff --git a/src/file_forward_reader.cpp b/src/file_forward_reader.cpp index d54a723..b9c1f9a 100644 --- a/src/file_forward_reader.cpp +++ b/src/file_forward_reader.cpp @@ -86,15 +86,16 @@ bool ForwardReader::readLine(FileIO &f) { } ++cur_symbol_id; - if (cur_symbol == '\n') { - lines[current_line_id][cur_symbol_id] = '\0'; - return true; - } else if (cur_symbol_id == line_max_len) { + if ((cur_symbol != '\n') && (cur_symbol_id < line_max_len)) { + continue; + } + + if (cur_symbol != '\n') { f.unread(); - --cur_symbol_id; - cur_symbol = '\0'; - return true; + cur_symbol = '\n'; } + lines[current_line_id][cur_symbol_id] = '\0'; + return true; } } @@ -147,9 +148,6 @@ std::wstring ForwardReader::getLine() const { } str.append(lines[current_out_line_id].data()); - if (lines[current_out_line_id][line_lens[current_out_line_id]] != '\n') { - str.append(L"\n"); - } ++current_out_line_id; return str;