OSDN Git Service

Fix #35795: A time consumed could be -1
[shogi-server/shogi-server.git] / utils / correct-bug14635.rb
1 #!/usr/bin/ruby
2 # == Synopsis
3 #
4 # This program corrects illegal lines introduced by the #14635 bug.
5 #
6 # Author::    Daigo Moriwaki <daigo at debian dot org>
7 # Copyright:: Copyright (C) 2008-2012  Daigo Moriwaki <daigo at debian dot org>
8 #
9 # $Id$
10 #
11 #--
12 # This program is free software; you can redistribute it and/or modify
13 # it under the terms of the GNU General Public License as published by
14 # the Free Software Foundation; either version 2 of the License, or
15 # (at your option) any later version.
16 #
17 # This program is distributed in the hope that it will be useful,
18 # but WITHOUT ANY WARRANTY; without even the implied warranty of
19 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 # GNU General Public License for more details.
21 #
22 # You should have received a copy of the GNU General Public License
23 # along with this program; if not, write to the Free Software
24 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
25 #++
26 #
27 # == Usage
28 #
29 # correct-bug14635.rb [OPTIONS] DIR ...
30
31 # --dry-run:
32 #   do not modify files, but show what will happen
33 #
34
35 require 'fileutils'
36 require 'getoptlong'
37 require 'nkf'
38 require 'pathname'
39
40 $KCODE="e"
41
42 class CheckCsaFile
43   def initialize(file_path)
44     @file = file_path
45     @lines = []
46   end
47
48   def check
49     puts @file if $DEBUG
50     ret = false
51
52     data = NKF.nkf("-e", @file.read)
53     data.each_line do |line|
54       case line.strip
55       when ""
56         puts "Found an empty line"
57         ret = true
58       when  /%%TORYO/
59         puts "Found %%TORYO"
60         @lines << line.gsub(/%%TORYO/, "%TORYO")
61         ret = true
62       when  /%%KACHI/
63         puts "Found %%KACHI"
64         @lines << line.gsub(/%%KACHI/, "%KACHI")
65         ret = true
66       else
67         @lines << line
68       end
69     end
70
71     return ret
72   end
73
74   def execute
75     backup_name = @file.to_s + ".back"
76     FileUtils.cp @file, backup_name
77     @file.open("w") {|f| f.write @lines.join}
78   end
79 end
80
81
82 if $0 == __FILE__
83   def usage
84     puts "Usage: #{$0} [OPTIONS] dir [...]"
85     puts "Options:"
86     puts "  --dry-run   do not modify files, but show what to do"
87     exit 1
88   end
89
90   usage if ARGV.empty?
91
92   parser = GetoptLong.new(
93              ['--dry-run', GetoptLong::NO_ARGUMENT]
94            )
95   begin
96     parser.each_option do |name, arg|
97       eval "$OPT_#{name.sub(/^--/, '').gsub(/-/, '_').upcase} = '#{arg}'"
98     end
99   rescue
100     usage
101   end
102   
103   while dir = ARGV.shift
104     Dir.glob(File.join(dir, "**", "*.csa")).each do |file|
105       path = Pathname.new(file)
106       csa = CheckCsaFile.new(path)
107       if csa.check
108         puts path
109         next if $OPT_DRY_RUN
110
111         csa.execute
112       end
113     end
114   end # while
115 end