crawlserv++  [under development]
Application for crawling and analyzing textual content of websites.
ZipSource.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  * ZipSource.hpp
24  *
25  * Wrapper for pointer to a libzip source.
26  * Does NOT have ownership of the pointer, but takes care of its deletion!
27  *
28  * Created on: Jan 4, 2021
29  * Author: ans
30  */
31 
32 #ifndef WRAPPER_ZIPSOURCE_HPP_
33 #define WRAPPER_ZIPSOURCE_HPP_
34 
35 #include <zip.h>
36 
37 namespace crawlservpp::Wrapper {
38 
39  /*
40  * DECLARATION
41  */
42 
44 
61  class ZipSource {
62  public:
65 
66  ZipSource();
67  ZipSource(const void * data, zip_uint64_t size);
68  virtual ~ZipSource();
69 
73 
74  [[nodiscard]] zip_source_t * get() noexcept;
75  [[nodiscard]] const zip_source_t * getc() const noexcept;
76  [[nodiscard]] bool valid() const noexcept;
77  [[nodiscard]] zip_error_t getError() const;
78 
82 
83  void clear() noexcept;
84 
86 
89 
92  ZipSource(ZipSource&) = delete;
93 
95  ZipSource& operator=(ZipSource&) = delete;
96 
97  ZipSource(ZipSource&& other) noexcept;
98  ZipSource& operator=(ZipSource&& other) noexcept;
99 
101 
102  private:
103  // underlying pointer to ZIP source
104  zip_source_t * ptr{nullptr};
105 
106  // last occured error
107  zip_error_t error{};
108  };
109 
110  /*
111  * IMPLEMENTATION
112  */
113 
116  this->ptr = zip_source_buffer_create(nullptr, 0, 0, &(this->error));
117  }
118 
120 
129  inline ZipSource::ZipSource(const void * data, zip_uint64_t size) {
130  this->ptr = zip_source_buffer_create(data, size, 0, &(this->error));
131  }
132 
135  this->clear();
136  }
137 
139 
143  inline zip_source_t * ZipSource::get() noexcept {
144  return this->ptr;
145  }
146 
148 
153  inline const zip_source_t * ZipSource::getc() const noexcept {
154  return this->ptr;
155  }
156 
158 
165  inline bool ZipSource::valid() const noexcept {
166  return this->ptr != nullptr;
167  }
168 
170 
176  inline zip_error_t ZipSource::getError() const {
177  return this->error;
178  }
179 
181 
190  inline void ZipSource::clear() noexcept {
191  if(this->ptr != nullptr) {
192  zip_source_free(this->ptr);
193 
194  this->ptr = nullptr;
195  }
196 
197  zip_error_fini(&(this->error));
198  }
199 
201 
213  inline ZipSource::ZipSource(ZipSource&& other) noexcept : ptr(other.ptr) {
214  other.ptr = nullptr;
215  }
216 
218 
237  inline ZipSource& ZipSource::operator=(ZipSource&& other) noexcept {
238  if(&other != this) {
239  this->clear();
240 
241  this->ptr = other.ptr;
242 
243  other.ptr = nullptr;
244  }
245 
246  return *this;
247  }
248 
249 } /* namespace crawlservpp::Wrapper */
250 
251 #endif /* WRAPPER_ZIPSOURCE_HPP_ */
ZipSource()
Constructor creating an empty source.
Definition: ZipSource.hpp:115
const zip_source_t * getc() const noexcept
Gets a const pointer to the underlying source.
Definition: ZipSource.hpp:153
ZipSource & operator=(ZipSource &)=delete
Deleted copy assignment operator.
Namespace for RAII wrappers and Wrapper::Database.
Definition: Database.hpp:109
bool valid() const noexcept
Checks whether the underlying source is valid.
Definition: ZipSource.hpp:165
zip_error_t getError() const
Get the last occurred error.
Definition: ZipSource.hpp:176
RAII wrapper for sources used by libzip.
Definition: ZipSource.hpp:61
zip_source_t * get() noexcept
Gets a pointer to the underlying source.
Definition: ZipSource.hpp:143
void clear() noexcept
Clears the underlying source if necessary.
Definition: ZipSource.hpp:190
virtual ~ZipSource()
Destructor clearing the source if necessary.
Definition: ZipSource.hpp:134