require 'thread' require 'ftools' require 'parallel_each.rb' require 'tools/thread_tool.rb' def asBaseDir File.expand_path('../..',__FILE__) end class File def self.load(filename) return nil unless File.file?(filename) File.open(filename) {|f|f.read} end end class Class def deprecated(*funcs) #puts "WARNING: #{funcs} are deprecated!!!" end end class Dir def self.mkdir_save(*args) return if File.directory?(*args) begin self.mkdir(*args) rescue Object=>e d=*args p=File.split(d)[0] self.mkdir_save(p) if p!=d self.mkdir(*args) end end def self.rmdir_f(dir) if File.directory?(dir) files=Dir[File.join(dir,"*")] files.each{|f|rmdir_f(f)} Dir.delete(dir) elsif File.file?(dir) File.delete(dir) end end end class String # convert lowercase underscored strings to camelcase def camelCase return self if length<2 (self[0..0].upcase+self[1..-1]).gsub(/(_.)/){|s|s[1..-1].upcase} end def underscored (self[0..0].downcase+self[1..-1]).gsub(/([A-Z])/){|s|"_"+s.downcase} end # convert camelcase to lowercase underscored def dirName underscored end # convvert (some) html-entities def to_hents self.gsub("&","&").gsub("<","<").gsub(">",">") end # convert plaintext to html (entities and newlines) def to_h self.to_hents.gsub("\n","
\n") end def to_base64 [self].pack("m") end end class AssertionFailed(o) self.to_s<=>o.to_s end end def getClass(name) #pp "TRY GETCLASS #{name}" $CLASS_CACHE||={} unless $CLASS_CACHE.key?(name) c=nil begin ObjectSpace.each_object{|o| c=o if o.to_s==name and o.is_a?(Class) } rescue c=instance_eval(name) end $CLASS_CACHE[name]=c end $CLASS_CACHE[name] end class FalseClass # make booleans sortable def <=>(o) case o when FalseClass 0 when TrueClass 1 else 0 end end end class TrueClass def <=>(o) case o when FalseClass -1 when TrueClass 0 else 0 end end end def hexDist(hash1,hash2) assert{hash1.length==hash2.length} dist=0 (0...hash1.length).each{|i| current=(hash1[i]^hash2[i]) dist*=256 dist+=current #r+=c } dist end def glog(*s) #puts "GLOBAL LOG #{caller[0]} #{Time.now} "+(s.map{|x|x.to_s}.join("; ")) end def measureTime(depth=1,&block) startTime=Time.now r=block.call endTime=Time.now glog caller[0...depth],"TIME:",(endTime-startTime) r end