www.pudn.com > mp3.rar > mp3baptismDlg.cpp


/* 
 
Full Source Code of MP3 Baptism version 1.0 
 
Author: Lucien Wang (Wang Zhen) 
Email: lucienwang@fudan.edu (alternative: gnosisoft@email.com) 
 
Brief Intro: It's a handy tool to rename your MP3 collection on a fly 
             with customizable formats, designed as a component of 
             LyricsShow suite for Winamp 
 
Dev Kit: Visual C++ 6.0 Enterprise Edition 
(No extra package required to recompile it) 
 
Beginning of the Project: Feb. 4th, 2001 
Release Date: Feb. 5th, 2001 
 
License: 
        This software is provided 'as-is', without any express or implied 
        warranty.  In no event will the authors be held liable for any damages 
        arising from the use of this software or its source code. 
 
        Permission is granted to anyone to use this software and its source code 
        for any purpose, including commercial applications, and to alter it 
        and redistribute it freely, subject to the following restrictions: 
        1. The origin of this software may not be misrepresented. 
        2. Altered source versions must be plainly marked as such, and must not 
           be misrepresented as being the original software. 
        3. This notice may not be removed or altered from any source distribution. 
 
Comments: 
        I made this software as most of the MP3 renaming tools I found were 
        either uncustomizable or too bulky.  The name "MP3 Baptism" is merely 
        a metaphor, not as religious as it may sound like.  I intend to keep 
        it public domain, even GNU is not involved. 
 
        Now you have your hands on the full source code, but my enthusiasm for 
        further development had faded when this tool gracefully renamed my whole 
        big MP3 collection.  If you happen to have the impulse to upgrade it, 
        feel free to let me know.  I'll appreciate it a lot if you achieve great 
        improvements on MP3 Baptism.  But, don't suggest ways to better it, 
        leave them in your own to-do list instead.  Don't ask questions about 
        the source codes(or other programming stuff), they look quite self- 
        explanatory I suppose.  And don't bash the imperfection in source codes, 
        after all, the program is written in a couple of hours, totally in a hurry. 
        And this is the first time I have formally practiced MFC. This may sound 
        a bit surprising, but it's true that most of my VC++ projects are MFC-free. 
        Personally I think MFC is coming to its fate, the same case with MP3. 
 
        Well, happy coding! 
 
 
 
        Yours, 
 
        Lucien Wang 
 
 
*/ 
 
// mp3baptismDlg.cpp : implementation file 
// 
 
#include "stdafx.h" 
#include "mp3baptism.h" 
#include "mp3baptismDlg.h" 
#include "DirDialog.h" 
 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
#define LOG(block) if(m_log){block} 
#define SLASH _T('\\') 
#define DOT _T('.') 
#define SPACE _T(' ') 
#define PERCENTSIGN _T('%') 
#define TAGHEADER _T("TAG") 
#define TITLEID _T('1') 
#define ARTISTID _T('2') 
#define ALBUMID _T('3') 
#define MSG_FAILTOLOG _T("Unable to open log file :(") 
#define FSTR_LOGHEADER _T("\r\n------\r\n%s %s") 
#define FSTR_LOGEVENT _T("\r\n[%d]\t'%s'=>'%s'") 
#define FSTR_DONE _T("%d file(s) renamed.") 
#define FSTR_DONEWITHLOG _T("%d file(s) renamed.\nWanna view the log file?") 
#define FSTR_COMMANDLINE _T("notepad.exe ""%s""") 
#define STR_LOGSUCCESS _T("\t[OK]") 
#define STR_LOGFAILURE _T("\t[failed]") 
#define STR_LOGFOOTER _T("\r\nThe end\r\n------") 
#define STR_NOTIFICATION _T("Notification") 
#define STR_HINT _T("Hint") 
#define STR_ABOUT _T("About") 
#define STR_OPENFILETYPE _T("MP3 files (*.mp3)|*.mp3||") 
#define STR_BROWSEFOLDERTITLE _T("Please specify the folder of MP3") 
#define STR_DEFAULTEXT _T("*.mp3") 
#define STR_SLASHANDDEFAULTEXT _T("\\*.mp3") 
#define STR_USAGE _T("%1 == Song title\n%2 == Artist\n%3 == Album\n\nWith these in mind, you can change your own style for MP3 filenames or just simply choose one of the predefined formats in the list. Once ready, click 'start' to proceed.") 
#define STR_INTRO _T("MP3 Baptism version 1.0\nAuthor: Lucien Wang (lucienwang@fudan.edu)\n\nThis helps you rename your MP3 collection on a fly.\n(originally designed as a component of LyricsShow for Winamp)\n\nMP3 Baptism is a freeware, moreover, it's open-source:)\n(please check readme.txt for more info)\nEnjoy!") 
 
