Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/tcl8.5.2/doc/fcopy.n @ 25

Last change on this file since 25 was 25, checked in by landauf, 16 years ago

added tcl to libs

File size: 5.6 KB
Line 
1'\"
2'\" Copyright (c) 1993 The Regents of the University of California.
3'\" Copyright (c) 1994-1997 Sun Microsystems, Inc.
4'\"
5'\" See the file "license.terms" for information on usage and redistribution
6'\" of this file, and for a DISCLAIMER OF ALL WARRANTIES.
7'\"
8'\" RCS: @(#) $Id: fcopy.n,v 1.17 2007/12/13 15:22:32 dgp Exp $
9'\"
10.so man.macros
11.TH fcopy n 8.0 Tcl "Tcl Built-In Commands"
12.BS
13'\" Note:  do not modify the .SH NAME line immediately below!
14.SH NAME
15fcopy \- Copy data from one channel to another
16.SH SYNOPSIS
17\fBfcopy \fIinchan\fR \fIoutchan\fR ?\fB\-size \fIsize\fR? ?\fB\-command \fIcallback\fR?
18.BE
19
20.SH DESCRIPTION
21.PP
22The \fBfcopy\fR command copies data from one I/O channel, \fIinchan\fR to another I/O channel, \fIoutchan\fR.
23The \fBfcopy\fR command leverages the buffering in the Tcl I/O system to
24avoid extra copies and to avoid buffering too much data in
25main memory when copying large files to slow destinations like
26network sockets.
27.PP
28The \fBfcopy\fR
29command transfers data from \fIinchan\fR until end of file
30or \fIsize\fR bytes have been
31transferred. If no \fB\-size\fR argument is given,
32then the copy goes until end of file.
33All the data read from \fIinchan\fR is copied to \fIoutchan\fR.
34Without the \fB\-command\fR option, \fBfcopy\fR blocks until the copy is complete
35and returns the number of bytes written to \fIoutchan\fR.
36.PP
37The \fB\-command\fR argument makes \fBfcopy\fR work in the background.
38In this case it returns immediately and the \fIcallback\fR is invoked
39later when the copy completes.
40The \fIcallback\fR is called with
41one or two additional
42arguments that indicates how many bytes were written to \fIoutchan\fR.
43If an error occurred during the background copy, the second argument is the
44error string associated with the error.
45With a background copy,
46it is not necessary to put \fIinchan\fR or \fIoutchan\fR into
47non-blocking mode; the \fBfcopy\fR command takes care of that automatically.
48However, it is necessary to enter the event loop by using
49the \fBvwait\fR command or by using Tk.
50.PP
51You are not allowed to do other I/O operations with
52\fIinchan\fR or \fIoutchan\fR during a background \fBfcopy\fR.
53If either \fIinchan\fR or \fIoutchan\fR get closed
54while the copy is in progress, the current copy is stopped
55and the command callback is \fInot\fR made.
56If \fIinchan\fR is closed,
57then all data already queued for \fIoutchan\fR is written out.
58.PP
59Note that \fIinchan\fR can become readable during a background copy.
60You should turn off any \fBfileevent\fR handlers during a background
61copy so those handlers do not interfere with the copy.
62Any I/O attempted by a \fBfileevent\fR handler will get a
63.QW "channel busy"
64error.
65.PP
66\fBFcopy\fR translates end-of-line sequences in \fIinchan\fR and \fIoutchan\fR
67according to the \fB\-translation\fR option
68for these channels.
69See the manual entry for \fBfconfigure\fR for details on the
70\fB\-translation\fR option.
71The translations mean that the number of bytes read from \fIinchan\fR
72can be different than the number of bytes written to \fIoutchan\fR.
73Only the number of bytes written to \fIoutchan\fR is reported,
74either as the return value of a synchronous \fBfcopy\fR or
75as the argument to the callback for an asynchronous \fBfcopy\fR.
76.PP
77\fBFcopy\fR obeys the encodings and character translations configured
78for the channels. This
79means that the incoming characters are converted internally first
80UTF-8 and then into the encoding of the channel \fBfcopy\fR writes
81to. See the manual entry for \fBfconfigure\fR for details on the
82\fB\-encoding\fR and \fB\-translation\fR options. No conversion is
83done if both channels are
84set to encoding
85.QW binary
86and have matching translations. If only the output channel is set to encoding
87.QW binary
88the system will write the internal UTF-8 representation of the incoming
89characters. If only the input channel is set to encoding
90.QW binary
91the system will assume that the incoming
92bytes are valid UTF-8 characters and convert them according to the
93output encoding. The behaviour of the system for bytes which are not
94valid UTF-8 characters is undefined in this case.
95
96.SH EXAMPLES
97.PP
98The first example transfers the contents of one channel exactly to
99another. Note that when copying one file to another, it is better to
100use \fBfile copy\fR which also copies file metadata (e.g. the file
101access permissions) where possible.
102.CS
103fconfigure $in -translation binary
104fconfigure $out -translation binary
105\fBfcopy\fR $in $out
106.CE
107.PP
108This second example shows how the callback gets
109passed the number of bytes transferred.
110It also uses vwait to put the application into the event loop.
111Of course, this simplified example could be done without the command
112callback.
113.CS
114proc Cleanup {in out bytes {error {}}} {
115    global total
116    set total $bytes
117    close $in
118    close $out
119    if {[string length $error] != 0} {
120        # error occurred during the copy
121    }
122}
123set in [open $file1]
124set out [socket $server $port]
125\fBfcopy\fR $in $out -command [list Cleanup $in $out]
126vwait total
127.CE
128.PP
129The third example copies in chunks and tests for end of file
130in the command callback
131.CS
132proc CopyMore {in out chunk bytes {error {}}} {
133    global total done
134    incr total $bytes
135    if {([string length $error] != 0) || [eof $in]} {
136        set done $total
137        close $in
138        close $out
139    } else {
140        \fBfcopy\fR $in $out -size $chunk \e
141                -command [list CopyMore $in $out $chunk]
142    }
143}
144set in [open $file1]
145set out [socket $server $port]
146set chunk 1024
147set total 0
148\fBfcopy\fR $in $out -size $chunk \e
149        -command [list CopyMore $in $out $chunk]
150vwait done
151.CE
152
153.SH "SEE ALSO"
154eof(n), fblocked(n), fconfigure(n), file(n)
155
156.SH KEYWORDS
157blocking, channel, end of line, end of file, nonblocking, read, translation
Note: See TracBrowser for help on using the repository browser.