ROSE  0.11.96.11
LineVector.h
1 // WARNING: Changes to this file must be contributed back to Sawyer or else they will
2 // be clobbered by the next update from Sawyer. The Sawyer repository is at
3 // https://github.com/matzke1/sawyer.
4 
5 
6 
7 
8 #ifndef Sawyer_Container_LineVector_H
9 #define Sawyer_Container_LineVector_H
10 
11 #include <Sawyer/Buffer.h>
12 #include <Sawyer/Sawyer.h>
13 
14 #include <algorithm>
15 #include <boost/filesystem.hpp>
16 #include <vector>
17 
18 namespace Sawyer {
19 namespace Container {
20 
24 class LineVector {
26  const char *charBuf_; // cached ptr to first byte of buffer
27  size_t bufSize_; // cached size of buffer
28  mutable std::vector<size_t> lineFeeds_;
29  mutable size_t nextCharToScan_;
30 
31 public:
33  LineVector(): charBuf_(NULL), bufSize_(0), nextCharToScan_(0) {}
34 
38  explicit LineVector(const boost::filesystem::path&);
39 
41  explicit LineVector(const Buffer<size_t, char>::Ptr&);
42 
44  LineVector(size_t nBytes, const char *buf);
45 
47  void load(const boost::filesystem::path&);
48 
50  void load(const Buffer<size_t, char>::Ptr&);
51 
53  void load(size_t nBytes, const char *buf);
54 
58  void clear() {
59  buffer_ = Buffer<size_t, char>::Ptr();
60  charBuf_ = NULL;
61  lineFeeds_.clear();
62  nextCharToScan_ = 0;
63  }
64 
69  size_t nLines() const;
70 
74  size_t nCharacters() const {
75  return bufSize_;
76  }
77 
82  size_t nCharacters(size_t lineIdx) const;
83 
88  int character(size_t charIdx) const {
89  return charIdx >= nCharacters() ? EOF : (int)charBuf_[charIdx];
90  }
91 
97  const char *characters(size_t charIdx) const;
98 
104  int character(size_t lineIdx, size_t colIdx) const;
105 
112  const char* lineChars(size_t lineIdx) const;
113 
120  std::string lineString(size_t lineIdx) const;
121 
126  size_t characterIndex(size_t lineIdx) const;
127 
132  size_t lineIndex(size_t charIndex) const;
133 
137  std::pair<size_t, size_t> location(size_t charIndex) const;
138 
143  bool isLastLineTerminated() const;
144 
148  bool isEmpty() const;
149 
150 private:
151  void cacheLines(size_t nLines) const;
152  void cacheCharacters(size_t nCharacters) const;
153 };
154 
155 } // namespace
156 } // namespace
157 
158 #endif
159 
Sawyer::Container::LineVector::isEmpty
bool isEmpty() const
Determines whether the file is empty.
Sawyer::Container::LineVector::nCharacters
size_t nCharacters() const
Number of characters.
Definition: LineVector.h:74
Sawyer::Container::LineVector::nLines
size_t nLines() const
Number of lines.
Sawyer::Container::Buffer::Ptr
SharedPointer< Buffer > Ptr
Reference counting smart pointer.
Definition: Buffer.h:45
Sawyer::Container::LineVector
A buffer of characters indexed by line number.
Definition: LineVector.h:24
Sawyer::Container::LineVector::characters
const char * characters(size_t charIdx) const
Characters at file offset.
Sawyer::Container::LineVector::lineString
std::string lineString(size_t lineIdx) const
Line as a string.
Sawyer::Container::LineVector::character
int character(size_t charIdx) const
Character at file offset.
Definition: LineVector.h:88
Sawyer::Container::LineVector::isLastLineTerminated
bool isLastLineTerminated() const
Determines whether the file ends with line termination.
Sawyer::Container::LineVector::LineVector
LineVector()
Constructor that creates an empty line vector.
Definition: LineVector.h:33
Sawyer::Container::Buffer
Base class for all buffers.
Definition: Buffer.h:25
Sawyer::Container::LineVector::clear
void clear()
Clears data.
Definition: LineVector.h:58
Sawyer
Name space for the entire library.
Definition: Access.h:13
Sawyer::Container::LineVector::characterIndex
size_t characterIndex(size_t lineIdx) const
Character index for start of line.
Sawyer::Container::LineVector::load
void load(const boost::filesystem::path &)
(Re)load a line vector from a file.
Sawyer::Container::LineVector::lineChars
const char * lineChars(size_t lineIdx) const
Characters for a line.
Sawyer::Container::LineVector::location
std::pair< size_t, size_t > location(size_t charIndex) const
Convert a character index to a line and column index.
Sawyer::Container::LineVector::lineIndex
size_t lineIndex(size_t charIndex) const
Convert a character index to a line index.