]> git.donarmstrong.com Git - lilypond.git/blob - flower/scalar.cc
476b6485bdd3727609f224bbeb1b64e3dc1a43bd
[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
14 Scalar::Scalar (Rational r)
15 {
16   (*this) = r.str ();
17 }
18
19 Scalar::operator Rational ()
20 {
21   return to_rat ();
22 }
23
24 Rational
25 Scalar::to_rat () const
26 {
27   int p = index_i ('/');
28   if (p == -1)
29     return this->to_i ();
30   
31   String s2 = right_str (length_i ()-p-1);
32   String s1 = left_str (p);
33
34   return Rational (s1.value_i (), s2.value_i ());
35 }
36
37 bool
38 Scalar::isdir_b () const
39 {
40   int conv = length_i ();
41   if (conv)
42     {
43       long l =0;
44       conv = sscanf (strh_.ch_C (), "%ld", &l);
45       conv = conv && (l >= -1 && l <= 1);
46     }
47   return conv;
48 }
49
50 bool
51 Scalar::isnum_b () const
52 {
53   int conv = false;
54   if (length_i ())
55     {
56       long l =0;
57       conv = sscanf (strh_.ch_C (), "%lf", &l);
58     }
59   return length_i () && conv;
60 }
61
62 Scalar::operator Real()
63 {
64   return to_f ();
65 }
66
67 Real
68 Scalar::to_f () const
69 {
70   assert (isnum_b ());
71   return value_f ();
72 }
73
74 Scalar::operator int ()
75 {
76   return to_i ();
77 }
78
79 int
80 Scalar::to_i () const
81 {
82   if (!length_i ())
83     return 0;                   // ugh
84   
85   assert (isnum_b());
86   return value_i ();
87 }
88
89 Scalar::operator bool () const
90 {
91   return to_bool ();
92 }
93
94 bool
95 Scalar::to_bool () const
96 {
97   if (!length_i ())
98     return false;
99   if (*this == "0")
100     return false;
101   String u (*this);
102   if (u.upper_str () == "FALSE")
103     return false;
104   return true;
105 }
106
107