Skip to content

Commit

Permalink
Fix file read
Browse files Browse the repository at this point in the history
  • Loading branch information
dhurum committed Nov 26, 2015
1 parent e6ef770 commit 6754970
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 17 deletions.
19 changes: 12 additions & 7 deletions src/file_backward_reader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
18 changes: 8 additions & 10 deletions src/file_forward_reader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 6754970

Please sign in to comment.