xml-copy-editor-code/src/playlistrenamer.cpp

144 lines
3.9 KiB
C++
Raw Normal View History

2009-11-06 18:10:56 +01:00
#include <sstream>
#include "playlistrenamer.h"
#include "binaryfile.h"
#include "replace.h"
#include "wrapregex.h"
#include <wx/wx.h>
bool PlayListRenamer::run (
const std::string& folder )
{
std::string m3uFile, m3uBuffer;
m3uFile = folder + "playlist.m3u";
if ( !readFile ( m3uFile, m3uBuffer ) )
return false;
std::vector<std::string> lines;
if ( !splitBuffer ( m3uBuffer, lines ) )
return false;
std::string title, from, to;
size_t lineCount = lines.size();
int trackNo = 0;
for ( size_t i = 0; i < lineCount; i++ )
{
if ( lines[i][0] == '#' )
{
if ( !lines[i].find ( "EXTINF" ) )
continue;
//isolate m3u trackname
std::string pattern0 = ".*?,";
WrapRegex re0 ( pattern0, true );
int replacements;
title = re0.replaceGlobal ( lines[i], &replacements );
bool abbrev = false;
if ( title.size() > 22 )
{
title.erase ( 18 );
abbrev = true;
}
// no trailing whitespace
std::string pattern1 = "\\s+$";
WrapRegex re1 ( pattern1, false );
title = re1.replaceGlobal ( title, &replacements );
// no contiguous whitespace
std::string pattern2 = "\\s\\s+";
std::string replace2 = " ";
WrapRegex re2 ( pattern2, false, replace2 );
title = re2.replaceGlobal ( title, &replacements );
// alphanumeric only
std::string pattern3 = "[;\\.\\[\\],!^&*()]";
WrapRegex re3 ( pattern3, false );
title = re3.replaceGlobal ( title, &replacements );
if ( abbrev )
title.append ( "... " );
title += ".mp3";
}
else
{
from = lines[i];
to = title;
renameFile ( from, to, folder );
editFiles ( from, to, folder );
title.clear();
}
}
}
bool PlayListRenamer::readFile ( const std::string& path, std::string& buffer )
{
buffer.clear();
try {
BinaryFile bf ( path.c_str() );
buffer.append ( bf.getData(), bf.getDataLen() );
}
catch ( ... )
{
return false;
}
return true;
}
bool PlayListRenamer::splitBuffer ( const std::string& buffer, std::vector<std::string> lineVector )
{
std::string line;
lineVector.clear();
size_t bufferSize = buffer.size();
for ( size_t i = 0; i < bufferSize; i++ )
{
if ( buffer[i] == '\n' )
{
if ( !line.empty() )
{
lineVector.push_back ( line );
line.clear();
}
}
else if ( buffer[i] == '\r' )
{
;
}
else
line.append ( buffer[i], 1 );
}
if ( !line.empty() )
lineVector.push_back ( line );
if ( lineVector.empty() )
return false;
return true;
}
std::string PlayListRenamer::numToString ( int i)
{
std::stringstream sstr;
sstr << i;
return sstr.str();
}
void PlayListRenamer::renameFile ( const std::string& from, const std::string& to, const std::string& folder )
{
wxString wideFrom, wideTo, wideFolder;
wideFolder = wxString ( folder.c_str(), wxConvUTF8, folder.size() );
wideFrom = wxString ( from.c_str(), wxConvUTF8, from.size() );
wideTo = wxString ( to.c_str(), wxConvUTF8, to.size() );
wxRenameFile ( wideFolder + wideFrom, wideFolder + wideTo );
}
void PlayListRenamer::editFiles ( const std::string& from, const std::string& to, const std::string& folder )
{
//tbd
}