///////////////////////////////////////////////////////////////////////////// 
// CMp3baptismDlg dialog 
 
CMp3baptismDlg::CMp3baptismDlg(CWnd* pParent /*=NULL*/) 
	: CDialog(CMp3baptismDlg::IDD, pParent) 
{ 
	//{{AFX_DATA_INIT(CMp3baptismDlg) 
	m_mp3location = _T("*.mp3"); 
	m_nametemplate = _T("%2 - %1.mp3"); 
	m_logfilename = _T("log.txt"); 
	m_log = FALSE; 
	m_clearoldlog = FALSE; 
	//}}AFX_DATA_INIT 
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); 
} 
 
void CMp3baptismDlg::DoDataExchange(CDataExchange* pDX) 
{ 
	CDialog::DoDataExchange(pDX); 
	//{{AFX_DATA_MAP(CMp3baptismDlg) 
	DDX_Text(pDX, IDC_LOCATION, m_mp3location); 
	DDX_Text(pDX, IDC_NAMETEMPLATE, m_nametemplate); 
	DDX_Text(pDX, IDC_LOGFILENAME, m_logfilename); 
	DDX_Check(pDX, IDC_LOGCHECK, m_log); 
	DDX_Check(pDX, IDC_CLEARLOGCHECK, m_clearoldlog); 
	//}}AFX_DATA_MAP 
} 
 
BEGIN_MESSAGE_MAP(CMp3baptismDlg, CDialog) 
	//{{AFX_MSG_MAP(CMp3baptismDlg) 
	ON_WM_PAINT() 
	ON_WM_QUERYDRAGICON() 
	ON_BN_CLICKED(IDC_START, OnStart) 
	ON_BN_CLICKED(IDC_LOCATE, OnOpendlg) 
	ON_BN_CLICKED(IDC_LOGCHECK, OnLogcheck) 
	ON_BN_CLICKED(IDC_GIVEAHINT, OnGivehints) 
	ON_BN_CLICKED(IDC_ABOUT, OnAbout) 
	//}}AFX_MSG_MAP 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CMp3baptismDlg message handlers 
 
BOOL CMp3baptismDlg::OnInitDialog() 
{ 
	CDialog::OnInitDialog(); 
 
	SetIcon(m_hIcon, TRUE);			// Set big icon 
	SetIcon(m_hIcon, FALSE);		// Set small icon 
	 
	// TODO: Add extra initialization 
	OnLogcheck(); 
	return TRUE;  // return TRUE  unless you set the focus to a control 
} 
 
// If you add a minimize button to your dialog, you will need the code below 
//  to draw the icon.  For MFC applications using the document/view model, 
//  this is automatically done for you by the framework. 
 
void CMp3baptismDlg::OnPaint()  
{ 
	if (IsIconic()) 
	{ 
		CPaintDC dc(this); // device context for painting 
 
		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0); 
 
		// Center icon in client rectangle 
		int cxIcon = GetSystemMetrics(SM_CXICON); 
		int cyIcon = GetSystemMetrics(SM_CYICON); 
		CRect rect; 
		GetClientRect(&rect); 
		int x = (rect.Width() - cxIcon + 1) / 2; 
		int y = (rect.Height() - cyIcon + 1) / 2; 
 
		// Draw the icon 
		dc.DrawIcon(x, y, m_hIcon); 
	} 
	else 
	{ 
		CDialog::OnPaint(); 
	} 
} 
 
