in progress...
[jalview.git] / forester / ruby / evoruby / lib / evo / util / util.rb
index 27bdb5e..7e8b554 100644 (file)
 #
 # = lib/evo/util/util.rb - Util class
 #
-# Copyright::  Copyright (C) 2006-2007 Christian M. Zmasek
-# License::    GNU Lesser General Public License (LGPL)
+# Copyright::    Copyright (C) 2017 Christian M. Zmasek
+# License::      GNU Lesser General Public License (LGPL)
 #
-# $Id: util.rb,v 1.17 2009/10/06 22:22:46 cmzmasek Exp $
-#
-# last modified: 05/15/2007
+# Last modified: 2017/04/27
 
+require 'pathname'
 require 'lib/evo/util/constants'
 
 module Evoruby
-
   class Util
+    def Util.canonical_path( parent, child = nil )
+      if child == nil
+        return  File.expand_path(Pathname.new(parent).cleanpath.to_s).to_s
+      end
+
+      s = nil
+      if parent.end_with?('/')
+        s = parent + child
+      else
+        s = parent + '/' + child
+      end
+      File.expand_path(Pathname.new(s).cleanpath.to_s).to_s
+    end
+
+    def Util.get_matching_files( files, prefix_pattern, suffix_pattern )
+      matching_files = Array.new
+      files.each { | file |
+        if ( !File.directory?( file ) &&
+        file !~ /^\./ &&
+        file =~ /^#{prefix_pattern}.*#{suffix_pattern}$/ )
+          matching_files << file
+        end
+      }
+      matching_files
+    end
+
+    def Util.get_matching_file( dir_name, prefix, suffix )
+      unless Dir.exist? dir_name
+        raise IOError, "directory [#{dir_name}] does not exist"
+      end
+
+      all_files = Dir.entries(dir_name)
+
+      if ( all_files.size <= 2 )
+        raise IOError, "directory [#{dir_name}] is empty"
+      end
+      matching_files = Array.new
+      all_files.each { | file |
+        if  (( !File.directory?( file )) && (file.end_with? suffix))
+          matching_files << file
+        end
+      }
+
+      if ( matching_files.size == 0 )
+        raise IOError, "no files ending with \"" + suffix + "\" found in [" + dir_name + "]"
+      end
+      my_prefix = prefix
+      done = false
+      more_than_one = false
+      the_one = nil;
+
+      loop do
+
+        matches = 0
+        matching_files.each { | file |
+          if file.start_with?( my_prefix )
+            matches += 1
+            if matches > 1
+              the_one = nil
+              break
+            end
+            the_one = file
+          end
+        }
+        if matches > 1
+          more_than_one = true
+          done = true
+        end
+        if matches == 1
+          done = true
+        else
+          if my_prefix.length <= 1
+            raise IOError, "no file matching \"" + prefix + "\" and ending with \"" + suffix + "\" found in [" + dir_name + "]"
+          end
+          my_prefix = my_prefix[ 0 ... ( my_prefix.length - 1 ) ]
+
+        end
+        break if done
+      end
+
+      if more_than_one
+        raise IOError, "multiple files matching \"" +  prefix  +
+        "\" and ending with \"" + suffix + "\" found in [" + dir_name + "]"
+      elsif the_one != nil
+      else
+        raise IOError, "no file matching \"" + prefix  + "\" and ending with \"" +
+        suffix + "\" found in [" + dir_name + "]"
+      end
+      the_one
+    end
 
     def Util.normalize_seq_name( name, length, exception_if_too_long = false )
       if name.length > length
@@ -22,7 +110,8 @@ module Evoruby
         end
         name = name[ 0, length ]
       elsif name.length < length
-        for i in 0 ... length - name.length
+        t = length - name.length
+        t.times do
           name = name + " "
         end
       end
@@ -104,7 +193,6 @@ module Evoruby
       value
     end
 
-
     # raises ArgumentError
     def Util.file2array( path, split_by_semicolon )
       Util.check_file_for_readability( path )
@@ -130,18 +218,11 @@ module Evoruby
     end
 
     def Util.print_program_information( prg_name,
-        prg_version,
-        prg_desc,
-        date,
-        copyright,
-        contact,
-        www,
-        io = STDOUT )
-
-    #  if RUBY_VERSION !~ /1.9/
-    #    puts( "Your ruby version is #{RUBY_VERSION}, expected 1.9.x " )
-    #    exit( -1 )
-    #  end
+      prg_version,
+      prg_desc,
+      date,
+      www,
+      io = STDOUT )
 
       ruby_version = RUBY_VERSION
       l = prg_name.length + prg_version.length + date.length + ruby_version.length + 12
@@ -156,11 +237,7 @@ module Evoruby
       io.print( prg_desc )
       io.print( Constants::LINE_DELIMITER )
       io.print( Constants::LINE_DELIMITER )
-      io.print( "Copyright (C) " + copyright )
-      io.print( Constants::LINE_DELIMITER )
-      io.print( "Contact: " + contact )
-      io.print( Constants::LINE_DELIMITER )
-      io.print( "         " + www )
+      io.print( "Website: " + www )
       io.print( Constants::LINE_DELIMITER )
       io.print( Constants::LINE_DELIMITER )
     end