Hi, Does anybody have example code that uses the ZLib bindings? I tried to implement the example described at http://zlib.net/zlib_how.html for inflation, but compilation failed with errors from gcc about STATUS_OK etc. symbols not being found. The following patch helped with that for some reason.
Also, I don't understand how Vala can handle memory for the next_in and next_out pointers when the inflate call is changing those pointers under its nose. Wouldn't it be more realistic not to wrap them as an array? I attached test code that works with this VAPI change (to be compiled with -X -lz). Any way to make it work without it? thanks Philipp diff --git a/vapi/zlib.vapi b/vapi/zlib.vapi index b615f6f..14cc973 100644 --- a/vapi/zlib.vapi +++ b/vapi/zlib.vapi @@ -49,16 +49,24 @@ namespace ZLib { public const int BLOCK; } - [CCode (cprefix = "Z_")] namespace Status { + [CCode (cname = "Z_OK")] public const int OK; + [CCode (cname = "Z_STREAM_END")] public const int STREAM_END; + [CCode (cname = "Z_NEED_DICT")] public const int NEED_DICT; + [CCode (cname = "Z_ERRNO")] public const int ERRNO; + [CCode (cname = "Z_STREAM_ERROR")] public const int STREAM_ERROR; + [CCode (cname = "Z_DATA_ERROR")] public const int DATA_ERROR; + [CCode (cname = "Z_MEM_ERROR")] public const int MEM_ERROR; + [CCode (cname = "Z_BUF_ERROR")] public const int BUF_ERROR; + [CCode (cname = "Z_VERSION_ERROR")] public const int VERSION_ERROR; } @@ -98,12 +106,12 @@ namespace ZLib { [CCode (cname = "z_stream", destroy_function = "deflateEnd")] public struct Stream { - [CCode (array_length_cname = "avail_in", array_length_type = "guint")] - public uchar[] next_in; + public uchar* next_in; + public uint avail_in; public ulong total_in; - [CCode (array_length_cname = "avail_out", array_length_type = "guint")] - public uchar[] next_out; + public uchar* next_out; + public uint avail_out; public ulong total_out; public string? msg;
using GLib; using ZLib; class ZLibTest : Object { // 16KB buffer size const int CHUNK = 16*1024; uchar[] buf_in; uchar[] buf_out; construct { buf_in = new uchar[CHUNK]; buf_out = new uchar[CHUNK]; } public int inflate (InputStream source, OutputStream dest) throws Error { uint have; int ret = Status.OK; var strm = InflateStream.full (15 | 32); // no way to check if this failed... // decompress until deflate stream ends or end of file do { strm.avail_in = (int) source.read (buf_in, CHUNK, null); if (strm.avail_in == 0) break; strm.next_in = buf_in; // run inflate() on input until output buffer not full do { strm.avail_out = buf_out.length; strm.next_out = buf_out; ret = strm.inflate (Flush.NONE); assert (ret != Status.STREAM_ERROR); // state not clobbered if (ret == Status.NEED_DICT) ret = Status.DATA_ERROR; // and fall through switch (ret) { case Status.DATA_ERROR: case Status.MEM_ERROR: return ret; } have = CHUNK - strm.avail_out; if (dest.write (buf_out, have, null) != have) return Status.ERRNO; } while (strm.avail_out == 0); // done when inflate () says it's done } while (ret != Status.STREAM_END); return ret == Status.STREAM_END ? Status.OK : Status.DATA_ERROR; } public static int main (string[] argv) { var zlib_test = new ZLibTest (); var infile = File.new_for_path ("test.gz"); var outfile = File.new_for_path ("test.jpg"); InputStream instream; OutputStream outstream; try { instream = infile.read (null); outstream = outfile.create (FileCreateFlags.NONE, null); return zlib_test.inflate (instream, outstream); } catch (Error e) { stderr.printf ("Error: %s\n", e.message); return -1; } } }
_______________________________________________ Vala-list mailing list Vala-list@gnome.org http://mail.gnome.org/mailman/listinfo/vala-list