2 * Copyright 2013 Facebook, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include "folly/experimental/symbolizer/LineReader.h"
21 #include "folly/FileUtil.h"
23 namespace folly { namespace symbolizer {
25 LineReader::LineReader(int fd, char* buf, size_t bufSize)
28 bufEnd_(buf_ + bufSize),
35 LineReader::State LineReader::readLine(StringPiece& line) {
36 bol_ = eol_; // Start past what we already returned
39 char* newline = static_cast<char*>(memchr(eol_, '\n', end_ - eol_));
43 } else if (state_ != kReading || (bol_ == buf_ && end_ == bufEnd_)) {
44 // If the buffer is full with one line (line too long), or we're
45 // at the end of the file, return what we have.
50 // We don't have a full line in the buffer, but we have room to read.
51 // Move to the beginning of the buffer.
52 memmove(buf_, eol_, end_ - eol_);
53 end_ -= (eol_ - buf_);
58 ssize_t available = bufEnd_ - end_;
59 ssize_t n = readFull(fd_, end_, available);
63 } else if (n < available) {
69 line.assign(bol_, eol_);
70 return eol_ != bol_ ? kReading : state_;