]> git.donarmstrong.com Git - lilypond.git/blob - flower/scalar.cc
release: 1.3.0
[lilypond.git] / flower / scalar.cc
1 #if 0
2 /*
3   scalar.cc -- implement Scalar
4
5   source file of the Flower Library
6
7   (c)  1997--1999 Han-Wen Nienhuys <hanwen@cs.uu.nl>
8 */
9
10 #include <assert.h>
11 #include <stdio.h>
12
13 #include "rational.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::isdir_b () const
40 {
41   int conv = length_i ();
42   if (conv)
43     {
44       long l =0;
45       conv = sscanf (strh_.ch_C (), "%ld", &l);
46       conv = conv && (l >= -1 && l <= 1);
47     }
48   return conv;
49 }
50
51 bool
52 Scalar::isnum_b () const
53 {
54   int conv = false;
55   if (length_i ())
56     {
57       long l =0;
58       conv = sscanf (strh_.ch_C (), "%lf", &l);
59     }
60   return length_i () && conv;
61 }
62
63 Scalar::operator Real()
64 {
65   return to_f ();
66 }
67
68 Real
69 Scalar::to_f () const
70 {
71   assert (isnum_b ());
72   return value_f ();
73 }
74
75 Scalar::operator int ()
76 {
77   return to_i ();
78 }
79
80 int
81 Scalar::to_i () const
82 {
83   if (!length_i ())
84     return 0;                   // ugh
85   
86   assert (isnum_b());
87   return value_i ();
88 }
89
90 Scalar::operator bool () const
91 {
92   return to_bool ();
93 }
94
95 bool
96 Scalar::to_bool () const
97 {
98   if (!length_i ())
99     return false;
100   if (*this == "0")
101     return false;
102   String u (*this);
103   if (u.upper_str () == "FALSE")
104     return false;
105   return true;
106 }
107
108
109 #endif