ROSE  0.9.9.109
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 
13 #include <algorithm>
14 #include <boost/filesystem.hpp>
15 #include <vector>
16 
17 namespace Sawyer {
18 namespace Container {
19 
23 class LineVector {
25  const char *charBuf_; // cached ptr to first byte of buffer
26  size_t bufSize_; // cached size of buffer
27  mutable std::vector<size_t> lineFeeds_;
28  mutable size_t nextCharToScan_;
29 
30 public:
32  LineVector(): charBuf_(NULL), bufSize_(0) {}
33 
37  explicit LineVector(const boost::filesystem::path&);
38 
40  explicit LineVector(const Buffer<size_t, char>::Ptr&);
41 
43  LineVector(size_t nBytes, const char *buf);
44 
46  void load(const boost::filesystem::path&);
47 
49  void load(const Buffer<size_t, char>::Ptr&);
50 
52  void load(size_t nBytes, const char *buf);
53 
57  void clear() {
58  buffer_ = Buffer<size_t, char>::Ptr();
59  charBuf_ = NULL;
60  lineFeeds_.clear();
61  nextCharToScan_ = 0;
62  }
63 
68  size_t nLines() const;
69 
73  size_t nCharacters() const {
74  return bufSize_;
75  }
76 
81  size_t nCharacters(size_t lineIdx) const;
82 
87  int character(size_t charIdx) const {
88  return charIdx >= nCharacters() ? EOF : (int)charBuf_[charIdx];
89  }
90 
96  const char *characters(size_t charIdx) const;
97 
103  int character(size_t lineIdx, size_t colIdx) const;
104 
111  const char* lineChars(size_t lineIdx) const;
112 
119  std::string lineString(size_t lineIdx) const;
120 
125  size_t characterIndex(size_t lineIdx) const;
126 
131  size_t lineIndex(size_t charIndex) const;
132 
136  std::pair<size_t, size_t> location(size_t charIndex) const;
137 
142  bool isLastLineTerminated() const;
143 
147  bool isEmpty() const;
148 
149 private:
150  void cacheLines(size_t nLines) const;
151  void cacheCharacters(size_t nCharacters) const;
152 };
153 
154 } // namespace
155 } // namespace
156 
157 #endif
158 
int character(size_t charIdx) const
Character at file offset.
Definition: LineVector.h:87
void clear()
Clears data.
Definition: LineVector.h:57
A buffer of characters indexed by line number.
Definition: LineVector.h:23
size_t characterIndex(size_t lineIdx) const
Character index for start of line.
const char * lineChars(size_t lineIdx) const
Characters for a line.
std::pair< size_t, size_t > location(size_t charIndex) const
Convert a character index to a line and column index.
size_t lineIndex(size_t charIndex) const
Convert a character index to a line index.
bool isLastLineTerminated() const
Determines whether the file ends with line termination.
void load(const boost::filesystem::path &)
(Re)load a line vector from a file.
std::string lineString(size_t lineIdx) const
Line as a string.
size_t nCharacters() const
Number of characters.
Definition: LineVector.h:73
LineVector()
Constructor that creates an empty line vector.
Definition: LineVector.h:32
Name space for the entire library.
Definition: Access.h:11
SharedPointer< Buffer > Ptr
Reference counting smart pointer.
Definition: Buffer.h:45
Base class for all buffers.
Definition: Buffer.h:25
size_t nLines() const
Number of lines.
bool isEmpty() const
Determines whether the file is empty.
const char * characters(size_t charIdx) const
Characters at file offset.