1
# utils.rb: Some small utility functions
2
# Copyright (c) 2006-2009 by Vincent Fourmond
4
# This program is free software; you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation; either version 2 of the License, or
7
# (at your option) any later version.
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
# GNU General Public License for more details (in the COPYING file).
16
# An exception to raise upon to-be-implemented-one-day features
17
class YetUnimplemented < Exception
21
# A small module to deal with versions and dates
24
# The current version of the program.
26
if CTIOGA_VERSION =~ /SVN/
27
return "SVN, revision #{SVN_INFO['revision']}, #{SVN_INFO['date']}"
33
# All files should use this function with the appropriate
34
# arguments and have the Date and Revision svn:keyword:. Use this
37
# Version::register_svn_info('$Revision: 194 $', '$Date: 2010-11-22 10:26:54 +0100 (Mon, 22 Nov 2010) $')
39
# To set the correct properties, the following command-line can be
42
# svn propset svn:keywords 'Date Revision'
43
def self.register_svn_info(rev_str, date_str)
47
date = date_str.gsub(/\$#{str}:\s*(.*)\$/) { $1 }
48
if SVN_INFO['revision'] < rev.to_i
49
SVN_INFO['revision'] = rev.to_i
50
SVN_INFO['date'] = date
55
# Returns the date ctioga2 was last modified.
56
def self.last_modified_date
57
SVN_INFO['date'] =~ /([\d-]+)/
62
# The constants are moved here, as they disturb rdoc parsing.
65
# Informations collected about subversion revisions
71
# The position of the URL, used for getting the version
72
SVN_URL = '$HeadURL: svn+ssh://rubyforge.org/var/svn/ctioga2/releases/ctioga2-0.1/lib/ctioga2/utils.rb $'
74
# The version of ctioga2
75
CTIOGA_VERSION = if SVN_URL =~ /releases\/ctioga2-([^\/]+)/
81
register_svn_info('$Revision: 194 $', '$Date: 2010-11-22 10:26:54 +0100 (Mon, 22 Nov 2010) $')
87
# Takes a string a returns a quoted version that should be able to
88
# go through shell expansion.
89
def self.shell_quote_string(str)
90
if str =~ /[\s"*$()\[\]{}';\\]/
92
a = str.gsub(/(["$\\])/) { "\\#$1" }
102
# Takes two arrays of the same size (vectors) and mix them
103
# a * r + b * (1 - r)
104
def self.mix_objects(a,b,r)
107
ret[i] = a[i] * r + b[i] * (1 - r)
112
# Quotes a string so it can be included directly within a
113
# \pdfinfo statement (for instance).
114
def self.pdftex_quote_string(str)
115
return str.gsub(/([%#])|([()])|([{}~_^])|\\/) do
118
elsif $2 # Quoting (), as they can be quite nasty !!
128
# Binomial coefficients (for the smooth filter)
131
n.downto(n - k) { |i| res *= i}
132
k.downto(1) {|i| res = res/i }