Add trivial zlib wrapper

This commit is contained in:
Danny Robson 2013-07-13 15:28:29 +10:00
parent 6e611b51be
commit 6e71e8674d
4 changed files with 128 additions and 1 deletions

View File

@ -139,7 +139,9 @@ UTIL_FILES = \
vector.cpp \ vector.cpp \
vector.hpp \ vector.hpp \
version.cpp \ version.cpp \
version.hpp version.hpp \
zlib.cpp \
zlib.hpp
if PLATFORM_LINUX if PLATFORM_LINUX

View File

@ -209,6 +209,7 @@ if test "x$ax_cv_boost_filesystem" != "xyes"; then
fi fi
## Optional packages ## Optional packages
PKG_CHECK_MODULES([ZLIB], [zlib >= 1.2.0])
## Output ## Output

52
zlib.cpp Normal file
View File

@ -0,0 +1,52 @@
#include "zlib.hpp"
#include "debug.hpp"
const char *
util::zlib::version (void) {
return zlibVersion ();
}
size_t
util::zlib::uncompress (uint8_t *dst, size_t dst_len,
const uint8_t *src, size_t src_len) {
size_t actual_len = dst_len;
int err = ::uncompress (dst, &actual_len, src, src_len);
error::try_code (err);
return actual_len;
}
void
util::zlib::error::try_code (int code) {
if (Z_OK == code)
return;
throw_code(code);
}
util::zlib::error::error (const std::string &_what):
runtime_error (_what)
{ ; }
void
util::zlib::error::throw_code (int code) {
CHECK_NEQ (code, Z_OK);
switch (code) {
#define do_code(c) case c: throw code_error<c> ();
do_code(Z_STREAM_ERROR);
do_code(Z_DATA_ERROR);
do_code(Z_MEM_ERROR);
do_code(Z_BUF_ERROR);
do_code(Z_VERSION_ERROR);
default:
panic ("Unknown code");
}
}

72
zlib.hpp Normal file
View File

@ -0,0 +1,72 @@
/*
* This file is part of libgim.
*
* libgim is free software: you can redistribute it and/or modify it under the
* terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any later
* version.
*
* libgim is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License
* along with libgim. If not, see <http://www.gnu.org/licenses/>.
*
* Copyright 2013 Danny Robson <danny@nerdcruft.net>
*/
#ifndef __UTIL_ZLIB_HPP
#define __UTIL_ZLIB_HPP
#include <stdexcept>
#include <string>
#include <zlib.h>
namespace util {
namespace zlib {
extern const char* version (void);
extern void compress (uint8_t *dst, size_t dst_len,
const uint8_t *src, size_t src_len,
int level);
extern size_t compress_bound (size_t len);
extern size_t uncompress (uint8_t *dst, size_t dst_len,
const uint8_t *src, size_t src_len);
class stream {
public:
public:
stream();
~stream();
int deflate (bool flush = false);
int inflate (bool flush = false);
};
class error : public std::runtime_error {
public:
static void try_code (int code);
static void throw_code (int code);
error(const std::string&);
};
template <int CODE>
class code_error : public error {
public:
code_error(void);
};
}
}
#endif