2 * Copyright 2016 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 #ifndef FOLLY_SYMBOLIZER_LINEREADER_H_
18 #define FOLLY_SYMBOLIZER_LINEREADER_H_
22 #include <boost/noncopyable.hpp>
24 #include <folly/Range.h>
26 namespace folly { namespace symbolizer {
29 * Async-signal-safe line reader.
31 class LineReader : private boost::noncopyable {
34 * Create a line reader that reads into a user-provided buffer (of size
37 LineReader(int fd, char* buf, size_t bufSize);
45 * Read the next line from the file.
47 * If the line is at most bufSize characters long, including the trailing
48 * newline, it will be returned (including the trailing newline).
50 * If the line is longer than bufSize, we return the first bufSize bytes
51 * (which won't include a trailing newline) and then continue from that
54 * The lines returned are not null-terminated.
56 * Returns kReading with a valid line, kEof if at end of file, or kError
57 * if a read error was encountered.
61 * input has "hello world\n"
62 * The first call returns "hello worl"
63 * The second call returns "d\n"
65 State readLine(StringPiece& line);
72 // buf_ <= bol_ <= eol_ <= end_ <= bufEnd_
74 // [buf_, end_): current buffer contents (read from file)
76 // [buf_, bol_): free (already processed, can be discarded)
77 // [bol_, eol_): current line, including \n if it exists, eol_ points
78 // 1 character past the \n
79 // [eol_, end_): read, unprocessed
80 // [end_, bufEnd_): free
90 #endif /* FOLLY_SYMBOLIZER_LINEREADER_H_ */