Hi Philipp
I tested your patch and it works very fine for me.
I also attached a deflate example based on the code of your inflate
example to this mail. The example takes a dummyfile.txt file and outputs
in gzip file format.
It is compiled with
valac --pkg gio-2.0 --pkg zlib -X -lz zlibtest_deflate.vala
Maybe somebody with a gnome git account can merge the patch into vala
repository?
Regards
Jörn
Am Sonntag, den 04.10.2009, 14:25 +0200 schrieb Philipp Zabel:
> 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;
>
> _______________________________________________
> Vala-list mailing list
> [email protected]
> http://mail.gnome.org/mailman/listinfo/vala-list
diff --git a/vapi/zlib.vapi b/vapi/zlib.vapi
index b615f6f..25a2a7d 100644
--- a/vapi/zlib.vapi
+++ b/vapi/zlib.vapi
@@ -45,20 +45,29 @@ namespace ZLib {
public const int SYNC;
[CCode (cname = "Z_FULL_FLUSH")]
public const int FULL;
+ [CCode (cname = "Z_FINISH")]
public const int FINISH;
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;
}
@@ -93,17 +102,22 @@ namespace ZLib {
[CCode (cprefix = "Z_")]
namespace Algorithm {
+ [CCode (cname = "Z_DEFLATED")]
public const int DEFLATED;
}
[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;
public int deflate (InputStream source, OutputStream dest) {
uchar[] buf_in;
uchar[] buf_out;
buf_in = new uchar[CHUNK];
buf_out = new uchar[CHUNK];
uint have;
int flush = Flush.NONE;
int ret = Status.OK;
var strm = DeflateStream.full(Compression.BEST_COMPRESSION,
Algorithm.DEFLATED,
(15 + 16), // + 16 for gzip output format
8,
Strategy.DEFAULT
);
// compress until deflate stream ends or end of file
do {
strm.avail_in = (int) source.read (buf_in, CHUNK, null);
if(strm.avail_in == 0) {
flush = Flush.FINISH;
}
else {
flush = Flush.NONE;
}
strm.next_in = buf_in;
// run deflate() on input until output buffer not full
do {
strm.avail_out = buf_out.length;
strm.next_out = buf_out;
ret = strm.deflate (flush);
assert (ret != Status.STREAM_ERROR); // state not clobbered
have = CHUNK - strm.avail_out;
//print("%u\n", have);
if (dest.write (buf_out, have, null) != have) {
print("write error\n");
return Status.ERRNO;
}
} while (strm.avail_out == 0);
assert (strm.avail_in == 0);
} while (flush != Flush.FINISH);
if(ret == Status.STREAM_END)
print("Deflate: Status.OK\n");
else
print("Deflate: Status.DATA_ERROR\n");
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 ("dummyfile.txt");
var outfile = File.new_for_path ("dummyfile.txt.gz");
InputStream instream;
OutputStream outstream;
try {
instream = infile.read (null);
if(outfile.query_exists(null)) outfile.delete(null);
outstream = outfile.create (FileCreateFlags.NONE, null);
zlib_test.deflate (instream, outstream);
} catch (Error e) {
stderr.printf ("Error: %s\n", e.message);
return -1;
}
return 0;
}
}
_______________________________________________
Vala-list mailing list
[email protected]
http://mail.gnome.org/mailman/listinfo/vala-list