1# -*- tcl -*-
2# # ## ### ##### ######## #############
3# (C) 2009 Andreas Kupries
4
5# @@ Meta Begin
6# Package tcl::transform::limitsize 1
7# Meta as::author {Andreas Kupries}
8# Meta as::copyright 2009
9# Meta as::license BSD
10# Meta as::notes   Possibilities for extension: Trigger the
11# Meta as::notes   EOF when finding specific patterns in
12# Meta as::notes   the input. Trigger the EOF based on some
13# Meta as::notes   external signal routed into the limiter.
14# Meta as::notes   Make the limit reconfigurable.
15# Meta description Implementation of a transformation
16# Meta description limiting the number of bytes read
17# Meta description from its channel. An observer instead of
18# Meta description a transformation, forcing an artificial
19# Meta description EOF marker. Based on Tcl 8.6's
20# Meta description transformation reflection support.
21# Meta description Exports a single command adding a new
22# Meta description transform of this type to a channel. One
23# Meta description argument, the channel to extend, and the
24# Meta description number of bytes to allowed to be read.
25# Meta description No result.
26# Meta platform tcl
27# Meta require tcl::transform::core
28# Meta require {Tcl 8.6}
29# @@ Meta End
30
31# This may help with things like zlib compression of messages. Have
32# the message format a length at the front, followed by a payload of
33# that size. Now we may compress messages. On the read side we can use
34# the limiter to EOF on a message, then reset the limit for the
35# next. This is a half-baked idea.
36
37# # ## ### ##### ######## #############
38
39package require Tcl 8.6
40package require tcl::transform::core
41
42# # ## ### ##### ######## #############
43
44namespace eval ::tcl::transform {}
45
46proc ::tcl::transform::limitsize {chan max} {
47    ::chan push $chan [limitsize::implementation new $max]
48}
49
50oo::class create ::tcl::transform::limitsize::implementation {
51    superclass tcl::transform::core ;# -> initialize, finalize, destructor
52
53    method write {c data} {
54	return $data
55    }
56
57    method read {c data} {
58	# Reduce the limit of bytes allowed in the future according to
59	# the number of bytes we have seen already.
60
61	if {$max > 0} {
62	    incr max -[string length $data]
63	    if {$max < 0} {
64		set max 0
65	    }
66	}
67	return $data
68    }
69
70    method limit? {c} {
71	return $max
72    }
73
74    # # ## ### ##### ######## #############
75
76    constructor {themax} {
77	set max $themax
78	return
79    }
80
81    variable max
82
83    # # ## ### ##### ######## #############
84}
85
86# # ## ### ##### ######## #############
87package provide tcl::transform::limitsize 1
88return
89