[beast/devel: 12/16] BSE: use constants in IDL files and avoid C-isms



commit adc286e44f4197bed651b59ab8a0a8dc1ee95385
Author: Tim Janik <timj gnu org>
Date:   Thu Jan 3 03:34:12 2013 +0100

    BSE: use constants in IDL files and avoid C-isms
    
    The IDL GConfig definition now makes use of a special unicode sequence to
    expand various kinds of default arguments. Uses of GConfig structures are
    wrapped into a unicode string expansion function.

 bse/bsecore.idl   |   80 ++++++++++++++++++++----------------
 bse/bsegconfig.cc |  115 +++++++++++++++++++++++++++++++++++++++++++++++++---
 bse/bseutils.cc   |   19 ---------
 bse/bseutils.hh   |    3 -
 4 files changed, 152 insertions(+), 65 deletions(-)
---
diff --git a/bse/bsecore.idl b/bse/bsecore.idl
index 7b33833..19f3c45 100644
--- a/bse/bsecore.idl
+++ b/bse/bsecore.idl
@@ -710,19 +710,32 @@ record ThreadTotals {
   ThreadInfoSeq synthesis;
 };
 ThreadTotals    collect_thread_totals ();
-/* BSE Global Config */
+
+// == BSE Global Config ==
+Const DEFAULT_AUTHOR  = "\357\277\271\357\277\272{{bse.idl/default-author}}\357\277\273";
+Const DEFAULT_LICENSE = "\357\277\271\357\277\272{{bse.idl/default-license}}\357\277\273";
+Const USER_DATA_PATH  = "\357\277\271\357\277\272{{bse.idl/user-data-path}}\357\277\273";
+Const DEFAULT_SAMPLE_PATH      = USER_DATA_PATH "samples";
+Const DEFAULT_EFFECTS_PATH     = USER_DATA_PATH "effects";
+Const DEFAULT_INSTRUMENTS_PATH = USER_DATA_PATH "instruments";
+Const DEFAULT_SCRIPTS_PATH     = USER_DATA_PATH "scripts";
+Const DEFAULT_PLUGINS_PATH     = USER_DATA_PATH "plugins";
+
 record GConfig {
   group _("Synthesis Settings") {
-    Int synth_latency           = SfiInt (_("Latency [ms]"),
-                                   _("Processing duration between input and output of a single sample, smaller values increase CPU load"),
-                                   50, 1, 2000, 5, STANDARD);
-    Int synth_mixing_freq       = SfiInt (_("Synth Mixing Frequency"),
-                                   _("Synthesis mixing frequency, common values are: 22050, 44100, 48000"),
-                                   44100, 8000, 192000, 0, STANDARD);
-    Int synth_control_freq      = SfiInt (_("Synth Control Frequency"),
-                                   _("Frequency at which control values are evaluated, "
-                                     "should be much smaller than Synth Mixing Frequency to reduce CPU load"),
-                                   1000, 1, 192000, 0, STANDARD);
+    Int synth_latency =
+      SfiInt (_("Latency [ms]"),
+	      _("Processing duration between input and output of a single sample, smaller values increase CPU load"),
+	      50, 1, 2000, 5, STANDARD);
+    Int synth_mixing_freq =
+      SfiInt (_("Synth Mixing Frequency"),
+	      _("Synthesis mixing frequency, common values are: 22050, 44100, 48000"),
+	      44100, 8000, 192000, 0, STANDARD);
+    Int synth_control_freq =
+      SfiInt (_("Synth Control Frequency"),
+	      _("Frequency at which control values are evaluated, "
+		"should be much smaller than Synth Mixing Frequency to reduce CPU load"),
+	      1000, 1, 192000, 0, STANDARD);
   };
   group _("MIDI") {
     Bool invert_sustain = SfiBool (_("Invert Sustain Pedal"),
@@ -731,39 +744,34 @@ record GConfig {
   };
   group _("Default Values") {
     String author_default  = SfiString (_("Default Author"), _("Default value for 'Author' fields"),
-			      bse_intern_default_author(), STANDARD);
+					DEFAULT_AUTHOR, STANDARD);
     String license_default = SfiString (_("Default License"), _("Default value for 'License' fields"),
-			      bse_intern_default_license(), STANDARD);
+					DEFAULT_LICENSE, STANDARD);
   };
   group _("Search Paths") {
     String sample_path     = SfiString (_("Sample Path"),
-                              g_intern_printf (_("Search path of directories, seperated by \"%s\", used to find audio samples."),
-                                               G_SEARCHPATH_SEPARATOR_S),
-                              bse_intern_path_user_data ("/samples"), STANDARD ":searchpath");
+					_("Search path of directories, seperated by \";\", used to find audio samples."),
+					DEFAULT_SAMPLE_PATH, STANDARD ":searchpath");
     String effect_path     = SfiString (_("Effect Path"),
-                              g_intern_printf (_("Search path of directories, seperated by \"%s\", used to find BSE effect files."),
-                                               G_SEARCHPATH_SEPARATOR_S),
-                              bse_intern_path_user_data ("/effects"), STANDARD ":searchpath");
+					_("Search path of directories, seperated by \";\", used to find BSE effect files."),
+					DEFAULT_EFFECTS_PATH, STANDARD ":searchpath");
     String instrument_path = SfiString (_("Instrument Path"),
-                              g_intern_printf (_("Search path of directories, seperated by \"%s\", used to find BSE instrument files."),
-                                               G_SEARCHPATH_SEPARATOR_S),
-                              bse_intern_path_user_data ("/instruments"), STANDARD ":searchpath");
+					_("Search path of directories, seperated by \";\", used to find BSE instrument files."),
+					DEFAULT_INSTRUMENTS_PATH, STANDARD ":searchpath");
     String script_path     = SfiString (_("Script Path"),
-                              g_intern_printf (_("Search path of directories, seperated by \"%s\", used to find BSE scheme scripts."),
-                                               G_SEARCHPATH_SEPARATOR_S),
-                              bse_intern_path_user_data ("/scripts"), STANDARD ":searchpath");
+					_("Search path of directories, seperated by \";\", used to find BSE scheme scripts."),
+					DEFAULT_SCRIPTS_PATH, STANDARD ":searchpath");
     String plugin_path     = SfiString (_("Plugin Path"),
-                              g_intern_printf (_("Search path of directories, seperated by \"%s\", used to find BSE plugins. This path "
-                                                 "is searched for in addition to the standard BSE plugin location on this system."),
-                                               G_SEARCHPATH_SEPARATOR_S),
-                              bse_intern_path_user_data ("/plugins"), STANDARD ":searchpath");
+					_("Search path of directories, seperated by \";\", used to find BSE plugins. This path "
+					  "is searched for in addition to the standard BSE plugin location on this system."),
+					DEFAULT_PLUGINS_PATH, STANDARD ":searchpath");
     String ladspa_path     = SfiString (_("LADSPA Path"),
-                              g_intern_printf (_("Search path of directories, seperated by \"%s\", used to find LADSPA plugins. This "
-                                                 "path is searched for in addition to the standard LADSPA location on this system. "
-                                                 "LADSPA is the Linux Audio Developer's Simple Plugin API, more informations about "
-                                                 "LADSPA plugins can be found at http://www.ladspa.org/.";), G_SEARCHPATH_SEPARATOR_S),
-                              "", STANDARD ":searchpath");
+					_("Search path of directories, seperated by \";\", used to find LADSPA plugins. This "
+					  "path is searched for in addition to the standard LADSPA location on this system. "
+					  "LADSPA is the Linux Audio Developer's Simple Plugin API, more informations about "
+					  "LADSPA plugins can be found at http://www.ladspa.org/.";),
+					"", STANDARD ":searchpath");
   };
 };
