proxygen
folly::symbolizer::LineReader Class Reference

#include <LineReader.h>

Inheritance diagram for folly::symbolizer::LineReader:

Public Types

enum  State { kReading, kEof, kError }
 

Public Member Functions

 LineReader (int fd, char *buf, size_t bufSize)
 
State readLine (StringPiece &line)
 

Private Attributes

int const fd_
 
char *const buf_
 
char *const bufEnd_
 
char * bol_
 
char * eol_
 
char * end_
 
State state_
 

Detailed Description

Async-signal-safe line reader.

Definition at line 31 of file LineReader.h.

Member Enumeration Documentation

Constructor & Destructor Documentation

folly::symbolizer::LineReader::LineReader ( int  fd,
char *  buf,
size_t  bufSize 
)

Create a line reader that reads into a user-provided buffer (of size bufSize).

Definition at line 26 of file LineReader.cpp.

Member Function Documentation

LineReader::State folly::symbolizer::LineReader::readLine ( StringPiece line)

Read the next line from the file.

If the line is at most bufSize characters long, including the trailing newline, it will be returned (including the trailing newline).

If the line is longer than bufSize, we return the first bufSize bytes (which won't include a trailing newline) and then continue from that point onwards.

The lines returned are not null-terminated.

Returns kReading with a valid line, kEof if at end of file, or kError if a read error was encountered.

Example: bufSize = 10 input has "hello world\n" The first call returns "hello worl" The second call returns "d\n"

Definition at line 35 of file LineReader.cpp.

References folly::Range< Iter >::assign(), bol_, buf_, bufEnd_, end_, eol_, fd_, kEof, kError, kReading, folly::readFull(), and state_.

Referenced by folly::symbolizer::test::expect().

35  {
36  bol_ = eol_; // Start past what we already returned
37  for (;;) {
38  // Search for newline
39  char* newline = static_cast<char*>(memchr(eol_, '\n', end_ - eol_));
40  if (newline) {
41  eol_ = newline + 1;
42  break;
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.
46  eol_ = end_;
47  break;
48  }
49 
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_);
54  bol_ = buf_;
55  eol_ = end_;
56 
57  // Refill
58  ssize_t available = bufEnd_ - end_;
59  ssize_t n = readFull(fd_, end_, available);
60  if (n < 0) {
61  state_ = kError;
62  n = 0;
63  } else if (n < available) {
64  state_ = kEof;
65  }
66  end_ += n;
67  }
68 
69  line.assign(bol_, eol_);
70  return eol_ != bol_ ? kReading : state_;
71 }
ssize_t readFull(int fd, void *buf, size_t count)
Definition: FileUtil.cpp:126

Member Data Documentation

char* folly::symbolizer::LineReader::bol_
private

Definition at line 82 of file LineReader.h.

Referenced by readLine().

char* const folly::symbolizer::LineReader::buf_
private

Definition at line 69 of file LineReader.h.

Referenced by readLine().

char* const folly::symbolizer::LineReader::bufEnd_
private

Definition at line 70 of file LineReader.h.

Referenced by readLine().

char* folly::symbolizer::LineReader::end_
private

Definition at line 84 of file LineReader.h.

Referenced by readLine().

char* folly::symbolizer::LineReader::eol_
private

Definition at line 83 of file LineReader.h.

Referenced by readLine().

int const folly::symbolizer::LineReader::fd_
private

Definition at line 68 of file LineReader.h.

Referenced by readLine().

State folly::symbolizer::LineReader::state_
private

Definition at line 85 of file LineReader.h.

Referenced by readLine().


The documentation for this class was generated from the following files: