]> git.donarmstrong.com Git - lilypond.git/blob - flower/scalar.cc
release: 1.1.29
[lilypond.git] / flower / scalar.cc
1 /*
2   scalar.cc -- implement Scalar
3
4   source file of the Flower Library
5
6   (c)  1997--1999 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
8
9 #include <assert.h>
10 #include <stdio.h>
11 #include "scalar.hh"
12 #include "rational.hh"
13 #include "matrix.hh"
14
15 Scalar::Scalar (Rational r)
16 {
17   (*this) = r.str ();
18 }
19
20 Scalar::operator Rational ()
21 {
22   return to_rat ();
23 }
24
25 Rational
26 Scalar::to_rat () const
27 {
28   int p = index_i ('/');
29   if (p == -1)
30     return this->to_i ();
31   
32   String s2 = right_str (length_i ()-p-1);
33   String s1 = left_str (p);
34
35   return Rational (s1.value_i (), s2.value_i ());
36 }
37
38 bool
39 Scalar::isnum_b () const
40 {
41   int conv = false;
42   if (length_i ())
43     {
44       long l =0;
45       conv = sscanf (strh_.ch_C (), "%ld", &l);
46     }
47   return length_i () && conv;
48 }
49
50 Scalar::operator Real()
51 {
52   return to_f ();
53 }
54
55 Real
56 Scalar::to_f () const
57 {
58   assert (isnum_b ());
59   return value_f ();
60 }
61
62 Scalar::operator int ()
63 {
64   return to_i ();
65 }
66
67 int
68 Scalar::to_i () const
69 {
70   if (!length_i ())
71     return 0;                   // ugh
72   
73   assert (isnum_b());
74   return value_i ();
75 }
76
77 Scalar::operator bool () const
78 {
79   return to_bool ();
80 }
81
82 bool
83 Scalar::to_bool () const
84 {
85   if (!length_i ())
86     return false;
87   if (*this == "0")
88     return false;
89   String u (*this);
90   if (u.upper_str () == "FALSE")
91     return false;
92   return true;
93 }
94
95 Scalar::Scalar(Matrix const &m)
96 {
97   *this = m.str ();
98 }
99
100 Scalar::Scalar (Vector const &v)
101 {
102   *this = v.str ();
103 }