Class Reference zlib.h

zlib compression for C++ macros. More...

Member

int compressText(string src, int level)
string uncompressText(int maxOut)
z_stream z_stream
int deflateInit(z_stream *strm, int level)
int deflate(z_stream *strm, int flush)
int deflateEnd(z_stream *strm)
int inflateInit(z_stream *strm)
int inflate(z_stream *strm, int flush)
int inflateEnd(z_stream *strm)
int Z_OK
int Z_STREAM_END
int Z_NEED_DICT
int Z_ERRNO
int Z_STREAM_ERROR
int Z_DATA_ERROR
int Z_MEM_ERROR
int Z_BUF_ERROR
int Z_VERSION_ERROR
int Z_NO_FLUSH
int Z_PARTIAL_FLUSH
int Z_SYNC_FLUSH
int Z_FULL_FLUSH
int Z_FINISH
int Z_NO_COMPRESSION
int Z_BEST_SPEED
int Z_BEST_COMPRESSION
int Z_DEFAULT_COMPRESSION
int Z_DEFAULT_STRATEGY
int Z_NULL

Detailed Description

zlib compression for C++ macros. Load with #include <zlib.h> (also accepts <zconf.h>). Available from LayoutEditor 20260918.

The plugin links zlib. LayoutEditor itself is not linked against zlib for macros.

Compression means: take a block of bytes and store them in fewer bytes (a zip-like format). Decompression (inflate) restores the original bytes. Typical uses are smaller files, smaller network payloads, or packing a large string.

For text in a macro, use the helpers compressText and uncompressText. They keep the compressed bytes inside the plugin. The raw stream fields z_stream.next_in / next_out cannot hold a LayoutEditor string pointer across calls (the host string buffer is cleared after each invoke).

The stream functions (deflate / inflate) are the native zlib API. They are for advanced macros that already manage memory buffers. Beginners should stay with the helpers.

Example

#include <zlib.h>

int main(){
    string src = "";
    int i;
    for (i = 0; i < 24; ++i)
        src = src + "The LayoutEditor macro language can call native zlib. ";

    int inSize = src.length();
    int compressedSize = compressText(src, Z_DEFAULT_COMPRESSION);
    if (compressedSize < 0){
        cout("compressText failed\n");
        return 1;
    }
    string restored = uncompressText(inSize);
    cout("input: ", inSize, "  compressed: ", compressedSize, "\n");
    if (restored == src)
        cout("round-trip: ok\n");
}

Python: import zlib. This include is for C++ macros only.

Member Function Documentation


int zlib.h::compressText(string src, int level)

Compresses the UTF-8 bytes of src and stores the result in the plugin. The next uncompressText call reads that stored buffer.

Parameters:

  • src (string) — the original text (or any byte string).
  • level (int) — how hard to squeeze:
    • Z_DEFAULT_COMPRESSION — usual choice (−1, zlib picks a default)
    • Z_NO_COMPRESSION (0) — store without compressing
    • Z_BEST_SPEED (1) — fastest, larger result
    • Z_BEST_COMPRESSION (9) — smallest result, slower

Returns: int — size of the compressed data in bytes, or −1 on error. A smaller number than src.length() means compression helped (repetitive text compresses well).

Call uncompressText afterwards in the same macro run. The stored blob is per thread; a second compressText overwrites it.


string zlib.h::uncompressText(int maxOut)

Inflates the last compressText result back into a string.

Parameters:

  • maxOut (int) — maximum number of bytes in the restored text. Pass at least the original length (src.length()). If you pass less than 1, 1 is used. If the buffer is too small, inflation fails and you get an empty string.

Returns: string — the restored text. Empty string on error (wrong data, maxOut too small, or no previous compressText).


z_stream zlib.h::z_stream

A z_stream is a structure zlib uses while it compresses or inflates in chunks. You declare z_stream strm;, call deflateInit / inflateInit, then deflate / inflate until the data is finished, then deflateEnd / inflateEnd.

Because next_in / next_out are raw pointers, they must point at memory that still exists for the whole sequence of calls. A temporary LayoutEditor string does not stay valid. Prefer compressText unless you know how to keep a buffer alive.

