shithub: sox

Download patch

ref: 7743179418edd2c9ab1d87aeabd4b54a126f4501
parent: 9d0d017571ee90f29e1c8a051ca79d5d92bdd656
author: cbagwell <cbagwell>
date: Mon Sep 3 23:11:04 EDT 2007

Hide format handlers internals from applications.

--- a/libsox.3
+++ b/libsox.3
@@ -24,6 +24,8 @@
 .nf
 .B #include <sox.h>
 .P
+.B int sox_format_init(void);
+.P
 .B ft_t sox_open_read(const char *\fIpath\fB, const sox_signalinfo_t *\fIinfo\fB, const char *\fIfiletype\fB);
 .P
 .B ft_t sox_open_write(sox_bool (*\fIoverwrite_permitted\fB)(const char *\fIfilename\fB), const char *\fIpath\fB, const sox_signalinfo_t *\fIinfo\fB, const char *\fIfiletype\fB, const char *\fIcomment\fB, sox_size_t \fIlength\fB, const sox_instrinfo_t *\fIinstr\fB, const sox_loopinfo_t *\fIloops\fB);
@@ -52,6 +54,12 @@
 effects processors. It is mainly developed for use by SoX but is
 useful for any sound application.
 .P
+\fBsox_format_init\fR function performs some required initialization
+related to all file format handlers.  If compiled with dynamic
+library support then this will detect and initialize all external
+libraries.  This should be called before any other file operations
+are performed.
+.P
 \fBsox_open_input\fR function opens the file for reading whose name is
 the string pointed to by \fIpath\fR and associates an ft_t with it. If
 \fIinfo\fR is non-NULL then it will be used to specify the data format
@@ -92,6 +100,9 @@
 The \fBsox_close\fR function dissociates the named \fIft_t\fR from its
 underlying file or set of functions. If the format handler was being
 used for output, any buffered data is written first.
+.P
+\fBsox_format_quite\fR function performs some required cleanup
+related to all file format handlers.
 .P
 The function \fBsox_find_effect\fR finds effect \fIname\fR, returning
 a pointer to its \fIsox_effect_handler_t\fR if it exists, and NULL
--- a/src/sox.c
+++ b/src/sox.c
@@ -39,10 +39,6 @@
   #include <io.h>
 #endif
 
-#ifdef HAVE_LIBLTDL
-  #include <ltdl.h>
-#endif
-
 #ifdef HAVE_SYS_TIME_H
   #include <sys/time.h>
 #endif
@@ -144,14 +140,6 @@
 static sox_bool user_skip = sox_false;
 static int success = 0;
 
-
-/* Plugins */
-
-#ifdef HAVE_LIBLTDL
-static sox_bool plugins_initted = sox_false;
-#endif
-
-
 /* local forward declarations */
 
 static sox_bool parse_gopts_and_fopts(file_t, int, char **);
@@ -358,15 +346,7 @@
     free(ofile);
   }
 
-#ifdef HAVE_LIBLTDL
-  {
-    int ret;
-    if (plugins_initted && (ret = lt_dlexit()) != 0) {
-      sox_fail("lt_dlexit failed with %d error(s): %s", ret, lt_dlerror());
-      exit(1);
-    }
-  }
-#endif
+  sox_format_quit();
 }
 
 static file_t new_file(void)
@@ -618,47 +598,6 @@
   }
 }
 
