| Colin Cross | bcb4ed3 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright (C) 2015 The Android Open Source Project | 
|  | 3 | * | 
|  | 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 | 
|  | 7 | * | 
|  | 8 | *      http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 9 | * | 
|  | 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. | 
|  | 15 | */ | 
|  | 16 |  | 
|  | 17 | // Copied from system/extras/memory_replay/LineBuffer.cpp | 
|  | 18 | // TODO(ccross): find a way to share between libmemunreachable and memory_replay? | 
|  | 19 |  | 
|  | 20 | #include <errno.h> | 
|  | 21 | #include <string.h> | 
|  | 22 | #include <unistd.h> | 
|  | 23 |  | 
|  | 24 | #include "LineBuffer.h" | 
|  | 25 |  | 
| Colin Cross | a9939e9 | 2017-06-21 13:13:00 -0700 | [diff] [blame] | 26 | namespace android { | 
|  | 27 |  | 
| Colin Cross | a83881e | 2017-06-22 10:50:05 -0700 | [diff] [blame] | 28 | LineBuffer::LineBuffer(int fd, char* buffer, size_t buffer_len) | 
|  | 29 | : fd_(fd), buffer_(buffer), buffer_len_(buffer_len) {} | 
| Colin Cross | bcb4ed3 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 30 |  | 
|  | 31 | bool LineBuffer::GetLine(char** line, size_t* line_len) { | 
|  | 32 | while (true) { | 
|  | 33 | if (bytes_ > 0) { | 
|  | 34 | char* newline = reinterpret_cast<char*>(memchr(buffer_ + start_, '\n', bytes_)); | 
|  | 35 | if (newline != nullptr) { | 
|  | 36 | *newline = '\0'; | 
|  | 37 | *line = buffer_ + start_; | 
|  | 38 | start_ = newline - buffer_ + 1; | 
|  | 39 | bytes_ -= newline - *line + 1; | 
|  | 40 | *line_len = newline - *line; | 
|  | 41 | return true; | 
|  | 42 | } | 
|  | 43 | } | 
|  | 44 | if (start_ > 0) { | 
|  | 45 | // Didn't find anything, copy the current to the front of the buffer. | 
|  | 46 | memmove(buffer_, buffer_ + start_, bytes_); | 
|  | 47 | start_ = 0; | 
|  | 48 | } | 
|  | 49 | ssize_t bytes = TEMP_FAILURE_RETRY(read(fd_, buffer_ + bytes_, buffer_len_ - bytes_ - 1)); | 
|  | 50 | if (bytes <= 0) { | 
|  | 51 | if (bytes_ > 0) { | 
|  | 52 | // The read data might not contain a nul terminator, so add one. | 
|  | 53 | buffer_[bytes_] = '\0'; | 
|  | 54 | *line = buffer_ + start_; | 
|  | 55 | *line_len = bytes_; | 
|  | 56 | bytes_ = 0; | 
|  | 57 | start_ = 0; | 
|  | 58 | return true; | 
|  | 59 | } | 
|  | 60 | return false; | 
|  | 61 | } | 
|  | 62 | bytes_ += bytes; | 
|  | 63 | } | 
|  | 64 | } | 
| Colin Cross | a9939e9 | 2017-06-21 13:13:00 -0700 | [diff] [blame] | 65 |  | 
|  | 66 | }  // namespace android |