-};
-/* vim:set ts=8 sts=2 sw=2 syntax=cpp: */
+
+} // Bse
diff --git a/bse/bsegconfig.cc b/bse/bsegconfig.cc
index 99f4fd0..b351b55 100644
--- a/bse/bsegconfig.cc
+++ b/bse/bsegconfig.cc
@@ -1,12 +1,20 @@
 // Licensed GNU LGPL v2.1 or later: http://www.gnu.org/licenses/lgpl.html
-#include	"bsegconfig.hh"
-#include	"bseserver.hh"
-#include	"bsepcmdevice.hh"	/* for frequency alignment */
-/* --- variables --- */
+#include "bsegconfig.hh"
+#include "bseserver.hh"
+#include "bsepcmdevice.hh"	/* for frequency alignment */
+#include "data/config-paths.h"
+#include <sys/types.h>
+#include <regex.h>
+
+using namespace Birnet;
+
+// == Declarations ==
+static BseGConfig*    gconfig_from_rec  (SfiRec *rec);
 BseGConfig           *bse_global_config = NULL;
 static GParamSpec    *pspec_global_config = NULL;
 static uint           gconfig_lock_count = 0;
-/* --- functions --- */
+
+// == functions ==
 void
 _bse_gconfig_init (void)
 {
@@ -25,12 +33,105 @@ _bse_gconfig_init (void)
   /* fill out missing values with defaults */
   g_param_value_validate (pspec_global_config, value);
   /* install global config */
-  gconfig = bse_gconfig_from_rec (rec);
+  gconfig = gconfig_from_rec (rec);
   bse_global_config = gconfig;
   /* cleanup */
   sfi_value_free (value);
   sfi_rec_unref (rec);
 }