-/* FIXME: Use vasprintf */
-#ifdef HAVE_LIBLTDL
-#define MAX_NAME_LEN 1024
-static int init_format(const char *file, lt_ptr data)
-{
-  lt_dlhandle lth = lt_dlopenext(file);
-  const char *end = file + strlen(file);
-  const char prefix[] = "libsox_fmt_";
-  char fnname[MAX_NAME_LEN];
-  char *start = strstr(file, prefix) + sizeof(prefix) - 1;
-
-  (void)data;
-  if (start < end) {
-    int ret = snprintf(fnname, MAX_NAME_LEN, "sox_%.*s_format_fn", end - start, start);
-    if (ret > 0 && ret < MAX_NAME_LEN) {
-      sox_format_fns[sox_formats].fn = (sox_format_fn_t)lt_dlsym(lth, fnname);
-      sox_debug("opening format plugin `%s': library %p, entry point %p\n", fnname, lth, sox_format_fns[sox_formats].fn);
-      if (sox_format_fns[sox_formats].fn)
-        sox_formats++;
-    }
-  }
-
-  return 0;
-}
-#endif
-
-static void find_formats(void)
-{
-#ifdef HAVE_LIBLTDL
-  int ret;
-
-  if ((ret = lt_dlinit()) != 0) {
-    sox_fail("lt_dlinit failed with %d error(s): %s", ret, lt_dlerror());
-    exit(1);
-  }
-  plugins_initted = sox_true;
-
-  lt_dlforeachfile(PKGLIBDIR, init_format, NULL);
-#endif
-}
-
 int main(int argc, char **argv)
 {
   size_t i;
@@ -675,15 +614,14 @@
   else if (strends(myname, "rec"))
     rec = sox_true;
 
+  sox_format_init();
+ 
   parse_options_and_filenames(argc, argv);
 
   if (sox_globals.verbosity > 2)
     display_SoX_version(stderr);
 
-  /* Load plugins (after options so we can output debugging messages
-     if desired) */
-  find_formats();
-  
+ 
   /* Make sure we got at least the required # of input filenames */
   input_count = file_count ? file_count - 1 : 0;
   if (input_count < (combine_method <= sox_concatenate ? 1 : 2))
--- a/src/sox.h
+++ b/src/sox.h
@@ -354,6 +354,7 @@
 extern const char * const sox_size_bits_str[];
 extern const char * const sox_encodings_str[];
 
+int sox_format_init(void);
 sox_format_t * sox_open_read(const char *path, const sox_signalinfo_t *info, 
                          const char *filetype);
 sox_format_t * sox_open_write(
@@ -374,7 +375,7 @@
 
 sox_format_handler_t const * sox_find_format(char const * name, sox_bool no_dev);
 int sox_gettype(sox_format_t *, sox_bool);
-sox_format_t * sox_initformat(void);
+void sox_format_quit(void);
 
 /*
  * Structures for effects.
--- a/src/soxio.c
+++ b/src/soxio.c
@@ -11,9 +11,63 @@
   #include <io.h>
 #endif
 
+#ifdef HAVE_LIBLTDL
+  #include <ltdl.h>
+#endif
+
 sox_globals_t sox_globals = {2, 8192, NULL, NULL, NULL, NULL};
 
+/* Plugins */
+
+#ifdef HAVE_LIBLTDL
+static sox_bool plugins_initted = sox_false;
+#endif
+
+/* FIXME: Use vasprintf */
+#ifdef HAVE_LIBLTDL
+#define MAX_NAME_LEN 1024
+static int init_format(const char *file, lt_ptr data)
+{
+  lt_dlhandle lth = lt_dlopenext(file);
+  const char *end = file + strlen(file);
+  const char prefix[] = "libsox_fmt_";
+  char fnname[MAX_NAME_LEN];
+  char *start = strstr(file, prefix) + sizeof(prefix) - 1;
+
+  (void)data;
+  if (start < end) {
+    int ret = snprintf(fnname, MAX_NAME_LEN, "sox_%.*s_format_fn", end - start, start);
+    if (ret > 0 && ret < MAX_NAME_LEN) {
+      sox_format_fns[sox_formats].fn = (sox_format_fn_t)lt_dlsym(lth, fnname);
+      sox_debug("opening format plugin `%s': library %p, entry point %p\n", fnname, lth, sox_format_fns[sox_formats].fn);
+      if (sox_format_fns[sox_formats].fn)
+        sox_formats++;
+    }
+  }
+
+  return 0;
+}
+#endif
+
 /*
+ * Initialize list of known format handlers.
+ */
+int sox_format_init(void)
+{
+#ifdef HAVE_LIBLTDL
+  int ret;
+
+  if ((ret = lt_dlinit()) != 0) {
+    sox_fail("lt_dlinit failed with %d error(s): %s", ret, lt_dlerror());
+    exit(1);
+  }
+  plugins_initted = sox_true;
+
+  lt_dlforeachfile(PKGLIBDIR, init_format, NULL);
+#endif
+}
+
+/*
  * Check that we have a known format suffix string.
  */
 int sox_gettype(sox_format_t * ft, sox_bool is_file_extension)
@@ -27,6 +81,22 @@
     sox_fail_errno(ft, SOX_EFMT, "unknown file type `%s'", ft->filetype);
   }
   return SOX_EFMT;
+}
+
+/*
+ * Cleanup things.
+ */
+void sox_format_quit(void)
+{
+#ifdef HAVE_LIBLTDL
+  {
+    int ret;
+    if (plugins_initted && (ret = lt_dlexit()) != 0) {
+      sox_fail("lt_dlexit failed with %d error(s): %s", ret, lt_dlerror());
+      exit(1);
+    }
+  }
+#endif
 }
 
 void set_endianness_if_not_already_set(sox_format_t * ft)