[beast/devel: 27/35] BSE: math: remove char buffer hacks, use std::string returns in API



commit 4f09c51ffb5864fc420fc793ac122b48fc2fc798
Author: Tim Janik <timj gnu org>
Date:   Tue May 14 01:17:47 2013 +0200

    BSE: math: remove char buffer hacks, use std::string returns in API

 bse/bsemath.cc          |  167 ++++++++++++++---------------------------------
 bse/bsemath.hh          |   14 ++--
 bse/gslfilter.cc        |    2 +-
 tests/bse/filtertest.cc |    6 +-
 4 files changed, 61 insertions(+), 128 deletions(-)
---
diff --git a/bse/bsemath.cc b/bse/bsemath.cc
index 71dd869..e88ba8b 100644
--- a/bse/bsemath.cc
+++ b/bse/bsemath.cc
@@ -3,9 +3,6 @@
 #include <stdlib.h>
 #include <string.h>
 #include <stdio.h>
-#define RING_BUFFER_LENGTH     (256) // FIXME: simlpy dup strings in the API
-#define        PRINTF_DIGITS           "1270"
-#define        FLOAT_STRING_SIZE       (2048)
 
 /* --- functions --- */
 std::string
@@ -18,145 +15,84 @@ bse_string_from_double (long double value)
   return str.substr (0, s - &str[0]);
 }
 
-char*
-bse_complex_list (uint         n_points,
-                 BseComplex  *points,
-                 const char  *indent)
+std::string
+bse_complex_list (uint n_points, BseComplex *points, const std::string &indent)
 {
-  static uint rbi = 0;
-  static char* rbuffer[RING_BUFFER_LENGTH] = { NULL, };
-  char *s, *tbuffer = g_newa (char, (FLOAT_STRING_SIZE * 2 * n_points));
-  uint i;
-
-  rbi = (rbi + 1) % RING_BUFFER_LENGTH;
-  if (rbuffer[rbi] != NULL)
-    g_free (rbuffer[rbi]);
-  s = tbuffer;
-  for (i = 0; i < n_points; i++)
+  std::string string;
+  for (uint i = 0; i < n_points; i++)
     {
-      *s = 0;
-      if (indent)
-       strcat (s, indent);
-      while (*s)
-        s++;
-      *s = 0; strcat (s, bse_string_from_double (points[i].re).c_str()); s += strlen (s);
-      *s++ = ' ';
-      *s = 0; strcat (s, bse_string_from_double (points[i].im).c_str()); s += strlen (s);
-      *s++ = '\n';
+      string += indent;
+      string += bse_string_from_double (points[i].re);
+      string += ' ';
+      string += bse_string_from_double (points[i].im);
+      string += '\n';
     }
-  *s++ = 0;
-  rbuffer[rbi] = g_strdup (tbuffer);
-  return rbuffer[rbi];
+  return string;
 }
 
-char*
+std::string
 bse_complex_str (BseComplex c)
 {
-  static uint rbi = 0;
-  static char* rbuffer[RING_BUFFER_LENGTH] = { NULL, };
-  char *s, tbuffer[FLOAT_STRING_SIZE * 2];
-
-  rbi = (rbi + 1) % RING_BUFFER_LENGTH;
-  if (rbuffer[rbi] != NULL)
-    g_free (rbuffer[rbi]);
-  s = tbuffer;
-  *s++ = '{';
-  *s = 0; strcat (s, bse_string_from_double (c.re).c_str()); s += strlen (s);
-  *s++ = ',';
-  *s++ = ' ';
-  *s = 0; strcat (s, bse_string_from_double (c.im).c_str()); s += strlen (s);
-  *s++ = '}';
-  *s++ = 0;
-  rbuffer[rbi] = g_strdup (tbuffer);
-  return rbuffer[rbi];
+  std::string s;
+  s += '{';
+  s += bse_string_from_double (c.re);
+  s += ", ";
+  s += bse_string_from_double (c.im);
+  s += '}';
+  return s;
 }
 
