crawlserv++  [under development]
Application for crawling and analyzing textual content of websites.
TidyBuffer.hpp
Go to the documentation of this file.
1 /*
2  *
3  * ---
4  *
5  * Copyright (C) 2020 Anselm Schmidt (ans[ät]ohai.su)
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version in addition to the terms of any
11  * licences already herein identified.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program. If not, see <https://www.gnu.org/licenses/>.
20  *
21  * ---
22  *
23  * TidyBuffer.hpp
24  *
25  * RAII wrapper for tidyhtml buffers.
26  *
27  * Created on: Feb 7, 2019
28  * Author: ans
29  */
30 
31 #ifndef WRAPPER_TIDYBUFFER_HPP_
32 #define WRAPPER_TIDYBUFFER_HPP_
33 
34 #include <tidybuffio.h>
35 
36 #include <cstddef> // std::size_t
37 #include <string> // std::string
38 #include <utility> // std::move
39 
40 namespace crawlservpp::Wrapper {
41 
42  /*
43  * DECLARATION
44  */
45 
47 
53  class TidyBuffer {
54  public:
57 
59  TidyBuffer() = default;
60 
61  virtual ~TidyBuffer();
62 
66 
67  [[nodiscard]] ::TidyBuffer * get() noexcept;
68  [[nodiscard]] const ::TidyBuffer * getc() const noexcept;
69  [[nodiscard]] std::string getString() const noexcept;
70  [[nodiscard]] bool valid() const noexcept;
71  [[nodiscard]] std::size_t size() const noexcept;
72  [[nodiscard]] bool empty() const noexcept;
73  [[nodiscard]] std::size_t capacity() const noexcept;
74 
78 
79  void clear() noexcept;
80 
82 
85 
87  TidyBuffer(const TidyBuffer& other);
88  TidyBuffer& operator=(const TidyBuffer& other);
89  TidyBuffer(TidyBuffer&& other) noexcept;
90  TidyBuffer& operator=(TidyBuffer&& other) noexcept;
91 
93 
94  private:
95  ::TidyBuffer buffer{};
96  };
97 
98  /*
99  * IMPLEMENTATION
100  */
101 
102  /*
103  * CONSTRUCTION AND DESTRUCTION
104  */
105 
108  this->clear();
109  }
110 
111  /*
112  * GETTERS
113  */
114 
116 
119  inline ::TidyBuffer * TidyBuffer::get() noexcept {
120  return &(this->buffer);
121  }
122 
124 
127  inline const ::TidyBuffer * TidyBuffer::getc() const noexcept {
128  return &(this->buffer);
129  }
130 
132 
136  inline std::string TidyBuffer::getString() const noexcept {
137  if(this->buffer.bp == nullptr || this->buffer.size == 0) {
138  return "";
139  }
140 
141  return std::string(
142  static_cast<const char *>(static_cast<void *>(this->buffer.bp)),
143  this->buffer.size
144  );
145  }
146 
148 
155  inline bool TidyBuffer::valid() const noexcept {
156  return this->buffer.bp != nullptr;
157  }
158 
160 
170  inline std::size_t TidyBuffer::size() const noexcept {
171  return this->buffer.size;
172  }
173 
175 
185  inline std::size_t TidyBuffer::capacity() const noexcept {
186  return this->buffer.allocated;
187  }
188 
190 
195  inline bool TidyBuffer::empty() const noexcept {
196  return this->buffer.bp == nullptr || this->buffer.size == 0;
197  }
198 
199  /*
200  * CLEANUP
201  */
202 
204 
209  inline void TidyBuffer::clear() noexcept {
210  if(this->buffer.bp != nullptr) {
211  tidyBufFree(&(this->buffer)); // (also sets buffer structure to zeroes)
212  }
213  }
214 
215  /*
216  * COPY AND MOVE
217  */
218 
220 
230  inline TidyBuffer::TidyBuffer(const TidyBuffer& other) : buffer{} {
231  if(other.valid()) {
232  if(other.buffer.allocator != nullptr) {
233  ::tidyBufAllocWithAllocator(
234  &(this->buffer),
235  other.buffer.allocator,
236  other.capacity()
237  );
238  }
239  else {
240  ::tidyBufAlloc(
241  &(this->buffer),
242  other.capacity()
243  );
244  }
245 
246  if(!other.empty()) {
247  ::tidyBufAppend(
248  &(this->buffer),
249  other.buffer.bp,
250  other.size()
251  );
252  }
253  }
254  }
255 
257 
273  if(this != &other) {
274  // clear old buffer if necessary
275  this->clear();
276 
277  if(other.valid()) {
278  // allocate memory for the copy
279  if(other.buffer.allocator != nullptr) {
280  ::tidyBufAllocWithAllocator(
281  &(this->buffer),
282  other.buffer.allocator,
283  other.capacity()
284  );
285  }
286  else {
287  ::tidyBufAlloc(
288  &(this->buffer),
289  other.capacity()
290  );
291  }
292 
293  // copy the bytes
294  if(!other.empty()) {
295  ::tidyBufAppend(
296  &(this->buffer),
297  other.buffer.bp,
298  other.size()
299  );
300  }
301  }
302  }
303 
304  return *this;
305  }
306 
308 
318  inline TidyBuffer::TidyBuffer(TidyBuffer&& other) noexcept : buffer{} {
319  this->buffer = other.buffer;
320 
321  other.buffer.allocator = nullptr;
322  other.buffer.bp = nullptr;
323  other.buffer.size = 0;
324  other.buffer.allocated = 0;
325  other.buffer.next = 0;
326  }
327 
329 
344  inline TidyBuffer& TidyBuffer::operator=(TidyBuffer&& other) noexcept {
345  if(this != &other) {
346  this->clear();
347 
348  this->buffer = other.buffer;
349 
350  other.buffer.allocator = nullptr;
351  other.buffer.bp = nullptr;
352  other.buffer.size = 0;
353  other.buffer.allocated = 0;
354  other.buffer.next = 0;
355  }
356 
357  return *this;
358  }
359 
360 } /* namespace crawlservpp::Wrapper */
361 
362 #endif /* WRAPPER_TIDYBUFFER_HPP_ */
std::size_t size() const noexcept
Gets the current size of the content in the underlying buffer in bytes.
Definition: TidyBuffer.hpp:170
const ::TidyBuffer * getc() const noexcept
Gets a const pointer to the underlying buffer.
Definition: TidyBuffer.hpp:127
::TidyBuffer * get() noexcept
Gets a pointer to the underlying buffer.
Definition: TidyBuffer.hpp:119
bool valid() const noexcept
Checks whether the underlying buffer is valid.
Definition: TidyBuffer.hpp:155
bool empty() const noexcept
Checks whether the underlying buffer is empty.
Definition: TidyBuffer.hpp:195
TidyBuffer & operator=(const TidyBuffer &other)
Copy assignment operator.
Definition: TidyBuffer.hpp:272
RAII wrapper for buffers used by the tidy-html5 API.
Definition: TidyBuffer.hpp:53
virtual ~TidyBuffer()
Destructor clearing the underlying buffer if necessary.
Definition: TidyBuffer.hpp:107
std::string getString() const noexcept
Copies the content of the underlying buffer into a string.
Definition: TidyBuffer.hpp:136
void clear() noexcept
Frees the underlying buffer.
Definition: TidyBuffer.hpp:209
Namespace for RAII wrappers and Wrapper::Database.
Definition: Database.hpp:109
std::size_t capacity() const noexcept
Gets the current capacity of the underlying buffer in bytes.
Definition: TidyBuffer.hpp:185
TidyBuffer()=default
Default constructor.