added this small script to clean the source files
[silc.git] / scripts / stripspaces.tcl
1 #! /usr/bin/tcl
2 #
3 #  stripspaces.tcl - strip trailing spaces from source files
4 #
5 #  Author: Johnny Mnemonic <johnny@themnemonic.org>
6 #
7 #  Copyright (C) 2002 Johnny Mnemonic
8 #
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.
12 #
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.
17 #
18
19 # Procedures
20 # ---
21 proc do_strip_main {in_file out_file} {
22   set lines 0
23   if {[catch {set fd [open "$in_file" r]} errtmp]} {return -1}
24   if {[catch {set fw [open "$out_file" w]} errtmp]} {return -1}
25
26   while {![eof $fd]} {
27     set str [string trimright [gets $fd]]
28     if {![eof $fd]} {
29       incr lines
30       puts $fw $str;
31     }
32   }
33
34   close $fd
35   close $fw
36   return $lines
37 }
38
39 # Main
40 # ---
41 if {$argc < 1} {
42   puts stderr "Usage: `./stripspaces.tcl <file> \[output\]'"
43   puts stderr ""
44   exit 1
45 }
46
47 set in_file [lindex $argv 0]
48
49 if {![file readable $in_file]} {
50   puts stderr "Error: Cannot open file \"$in_file\"."
51   puts stderr ""
52   exit 1
53 }
54
55 if {$argc > 1} {
56   set out_file [lindex $argv 1]
57 } else {
58   set out_file "$in_file.strip"
59 }
60
61 puts stderr "Stripping trailing spaces from \"$in_file\" (output: \"$out_file\")"
62
63 set ret [do_strip_main $in_file $out_file]
64
65 if {$ret < 0} {
66   puts stderr "Failed. Couldn't open the input/output filename."
67   puts stderr ""
68 } else {
69   puts stderr "Done. Parsed $ret lines."
70 }