HCURSOR CMp3baptismDlg::OnQueryDragIcon() 
{ 
	return (HCURSOR) m_hIcon; 
} 
 
//Trim off trailing space characters 
void trimspace(char *c) 
{ 
	for(*(c--)=NULL;SPACE==*c||NULL==*c;c--)*c=NULL; 
} 
 
void CMp3baptismDlg::OnStart()  
{ 
	// TODO: Add your control notification handler code here 
 
	//For specs of id3tag v1 and v2, please visit http://www.id3.org 
	//Following codes ignore id3tag v2 
	CFile mp3file,logfile; 
	CStringArray candidates, christening; 
	TCHAR *oldname, *newname, *c, 
		  oldpath[MAX_PATH], newpath[MAX_PATH], loginfo[MAX_PATH], datebuf[11], timebuf[9], 
		  id3tagv1[128], title[30+1], artist[30+1], album[30+1]; 
	WIN32_FIND_DATA fd; 
	HANDLE hff; 
	BOOL icanlog=FALSE; 
	int i, fcount=0; 
 
	//Extract the path 
	UpdateData(TRUE); 
	_tcscpy(oldpath, m_mp3location); 
	if(NULL==(oldname=_tcsrchr(oldpath,SLASH))) 
		oldname=oldpath; 
	else 
		*(++oldname)=NULL; 
	 
	_tcscpy(newpath, oldpath); 
	newname=newpath+_tcslen(newpath); 
 
 
	LOG( 
		if(m_clearoldlog) 
			icanlog=logfile.Open(m_logfilename,CFile::modeCreate|CFile::modeWrite); 
		else 
			icanlog=logfile.Open(m_logfilename,CFile::modeCreate|CFile::modeNoTruncate|CFile::modeWrite); 
		if(!icanlog) 
			MessageBox(MSG_FAILTOLOG, STR_NOTIFICATION, MB_ICONSTOP); 
		else 
		{ 
			logfile.SeekToEnd(); 
			_strdate(datebuf); 
			_strtime(timebuf); 
			_stprintf(loginfo, FSTR_LOGHEADER, timebuf, datebuf); 
			logfile.Write(loginfo, _tcslen(loginfo)); 
		} 
    ) 
 
	//Look for mp3 files 
	if(INVALID_HANDLE_VALUE!=(hff=FindFirstFile(m_mp3location, &fd))) 
	do 
	{ 
		if(NULL==_tcsrchr(fd.cFileName,DOT)||DOT==fd.cFileName[0])continue; 
		_tcscpy(oldname, fd.cFileName); 
		if(!mp3file.Open(oldpath,CFile::modeRead|CFile::typeBinary))continue; 
		//Look for id3tag v1 
		if(128<=mp3file.GetLength()) 
		{ 
			mp3file.Seek(-128, CFile::end); 
			mp3file.Read(id3tagv1, 128); 
			mp3file.Close(); 
			//Validate the tag 
			if(!_tcsnccmp(id3tagv1, TAGHEADER, 3)) 
			{ 
				//Extract tags 
				_tcsncpy(title, &id3tagv1[3], 30); trimspace(&title[30]); 
				_tcsncpy(artist, &id3tagv1[3+30], 30); trimspace(&artist[30]); 
				_tcsncpy(album, &id3tagv1[3+30+30], 30); trimspace(&album[30]); 
				 
				//Generate a new filename 
				for(c=newname, i=0;iEnableWindow(m_log); 
	GetDlgItem(IDC_CLEARLOGCHECK)->EnableWindow(m_log);	 
} 
 
void CMp3baptismDlg::OnAbout()  
{ 
	// TODO: Add your control notification handler code here 
	MessageBox(STR_INTRO, STR_ABOUT, MB_ICONINFORMATION);	 
} 
 
// The end of crappy code