~ubuntu-branches/ubuntu/oneiric/ctioga2/oneiric

« back to all changes in this revision

Viewing changes to lib/ctioga2/utils.rb

  • Committer: Bazaar Package Importer
  • Author(s): Vincent Fourmond
  • Date: 2011-01-24 21:36:06 UTC
  • Revision ID: james.westby@ubuntu.com-20110124213606-9ettx0ugl83z0bzp
Tags: upstream-0.1
ImportĀ upstreamĀ versionĀ 0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# utils.rb: Some small utility functions
 
2
# Copyright (c) 2006-2009 by Vincent Fourmond
 
3
  
 
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.
 
8
  
 
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).
 
13
 
 
14
module CTioga2
 
15
 
 
16
  # An exception to raise upon to-be-implemented-one-day features
 
17
  class YetUnimplemented < Exception
 
18
  end
 
19
 
 
20
  
 
21
  # A small module to deal with versions and dates
 
22
  module Version
 
23
 
 
24
    # The current version of the program.
 
25
    def self.version
 
26
      if CTIOGA_VERSION =~ /SVN/
 
27
        return "SVN, revision #{SVN_INFO['revision']}, #{SVN_INFO['date']}"
 
28
      else
 
29
        return CTIOGA_VERSION
 
30
      end
 
31
    end
 
32
 
 
33
    # All files should use this function with the appropriate
 
34
    # arguments and have the Date and Revision svn:keyword:. Use this
 
35
    # way:
 
36
    #
 
37
    #  Version::register_svn_info('$Revision: 194 $', '$Date: 2010-11-22 10:26:54 +0100 (Mon, 22 Nov 2010) $')
 
38
    #
 
39
    # To set the correct properties, the following command-line can be
 
40
    # used:
 
41
    #
 
42
    #  svn propset svn:keywords 'Date Revision'
 
43
    def self.register_svn_info(rev_str, date_str)
 
44
      if rev_str =~ /(\d+)/
 
45
        rev = $1
 
46
        str = 'Date'
 
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
 
51
        end
 
52
      end
 
53
    end
 
54
 
 
55
    # Returns the date ctioga2 was last modified.
 
56
    def self.last_modified_date
 
57
      SVN_INFO['date'] =~ /([\d-]+)/
 
58
      return $1
 
59
    end
 
60
 
 
61
 
 
62
    # The constants are moved here, as they disturb rdoc parsing.
 
63
 
 
64
 
 
65
    # Informations collected about subversion revisions
 
66
    SVN_INFO = { 
 
67
      'revision' => 0,
 
68
      'date' => "old"
 
69
    }
 
70
 
 
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 $'
 
73
    
 
74
    # The version of ctioga2
 
75
    CTIOGA_VERSION = if SVN_URL =~ /releases\/ctioga2-([^\/]+)/
 
76
                       $1
 
77
                     else
 
78
                       "SVN version"
 
79
                     end
 
80
 
 
81
    register_svn_info('$Revision: 194 $', '$Date: 2010-11-22 10:26:54 +0100 (Mon, 22 Nov 2010) $')
 
82
 
 
83
  end
 
84
 
 
85
  # Various utilities
 
86
  module Utils
 
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"*$()\[\]{}';\\]/
 
91
        if str =~ /'/
 
92
          a = str.gsub(/(["$\\])/) { "\\#$1" }
 
93
          return "\"#{a}\""
 
94
        else 
 
95
          return "'#{str}'"
 
96
        end
 
97
      else
 
98
        return str
 
99
      end
 
100
    end
 
101
 
 
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)
 
105
      ret = a.dup
 
106
      a.each_index do |i|
 
107
        ret[i] = a[i] * r + b[i] * (1 - r)
 
108
      end
 
109
      return ret
 
110
    end
 
111
 
 
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 
 
116
        if $1
 
117
          "\\#{$1}"
 
118
        elsif $2                  # Quoting (), as they can be quite nasty !!
 
119
          "\\string\\#{$2}"
 
120
        elsif $3
 
121
          "\\string#{$3}"
 
122
        else                      # Quoting \
 
123
          "\\string\\\\"
 
124
        end
 
125
      end
 
126
    end
 
127
 
 
128
    # Binomial coefficients (for the smooth filter)
 
129
    def self.cnk(n,k)
 
130
      res = 1.0
 
131
      n.downto(n - k) { |i| res *= i}
 
132
      k.downto(1) {|i| res = res/i }
 
133
      return res
 
134
    end
 
135
    
 
136
 
 
137
  end
 
138
 
 
139
end