automatically strip redundant spaces from filenames

Effects, Recipes, Interfacing with other software, etc.
Forum rules
If you require help using Audacity, please post on the forum board relevant to your operating system:
Windows
Mac OS X
GNU/Linux and Unix-like
Post Reply
Edgar
Forum Crew
Posts: 2042
Joined: Thu Sep 03, 2009 9:13 pm
Operating System: Windows 10

automatically strip redundant spaces from filenames

Post by Edgar » Fri Aug 09, 2013 9:58 pm

Occasionally when I am inserting text into a label (for Export Multiple), or the file requester, it will have redundant (leading, trailing or extra-between-words) spaces - maybe even tabs. wxString has a partial solution:

Code: Select all

wxString::Trim
wxString& Trim(bool fromRight = true)

Removes white-space (space, tabs, form feed, newline and carriage return) from the left or from the right end of the string (right is default).
but will only strip from one end or the other so would need to be called twice and does not deal with multiple spaces between words.

If one is not concerned with Preferences creep one would probably add a preference to turn this automatic behavior on and off. Personally, I do not bother with this preference because I always want it on!

Here is the code:
at or near line number 32 in Export.h add two new function declarations:

Code: Select all

class TimeTrack;
class Mixer;

wxString Trim(const wxString & pString, const wxString & pWhitespace = wxT(" t"));
wxString Reduce(const wxString & pString, const wxString & pFiller = wxT(" "), const wxString & pWhitespace = wxT(" t"));

class FormatInfo
{
at or near line number 777 in Export.cpp define the functions:

Code: Select all

wxString Trim(const wxString & pString, const wxString & pWhitespace) {
    const auto stringBeginning = pString.find_first_not_of(pWhitespace);
    //if (stringBeginning == std::string::npos)
    if (stringBeginning == wxString::npos)
        return wxString(wxEmptyString); // no content

    const auto stringEnding = pString.find_last_not_of(pWhitespace);
    const auto stringRange = stringEnding - stringBeginning + 1;

    return pString.substr(stringBeginning, stringRange);
}

wxString Reduce(const wxString & pString, const wxString & pFiller, const wxString & pWhitespace)
{
    // trim first
    wxString result = Trim(pString, pWhitespace);

    // replace sub ranges
    auto beginSpace = result.find_first_of(pWhitespace);
    //while (beginSpace != std::string::npos)
    while (beginSpace != wxString::npos)
    {
        const auto endSpace = result.find_first_not_of(pWhitespace, beginSpace);
        const auto range = endSpace - beginSpace;

        result.replace(beginSpace, range, pFiller);

        const auto newStart = beginSpace + pFiller.length();
        beginSpace = result.find_first_of(pWhitespace, newStart);
    }

    return result;
}
at or near what will now be line number 827 in Export.cpp exercise the two new functions (note that Reduce calls Trim):

Code: Select all

bool Exporter::CheckFilename()
{
   //[…]
   // Strip leading and trailing spaces & remove redundant spaces
   wxString fileName(mFilename.GetName());
   fileName = Reduce (fileName);
   mFilename.SetName(fileName);
   
   if (!mProject->GetDirManager()->EnsureSafeFilename(mFilename))
      return false;
Note that I am using Reduce with only one parameter thus accepting the defaults. Both Reduce and Trim could be made a little simpler for this implementation by just completely eliminating the parameters which have default values but I left them in in case they could be useful in the future.

steve
Site Admin
Posts: 80752
Joined: Sat Dec 01, 2007 11:43 am
Operating System: Linux *buntu

Re: automatically strip redundant spaces from filenames

Post by steve » Mon Aug 26, 2013 11:03 am

This looks like a good case for a plug-in.
Nyquist could handle stripping/replacing spaces in labels very easily (for example, changing " My New album Tack 5 " to "my_new_album_track_05") if only Nyquist had read/write access to label tracks.
9/10 questions are answered in the FREQUENTLY ASKED QUESTIONS (FAQ)

waxcylinder
Forum Staff
Posts: 14585
Joined: Tue Jul 31, 2007 11:03 am
Operating System: Windows 10

Re: automatically strip redundant spaces from filenames

Post by waxcylinder » Fri Oct 04, 2013 3:27 pm

So is "somebody" going to write the plugin? :)

Peter
________________________________________FOR INSTANT HELP: (Click on Link below)
* * * * * FAQ * * * * * Tutorials * * * * * Audacity Manual * * * * *

steve
Site Admin
Posts: 80752
Joined: Sat Dec 01, 2007 11:43 am
Operating System: Linux *buntu

Re: automatically strip redundant spaces from filenames

Post by steve » Fri Oct 04, 2013 3:57 pm

waxcylinder wrote:So is "somebody" going to write the plugin? :)
Currently Nyquist plug-ins do not have read/write access to label tracks :( otherwise the plug-in would be done already.
I guess that this is my vote for giving Nyquist access to label tracks.
9/10 questions are answered in the FREQUENTLY ASKED QUESTIONS (FAQ)

Edgar
Forum Crew
Posts: 2042
Joined: Thu Sep 03, 2009 9:13 pm
Operating System: Windows 10

Re: automatically strip redundant spaces from filenames

Post by Edgar » Fri Oct 04, 2013 5:58 pm

As it turns out the supplied C++ code is not always reliable – just throw it out!

Robert J. H.
Posts: 3633
Joined: Thu May 31, 2012 8:33 am
Operating System: Windows 10

Re: automatically strip redundant spaces from filenames

Post by Robert J. H. » Fri Oct 04, 2013 7:50 pm

steve wrote:
waxcylinder wrote:So is "somebody" going to write the plugin? :)
Currently Nyquist plug-ins do not have read/write access to label tracks :( otherwise the plug-in would be done already.
I guess that this is my vote for giving Nyquist access to label tracks.
As well as mine.

Post Reply