| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2009, Google Inc. | |
| 2 // All rights reserved. | |
| 3 // | |
| 4 // Redistribution and use in source and binary forms, with or without | |
| 5 // modification, are permitted provided that the following conditions are | |
| 6 // met: | |
| 7 // | |
| 8 // * Redistributions of source code must retain the above copyright | |
| 9 // notice, this list of conditions and the following disclaimer. | |
| 10 // * Redistributions in binary form must reproduce the above | |
| 11 // copyright notice, this list of conditions and the following disclaimer | |
| 12 // in the documentation and/or other materials provided with the | |
| 13 // distribution. | |
| 14 // * Neither the name of Google Inc. nor the names of its | |
| 15 // contributors may be used to endorse or promote products derived from | |
| 16 // this software without specific prior written permission. | |
| 17 // | |
| 18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
| 19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
| 20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
| 21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
| 22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
| 23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
| 24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
| 25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
| 26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
| 27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
| 28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
| 29 | |
| 30 #include <stdint.h> | |
| 31 #include <assert.h> | |
| 32 #include <string.h> | |
| 33 | |
| 34 #include "breakpad/linux/linux_syscall_support.h" | |
| 35 | |
| 36 namespace google_breakpad { | |
| 37 | |
| 38 // A class for reading a file, line by line, without using fopen/fgets or other | |
| 39 // functions which may allocate memory. | |
| 40 class LineReader { | |
| 41 public: | |
| 42 LineReader(int fd) | |
| 43 : fd_(fd), | |
| 44 hit_eof_(false), | |
| 45 buf_used_(0) { | |
| 46 } | |
| 47 | |
| 48 // The maximum length of a line. | |
| 49 static const size_t kMaxLineLen = 512; | |
| 50 | |
| 51 // Return the next line from the file. | |
| 52 // line: (output) a pointer to the start of the line. The line is NUL | |
| 53 // terminated. | |
| 54 // len: (output) the length of the line (not inc the NUL byte) | |
| 55 // | |
| 56 // Returns true iff successful (false on EOF). | |
| 57 // | |
| 58 // One must call |PopLine| after this function, otherwise you'll continue to | |
| 59 // get the same line over and over. | |
| 60 bool GetNextLine(const char **line, unsigned *len) { | |
| 61 for (;;) { | |
| 62 if (buf_used_ == 0 && hit_eof_) | |
| 63 return false; | |
| 64 | |
| 65 for (unsigned i = 0; i < buf_used_; ++i) { | |
| 66 if (buf_[i] == '\n' || buf_[i] == 0) { | |
| 67 buf_[i] = 0; | |
| 68 *len = i; | |
| 69 *line = buf_; | |
| 70 return true; | |
| 71 } | |
| 72 } | |
| 73 | |
| 74 if (buf_used_ == sizeof(buf_)) { | |
| 75 // we scanned the whole buffer and didn't find an end-of-line marker. | |
| 76 // This line is too long to process. | |
| 77 return false; | |
| 78 } | |
| 79 | |
| 80 // We didn't find any end-of-line terminators in the buffer. However, if | |
| 81 // this is the last line in the file it might not have one: | |
| 82 if (hit_eof_) { | |
| 83 assert(buf_used_); | |
| 84 // There's room for the NUL because of the buf_used_ == sizeof(buf_) | |
| 85 // check above. | |
| 86 buf_[buf_used_] = 0; | |
| 87 *len = buf_used_; | |
| 88 buf_used_ += 1; // since we appended the NUL. | |
| 89 *line = buf_; | |
| 90 return true; | |
| 91 } | |
| 92 | |
| 93 // Otherwise, we should pull in more data from the file | |
| 94 const ssize_t n = sys_read(fd_, buf_ + buf_used_, | |
| 95 sizeof(buf_) - buf_used_); | |
| 96 if (n < 0) { | |
| 97 return false; | |
| 98 } else if (n == 0) { | |
| 99 hit_eof_ = true; | |
| 100 } else { | |
| 101 buf_used_ += n; | |
| 102 } | |
| 103 | |
| 104 // At this point, we have either set the hit_eof_ flag, or we have more | |
| 105 // data to process... | |
| 106 } | |
| 107 } | |
| 108 | |
| 109 void PopLine(unsigned len) { | |
| 110 // len doesn't include the NUL byte at the end. | |
| 111 | |
| 112 assert(buf_used_ >= len + 1); | |
| 113 buf_used_ -= len + 1; | |
| 114 memmove(buf_, buf_ + len + 1, buf_used_); | |
| 115 } | |
| 116 | |
| 117 private: | |
| 118 const int fd_; | |
| 119 | |
| 120 bool hit_eof_; | |
| 121 unsigned buf_used_; | |
| 122 char buf_[kMaxLineLen]; | |
| 123 }; | |
| 124 | |
| 125 } // namespace google_breakpad | |
| OLD | NEW |