From f12baed5df6d3c213dd75d2f0d9f36bb179fb843 Mon Sep 17 00:00:00 2001 From: "(no author)" <(no author)@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> Date: Fri, 16 Jan 1998 12:19:22 +0000 Subject: This commit was manufactured by cvs2svn to create branch 'RUBY'. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/RUBY@9 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- lib/delegate.rb | 44 +++++++ lib/eregex.rb | 39 +++++++ lib/ftools.rb | 163 ++++++++++++++++++++++++++ lib/importenv.rb | 29 +++++ lib/mkmf.rb | 343 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ lib/ostruct.rb | 55 +++++++++ lib/pstore.rb | 121 +++++++++++++++++++ lib/shellwords.rb | 48 ++++++++ lib/tkdialog.rb | 62 ++++++++++ lib/weakref.rb | 70 +++++++++++ 10 files changed, 974 insertions(+) create mode 100644 lib/delegate.rb create mode 100644 lib/eregex.rb create mode 100644 lib/ftools.rb create mode 100644 lib/importenv.rb create mode 100644 lib/mkmf.rb create mode 100644 lib/ostruct.rb create mode 100644 lib/pstore.rb create mode 100644 lib/shellwords.rb create mode 100644 lib/tkdialog.rb create mode 100644 lib/weakref.rb (limited to 'lib') diff --git a/lib/delegate.rb b/lib/delegate.rb new file mode 100644 index 0000000000..e5943cead8 --- /dev/null +++ b/lib/delegate.rb @@ -0,0 +1,44 @@ +# Delegation class that delegates even methods defined in super class, +# which can not be covered with normal method_missing hack. +# +# Delegater is the abstract delegation class. Need to redefine +# `__getobj__' method in the subclass. SimpleDelegater is the +# concrete subclass for simple delegation. +# +# Usage: +# foo = Object.new +# foo = SimpleDelegater.new(foo) +# foo.type # => Object + +class Delegater + + def initialize(obj) + preserved = ["id", "equal?", "__getobj__"] + for t in self.type.ancestors + preserved |= t.instance_methods + break if t == Delegater + end + for method in obj.methods + next if preserved.include? method + eval "def self.#{method}(*args); __getobj__.send :#{method}, *args; end" + end + end + + def __getobj__ + raise NotImplementError, "need to define `__getobj__'" + end + +end + +class SimpleDelegater TOO_BIG + + from = open(from, "r") + from.binmode + to = open(to, "w") + to.binmode + + begin + while TRUE + r = from.sysread(fsize) + rsize = r.size + w = 0 + while w < rsize + t = to.syswrite(r[w, rsize - w]) + w += t + end + end + rescue EOFError + ret = TRUE + rescue + ret = FALSE + ensure + to.close + from.close + end + ret + end + + def copy from, to, verbose = FALSE + $stderr.print from, " -> ", catname(from, to), "\n" if verbose + syscopy from, to + end + + alias cp copy + +# move file + + def move from, to, verbose = FALSE + to = catname(from, to) + $stderr.print from, " -> ", to, "\n" if verbose + + if PLATFORM =~ /djgpp|cygwin32|mswin32/ and FileTest.file? to + unlink to + end + begin + rename from, to + rescue + syscopy from, to and unlink from + end + end + + alias mv move + +# compare two files +# TRUE: identical +# FALSE: not identical + + def compare from, to, verbose = FALSE + $stderr.print from, " <=> ", to, "\n" if verbose + fsize = size(from) + fsize = 1024 if fsize < 512 + fsize = TOO_BIG if fsize > TOO_BIG + + from = open(from, "r") + from.binmode + to = open(to, "r") + to.binmode + + ret = FALSE + fr = tr = '' + + begin + while fr == tr + if fr = from.read(fsize) + tr = to.read(fr.size) + else + ret = !to.read(fsize) + break + end + end + rescue + ret = FALSE + ensure + to.close + from.close + end + ret + end + + alias cmp compare + +# unlink files safely + + def safe_unlink(*files) + verbose = if files[-1].is_a? String then FALSE else files.pop end + begin + $stderr.print files.join(" "), "\n" if verbose + chmod 0777, *files + unlink *files + rescue +# STDERR.print "warning: Couldn't unlink #{files.join ' '}\n" + end + end + + alias rm_f safe_unlink + + def makedirs(*dirs) + verbose = if dirs[-1].is_a? String then FALSE else dirs.pop end +# mode = if dirs[-1].is_a? Fixnum then dirs.pop else 0755 end + mode = 0755 + for dir in dirs + next if FileTest.directory? dir + parent = dirname(dir) + makedirs parent unless FileTest.directory? parent + $stderr.print "mkdir ", dir, "\n" if verbose + Dir.mkdir dir, mode + end + end + + alias mkpath makedirs + + alias o_chmod chmod + + def chmod(mode, *files) + verbose = if files[-1].is_a? String then FALSE else files.pop end + $stderr.printf "chmod %04o %s\n", mode, files.join(" ") if verbose + o_chmod mode, *files + end + + def install(from, to, mode, verbose) + to = catname(from, to) + unless FileTest.exist? to and cmp from, to + cp from, to, verbose + chmod mode, to, verbose if mode + end + end + +end +# vi:set sw=2: diff --git a/lib/importenv.rb b/lib/importenv.rb new file mode 100644 index 0000000000..41253765ea --- /dev/null +++ b/lib/importenv.rb @@ -0,0 +1,29 @@ +# importenv.rb -- imports environment variables as global variables +# +# Usage: +# +# require 'importenv' +# p $USER +# $USER = "matz" +# p ENV["USER"] + +for k,v in ENV + next unless /^[a-zA-Z][_a-zA-Z0-9]*/ =~ k + eval <&1" +CPP = CONFIG["CPP"] + " -E -I#{$srcdir} " + CFLAGS + " %s conftest.c " + nul + " 2>&1" + +def try_link(libs) + system(format(LINK, $CFLAGS, $LDFLAGS, libs)) +end + +def try_cpp + system(format(CPP, $CFLAGS)) +end + +def have_library(lib, func) + printf "checking for %s() in -l%s... ", func, lib + STDOUT.flush + if $lib_cache[lib] + if $lib_cache[lib] == "yes" + if $libs + $libs = "-l" + lib + " " + $libs + else + $libs = "-l" + lib + end + print "(cached) yes\n" + return TRUE + else + print "(cached) no\n" + return FALSE + end + end + + cfile = open("conftest.c", "w") + cfile.printf "\ +int main() { return 0; } +int t() { %s(); return 0; } +", func + cfile.close + + begin + if $libs + libs = "-l" + lib + " " + $libs + else + libs = "-l" + lib + end + unless try_link(libs) + $lib_found[lib] = 'no' + $found = TRUE + print "no\n" + return FALSE + end + ensure + system "rm -f conftest*" + end + + $libs = libs + $lib_found[lib] = 'yes' + $found = TRUE + print "yes\n" + return TRUE +end + +def have_func(func) + printf "checking for %s()... ", func + STDOUT.flush + if $func_cache[func] + if $func_cache[func] == "yes" + $defs.push(format("-DHAVE_%s", func.upcase)) + print "(cached) yes\n" + return TRUE + else + print "(cached) no\n" + return FALSE + end + end + + cfile = open("conftest.c", "w") + cfile.printf "\ +char %s(); +int main() { return 0; } +int t() { %s(); return 0; } +", func, func + cfile.close + + libs = $libs + libs = "" if libs == nil + + begin + unless try_link(libs) + $func_found[func] = 'no' + $found = TRUE + print "no\n" + return FALSE + end + ensure + system "rm -f conftest*" + end + $defs.push(format("-DHAVE_%s", func.upcase)) + $func_found[func] = 'yes' + $found = TRUE + print "yes\n" + return TRUE +end + +def have_header(header) + printf "checking for %s... ", header + STDOUT.flush + if $hdr_cache[header] + if $hdr_cache[header] == "yes" + header.tr!("a-z./\055", "A-Z___") + $defs.push(format("-DHAVE_%s", header)) + print "(cached) yes\n" + return TRUE + else + print "(cached) no\n" + return FALSE + end + end + + cfile = open("conftest.c", "w") + cfile.printf "\ +#include <%s> +", header + cfile.close + + begin + unless try_cpp + $hdr_found[header] = 'no' + $found = TRUE + print "no\n" + return FALSE + end + ensure + system "rm -f conftest*" + end + $hdr_found[header] = 'yes' + header.tr!("a-z./\055", "A-Z___") + $defs.push(format("-DHAVE_%s", header)) + $found = TRUE + print "yes\n" + return TRUE +end + +def create_header() + print "creating extconf.h\n" + STDOUT.flush + if $defs.length > 0 + hfile = open("extconf.h", "w") + for line in $defs + line =~ /^-D(.*)/ + hfile.printf "#define %s 1\n", $1 + end + hfile.close + end +end + +def create_makefile(target) + print "creating Makefile\n" + STDOUT.flush + if $libs and CONFIG["DLEXT"] == "o" + libs = $libs.split + for lib in libs + lib.sub!(/-l(.*)/, '"lib\1.a"') + end + $defs.push(format("-DEXTLIB='%s'", libs.join(","))) + end + $libs = "" unless $libs + + if !$objs then + $objs = Dir["*.c"] + for f in $objs + f.sub!(/\.(c|cc)$/, ".o") + end + end + $objs = $objs.join(" ") + + mfile = open("Makefile", "w") + mfile.print < Raises WeakRef::RefError (because original GC'ed) + +require "delegate" + +class WeakRef