+
+static const char*
+intern_path_user_data ()
+{
+  return g_intern_strconcat (BSE_PATH_USER_DATA ("/"), "", NULL);
+}
+
+static const char*
+intern_default_author ()
+{
+  const char *user = g_get_user_name();
+  const char *name = g_get_real_name();
+  if (name && user && name[0] && strcmp (user, name) != 0)
+    return g_intern_string (name);
+  return g_intern_static_string ("");
+}
+
+static const char*
+intern_default_license ()
+{
+  return g_intern_static_string ("Creative Commons Attribution 2.5 (http://creativecommons.org/licenses/by/2.5/)");
+}
+
+struct Substitutions {
+  typedef const char* (*SubstituteFunc) ();
+  const char *key;
+  SubstituteFunc sfunc;
+};
+
+static const Substitutions subs[] = {
+  { "bse.idl/default-author",   intern_default_author },
+  { "bse.idl/default-license",  intern_default_license },
+  { "bse.idl/user-data-path",   intern_path_user_data },
+  { NULL, NULL, },
+};
+
+static char*
+expand_sub14 (char *gstr)
+{
+  g_return_val_if_fail (gstr, gstr);
+  static regex_t preg = { 0, };
+  int rc;
+  if (UNLIKELY (!preg.re_nsub))
+    {
+      // MATCH: "\uFFF9\u001A\uFFFA{{...}}\uFFFB" - annotated SUB with contents: {{...}}
+      // UTF-8: "\xef\xbf\xb9\x1a\xef\xbf\xba{{...}}\xef\xbf\xbb";
+      const char *pattern = "\357\277\271\357\277\272\\{\\{([^{}]*)\\}\\}\357\277\273";
+      rc = regcomp (&preg, pattern, REG_EXTENDED); // FIXME: should be atomic
+      g_assert (rc == 0 && preg.re_nsub);
+      // if non-static: regfree (&preg);
+    }
+  regmatch_t pm[2] = { { 0, }, };
+  int so = 0;
+  rc = regexec (&preg, gstr + so, 2, pm, 0);
+  if (rc != 0)
+    return gstr; // no match
+  String result;
+  for ( ; rc == 0; so = pm[0].rm_eo, rc = regexec (&preg, gstr + so, 2, pm, REG_NOTBOL))
+    {
+      if (pm[0].rm_so > so)
+        result += String (gstr + so, pm[0].rm_so - so);
+      int i, l = pm[1].rm_eo - pm[1].rm_so;
+      for (i = 0; subs[i].key; i++)
+        if (strncmp (subs[i].key, gstr + pm[1].rm_so, l) == 0 && 0 == subs[i].key[l])
+          {
+            result += subs[i].sfunc();
+            i = -1;
+            break;
+          }
+      if (i != -1)
+        result += "{{" + String (gstr + pm[1].rm_so, l) + "}}";
+    }
+  if (gstr[so] != 0)
+    result += gstr + so;
+  g_free (gstr);
+  return g_strdup (result.c_str());
+}
+
+static BseGConfig*
+gconfig_from_rec (SfiRec *rec)
+{
+  BseGConfig *cnf = bse_gconfig_from_rec (rec);
+  cnf->author_default  = expand_sub14 (cnf->author_default);
+  cnf->license_default = expand_sub14 (cnf->license_default);
+  cnf->sample_path     = expand_sub14 (cnf->sample_path);
+  cnf->effect_path     = expand_sub14 (cnf->effect_path);
+  cnf->instrument_path = expand_sub14 (cnf->instrument_path);
+  cnf->script_path     = expand_sub14 (cnf->script_path);
+  cnf->plugin_path     = expand_sub14 (cnf->plugin_path);
+  cnf->ladspa_path     = expand_sub14 (cnf->ladspa_path);
+  return cnf;
+}
+
 static void
 set_gconfig (BseGConfig *gconfig)
 {
@@ -58,7 +159,7 @@ bse_gconfig_apply (SfiRec *rec)
       BseGConfig *gconfig;
       SfiRec *vrec = sfi_rec_copy_deep (rec);
       sfi_rec_validate (vrec, sfi_pspec_get_rec_fields (pspec_global_config));
-      gconfig = bse_gconfig_from_rec (vrec);
+      gconfig = gconfig_from_rec (vrec);
       sfi_rec_unref (vrec);
       set_gconfig (gconfig);
     }
