Nvwa  1.1
file_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 
37 #ifndef NVWA_FILE_LINE_READER_H
38 #define NVWA_FILE_LINE_READER_H
39 
40 #include <assert.h> // assert
41 #include <stdio.h> // file streams
42 #include <iterator> // std::input_iterator_tag
43 #include "_nvwa.h" // NVWA_NAMESPACE_*
44 #include "c++11.h" // _NOEXCEPT/_NULLPTR
45 
46 NVWA_NAMESPACE_BEGIN
47 
50 {
51 public:
57  class iterator // implements InputIterator
58  {
59  public:
60  typedef int difference_type;
61  typedef char* value_type;
62  typedef value_type* pointer_type;
63  typedef value_type& reference;
64  typedef std::input_iterator_tag iterator_category;
65 
66  iterator() _NOEXCEPT : _M_reader(_NULLPTR), _M_line(_NULLPTR) {}
67  explicit iterator(file_line_reader* reader);
68  ~iterator();
69 
70  iterator(const iterator& rhs);
71  iterator& operator=(const iterator& rhs);
72 
73 #if HAVE_CXX11_RVALUE_REFERENCE
74  iterator(iterator&& rhs) _NOEXCEPT;
75  iterator& operator=(iterator&& rhs) _NOEXCEPT;
76 #endif
77 
78  void swap(iterator& rhs) _NOEXCEPT;
79 
80  reference operator*()
81  {
82  assert(_M_reader != _NULLPTR);
83  return _M_line;
84  }
85  pointer_type operator->()
86  {
87  assert(_M_reader != _NULLPTR);
88  return &_M_line;
89  }
90  iterator& operator++()
91  {
92  if (!_M_reader->read(_M_line, _M_size, _M_capacity))
93  _M_reader = _NULLPTR;
94  return *this;
95  }
96  iterator operator++(int)
97  {
98  iterator temp(*this);
99  ++*this;
100  return temp;
101  }
102 
103  bool operator==(const iterator& rhs) const
104  {
105  return _M_reader == rhs._M_reader;
106  }
107  bool operator!=(const iterator& rhs) const
108  {
109  return !operator==(rhs);
110  }
111 
112  size_t size() const { return _M_size; }
113 
114  private:
115  file_line_reader* _M_reader;
116  char* _M_line;
117  size_t _M_size;
118  size_t _M_capacity;
119  };
120 
123  {
126  };
127 
128  explicit file_line_reader(FILE* stream, char delimiter = '\n',
129  strip_type strip = strip_delimiter);
130  ~file_line_reader();
131 
132  iterator begin() { return iterator(this); }
133  iterator end() const _NOEXCEPT { return iterator(); }
134  bool read(char*& output, size_t& size, size_t& capacity);
135 
136 private:
137  file_line_reader(const file_line_reader&) _DELETED;
138  file_line_reader& operator=(const file_line_reader&) _DELETED;
139 
140  FILE* _M_stream;
141  char _M_delimiter;
142  bool _M_strip_delimiter;
143  char* _M_buffer;
144  size_t _M_read_pos;
145  size_t _M_size;
146 };
147 
148 inline void swap(file_line_reader::iterator& lhs,
150 {
151  lhs.swap(rhs);
152 }
153 
154 NVWA_NAMESPACE_END
155 
156 #endif // NVWA_FILE_LINE_READER_H
void swap(iterator &rhs) noexcept
Swaps the iterator with another.
Definition: file_line_reader.cpp:142
The delimiter should be retained.
Definition: file_line_reader.h:125
void swap(bool_array &lhs, bool_array &rhs) noexcept
Exchanges the content of two bool_arrays.
Definition: bool_array.h:358
Iterator that contains the line content.
Definition: file_line_reader.h:57
strip_type
Enumeration of whether the delimiter should be stripped.
Definition: file_line_reader.h:122
C++11 feature detection macros and workarounds.
Class to allow iteration over all lines of a text file.
Definition: file_line_reader.h:49
The delimiter should be stripped.
Definition: file_line_reader.h:124
Common definitions for preprocessing.