-char*
-bse_poly_str (uint         degree,
-             double      *a,
-             const char  *var)
+std::string
+bse_poly_str (uint degree, double *a, const std::string &uvar)
 {
-  static uint rbi = 0;
-  static char* rbuffer[RING_BUFFER_LENGTH] = { NULL, };
-  char *s, *tbuffer = g_newa (char, degree * FLOAT_STRING_SIZE);
+  const std::string var = uvar.empty() ? "x" : uvar;
+  std::string s;
+  s += '(';
+  s += bse_string_from_double (a[0]);
   uint i;
-
-  if (!var)
-    var = "x";
-  rbi = (rbi + 1) % RING_BUFFER_LENGTH;
-  if (rbuffer[rbi] != NULL)
-    g_free (rbuffer[rbi]);
-  s = tbuffer;
-  *s++ = '(';
-  *s = 0; strcat (s, bse_string_from_double (a[0]).c_str()); s += strlen (s);
   for (i = 1; i <= degree; i++)
     {
-      *s++ = '+';
-      *s = 0; strcat (s, var); while (*s) s++;
-      *s++ = '*';
-      *s++ = '(';
-      *s = 0; strcat (s, bse_string_from_double (a[i]).c_str()); s += strlen (s);
+      s += '+';
+      s += var;
+      s += "*(";
+      s += bse_string_from_double (a[i]);
     }
   while (i--)
-    *s++ = ')';
-  *s++ = 0;
-  rbuffer[rbi] = g_strdup (tbuffer);
-  return rbuffer[rbi];
+    s += ')';
+  return s;
 }
 
-char*
-bse_poly_str1 (uint         degree,
-              double      *a,
-              const char  *var)
+std::string
+bse_poly_str1 (uint degree, double *a, const std::string &uvar)
 {
-  static uint rbi = 0;
-  static char* rbuffer[RING_BUFFER_LENGTH] = { NULL, };
-  char *s, *tbuffer = g_newa (char, degree * FLOAT_STRING_SIZE);
-  uint i, need_plus = 0;
-
-  if (!var)
-    var = "x";
-  rbi = (rbi + 1) % RING_BUFFER_LENGTH;
-  if (rbuffer[rbi] != NULL)
-    g_free (rbuffer[rbi]);
-  s = tbuffer;
-  *s++ = '(';
+  const std::string var = uvar.empty() ? "x" : uvar;
+  std::string s;
+  bool need_plus = 0;
+  s += '(';
   if (a[0] != 0.0)
     {
-      *s = 0; strcat (s, bse_string_from_double (a[0]).c_str()); s += strlen (s);
-      need_plus = 1;
+      s += bse_string_from_double (a[0]);
+      need_plus = true;
     }
+  uint i;
   for (i = 1; i <= degree; i++)
     {
       if (a[i] == 0.0)
        continue;
       if (need_plus)
-       {
-         *s++ = ' ';
-         *s++ = '+';
-         *s++ = ' ';
-       }
+        s += " + ";
       if (a[i] != 1.0)
        {
-          *s = 0; strcat (s, bse_string_from_double (a[i]).c_str()); s += strlen (s);
-         *s++ = '*';
+          s += bse_string_from_double (a[i]);
+         s += '*';
        }
-      *s = 0;
-      strcat (s, var);
-      while (*s) s++;
+      s += var;
       if (i > 1)
-       {
-         *s++ = '*';
-         *s++ = '*';
-         sprintf (s, "%u", i);
-         while (*s) s++;
-       }
-      need_plus = 1;
+        s += Rapicorn::string_format ("**%u", i);
+      need_plus = true;
     }
-  *s++ = ')';
-  *s++ = 0;
-  rbuffer[rbi] = g_strdup (tbuffer);
-  return rbuffer[rbi];
+  s += ')';
+  return s;
 }
 
 void
