Planeshift

line_reader.h

Go to the documentation of this file.
00001 // Copyright (c) 2009, Google Inc.
00002 // All rights reserved.
00003 //
00004 // Redistribution and use in source and binary forms, with or without
00005 // modification, are permitted provided that the following conditions are
00006 // met:
00007 //
00008 //     * Redistributions of source code must retain the above copyright
00009 // notice, this list of conditions and the following disclaimer.
00010 //     * Redistributions in binary form must reproduce the above
00011 // copyright notice, this list of conditions and the following disclaimer
00012 // in the documentation and/or other materials provided with the
00013 // distribution.
00014 //     * Neither the name of Google Inc. nor the names of its
00015 // contributors may be used to endorse or promote products derived from
00016 // this software without specific prior written permission.
00017 //
00018 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00019 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00020 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00021 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
00022 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00023 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00024 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00025 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00026 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00027 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00028 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00029 
00030 #ifndef CLIENT_LINUX_MINIDUMP_WRITER_LINE_READER_H_
00031 #define CLIENT_LINUX_MINIDUMP_WRITER_LINE_READER_H_
00032 
00033 #include <stdint.h>
00034 #include <assert.h>
00035 #include <string.h>
00036 
00037 #include "common/linux/linux_libc_support.h"
00038 #include "third_party/lss/linux_syscall_support.h"
00039 
00040 namespace google_breakpad {
00041 
00042 // A class for reading a file, line by line, without using fopen/fgets or other
00043 // functions which may allocate memory.
00044 class LineReader {
00045  public:
00046   LineReader(int fd)
00047       : fd_(fd),
00048         hit_eof_(false),
00049         buf_used_(0) {
00050   }
00051 
00052   // The maximum length of a line.
00053   static const size_t kMaxLineLen = 512;
00054 
00055   // Return the next line from the file.
00056   //   line: (output) a pointer to the start of the line. The line is NUL
00057   //     terminated.
00058   //   len: (output) the length of the line (not inc the NUL byte)
00059   //
00060   // Returns true iff successful (false on EOF).
00061   //
00062   // One must call |PopLine| after this function, otherwise you'll continue to
00063   // get the same line over and over.
00064   bool GetNextLine(const char **line, unsigned *len) {
00065     for (;;) {
00066       if (buf_used_ == 0 && hit_eof_)
00067         return false;
00068 
00069       for (unsigned i = 0; i < buf_used_; ++i) {
00070         if (buf_[i] == '\n' || buf_[i] == 0) {
00071           buf_[i] = 0;
00072           *len = i;
00073           *line = buf_;
00074           return true;
00075         }
00076       }
00077 
00078       if (buf_used_ == sizeof(buf_)) {
00079         // we scanned the whole buffer and didn't find an end-of-line marker.
00080         // This line is too long to process.
00081         return false;
00082       }
00083 
00084       // We didn't find any end-of-line terminators in the buffer. However, if
00085       // this is the last line in the file it might not have one:
00086       if (hit_eof_) {
00087         assert(buf_used_);
00088         // There's room for the NUL because of the buf_used_ == sizeof(buf_)
00089         // check above.
00090         buf_[buf_used_] = 0;
00091         *len = buf_used_;
00092         buf_used_ += 1;  // since we appended the NUL.
00093         *line = buf_;
00094         return true;
00095       }
00096 
00097       // Otherwise, we should pull in more data from the file
00098       const ssize_t n = sys_read(fd_, buf_ + buf_used_,
00099                                  sizeof(buf_) - buf_used_);
00100       if (n < 0) {
00101         return false;
00102       } else if (n == 0) {
00103         hit_eof_ = true;
00104       } else {
00105         buf_used_ += n;
00106       }
00107 
00108       // At this point, we have either set the hit_eof_ flag, or we have more
00109       // data to process...
00110     }
00111   }
00112 
00113   void PopLine(unsigned len) {
00114     // len doesn't include the NUL byte at the end.
00115 
00116     assert(buf_used_ >= len + 1);
00117     buf_used_ -= len + 1;
00118     my_memmove(buf_, buf_ + len + 1, buf_used_);
00119   }
00120 
00121  private:
00122   const int fd_;
00123 
00124   bool hit_eof_;
00125   unsigned buf_used_;
00126   char buf_[kMaxLineLen];
00127 };
00128 
00129 }  // namespace google_breakpad
00130 
00131 #endif  // CLIENT_LINUX_MINIDUMP_WRITER_LINE_READER_H_