4 # Created by Toshi Nagata.
5 # Copyright 2008 Toshi Nagata. All rights reserved.
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation version 2 of the License.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
19 Deg2Rad = Math::PI / 180.0
20 Rad2Deg = 180.0 / Math::PI
22 $startup_dir = Dir.pwd
24 when /mswin|mingw|cygwin|bccwin/
26 Encoding.default_external = "shift_jis"
27 $home_directory = ENV['USERPROFILE'].gsub(/\\/, "/")
30 Encoding.default_external = "utf-8"
31 $home_directory = ENV['HOME']
34 Encoding.default_external = "locale"
35 $home_directory = ENV['HOME']
42 print $backtrace.join("\n")
50 self.inject(0) { |sum, v| sum + block.call(v) }
52 self.inject(0) { |sum, v| sum + v }
56 sum(&block) / Float(self.length)
72 arg <= 0.0 ? 0.0 : sqrt(arg)
78 def filecopy(src, dst)
79 fpin = File.open(src, "rb")
80 return nil if fpin == nil
81 fpout = File.open(dst, "wb")
87 while fpin.read(4096, a)
94 def mkdir_recursive(path)
95 if FileTest.directory?(path)
98 dir = File.dirname(path)
99 if !FileTest.exist?(dir)
102 return Dir.mkdir(path)
105 def create_temp_dir(tag, name = nil)
106 # Create a temporary directory like HomeDirectory/Molby/tag/name.xxxxxx
108 base = $home_directory + "/Molby/" + tag
109 mkdir_recursive(base)
111 p = sprintf("%s/%s.%05d", base, name, n)
112 if !FileTest.exist?(p)
117 raise "Cannot create temporary directory in #{base}"
119 def rm_recursive(path)
120 if FileTest.directory?(path)
121 Dir.foreach(path) { |en|
122 next if en == "." || en == ".."
123 rm_recursive(path + "/" + en)
130 def cleanup_temp_dir(path, option = nil)
131 # Clean-up temporary directories
132 # If option is nil, then the directory at path is removed
133 # If option is a number, then the directories that are in the same parent directory as path
134 # are removed except for the newest ones
135 if path.index($home_directory + "/Molby/") != 0
136 raise "Bad cleanup_temp_dir call: the path does not begin with $HOME/Molby/ (#{path})"
138 if !FileTest.directory?(path)
139 raise "Bad cleanup_temp_dir call: the path is not a directory (#{path})"
145 base = File.dirname(path)
146 ent = Dir.entries.sort_by { |en| File.mtime("#{base}/#{en}").to_i * (-1) } - [".", ".."]
147 if $platform == "mac"
150 ent[0, option] = [] # Remove newest #{option} entries
151 # Mark this directory to be ready to remove (see below)
152 open("#{path}/.done", "w") { |fp| }
153 # Remove the older directories
155 # Check the existence of ".done" file (otherwise, we may accidentarily remove a directory
156 # that is still in use in other threads)
157 if File.exist?("#{base}/#{en}/.done")
158 rm_recursive("#{base}/#{en}")
165 entries = Dir.entries(dir)
167 next if en == "." || en == ".."
168 fname = "#{dir}/#{en}"
169 if File.directory?(fname)
178 def erase_old_logs(tdir, level, keep_number)
179 log_dir = File.dirname(tdir)
180 if level == nil || level == "none"
182 elsif level == "latest"
183 if keep_number == nil
186 keep_number = keep_number.to_i
188 entries = Dir.entries(log_dir).select { |en| en != "." && en != ".." && File.directory?("#{log_dir}/#{en}") }
189 # Sort by modification date
190 entries = entries.sort_by { |en| File.mtime("#{log_dir}/#{en}").to_i }
191 (0...entries.count - keep_number).each { |i|
192 remove_dir("#{log_dir}/#{entries[i]}")
200 alias :gets_original :gets
203 return gets_original(rs)
206 s = gets_original(@end_of_line)
207 if s && s.chomp!(@end_of_line)
215 if (c = getc) && c != "\n"
219 @end_of_line = "\r\n"
238 # Additional method definitions