summaryrefslogtreecommitdiffstats
path: root/tcllib/modules/virtchannel_transform/zlib.tcl
blob: d7c244d25b0bb6b212d61eca0616f1674eaee809 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# -*- tcl -*-
# # ## ### ##### ######## #############
# (C) 2009 Andreas Kupries

# @@ Meta Begin
# Package tcl::transform::zlib 1.0.1
# Meta as::author {Andreas Kupries}
# Meta as::copyright 2009
# Meta as::license BSD
# Meta as::notes   Possibilities for extension: Currently
# Meta as::notes   the mapping between read/write and
# Meta as::notes   de/compression is fixed. Allow it to be
# Meta as::notes   configured at construction time.
# Meta description Implementation of a zlib (de)compressor.
# Meta description Based on Tcl 8.6's transformation
# Meta description reflection support (TIP 230) and zlib
# Meta description support (TIP 234). Compresses on write.
# Meta description Exports a single command adding a new
# Meta description transformation of this type to a channel.
# Meta description Two arguments, the channel to extend,
# Meta description and the compression level. No result.
# Meta platform tcl
# Meta require tcl::transform::core
# Meta require {Tcl 8.6}
# @@ Meta End

# # ## ### ##### ######## #############

package require Tcl 8.6
package require tcl::transform::core

# # ## ### ##### ######## #############

namespace eval ::tcl::transform {}

proc ::tcl::transform::zlib {chan {level 4}} {
    ::chan push $chan [zlib::implementation new $level]
    return
}

oo::class create ::tcl::transform::zlib::implementation {
    superclass tcl::transform::core ;# -> initialize, finalize, destructor

    # This transformation is intended for streaming operation. Seeking
    # the channel while it is active may cause undesirable
    # output. Proper behaviour may require the destruction of the
    # transform before seeking.

    method initialize {c mode} {
	set compressor   [zlib stream deflate -level $level]
	set decompressor [zlib stream inflate]

	next $c $mode
    }

    method finalize {c} {
	$compressor   close
	$decompressor close

	next $c
    }

    method write {c data} {
	$compressor put $data
	return [$compressor get]
    }

    method read {c data} {
	$decompressor put $data
	return [$decompressor get]
    }

    method flush {c} {
	$compressor flush
	return [$compressor get]
    }

    method drain {c} {
	$decompressor flush
	return [$decompressor get]
    }

    # # ## ### ##### ######## #############

    constructor {thelevel} {
	# Should validate input (level in (0 ...9))
	set level $thelevel
	return
    }

    # # ## ### ##### ######## #############

    variable level compressor decompressor

    # # ## ### ##### ######## #############
}

# # ## ### ##### ######## #############
package provide tcl::transform::zlib 1.0.1
return