Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(292)

Side by Side Diff: third_party/crashpad/crashpad/util/file/delimited_file_reader.cc

Issue 2773813002: Update Crashpad to 8e37886d418dd042c3c7bfadac99214739ee4d98 (Closed)
Patch Set: Update Crashpad to 8e37886d418dd042c3c7bfadac99214739ee4d98 Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2017 The Crashpad Authors. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "util/file/delimited_file_reader.h"
16
17 #include <sys/types.h>
18
19 #include <algorithm>
20 #include <limits>
21
22 #include "base/logging.h"
23 #include "base/numerics/safe_conversions.h"
24
25 namespace crashpad {
26
27 DelimitedFileReader::DelimitedFileReader(FileReaderInterface* file_reader)
28 : file_reader_(file_reader), buf_pos_(0), buf_len_(0), eof_(false) {
29 static_assert(sizeof(buf_) <= std::numeric_limits<decltype(buf_pos_)>::max(),
30 "buf_pos_ must cover buf_");
31 static_assert(sizeof(buf_) <= std::numeric_limits<decltype(buf_len_)>::max(),
32 "buf_len_ must cover buf_");
33 }
34
35 DelimitedFileReader::~DelimitedFileReader() {}
36
37 DelimitedFileReader::Result DelimitedFileReader::GetDelim(char delimiter,
38 std::string* field) {
39 if (eof_) {
40 DCHECK_EQ(buf_pos_, buf_len_);
41
42 // Allow subsequent calls to attempt to read more data from the file. If the
43 // file is still at EOF in the future, the read will return 0 and cause
44 // kEndOfFile to be returned anyway.
45 eof_ = false;
46
47 return Result::kEndOfFile;
48 }
49
50 std::string local_field;
51 while (true) {
52 if (buf_pos_ == buf_len_) {
53 // buf_ is empty. Refill it.
54 FileOperationResult read_result = file_reader_->Read(buf_, sizeof(buf_));
55 if (read_result < 0) {
56 return Result::kError;
57 } else if (read_result == 0) {
58 if (!local_field.empty()) {
59 // The file ended with a field that wasn’t terminated by a delimiter
60 // character.
61 //
62 // This is EOF, but EOF can’t be returned because there’s a field that
63 // needs to be returned to the caller. Cache the detected EOF so it
64 // can be returned next time. This is done to support proper semantics
65 // for weird “files” like terminal input that can reach EOF and then
66 // “grow”, allowing subsequent reads past EOF to block while waiting
67 // for more data. Once EOF is detected by a read that returns 0, that
68 // EOF signal should propagate to the caller before attempting a new
69 // read. Here, it will be returned on the next call to this method
70 // without attempting to read more data.
71 eof_ = true;
72 field->swap(local_field);
73 return Result::kSuccess;
74 }
75 return Result::kEndOfFile;
76 }
77
78 DCHECK_LE(static_cast<size_t>(read_result), arraysize(buf_));
79 DCHECK(
80 base::IsValueInRangeForNumericType<decltype(buf_len_)>(read_result));
81 buf_len_ = static_cast<decltype(buf_len_)>(read_result);
82 buf_pos_ = 0;
83 }
84
85 const char* const start = buf_ + buf_pos_;
86 const char* const end = buf_ + buf_len_;
87 const char* const found = std::find(start, end, delimiter);
88
89 local_field.append(start, found);
90 buf_pos_ = static_cast<decltype(buf_pos_)>(found - buf_);
91 DCHECK_LE(buf_pos_, buf_len_);
92
93 if (found != end) {
94 // A real delimiter character was found. Append it to the field being
95 // built and return it.
96 local_field.push_back(delimiter);
97 ++buf_pos_;
98 DCHECK_LE(buf_pos_, buf_len_);
99 field->swap(local_field);
100 return Result::kSuccess;
101 }
102 }
103 }
104
105 DelimitedFileReader::Result DelimitedFileReader::GetLine(std::string* line) {
106 return GetDelim('\n', line);
107 }
108
109 } // namespace crashpad
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698