if {[catch {package require Tcl 8.1}]} {
    # We need the unicode support for these.
    return
}

# Read a file using gets
proc read-gets {enc filename} {
    set fp [open $filename r]
    fconfigure $fp -encoding $enc
    while {[gets $fp line] != -1} {}
    close $fp
    return $line ; # just last line
}

# Read a file using read only
proc read-read {enc filename} {
    set fp [open $filename r]
    fconfigure $fp -encoding $enc
    set data [read -nonewline $fp]
    close $fp
    return $data
}

# Read a file using read and [file size]
proc read-read-size {enc filename} {
    set fp [open $filename r]
    fconfigure $fp -encoding $enc
    set data [read $fp [file size $filename]]
    close $fp
    return $data
}

set ::iso2022data "\u001b\$B;d\$I\$b\$G\$O!\"%A%C%W\$49XF~;~\$K\$4EPO?\$\$\$?\$@\$\$\$?\$4=;=j\$r%-%c%C%7%e%\"%&%H\$N:]\$N\u001b(B
\u001b\$B>.@Z<jAwIU@h\$H\$7\$F;HMQ\$7\$F\$*\$j\$^\$9!#62\$lF~\$j\$^\$9\$,!\"@5\$7\$\$=;=j\$r\$4EPO?\$7\$J\$*\u001b(B
\u001b\$B\$7\$F\$\$\$?\$@\$1\$J\$\$\$G\$7\$g\$&\$+!#!J%W%l!<%d!<\$NJ}!9\$N0BA4\$r?^\$k\$?\$a!\"\$3\$A\$i\$G\$O!\"\u001b(B
\u001b\$B%W%l!<%d!<=;=j\$NJQ99\$O=PMh\$J\$\$\$3\$H\$K\$J\$C\$F\$*\$j\$^\$9!#!K@5\$7\$\$=;=j\$O!\"3N<B\$K\$\"\u001b(B
\u001b\$B\$J\$?MM\$N\$*<j85\$K>.@Z<j\$r\$*FO\$1\$9\$k\$?\$a\$KIT2D7g\$G\$9\$N\$G!\"\$46(NO\$N\$\[\$I\$h\$m\$7\$/\u001b(B
\u001b\$B\$*4j\$\$\$\$\$?\$7\$^\$9!#\$^\$?!\"BgJQ62=L\$G\$9\$,!\"=;=jJQ99\$N\$\"\$H!\"F|K\\8l%5!<%S%9It!J\u001b(B
casino_japanese@___.com \u001b\$B!K\$^\$G\$4=;=jJQ99:Q\$NO\"Mm\$r\$\$\$?\$@\$1\$J\$\$\$G\u001b(B
\u001b\$B\$7\$g\$&\$+!)\u001b(B"

set ::RES(iso2022-jp) "\u79c1\u3069\u3082\u3067\u306f\u3001\u30c1\u30c3\u30d7\u3054\u8cfc\u5165\u6642\u306b\u3054\u767b\u9332\u3044\u305f\u3060\u3044\u305f\u3054\u4f4f\u6240\u3092\u30ad\u30e3\u30c3\u30b7\u30e5\u30a2\u30a6\u30c8\u306e\u969b\u306e
\u5c0f\u5207\u624b\u9001\u4ed8\u5148\u3068\u3057\u3066\u4f7f\u7528\u3057\u3066\u304a\u308a\u307e\u3059\u3002\u6050\u308c\u5165\u308a\u307e\u3059\u304c\u3001\u6b63\u3057\u3044\u4f4f\u6240\u3092\u3054\u767b\u9332\u3057\u306a\u304a
\u3057\u3066\u3044\u305f\u3060\u3051\u306a\u3044\u3067\u3057\u3087\u3046\u304b\u3002\uff08\u30d7\u30ec\u30fc\u30e4\u30fc\u306e\u65b9\u3005\u306e\u5b89\u5168\u3092\u56f3\u308b\u305f\u3081\u3001\u3053\u3061\u3089\u3067\u306f\u3001
\u30d7\u30ec\u30fc\u30e4\u30fc\u4f4f\u6240\u306e\u5909\u66f4\u306f\u51fa\u6765\u306a\u3044\u3053\u3068\u306b\u306a\u3063\u3066\u304a\u308a\u307e\u3059\u3002\uff09\u6b63\u3057\u3044\u4f4f\u6240\u306f\u3001\u78ba\u5b9f\u306b\u3042
\u306a\u305f\u69d8\u306e\u304a\u624b\u5143\u306b\u5c0f\u5207\u624b\u3092\u304a\u5c4a\u3051\u3059\u308b\u305f\u3081\u306b\u4e0d\u53ef\u6b20\u3067\u3059\u306e\u3067\u3001\u3054\u5354\u529b\u306e\u307b\u3069\u3088\u308d\u3057\u304f
\u304a\u9858\u3044\u3044\u305f\u3057\u307e\u3059\u3002\u307e\u305f\u3001\u5927\u5909\u6050\u7e2e\u3067\u3059\u304c\u3001\u4f4f\u6240\u5909\u66f4\u306e\u3042\u3068\u3001\u65e5\u672c\u8a9e\u30b5\u30fc\u30d3\u30b9\u90e8\uff08
casino_japanese@___.com \uff09\u307e\u3067\u3054\u4f4f\u6240\u5909\u66f4\u6e08\u306e\u9023\u7d61\u3092\u3044\u305f\u3060\u3051\u306a\u3044\u3067
\u3057\u3087\u3046\u304b\uff1f"

set ::RES(iso8859-2) [encoding convertto iso8859-2 $iso2022data]

set file [bench_tmpfile]
set fid [open $file w]
fconfigure $fid -encoding binary
puts -nonewline $fid $::iso2022data
close $fid

set iters 30
foreach enc {iso2022-jp iso8859-2} {
    bench -iter $iters -desc "ENC $enc, gets" \
	    -body {read-gets $enc $file}
    bench -iter $iters -desc "ENC $enc, read" \
	    -body {read-read $enc $file} -result $::RES($enc)
    bench -iter $iters -desc "ENC $enc, read & size" \
	    -body {read-read-size $enc $file} -result $::RES($enc)
}

bench_rm $file
