scc 2026.07
SystemC components library
gzip_streambuf.cpp
1/*******************************************************************************
2 * Copyright 2026 MINRES Technologies GmbH
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *******************************************************************************/
16
17#include "gzip_streambuf.h"
18
19util::gzip_streambuf::gzip_streambuf(const std::string& path)
20: gz_(gzopen(path.c_str(), "rb"))
21, buffer_(64 * 1024) {
22 if(!gz_)
23 throw std::runtime_error("gzopen failed");
24
25 setg(buffer_.data(), buffer_.data(), buffer_.data());
26}
27
28util::gzip_streambuf::~gzip_streambuf() {
29 if(gz_)
30 gzclose(gz_);
31}
32
33auto util::gzip_streambuf::underflow() -> int_type {
34 int n = gzread(gz_, buffer_.data(), buffer_.size());
35 if(n <= 0)
36 return traits_type::eof();
37
38 setg(buffer_.data(), buffer_.data(), buffer_.data() + n);
39 return traits_type::to_int_type(*gptr());
40}