]> git.donarmstrong.com Git - lilypond.git/blobdiff - flower/scalar.cc
partial: 0.1.61.jcn
[lilypond.git] / flower / scalar.cc
index b53693928fc613d6326ce202e4ee2b7e20a6a336..79bfd53d5f954eeeada0810c3462327e3bd6cc5e 100644 (file)
@@ -1,38 +1,74 @@
+/*
+  scalar.cc -- implement Scalar
+
+  source file of the Flower Library
+
+  (c) 1997 Han-Wen Nienhuys <hanwen@stack.nl>
+*/
+
+#include <assert.h>
 #include <stdio.h>
 #include "scalar.hh"
+#include "rational.hh"
+
+Scalar::Scalar (Rational r)
+{
+  (*this) = r.str ();
+}
+
+Scalar::operator Rational ()
+{
+  int p = index_i ('/');
+  if (p == -1)
+    return int (*this);
+  
+  String s2 = right_str (length_i ()-p-1);
+  String s1 = left_str (p);
+
+  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();
+  assert (isnum_b ());
+  return value_f ();
 }
 
 Scalar::operator int()
 {
-    assert (isnum());
-    return value();
+  if (!length_i ())
+    return 0;                  // ugh
+  
+  assert (isnum_b());
+  return value_i ();
 }
+
+Scalar::operator bool () const
+{
+  return to_bool ();
+}
+
 bool
-Scalar::to_bool() const
+Scalar::to_bool () const
 {
-    if (!len())
-       return false;
-    if (*this == "0")
-       return false;
-    String u (*this);
-    u.upper();
-    if (u== "FALSE")
-       return false;
-    return true;
+  if (!length_i ())
+    return false;
+  if (*this == "0")
+    return false;
+  String u (*this);
+  if (u.upper_str () == "FALSE")
+    return false;
+  return true;
 }