]> git.donarmstrong.com Git - lilypond.git/blobdiff - flower/scalar.cc
release: 1.1.29
[lilypond.git] / flower / scalar.cc
index d8e6f9e74b927876f92c384d339daba951be650d..c0ef60d744c311d88a6b462aff3da26389acc607 100644 (file)
+/*
+  scalar.cc -- implement Scalar
+
+  source file of the Flower Library
+
+  (c)  1997--1999 Han-Wen Nienhuys <hanwen@cs.uu.nl>
+*/
+
+#include <assert.h>
 #include <stdio.h>
 #include "scalar.hh"
+#include "rational.hh"
+#include "matrix.hh"
 
-Scalar::Scalar(Rational r)
-    :String(r)
+Scalar::Scalar (Rational r)
 {
+  (*this) = r.str ();
+}
 
+Scalar::operator Rational ()
+{
+  return to_rat ();
 }
 
-Scalar::operator Rational()
+Rational
+Scalar::to_rat () const
 {
-    int p = pos('/');
-    if (!p)
-       return int(*this);
-    
-    String s2 = right(len()-p);
-    p--;
-    String s1 = left(p);
+  int p = index_i ('/');
+  if (p == -1)
+    return this->to_i ();
+  
+  String s2 = right_str (length_i ()-p-1);
+  String s1 = left_str (p);
 
-    return Rational(s1.value(), s2.value());
+  return Rational (s1.value_i (), s2.value_i ());
 }
 
 bool
-Scalar::isnum()
+Scalar::isnum_b () const
 {
-    int conv = false;
-    if (len()) {
-       long l =0;
-       conv = sscanf(data, "%ld", &l);
+  int conv = false;
+  if (length_i ())
+    {
+      long l =0;
+      conv = sscanf (strh_.ch_C (), "%ld", &l);
     }
-    return len() && conv;
+  return length_i () && conv;
 }
 
 Scalar::operator Real()
 {
-    assert (isnum());
-    return fvalue();
+  return to_f ();
+}
+
+Real
+Scalar::to_f () const
+{
+  assert (isnum_b ());
+  return value_f ();
+}
+
+Scalar::operator int ()
+{
+  return to_i ();
+}
+
+int
+Scalar::to_i () const
+{
+  if (!length_i ())
+    return 0;                  // ugh
+  
+  assert (isnum_b());
+  return value_i ();
+}
+
+Scalar::operator bool () const
+{
+  return to_bool ();
 }
 
-Scalar::operator int()
+bool
+Scalar::to_bool () const
 {
-    assert (isnum());
-    return value();
+  if (!length_i ())
+    return false;
+  if (*this == "0")
+    return false;
+  String u (*this);
+  if (u.upper_str () == "FALSE")
+    return false;
+  return true;
 }
 
+Scalar::Scalar(Matrix const &m)
+{
+  *this = m.str ();
+}
 
-Scalar::operator bool() const
+Scalar::Scalar (Vector const &v)
 {
-    if (!len())
-       return false;
-    if (*this == "0")
-       return false;
-    String u (*this);
-    u.upper();
-    if (u== "FALSE")
-       return false;
-    return true;
+  *this = v.str ();
 }