3 # stripspaces.tcl - strip trailing spaces from source files
5 # Author: Johnny Mnemonic <johnny@themnemonic.org>
7 # Copyright (C) 2002 Johnny Mnemonic
9 # This program is free software; you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; version 2 of the License.
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
25 proc do_strip_main {in_file out_file} {
27 if {[catch {set fd [open "$in_file" r]} errtmp]} {return -1}
28 if {[catch {set fw [open "$out_file" w]} errtmp]} {return -1}
31 set str [string trimright [gets $fd]]
43 global argc argv Targets Output
45 # unset this if we find a "--" argument
48 while {[llength $argv_safe] > 0} {
49 set this_arg [lvarpop argv_safe]
50 switch -exact -- $this_arg {
52 if {!$parse_args} {lappend Targets $this_arg; continue}
53 set next_arg [lvarpop argv_safe]
54 if {$next_arg == ""} {
55 puts stderr "Error: option requires an argument -- o"
62 if {!$parse_args} {lappend Targets $this_arg; continue}
66 lappend Targets $this_arg
77 # check what they gave us
80 if {[llength $Targets] < 1} {
81 puts stderr "Usage: ./stripspaces.tcl \[-o output\] <file1> \[file2\] ..."
86 if {([llength $Targets] > 1) && ($Output != "")} {
87 if {[file exists $Output]} {
88 if {![file isdirectory $Output]} {
89 puts stderr "Error: Specified multiple files but output target exists and is not a directory!"
94 if {[catch {mkdir $Output} errtmp]} {
95 puts stderr "Error: Couldn't create directory \"$Output\""
104 foreach xfile $Targets {
105 if {![file readable $xfile]} {
106 puts stderr "Error: Cannot open file \"$xfile\" (skipped)."
111 set xoutput "$xfile.strip"
112 } elseif {[file isdirectory $Output]} {
113 set xoutput "$Output/$xfile.strip"
118 puts stderr "Stripping trailing spaces from \"$xfile\" (output: \"$xoutput\")"
119 set ret [do_strip_main $xfile $xoutput]
121 puts stderr "$xfile: Failed. Couldn't open the input/output filename."
124 puts stderr "File $xfile done. Parsed $ret lines."