]> git.donarmstrong.com Git - lilypond.git/blob - flower/scalar.cc
release: 1.0.2
[lilypond.git] / flower / scalar.cc
1 /*
2   scalar.cc -- implement Scalar
3
4   source file of the Flower Library
5
6   (c)  1997--1998 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   int p = index_i ('/');
23   if (p == -1)
24     return int (*this);
25   
26   String s2 = right_str (length_i ()-p-1);
27   String s1 = left_str (p);
28
29   return Rational (s1.value_i (), s2.value_i ());
30 }
31
32 bool
33 Scalar::isnum_b () const
34 {
35   int conv = false;
36   if (length_i ())
37     {
38       long l =0;
39       conv = sscanf (strh_.ch_C (), "%ld", &l);
40     }
41   return length_i () && conv;
42 }
43
44 Scalar::operator Real()
45 {
46   assert (isnum_b ());
47   return value_f ();
48 }
49
50 Scalar::operator int()
51 {
52   if (!length_i ())
53     return 0;                   // ugh
54   
55   assert (isnum_b());
56   return value_i ();
57 }
58
59 Scalar::operator bool () const
60 {
61   return to_bool ();
62 }
63
64 bool
65 Scalar::to_bool () const
66 {
67   if (!length_i ())
68     return false;
69   if (*this == "0")
70     return false;
71   String u (*this);
72   if (u.upper_str () == "FALSE")
73     return false;
74   return true;
75 }
76
77 Scalar::Scalar(Matrix const &m)
78 {
79   *this = m.str ();
80 }
81
82 Scalar::Scalar (Vector const &v)
83 {
84   *this = v.str ();
85 }