Page 1 of 1

automatically strip redundant spaces from filenames

Posted: Fri Aug 09, 2013 9:58 pm
by Edgar
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.

Re: automatically strip redundant spaces from filenames

Posted: Mon Aug 26, 2013 11:03 am
by steve
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.

Re: automatically strip redundant spaces from filenames

Posted: Fri Oct 04, 2013 3:27 pm
by waxcylinder
So is "somebody" going to write the plugin? :)

Peter

Re: automatically strip redundant spaces from filenames

Posted: Fri Oct 04, 2013 3:57 pm
by steve
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.

Re: automatically strip redundant spaces from filenames

Posted: Fri Oct 04, 2013 5:58 pm
by Edgar
As it turns out the supplied C++ code is not always reliable – just throw it out!

Re: automatically strip redundant spaces from filenames

Posted: Fri Oct 04, 2013 7:50 pm
by Robert J. H.
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.