/cdex1/tags/cdex_140_b6/cdexos/GenreTable.cpp
# · C++ · 235 lines · 166 code · 50 blank · 19 comment · 19 complexity · a05358ea5d31beea11d3a8a41a42e6ca MD5 · raw file
- /*
- ** Copyright (C) 2001 Albert L. Faber
- **
- ** This program is free software; you can redistribute it and/or modify
- ** it under the terms of the GNU General Public License as published by
- ** the Free Software Foundation; either version 2 of the License, or
- ** (at your option) any later version.
- **
- ** This program is distributed in the hope that it will be useful,
- ** but WITHOUT ANY WARRANTY; without even the implied warranty of
- ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- ** GNU General Public License for more details.
- **
- ** You should have received a copy of the GNU General Public License
- ** along with this program; if not, write to the Free Software
- ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
- */
- #include "StdAfx.h"
- #include "GenreTable.h"
- #include <ALGORITHM>
- #include "resource.h"
- // CONSTRUCTOR
- CGenreTable::CGenreTable()
- {
- }
- // DESTRUCTOR
- CGenreTable::~CGenreTable()
- {
- v_Entries.clear();
- }
- BYTE CGenreTable::GetID3V1ID( WORD wIdx ) const
- {
- ASSERT( wIdx < v_Entries.size() );
- return v_Entries[wIdx].btID3V1ID;
- }
- void CGenreTable::SetID3V1ID( WORD wIdx, BYTE btValue )
- {
- ASSERT( wIdx < v_Entries.size() );
- v_Entries[wIdx].btID3V1ID = btValue;
- }
- CString CGenreTable::GetGenre( WORD wIdx ) const
- {
- return v_Entries[wIdx].strGenre;
- }
- void CGenreTable::SetGenre( WORD wIdx, CString strValue )
- {
- v_Entries[wIdx].strGenre = strValue;
- }
- CString CGenreTable::GetCDDBGenre( WORD wIdx ) const
- {
- return v_Entries[wIdx].strCDDBGenre;
- }
- void CGenreTable::SetCDDBGenre( WORD wIdx, CString strValue )
- {
- v_Entries[wIdx].strCDDBGenre = strValue;
- }
- int CGenreTable::Load( CString strFileName )
- {
- FILE* pFile = NULL;
- CHAR lpszLine[255] = { '\0',};
- pFile = fopen( strFileName, "r" );
- int g_nNumGenres = 0;
- if ( pFile )
- {
- while ( NULL != fgets( lpszLine, sizeof( lpszLine ), pFile ) )
- {
- int i = 0;
- CHAR* lpszToken = NULL;
- int nIndex = -1;
- CString strCategory;
- CString strCDDBCategory;
- lpszToken = strtok( lpszLine, "\t" );
- GENRETABLEENTRY newEntry;
- newEntry.btID3V1ID = -1;
- while ( lpszToken != NULL )
- {
- switch ( i )
- {
- case 0:
- newEntry.btID3V1ID = atoi( lpszToken );
- i++;
- break;
- case 1:
- newEntry.strGenre = lpszToken;
- newEntry.strGenre.TrimLeft();
- newEntry.strGenre.TrimRight();
- i++;
- break;
- case 2:
- newEntry.strCDDBGenre = lpszToken;
- newEntry.strCDDBGenre.TrimLeft();
- newEntry.strCDDBGenre.TrimRight();
- i++;
- break;
- }
- lpszToken = strtok( NULL, "\t" );
- }
- if ( ( newEntry.btID3V1ID ) >= 0 && ( i >= 3 ) )
- {
- v_Entries.push_back( newEntry );
- }
- }
- fclose( pFile );
- }
- sort( v_Entries.begin(), v_Entries.end() );
- return v_Entries.size() ;
- }
- void CGenreTable::Save( CString strFileName )
- {
- FILE* pFile = NULL;
- CHAR lpszLine[255] = { '\0',};
- int i = 0;
- pFile = fopen( strFileName, "w" );
- if ( pFile )
- {
- for ( i = 0 ; i < v_Entries.size(); i++ )
- {
- CString strLine;
- GENRETABLEENTRY newEntry = v_Entries[i] ;
- strLine.Format( "%d\t%s\t%s\n",
- newEntry.btID3V1ID,
- newEntry.strGenre,
- newEntry.strCDDBGenre );
- fputs( strLine, pFile );
- }
- }
- fclose( pFile );
- }
- int CGenreTable::AddEntry( BYTE btID3V1ID, CString strGenre, CString strCDDBGenre )
- {
- GENRETABLEENTRY newEntry;
- newEntry.btID3V1ID = btID3V1ID;
- newEntry.strGenre = strGenre;
- newEntry.strGenre.TrimLeft();
- newEntry.strGenre.TrimRight();
- newEntry.strCDDBGenre = strCDDBGenre;
- newEntry.strCDDBGenre.TrimLeft();
- newEntry.strCDDBGenre.TrimRight();
- v_Entries.push_back( newEntry );
- return v_Entries.size() -1 ;
- }
- int CGenreTable::SearchGenre( CString strSearch ) const
- {
- int i;
- strSearch.TrimLeft();
- strSearch.TrimRight();
- for ( i = 0 ; i < v_Entries.size(); i++ )
- {
- if ( 0 == strSearch.CompareNoCase( GetGenre( i ) ) )
- {
- return i;
- }
- }
- return -1;
- }
- CString CGenreTable::GetID3V1GenreString( int nID3TagIdx ) const
- {
- int i;
- CString strRet;
- for ( i = 0 ; i < v_Entries.size(); i++ )
- {
- if ( nID3TagIdx == GetID3V1ID( i ) )
- {
- return GetGenre( i );
- }
- }
- strRet.LoadString( IDS_UNKNOWN );
- return strRet;
- }
- void CGenreTable::Sort( )
- {
- sort( v_Entries.begin(), v_Entries.end() );
- }
- int CGenreTable::SearchID3V1ID( int btSearch ) const
- {
- int i;
- for ( i = 0 ; i < v_Entries.size(); i++ )
- {
- if ( GetID3V1ID( i ) == btSearch )
- {
- return i;
- }
- }
- return -1;
- }