Home | History | Annotate | Download | only in src
      1 // Copyright 2014 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #ifndef CRAZY_LINKER_LINE_READER_H
      6 #define CRAZY_LINKER_LINE_READER_H
      7 
      8 #include <string.h>
      9 
     10 #include "crazy_linker_system.h"
     11 
     12 namespace crazy {
     13 
     14 // A class used to read text files line-by-line.
     15 // Usage:
     16 //    LineReader reader("/path/to/file");
     17 //    while (reader.GetNextLine()) {
     18 //       const char* line = reader.line();
     19 //       size_t line_len = reader.length();
     20 //       ... line is not necessarily zero-terminated.
     21 //    }
     22 
     23 class LineReader {
     24  public:
     25   LineReader();
     26   explicit LineReader(const char* path);
     27   ~LineReader();
     28 
     29   // Open a new file for testing. Doesn't fail. If there was an error
     30   // opening the file, GetNextLine() will simply return false.
     31   void Open(const char* file_path);
     32 
     33   // Grab next line. Returns true on success, or false otherwise.
     34   bool GetNextLine();
     35 
     36   // Return the start of the current line, this is _not_ zero-terminated
     37   // and always contains a final newline (\n).
     38   // Only call this after a successful GetNextLine().
     39   const char* line() const;
     40 
     41   // Return the line length, this includes the final \n.
     42   // Only call this after a successful GetNextLine().
     43   size_t length() const;
     44 
     45  private:
     46   void Reset();
     47 
     48   FileDescriptor fd_;
     49   bool eof_;
     50   size_t line_start_;
     51   size_t line_len_;
     52   size_t buff_size_;
     53   size_t buff_capacity_;
     54   char* buff_;
     55   char buff0_[128];
     56 };
     57 
     58 }  // namespace crazy
     59 
     60 #endif  // CRAZY_LINKER_LINE_READER_H
     61