Class TCODZip¶
Defined in File zip.hpp
Class Documentation¶
-
class TCODZip¶
This toolkit provides functions to save or read compressed data from a file. While the module is named Zip, it has nothing to do with the .zip format as it uses zlib compression (.gz format). Note that this modules has no Python wrapper. Use Python built-in zip module instead.
You can use the compression buffer in two modes: put data in the buffer, then save it to a file, load a file into the buffer, then get data from it.
Public Functions
-
TCODZip()¶
This function initializes a compression buffer.
-
~TCODZip()¶
Once you don’t need the buffer anymore, you can release resources.
Note that the addresses returned by the getString function are no longer valid once the buffer has been destroyed.
TCODZip *zip = new TCODZip(); zip->loadFromFile(“myCompressedFile.gz”); char c=zip->getChar(); int i=zip->getInt(); float f= zip->getFloat(); const char *s=strdup(zip->getString()); // we duplicate the string to be able to use it after the buffer deletion zip->getData(nbBytes, dataPtr); delete zip;
TCOD_zip_t zip=TCOD_zip_new(); TCOD_zip_load_from_file(zip,”myCompressedFile.gz”); char c=TCOD_zip_get_char(zip); int i=TCOD_zip_get_int(zip); float f=TCOD_zip_get_float(zip); const char *s=strdup(TCOD_zip_get_string(zip)); TCOD_zip_get_data(zip,nbBytes, dataPtr); TCOD_zip_delete(zip);
- Parameters
zip – In the C version, the buffer handler, returned by the constructor.
-
void putChar(char val)¶
- Parameters
zip – In the C version, the buffer handler, returned by the constructor.
val – A 8 bits value to store in the buffer
-
void putInt(int val)¶
- Parameters
zip – In the C version, the buffer handler, returned by the constructor.
val – An integer value to store in the buffer
-
void putFloat(float val)¶
- Parameters
zip – In the C version, the buffer handler, returned by the constructor.
val – A float value to store in the buffer
-
void putString(const char *val)¶
- Parameters
zip – In the C version, the buffer handler, returned by the constructor.
val – A string to store in the buffer
-
void putColor(const TCODColor *val)¶
- Parameters
zip – In the C version, the buffer handler, returned by the constructor.
val – A color to store in the buffer
-
void putImage(const TCODImage *val)¶
- Parameters
zip – In the C version, the buffer handler, returned by the constructor.
val – An image to store in the buffer
-
void putConsole(const TCODConsole *val)¶
- Parameters
zip – In the C version, the buffer handler, returned by the constructor.
val – A console to store in the buffer
-
void putRandom(const TCODRandom *val)¶
- Parameters
zip – In the C version, the buffer handler, returned by the constructor.
val – An RNG state to store in the buffer
-
void putData(int nbBytes, const void *data)¶
- Parameters
zip – In the C version, the buffer handler, returned by the constructor.
nbBytes – Number of bytes to store in the buffer
val – Address of the data to store in the buffer
-
uint32_t getCurrentBytes() const¶
- Parameters
zip – In the C version, the buffer handler, returned by the constructor.
-
int saveToFile(const char *filename)¶
Once you have finished adding data in the buffer, you can compress it and save it in a file.
The function returns the number of (uncompressed) bytes saved.
TCODZip zip; zip.putChar(‘A’); zip.putInt(1764); zip.putFloat(3.14f); zip.putString(“A string”); zip.putData(nbBytes, dataPtr); zip.saveToFile(“myCompressedFile.gz”);
TCOD_zip_t zip=TCOD_zip_new(); TCOD_zip_put_char(zip,’A’); TCOD_zip_put_int(zip,1764); TCOD_zip_put_float(zip,3.14f); TCOD_zip_put_string(zip,”A string”); TCOD_zip_put_data(zip,nbBytes, dataPtr); TCOD_zip_save_to_file(zip,”myCompressedFile.gz”);
- Parameters
zip – In the C version, the buffer handler, returned by the constructor.
filename – Name of the file
-
inline void saveToFile(const std::filesystem::path &path)¶
Save this objects buffered objects to the file at
path
.New in version 1.24.
- Parameters
path – The file to write.
-
int loadFromFile(const char *filename)¶
You can read data from a file (compressed or not) into the buffer.
The function returns the number of (uncompressed) bytes read or 0 if an error occurred.
- Parameters
zip – In the C version, the buffer handler, returned by the constructor.
filename – Name of the file
-
inline void loadFromFile(const std::filesystem::path &path)¶
Load objects from the file at
path
.New in version 1.24.
- Parameters
path – The file to read. Must exist and be valid.
- Throws
std::runtime_error – on any failure to load the file.
-
char getChar()¶
- Parameters
zip – In the C version, the buffer handler, returned by the constructor
-
int getInt()¶
- Parameters
zip – In the C version, the buffer handler, returned by the constructor.
-
float getFloat()¶
- Parameters
zip – In the C version, the buffer handler, returned by the constructor.
-
const char *getString()¶
The address returned is in the buffer.
It is valid as long as you don’t destroy the buffer.
- Parameters
zip – In the C version, the buffer handler, returned by the constructor.
-
TCODColor getColor()¶
- Parameters
zip – In the C version, the buffer handler, returned by the constructor.
-
TCODImage *getImage()¶
- Parameters
zip – In the C version, the buffer handler, returned by the constructor.
-
TCODConsole *getConsole()¶
- Parameters
zip – In the C version, the buffer handler, returned by the constructor.
-
TCODRandom *getRandom()¶
- Parameters
zip – In the C version, the buffer handler, returned by the constructor.
-
int getData(int nbBytes, void *data)¶
Note that the getData length must match the length of the data when the file was created (with putData).
The function returns the number of bytes that were stored in the file by the putData call. If more than nbBytes were stored, the function read only nbBytes and skip the rest of them.
TCODZip zip; zip.loadFromFile(“myCompressedFile.gz”); char c=zip.getChar(); int i=zip.getInt(); float f= zip.getFloat(); const char *s=zip.getString(); zip.getData(nbBytes, dataPtr);
TCOD_zip_t zip=TCOD_zip_new(); TCOD_zip_load_from_file(zip,”myCompressedFile.gz”); char c=TCOD_zip_get_char(zip); int i=TCOD_zip_get_int(zip); float f=TCOD_zip_get_float(zip); const char *s=TCOD_zip_get_string(zip); TCOD_zip_get_data(zip,nbBytes, dataPtr);
- Parameters
zip – In the C version, the buffer handler, returned by the constructor.
nbBytes – Number of bytes to read
data – Address of a pre-allocated buffer (at least nbBytes bytes)
-
uint32_t getRemainingBytes() const¶
- Parameters
zip – In the C version, the buffer handler, returned by the constructor.
-
void skipBytes(uint32_t nbBytes)¶
- Parameters
zip – In the C version, the buffer handler, returned by the constructor.
nbBytes – number of uncompressed bytes to skip
-
inline void put(char value)¶
Save a char to this zip.
New in version 1.24.
-
inline void put(int value)¶
Save an int to this zip.
New in version 1.24.
-
inline void put(float value)¶
Save a float to this zip.
New in version 1.24.
-
inline void put(const char *value)¶
Save a string to this zip.
Can be nullptr.
New in version 1.24.
-
inline void put(const std::string &value)¶
Save a string to this zip.
New in version 1.24.
-
inline void put(const std::optional<std::string> &value)¶
Save an optional string to this zip.
New in version 1.24.
- inline TCODLIB_BEGIN_IGNORE_DEPRECATIONS void put (const TCODConsole &value)
Save a console to this zip.
New in version 1.24.
- inline TCODLIB_END_IGNORE_DEPRECATIONS void put (const TCODRandom &value)
Save an RNG state to this zip.
New in version 1.24.
-
template<typename T>
inline T get()¶ Return a value of T from this zip object.
New in version 1.24.
- Template Parameters
T – A type which must match one of the
get(T)
overloads.- Returns
T
-
inline void get(char &out)¶
Extract a char to
out
.New in version 1.24.
-
inline void get(int &out)¶
Extract an int to
out
.New in version 1.24.
-
inline void get(float &out)¶
Extract a float to
out
.New in version 1.24.
-
inline void get(std::optional<std::string> &out)¶
Extract an optional string to
out
.New in version 1.24.
-
inline void get(std::string &out)¶
Extract a string to
out
.Will throw if nullptr was put.
New in version 1.24.
-
inline void get(tcod::ConsolePtr &out)¶
Extract a console pointer to
out
.New in version 1.24.
-
inline void get(TCODConsole &out)¶
Extract a console to
out
.New in version 1.24.
-
inline void get(TCODRandom &out)¶
Extract an RNG state to
out
.New in version 1.24.
Protected Attributes
-
TCOD_zip_t data = {}¶
-
TCODZip()¶