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
|
#!/usr/local/bin/tclsh
# demonstration application, encodes the incoming byte stream to base64
package require Trf
# line buffer for parcel below.
set line ""
proc parcel {max cmd buffer} {
# this transformation takes the incoming byte stream and parcels it into
# lines of at most $max characters each. The last line is possibly shorter.
# only the write part is implemented.
#
# hm, this could be generalized into a transformation parceling the input
# into packets and adding some sort of frame around these.
#puts stderr "$max $cmd [string length $buffer]"
global line
switch -- $cmd {
create/write {
set line ""
}
delete/write {
set line ""
}
write {
append line $buffer
set res ""
while {[string length $line] > $max} {
append res "[string range $line 0 [expr {$max-1}]]\n"
set line [string range $line $max end]
}
return $res
}
flush/write {
if {[string length $line] > 0} {
set res "$line\n"
set line ""
return $res
} else {
return {}
}
}
clear/write {
set line ""
}
}
}
transform -attach stdout -command {parcel 72}
base64 -attach stdout -mode encode
fconfigure stdin -translation binary
fcopy stdin stdout
close stdout
# close is *required* to flush out the internal buffers.
# This is not done during a simple exit :-(
exit
|