diff --git a/bse/bseutils.cc b/bse/bseutils.cc
index c881eef..573409e 100644
--- a/bse/bseutils.cc
+++ b/bse/bseutils.cc
@@ -622,25 +622,6 @@ bse_string_equals (gconstpointer string1,
   else
     return string1 == string2;
 }
-const gchar*
-bse_intern_path_user_data (const gchar *dir)
-{
-  return g_intern_strconcat (BSE_PATH_USER_DATA ("/"), dir, NULL);
-}
-const gchar*
-bse_intern_default_author (void)
-{
-  const char *user = g_get_user_name();
-  const char *name = g_get_real_name();
-  if (name && user && name[0] && strcmp (user, name) != 0)
-    return g_intern_string (name);
-  return g_intern_static_string ("");
-}
-const gchar*
-bse_intern_default_license (void)
-{
-  return g_intern_static_string ("Creative Commons Attribution 2.5 (http://creativecommons.org/licenses/by/2.5/)");
-}
 void
 bse_bbuffer_puts (gchar        bbuffer[BSE_BBUFFER_SIZE],
 		  const gchar *string)
diff --git a/bse/bseutils.hh b/bse/bseutils.hh
index 133d878..c68a479 100644
--- a/bse/bseutils.hh
+++ b/bse/bseutils.hh
@@ -84,9 +84,6 @@ gint          bse_xinfo_stub_compare            (const gchar     *xinfo1,  /* mu
 guint		bse_string_hash			(gconstpointer   string);
 gint		bse_string_equals		(gconstpointer	 string1,
 						 gconstpointer	 string2);
-const gchar*    bse_intern_path_user_data       (const gchar    *dir);
-const gchar*    bse_intern_default_author	(void);
-const gchar*    bse_intern_default_license	(void);
 /* --- bbuffer utils --- */
 #define BSE_BBUFFER_SIZE        (128)
 void	bse_bbuffer_puts	(gchar       	 bbuffer[BSE_BBUFFER_SIZE],



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