1#
2# forfile.tcl --
3#
4# Proc to execute code on every line of a file.
5#------------------------------------------------------------------------------
6# Copyright 1992-1999 Karl Lehenbauer and Mark Diekhans.
7#
8# Permission to use, copy, modify, and distribute this software and its
9# documentation for any purpose and without fee is hereby granted, provided
10# that the above copyright notice appear in all copies.  Karl Lehenbauer and
11# Mark Diekhans make no representations about the suitability of this
12# software for any purpose.  It is provided "as is" without express or
13# implied warranty.
14#------------------------------------------------------------------------------
15# $Id: forfile.tcl,v 1.1 2001/10/24 23:31:48 hobbs Exp $
16#------------------------------------------------------------------------------
17#
18
19#@package: TclX-forfile for_file
20
21proc for_file {var filename cmd} {
22    upvar 1 $var line
23    set fp [open $filename r]
24    try_eval {
25        set code 0
26        set result {}
27        while {[gets $fp line] >= 0} {
28            set code [catch {uplevel 1 $cmd} result]
29            if {$code != 0 && $code != 4} break
30        }
31    } {} {
32        close $fp
33    }
34
35    if {$code == 0 || $code == 3 || $code == 4} {
36        return $result
37    }
38    if {$code == 1} {
39        global errorCode errorInfo
40        return -code $code -errorcode $errorCode -errorinfo $errorInfo $result
41    }
42    return -code $code $result
43}
44
45
46