shithub: sox

Download patch

ref: 7289e815250f95d076562fdc438d1aa9b51f694f
parent: 10c6b92eb3dbd106e765942b4a868e7e9c0f2558
author: cbagwell <cbagwell>
date: Sat Sep 20 10:23:56 EDT 2008

Support substitutions in multiple output filename.

--- a/sox.1
+++ b/sox.1
@@ -475,9 +475,12 @@
 as the trim and silence effects so the application for multiple files
 is very limited.
 
-If the user specifies 'filename.ext' as the output file then SoX will 
-first use a filename of 'filename0001.ext' and will increment the number 
-for each new file.
+In multiple output mode, a unique number will automatically be appended
+to the end of the filename.  If the filename has an extension 
+then the number is inserted before the extension.  This behavior can
+be customized by placing a %n anywhere in the filename where the
+number should be substituted.  An optional number can be placed after
+the % to indicate a minimum fixed width for the number.
 
 .SS Stopping SoX
 Usually SoX will complete its processing and exit automatically, however
--- a/src/sox.c
+++ b/src/sox.c
@@ -579,7 +579,7 @@
     {
       if (user_effargs[i].argv[j])
         free(user_effargs[i].argv[j]);
-      user_effargs[i].argv[i] = NULL;
+      user_effargs[i].argv[j] = NULL;
     }
     user_effargs[i].argc = 0;
   }
@@ -958,8 +958,46 @@
 
     while (fn < end)
     {
-        /* FIXME: Look for %n and if found replacew with a sprintf of count */
-        *efn++ = *fn++;
+        /* Look for %n. If found, replace with count.  Can specify an
+         * option width of 1-9.
+         */
+        if (*fn == '%')
+        {
+            int width = 0;
+            fn++;
+            if (*fn >= '1' || *fn <= '9')
+            {
+                width = *fn++;
+            }
+            if (*fn == 'n')
+            {
+                char num[10];
+                char format[5];
+
+                found_marker = 1;
+
+                strcpy(format, "%");
+                if (width)
+                {
+                    char tmps[2];
+                    tmps[0] = width;
+                    tmps[1] = 0;
+                    strcat(format, "0");
+                    strcat(format, tmps);
+                }
+                strcat(format, "d");
+                strcpy(format, "%02d");
+                sprintf(num, format, count);
+                *efn = 0;
+                strcat(efn, num);
+                efn += strlen(num);
+                fn++;
+            }
+            else
+                *efn++ = *fn++;
+        }
+        else
+            *efn++ = *fn++;
     }
 
     *efn = 0;
@@ -971,7 +1009,7 @@
     {
         efn -= strlen (ext);
 
-        sprintf(efn, "%04lu", (unsigned long)count);
+        sprintf(efn, "%03lu", (unsigned long)count);
         efn = efn + 4;
         strcat(efn, ext);
     }