scan.n
上传用户:rrhhcc
上传日期:2015-12-11
资源大小:54129k
文件大小:10k
- '"
- '" Copyright (c) 1993 The Regents of the University of California.
- '" Copyright (c) 1994-1996 Sun Microsystems, Inc.
- '" Copyright (c) 2000 Scriptics Corporation.
- '"
- '" See the file "license.terms" for information on usage and redistribution
- '" of this file, and for a DISCLAIMER OF ALL WARRANTIES.
- '"
- '" RCS: @(#) $Id: scan.n,v 1.9.2.1 2004/10/27 14:23:58 dkf Exp $
- '"
- .so man.macros
- .TH scan n 8.4 Tcl "Tcl Built-In Commands"
- .BS
- '" Note: do not modify the .SH NAME line immediately below!
- .SH NAME
- scan - Parse string using conversion specifiers in the style of sscanf
- .SH SYNOPSIS
- fBscan fIstring format fR?fIvarName varName ...fR?
- .BE
- .SH INTRODUCTION
- .PP
- This command parses fields from an input string in the same fashion as the
- ANSI C fBsscanffR procedure and returns a count of the number of
- conversions performed, or -1 if the end of the input string is reached
- before any conversions have been performed. fIStringfR gives the input
- to be parsed and fIformatfR indicates how to parse it, using fB%fR
- conversion specifiers as in fBsscanffR. Each fIvarNamefR gives the
- name of a variable; when a field is scanned from fIstringfR the result is
- converted back into a string and assigned to the corresponding variable.
- If no fIvarNamefR variables are specified, then fBscanfR works in an
- inline manner, returning the data that would otherwise be stored in the
- variables as a list. In the inline case, an empty string is returned when
- the end of the input string is reached before any conversions have been
- performed.
- .SH "DETAILS ON SCANNING"
- .PP
- fBScanfR operates by scanning fIstringfR and fIformatfR together.
- If the next character in fIformatfR is a blank or tab then it
- matches any number of white space characters in fIstringfR (including
- zero).
- Otherwise, if it isn't a fB%fR character then it
- must match the next character of fIstringfR.
- When a fB%fR is encountered in fIformatfR, it indicates
- the start of a conversion specifier.
- .VS 8.4
- A conversion specifier contains up to four fields after the fB%fR:
- a fB*fR, which indicates that the converted value is to be discarded
- instead of assigned to a variable; a XPG3 position specifier; a number
- indicating a maximum field width; a field size modifier; and a
- conversion character.
- .VE 8.4
- All of these fields are optional except for the conversion character.
- The fields that are present must appear in the order given above.
- .PP
- When fBscanfR finds a conversion specifier in fIformatfR, it
- first skips any white-space characters in fIstringfR (unless the
- specifier is fB[fR or fBcfR).
- Then it converts the next input characters according to the
- conversion specifier and stores the result in the variable given
- by the next argument to fBscanfR.
- .PP
- If the fB%fR is followed by a decimal number and a fB$fR, as in
- ``fB%2$dfR'', then the variable to use is not taken from the next
- sequential argument. Instead, it is taken from the argument indicated
- by the number, where 1 corresponds to the first fIvarNamefR. If
- there are any positional specifiers in fIformatfR then all of the
- specifiers must be positional. Every fIvarNamefR on the argument
- list must correspond to exactly one conversion specifier or an error
- is generated, or in the inline case, any position can be specified
- at most once and the empty positions will be filled in with empty strings.
- .PP
- The following conversion characters are supported:
- .TP 10
- fBdfR
- The input field must be a decimal integer.
- It is read in and the value is stored in the variable as a decimal string.
- .VS 8.4
- If the fBlfR or fBLfR field size modifier is given, the scanned
- value will have an internal representation that is at least 64-bits in
- size.
- .VE 8.4
- .TP 10
- fBofR
- The input field must be an octal integer. It is read in and the
- value is stored in the variable as a decimal string.
- .VS 8.4
- If the fBlfR or fBLfR field size modifier is given, the scanned
- value will have an internal representation that is at least 64-bits in
- size.
- If the value exceeds MAX_INT (017777777777 on platforms using 32-bit
- integers when the fBlfR and fBLfR modifiers are not given), it
- will be truncated to a signed integer. Hence, 037777777777 will
- appear as -1 on a 32-bit machine by default.
- .VE 8.4
- .TP 10
- fBxfR
- The input field must be a hexadecimal integer. It is read in
- and the value is stored in the variable as a decimal string.
- .VS 8.4
- If the fBlfR or fBLfR field size modifier is given, the scanned
- value will have an internal representation that is at least 64-bits in
- size.
- If the value exceeds MAX_INT (0x7FFFFFFF on platforms using 32-bit
- integers when the fBlfR and fBLfR modifiers are not given), it
- will be truncated to a signed integer. Hence, 0xFFFFFFFF will appear
- as -1 on a 32-bit machine.
- .VE 8.4
- .TP 10
- fBufR
- The input field must be a decimal integer. The value is stored in the
- variable as an unsigned decimal integer string.
- .VS 8.4
- If the fBlfR or fBLfR field size modifier is given, the scanned
- value will have an internal representation that is at least 64-bits in
- size.
- .VE 8.4
- .TP 10
- fBifR
- The input field must be an integer. The base (i.e. decimal, octal, or
- hexadecimal) is determined in the same fashion as described in
- fBexprfR. The value is stored in the variable as a decimal string.
- .VS 8.4
- If the fBlfR or fBLfR field size modifier is given, the scanned
- value will have an internal representation that is at least 64-bits in
- size.
- .VE 8.4
- .TP 10
- fBcfR
- A single character is read in and its binary value is stored in
- the variable as a decimal string.
- Initial white space is not skipped in this case, so the input
- field may be a white-space character.
- This conversion is different from the ANSI standard in that the
- input field always consists of a single character and no field
- width may be specified.
- .TP 10
- fBsfR
- The input field consists of all the characters up to the next
- white-space character; the characters are copied to the variable.
- .TP 10
- fBefR or fBffR or fBgfR
- The input field must be a floating-point number consisting
- of an optional sign, a string of decimal digits possibly
- containing a decimal point, and an optional exponent consisting
- of an fBefR or fBEfR followed by an optional sign and a string of
- decimal digits.
- It is read in and stored in the variable as a floating-point string.
- .TP 10
- fB[fIcharsfB]fR
- The input field consists of any number of characters in
- fIcharsfR.
- The matching string is stored in the variable.
- If the first character between the brackets is a fB]fR then
- it is treated as part of fIcharsfR rather than the closing
- bracket for the set.
- If fIcharsfR
- contains a sequence of the form fIafB-fIbfR then any
- character between fIafR and fIbfR (inclusive) will match.
- If the first or last character between the brackets is a fB-fR, then
- it is treated as part of fIcharsfR rather than indicating a range.
- .TP 10
- fB[^fIcharsfB]fR
- The input field consists of any number of characters not in
- fIcharsfR.
- The matching string is stored in the variable.
- If the character immediately following the fB^fR is a fB]fR then it is
- treated as part of the set rather than the closing bracket for
- the set.
- If fIcharsfR
- contains a sequence of the form fIafB-fIbfR then any
- character between fIafR and fIbfR (inclusive) will be excluded
- from the set.
- If the first or last character between the brackets is a fB-fR, then
- it is treated as part of fIcharsfR rather than indicating a range.
- .TP 10
- fBnfR
- No input is consumed from the input string. Instead, the total number
- of characters scanned from the input string so far is stored in the variable.
- .LP
- The number of characters read from the input for a conversion is the
- largest number that makes sense for that particular conversion (e.g.
- as many decimal digits as possible for fB%dfR, as
- many octal digits as possible for fB%ofR, and so on).
- The input field for a given conversion terminates either when a
- white-space character is encountered or when the maximum field
- width has been reached, whichever comes first.
- If a fB*fR is present in the conversion specifier
- then no variable is assigned and the next scan argument is not consumed.
- .SH "DIFFERENCES FROM ANSI SSCANF"
- .PP
- The behavior of the fBscanfR command is the same as the behavior of
- the ANSI C fBsscanffR procedure except for the following differences:
- .IP [1]
- fB%pfR conversion specifier is not currently supported.
- .IP [2]
- For fB%cfR conversions a single character value is
- converted to a decimal string, which is then assigned to the
- corresponding fIvarNamefR;
- no field width may be specified for this conversion.
- .IP [3]
- .VS 8.4
- The fBhfR modifier is always ignored and the fBlfR and fBLfR
- modifiers are ignored when converting real values (i.e. type
- fBdoublefR is used for the internal representation).
- .VE 8.4
- .IP [4]
- If the end of the input string is reached before any conversions have been
- performed and no variables are given, an empty string is returned.
- .SH EXAMPLES
- Parse a simple color specification of the form fI#RRGGBBfR using
- hexadecimal conversions with field sizes:
- .CS
- set string "#08D03F"
- fBscanfR $string "#%2x%2x%2x" r g b
- .CE
- .PP
- Parse a fIHH:MMfR time string, noting that this avoids problems with
- octal numbers by forcing interpretation as decimals (if we did not
- care, we would use the fB%ifR conversion instead):
- .CS
- set string "08:08" ;# *Not* octal!
- if {[fBscanfR $string "%d:%d" hours minutes] != 2} {
- error "not a valid time string"
- }
- # We have to understand numeric ranges ourselves...
- if {$minutes < 0 || $minutes > 59} {
- error "invalid number of minutes"
- }
- .CE
- .PP
- Break a string up into sequences of non-whitespace characters (note
- the use of the fB%nfR conversion so that we get skipping over
- leading whitespace correct):
- .CS
- set string " a string {with braced words} + leading space "
- set words {}
- while {[fBscanfR $string %s%n word length] == 2} {
- lappend words $word
- set string [string range $string $length end]
- }
- .CE
- .PP
- Parse a simple coordinate string, checking that it is complete by
- looking for the terminating character explicitly:
- .CS
- set string "(5.2,-4e-2)"
- # Note that the spaces before the literal parts of
- # the scan pattern are significant, and that ")" is
- # the Unicode character \u0029
- if {
- [fBscanfR $string " (%f ,%f %c" x y last] != 3
- || $last != 0x0029
- } then {
- error "invalid coordinate string"
- }
- puts "X=$x, Y=$y"
- .CE
- .SH "SEE ALSO"
- format(n), sscanf(3)
- .SH KEYWORDS
- conversion specifier, parse, scan