@@ -166,7 +102,7 @@ bse_complex_gnuplot (const char  *file_name,
 {
   FILE *fout = fopen (file_name, "w");
 
-  fputs (bse_complex_list (n_points, points, ""), fout);
+  fputs (bse_complex_list (n_points, points, "").c_str(), fout);
   fclose (fout);
 }
 
@@ -180,12 +116,7 @@ bse_float_gnuplot (const char    *file_name,
   FILE *fout = fopen (file_name, "w");
   uint i;
   for (i = 0; i < n_ypoints; i++)
-    {
-      char xbuf[FLOAT_STRING_SIZE], *xstr = xbuf, ybuf[FLOAT_STRING_SIZE], *ystr = ybuf;
-      *xstr = 0; strcat (xstr, bse_string_from_double (xstart + i * xstep).c_str()); xstr += strlen (xstr);
-      *ystr = 0; strcat (ystr, bse_string_from_double (ypoints[i]).c_str()); ystr += strlen (ystr);
-      fprintf (fout, "%s %s\n", xstr, ystr);
-    }
+    fprintf (fout, "%s %s\n", bse_string_from_double (xstart + i * xstep).c_str(), bse_string_from_double 
(ypoints[i]).c_str());
   fclose (fout);
 }
 
diff --git a/bse/bsemath.hh b/bse/bsemath.hh
index 92e2eec..985f560 100644
--- a/bse/bsemath.hh
+++ b/bse/bsemath.hh
@@ -78,10 +78,10 @@ static inline BseComplex bse_complex_tan        (BseComplex     c);
 static inline BseComplex bse_complex_sinh       (BseComplex     c);
 static inline BseComplex bse_complex_cosh       (BseComplex     c);
 static inline BseComplex bse_complex_tanh       (BseComplex     c);
-char*                    bse_complex_str        (BseComplex     c);
-char*                    bse_complex_list       (uint           n_points,
+std::string              bse_complex_str        (BseComplex     c);
+std::string              bse_complex_list       (uint           n_points,
                                                  BseComplex    *points,
-                                                 const char    *indent);
+                                                 const std::string &indent);
 void                     bse_complex_gnuplot    (const char    *file_name,
                                                  uint           n_points,
                                                  BseComplex    *points);
@@ -133,12 +133,12 @@ gboolean               bse_poly2_droots         (gdouble        roots[2],
                                                  gdouble        a,
                                                  gdouble        b,
                                                  gdouble        c);
-char*                  bse_poly_str             (uint           degree,
+std::string            bse_poly_str             (uint           degree,
                                                  double        *a,
-                                                 const char    *var);
-char*                  bse_poly_str1            (uint           degree,
+                                                 const std::string &var);
+std::string            bse_poly_str1            (uint           degree,
                                                  double        *a,
-                                                 const char    *var);
+                                                 const std::string &var);
 
 /* --- transformations --- */
 double                 bse_temp_freq            (double         kammer_freq,
diff --git a/bse/gslfilter.cc b/bse/gslfilter.cc
index 19af598..27d8305 100644
--- a/bse/gslfilter.cc
+++ b/bse/gslfilter.cc
@@ -776,7 +776,7 @@ gsl_filter_tscheb1_test     (uint         iorder,
 
   /* choose roots from the left half-plane */
   if (0)
-    g_print ("zhqr-root:\n%s\n", bse_complex_list (iorder, roots, "  "));
+    g_print ("zhqr-root:\n%s\n", bse_complex_list (iorder, roots, "  ").c_str());
   r = roots;
   for (i = 0; i < iorder; i++)
     if (roots[i].re < 0)
diff --git a/tests/bse/filtertest.cc b/tests/bse/filtertest.cc
index b2b95bc..a5662cb 100644
--- a/tests/bse/filtertest.cc
+++ b/tests/bse/filtertest.cc
@@ -342,8 +342,10 @@ public:
     /* we don't need H(z) that because we do it ourselves, but we can print it
      * in case somebody wants to play around with gnuplot
      */
-    fprintf (gp_file, "H(z)=%s/%s\n", bse_poly_str (m_order, &m_a[0], "z"),
-                                     bse_poly_str (m_order, &m_b[0], "z"));
+    fprintf (gp_file,
+             "H(z)=%s/%s\n",
+             bse_poly_str (m_order, &m_a[0], "z").c_str(),
+             bse_poly_str (m_order, &m_b[0], "z").c_str());
     fprintf (gp_file, "load '../../bse/tests/filter-defs.gp'\n");
     fprintf (gp_file, "call '../../bse/tests/arrows.gp' %zd", m_gp_arrows.size());
     for (set<double>::iterator ai = m_gp_arrows.begin(); ai != m_gp_arrows.end(); ai++)


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]