Declare z_stream s;. Fields you can read and write:

Field Meaning
next_in pointer to the next input byte
avail_in how many input bytes remain
next_out pointer to the next output byte
avail_out how many output bytes still fit
zalloc allocator (usually leave as 0 / Z_NULL)
zfree free function (usually Z_NULL)
opaque extra pointer for the allocator (usually Z_NULL)

int zlib.h::deflateInit(z_stream *strm, int level)

Prepares strm for compression. Call this once before deflate.

Parameters:

  • strm (z_stream *) — address of your stream (&s).
  • level (int) — same values as compressText (Z_DEFAULT_COMPRESSION, 0 … 9).

Returns: Z_OK on success, or a negative error code (Z_MEM_ERROR, Z_STREAM_ERROR, …).


int zlib.h::deflate(z_stream *strm, int flush)

Compresses more input into the output buffer. Update next_in / avail_in / next_out / avail_out before each call.

Parameters:

  • strm (z_stream *).
  • flush (int) — usually Z_NO_FLUSH while more input remains, and Z_FINISH when all input has been given.

Returns: Z_OK if more work remains, Z_STREAM_END when everything is flushed, or a negative error code.


int zlib.h::deflateEnd(z_stream *strm)

Frees zlib’s internal state for this stream. Always call this after deflateInit, even on error.

Parameters:

  • strm (z_stream *).

Returns: Z_OK on success, or an error code.


int zlib.h::inflateInit(z_stream *strm)

Prepares strm for decompression.

Parameters:

  • strm (z_stream *) — &s.

Returns: Z_OK on success, or a negative error code.


int zlib.h::inflate(z_stream *strm, int flush)

Decompresses more input into the output buffer.

Parameters:

  • strm (z_stream *).
  • flush (int) — typically Z_NO_FLUSH or Z_FINISH.

Returns: Z_OK, Z_STREAM_END when the compressed stream is complete, or a negative error code (Z_DATA_ERROR if the bytes are not valid zlib data).


int zlib.h::inflateEnd(z_stream *strm)

Frees zlib’s internal inflate state. Always pair with inflateInit.

Parameters:

  • strm (z_stream *).

Returns: Z_OK on success, or an error code.


int zlib.h::Z_OK

Returns: the named constant. success; more work may remain


int zlib.h::Z_STREAM_END

Returns: the named constant. the stream is finished


int zlib.h::Z_NEED_DICT

Returns: the named constant. a preset dictionary is required (rare)


int zlib.h::Z_ERRNO

Returns: the named constant. file / system error


int zlib.h::Z_STREAM_ERROR

Returns: the named constant. the z_stream was used incorrectly


int zlib.h::Z_DATA_ERROR

Returns: the named constant. the compressed data is damaged or not zlib


int zlib.h::Z_MEM_ERROR

Returns: the named constant. not enough memory


int zlib.h::Z_BUF_ERROR

Returns: the named constant. the output buffer is full (not always fatal)


int zlib.h::Z_VERSION_ERROR

Returns: the named constant. zlib version mismatch


int zlib.h::Z_NO_FLUSH

Returns: the named constant. compress as much as convenient


int zlib.h::Z_PARTIAL_FLUSH

Returns: the named constant. flush some output


int zlib.h::Z_SYNC_FLUSH

Returns: the named constant. flush so a decompressor can catch up


int zlib.h::Z_FULL_FLUSH

Returns: the named constant. reset compression state (larger output)


int zlib.h::Z_FINISH

Returns: the named constant. no more input; emit everything


int zlib.h::Z_NO_COMPRESSION

Returns: the named constant. 0 — store only


int zlib.h::Z_BEST_SPEED

Returns: the named constant. 1


int zlib.h::Z_BEST_COMPRESSION

Returns: the named constant. 9


int zlib.h::Z_DEFAULT_COMPRESSION

Returns: the named constant. default level


int zlib.h::Z_DEFAULT_STRATEGY

Returns: the named constant. default algorithm strategy


int zlib.h::Z_NULL

Returns: the named constant. empty pointer (0)