The SILC Project

source navigation ]
identifier search ]
freetext search ]
file search ]

silc/scripts/stripspaces.tcl

  1 #! /usr/bin/tcl
  2 #
  3 #  stripspaces.tcl - strip trailing spaces from source files
  4 #
  5 #  Author: Giovanni Giacobbi <giovanni@giacobbi.net>
  6 #
  7 #  Copyright (C) 2002 - 2003 Giovanni Giacobbi
  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 # Global variables
 20 set Targets ""
 21 set Output ""
 22 
 23 # Procedures
 24 # ---
 25 proc do_strip_main {in_file out_file} {
 26   set lines 0
 27   if {[catch {set fd [open "$in_file" r]} errtmp]} {return -1}
 28   if {[catch {set fw [open "$out_file" w]} errtmp]} {return -1}
 29 
 30   while {![eof $fd]} {
 31     set str [string trimright [gets $fd]]
 32     if {![eof $fd]} {
 33       incr lines
 34       puts $fw $str;
 35     }
 36   }
 37 
 38   close $fd
 39   close $fw
 40   return $lines
 41 }
 42 proc parse_args {} {
 43   global argc argv Targets Output
 44 
 45   # unset this if we find a "--" argument
 46   set argv_safe $argv
 47   set parse_args 1
 48   while {[llength $argv_safe] > 0} {
 49     set this_arg [lvarpop argv_safe]
 50     switch -exact -- $this_arg {
 51       "-o" {
 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"
 56           puts stderr ""
 57           exit 1
 58         }
 59         set Output $next_arg
 60       }
 61       "--" {
 62         if {!$parse_args} {lappend Targets $this_arg; continue}
 63         set parse_args 0
 64       }
 65       default {
 66         lappend Targets $this_arg
 67       }
 68     }
 69   }
 70 
 71   return
 72 }
 73 
 74 # Main
 75 # ---
 76 
 77 # check what they gave us
 78 parse_args
 79 
 80 if {[llength $Targets] < 1} {
 81   puts stderr "Usage: ./stripspaces.tcl \[-o output\] <file1> \[file2\] ..."
 82   puts stderr ""
 83   exit 1
 84 }
 85 
 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!"
 90       puts stderr ""
 91       exit 1
 92     }
 93   } else {
 94     if {[catch {mkdir $Output} errtmp]} {
 95       puts stderr "Error: Couldn't create directory \"$Output\""
 96       puts stderr ""
 97       exit 1
 98     }
 99   }
100 }
101 
102 set done 0
103 
104 foreach xfile $Targets {
105   if {![file readable $xfile]} {
106     puts stderr "Error: Cannot open file \"$xfile\" (skipped)."
107     continue
108   }
109 
110   if {$Output == ""} {
111     set xoutput "$xfile.strip"
112   } elseif {[file isdirectory $Output]} {
113     set xoutput "$Output/$xfile.strip"
114   } else {
115     set xoutput $Output
116   }
117 
118   puts stderr "Stripping trailing spaces from \"$xfile\" (output: \"$xoutput\")"
119   set ret [do_strip_main $xfile $xoutput]
120   if {$ret < 0} {
121     puts stderr "$xfile: Failed. Couldn't open the input/output filename."
122     puts stderr ""
123   } else {
124     puts stderr "File $xfile done. Parsed $ret lines."
125   }
126 
127   incr done
128 }

This page was automatically generated by the LXR engine.
Free-text search provided by Glimpse