crawlserv++  [under development]
Application for crawling and analyzing textual content of websites.
CurlList.hpp
Go to the documentation of this file.
1 /*
2  *
3  * ---
4  *
5  * Copyright (C) 2021 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  * CurlList.hpp
24  *
25  * RAII wrapper for pointer to libcurl list.
26  * Does NOT have ownership of the pointer!
27  *
28  * Created on: Feb 7, 2019
29  * Author: ans
30  */
31 
32 #ifndef WRAPPER_CURLLIST_HPP_
33 #define WRAPPER_CURLLIST_HPP_
34 
35 #ifndef CRAWLSERVPP_TESTING
36 
37 #include <curl/curl.h>
38 
39 #else
40 
41 #include "FakeCurl/FakeCurl.hpp"
42 
43 #endif
44 
45 #include <cstddef> // std::size_t
46 #include <stdexcept> // std::runtime_error
47 #include <string> // std::string
48 #include <vector> // std::vector
49 
50 namespace crawlservpp::Wrapper {
51 
52  /*
53  * DECLARATION
54  */
55 
57 
75  class CurlList {
76  public:
79 
81  CurlList() = default;
82 
83  virtual ~CurlList();
84 
88 
89  [[nodiscard]] curl_slist * get() noexcept;
90  [[nodiscard]] const curl_slist * getc() const noexcept;
91  [[nodiscard]] bool valid() const noexcept;
92  [[nodiscard]] std::size_t size() const noexcept;
93  [[nodiscard]] bool empty() const noexcept;
94 
98 
99  void append(const CurlList& other);
100  void append(const std::vector<std::string>& newElements);
101  void append(const std::string& newElement);
102  void clear() noexcept;
103 
105 
109 
111  CurlList(const CurlList& other);
112  CurlList& operator=(const CurlList& other);
113  CurlList(CurlList&& other) noexcept;
114  CurlList& operator=(CurlList&& other) noexcept;
115 
117 
118  private:
119  curl_slist * ptr{nullptr};
120  };
121 
122  /*
123  * IMPLEMENTATION
124  */
125 
126  /*
127  * CONSTRUCTION AND DESTRUCTION
128  */
129 
132  this->clear();
133  }
134 
135  /*
136  * GETTERS
137  */
138 
140 
146  inline curl_slist * CurlList::get() noexcept {
147  return this->ptr;
148  }
149 
151 
158  inline const curl_slist * CurlList::getc() const noexcept {
159  return this->ptr;
160  }
161 
163 
168  inline bool CurlList::valid() const noexcept {
169  return this->ptr != nullptr;
170  }
171 
173 
180  inline std::size_t CurlList::size() const noexcept {
181  std::size_t count{};
182  const auto * element{this->ptr};
183 
184  while(element != nullptr) {
185  if(element->data != nullptr) {
186  ++count;
187  }
188 
189  element = element->next;
190  }
191 
192  return count;
193  }
194 
196 
202  inline bool CurlList::empty() const noexcept {
203  const auto * element{this->ptr};
204 
205  while(element != nullptr) {
206  if(element->data != nullptr) {
207  return false;
208  }
209  }
210 
211  return true;
212  }
213 
214  /*
215  * MANIPULATION AND CLEANUP
216  */
217 
219 
230  inline void CurlList::append(const CurlList& other) {
231  if(this == &other) {
232  return;
233  }
234 
235  auto * item = other.ptr;
236 
237  while(item != nullptr) {
238  this->append(item->data);
239 
240  item = item->next;
241  }
242  }
243 
245 
253  inline void CurlList::append(const std::vector<std::string>& newElements) {
254  for(const auto& element : newElements) {
255  this->append(element);
256  }
257  }
258 
260 
275  inline void CurlList::append(const std::string& newElement) {
276  auto * const temp{
277  curl_slist_append(this->ptr, newElement.c_str())
278  };
279 
280  if(temp == nullptr) {
281  throw std::runtime_error("curl_slist_append() failed");
282  }
283 
284  this->ptr = temp;
285  }
286 
288 
297  inline void CurlList::clear() noexcept {
298  if(this->ptr != nullptr) {
299  curl_slist_free_all(this->ptr);
300  }
301 
302  this->ptr = nullptr;
303  }
304 
305  /*
306  * COPY AND MOVE
307  */
308 
310 
324  inline CurlList::CurlList(const CurlList& other) {
325  this->append(other);
326  }
327 
329 
348  inline CurlList& CurlList::operator=(const CurlList& other) {
349  if(&other != this) {
350  this->clear();
351 
352  this->append(other);
353  }
354 
355  return *this;
356  }
357 
359 
371  inline CurlList::CurlList(CurlList&& other) noexcept : ptr(other.ptr) {
372  other.ptr = nullptr;
373  }
374 
376 
395  inline CurlList& CurlList::operator=(CurlList&& other) noexcept {
396  if(&other != this) {
397  this->clear();
398 
399  this->ptr = other.ptr;
400 
401  other.ptr = nullptr;
402  }
403 
404  return *this;
405  }
406 
407  } /* namespace crawlservpp::Wrapper */
408 
409 #endif /* WRAPPER_CURLLIST_HPP_ */
curl_slist * get() noexcept
Gets a pointer to the underlying list.
Definition: CurlList.hpp:146
virtual ~CurlList()
Destructor resetting the list if necessary.
Definition: CurlList.hpp:131
RAII wrapper for lists used by the libcurl API.
Definition: CurlList.hpp:75
CurlList()=default
Default constructor.
void append(const CurlList &other)
Appends another list to the list.
Definition: CurlList.hpp:230
bool empty() const noexcept
Checks whether the list is empty.
Definition: CurlList.hpp:202
std::size_t size() const noexcept
Gets the current number of elements in the list.
Definition: CurlList.hpp:180
Namespace for RAII wrappers and Wrapper::Database.
Definition: Database.hpp:109
CurlList & operator=(const CurlList &other)
Copy assignment operator.
Definition: CurlList.hpp:348
bool valid() const noexcept
Checks whether the underlying list is valid.
Definition: CurlList.hpp:168
const curl_slist * getc() const noexcept
Gets a const pointer to the underlying list.
Definition: CurlList.hpp:158
void clear() noexcept
Resets the list and frees its memory.
Definition: CurlList.hpp:297