Program Listing for File zip.hpp¶
↰ Return to documentation for file (libtcod/zip.hpp)
/* BSD 3-Clause License
*
* Copyright © 2008-2026, Jice and the libtcod contributors.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
#ifndef TCOD_ZIP_HPP_
#define TCOD_ZIP_HPP_
#ifndef TCOD_NO_ZLIB
#include <filesystem>
#include <optional>
#include <string>
#include "color.hpp"
#include "config.h"
#include "console.hpp"
#include "image.hpp"
#include "mersenne.hpp"
#include "zip.h"
// clang-format off
class TCODLIB_API TCODZip {
public :
[[deprecated("This method of serialization is not cross-platform. An alternative such as C++ Cereal should be used instead.")]]
TCODZip();
~TCODZip();
void putChar(char val);
void putInt(int val);
void putFloat(float val);
void putString(const char *val);
void putColor(const TCODColor *val);
void putImage(const TCODImage *val);
[[deprecated("This function will corrupt console characters greater than 255.")]]
void putConsole(const TCODConsole *val);
void putRandom(const TCODRandom *val);
void putData(int nbBytes, const void *data);
uint32_t getCurrentBytes() const;
int saveToFile(const char *filename);
void saveToFile(const std::filesystem::path& path) { saveToFile(path.string().c_str()); }
int loadFromFile(const char *filename);
void loadFromFile(const std::filesystem::path& path) {
if (loadFromFile(path.string().c_str()) == 0) {
throw std::runtime_error{"Failed to load file: " + path.string()};
}
}
char getChar();
int getInt();
float getFloat();
const char *getString();
TCODColor getColor();
TCODImage *getImage();
TCODConsole *getConsole();
TCODRandom *getRandom();
int getData(int nbBytes, void *data);
uint32_t getRemainingBytes() const;
void skipBytes(uint32_t nbBytes);
// clang-format on
void put(char value) { TCOD_zip_put_char(data, value); }
void put(int value) { TCOD_zip_put_int(data, value); }
void put(float value) { TCOD_zip_put_float(data, value); }
void put(const char* value) { TCOD_zip_put_string(data, value); }
void put(const std::string& value) { TCOD_zip_put_string(data, value.c_str()); }
void put(const std::optional<std::string>& value) {
if (value) return put(value.value());
TCOD_zip_put_string(data, nullptr);
}
void put(const tcod::ColorRGB& value) { TCOD_zip_put_color(data, value); }
void put(const TCODColor& value) { put(tcod::ColorRGB{value}); }
void put(const TCODImage& value) { TCOD_zip_put_image(data, value.get_data()); }
TCODLIB_BEGIN_IGNORE_DEPRECATIONS
[[deprecated("This function will corrupt console characters greater than 255.")]] void put(const TCODConsole& value) {
TCOD_zip_put_console(data, value.get_data());
}
[[deprecated("This function will corrupt console characters greater than 255.")]] void put(
const tcod::Console& value) {
TCOD_zip_put_console(data, value.get());
}
TCODLIB_END_IGNORE_DEPRECATIONS
void put(const TCODRandom& value) { TCOD_zip_put_random(data, value.get_data()); }
template <typename T>
T get() {
T out{};
get(out);
return out;
}
void get(char& out) { out = TCOD_zip_get_char(data); }
void get(int& out) { out = TCOD_zip_get_int(data); }
void get(float& out) { out = TCOD_zip_get_float(data); }
void get(std::optional<std::string>& out) {
out = {};
const char* string = TCOD_zip_get_string(data);
if (string) out = string;
}
void get(std::string& out) { out = get<std::optional<std::string>>().value(); }
void get(tcod::ColorRGB& out) { out = tcod::ColorRGB{TCOD_zip_get_color(data)}; }
void get(TCODColor& out) { out = TCODColor{TCOD_zip_get_color(data)}; }
void get(tcod::ImagePtr& out) { out = tcod::ImagePtr{TCOD_zip_get_image(data)}; }
void get(TCODImage& out) { out = TCODImage{get<tcod::ImagePtr>()}; }
void get(tcod::ConsolePtr& out) { out = tcod::ConsolePtr{TCOD_zip_get_console(data)}; }
void get(tcod::Console& out) { out = tcod::Console{get<tcod::ConsolePtr>()}; }
void get(TCODConsole& out) { out = TCODConsole{get<tcod::ConsolePtr>()}; }
void get(TCODRandom& out) { out = TCODRandom{TCOD_zip_get_random(data)}; }
protected:
TCOD_zip_t data{};
};
#endif // TCOD_NO_ZLIB
#endif // TCOD_ZIP_HPP_