Nvwa  1.1
mmap_line_reader.h
Go to the documentation of this file.
1 // -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*-
2 // vim:tabstop=4:shiftwidth=4:expandtab:
3 
4 /*
5  * Copyright (C) 2016-2017 Wu Yongwei <adah at users dot sourceforge dot net>
6  *
7  * This software is provided 'as-is', without any express or implied
8  * warranty. In no event will the authors be held liable for any
9  * damages arising from the use of this software.
10  *
11  * Permission is granted to anyone to use this software for any purpose,
12  * including commercial applications, and to alter it and redistribute
13  * it freely, subject to the following restrictions:
14  *
15  * 1. The origin of this software must not be misrepresented; you must
16  * not claim that you wrote the original software. If you use this
17  * software in a product, an acknowledgement in the product
18  * documentation would be appreciated but is not required.
19  * 2. Altered source versions must be plainly marked as such, and must
20  * not be misrepresented as being the original software.
21  * 3. This notice may not be removed or altered from any source
22  * distribution.
23  *
24  * This file is part of Stones of Nvwa:
25  * http://sourceforge.net/projects/nvwa
26  *
27  */
28 
38 #ifndef NVWA_MMAP_LINE_READER_H
39 #define NVWA_MMAP_LINE_READER_H
40 
41 #include <assert.h> // assert
42 #include <unistd.h> // off_t
43 #include <iterator> // std::input_iterator_tag
44 #include <string> // std::string
45 #include "_nvwa.h" // NVWA_NAMESPACE_*
46 #include "c++11.h" // _NULLPTR
47 
48 NVWA_NAMESPACE_BEGIN
49 
52 {
53 public:
55  class iterator // implements InputIterator
56  {
57  public:
58  typedef int difference_type;
59  typedef std::string value_type;
60  typedef value_type* pointer_type;
61  typedef value_type& reference;
62  typedef std::input_iterator_tag iterator_category;
63 
64  iterator() : _M_reader(_NULLPTR) {}
65  explicit iterator(mmap_line_reader* reader)
66  : _M_reader(reader) , _M_offset(0) {}
67 
68  reference operator*()
69  {
70  assert(_M_reader != _NULLPTR);
71  return _M_line;
72  }
73  value_type* operator->()
74  {
75  assert(_M_reader != _NULLPTR);
76  return &_M_line;
77  }
78  iterator& operator++()
79  {
80  if (!_M_reader->read(_M_line, _M_offset))
81  _M_reader = _NULLPTR;
82  return *this;
83  }
84  iterator operator++(int)
85  {
86  iterator temp(*this);
87  ++*this;
88  return temp;
89  }
90 
91  bool operator==(const iterator& rhs) const
92  {
93  return _M_reader == rhs._M_reader;
94  }
95  bool operator!=(const iterator& rhs) const
96  {
97  return !operator==(rhs);
98  }
99 
100  private:
101  mmap_line_reader* _M_reader;
102  off_t _M_offset;
103  std::string _M_line;
104  };
105 
108  {
111  };
112 
113  explicit mmap_line_reader(const char* path, char delimiter = '\n',
114  strip_type strip = strip_delimiter);
115  explicit mmap_line_reader(int fd, char delimiter = '\n',
116  strip_type strip = strip_delimiter);
117  ~mmap_line_reader();
118 
119  iterator begin() { return iterator(this); }
120  iterator end() const { return iterator(); }
121 
122  bool read(std::string& output, off_t& offset);
123 
124 private:
125  mmap_line_reader(const mmap_line_reader&) _DELETED;
126  mmap_line_reader& operator=(const mmap_line_reader&) _DELETED;
127 
128  void initialize();
129 
130  int _M_fd;
131  char _M_delimiter;
132  bool _M_strip_delimiter;
133  char* _M_mmap_ptr;
134  off_t _M_size;
135 };
136 
137 NVWA_NAMESPACE_END
138 
139 #endif // NVWA_MMAP_LINE_READER_H
Iterator that contains the line content.
Definition: mmap_line_reader.h:55
The delimiter should be stripped.
Definition: mmap_line_reader.h:109
Class to allow iteration over all lines of a mmappable file.
Definition: mmap_line_reader.h:51
strip_type
Enumeration of whether the delimiter should be stripped.
Definition: mmap_line_reader.h:107
C++11 feature detection macros and workarounds.
The delimiter should be retained.
Definition: mmap_line_reader.h:110
Common definitions for preprocessing.