/cdex1/tags/cdex_140_b6/cdexos/GenreTable.cpp

# · C++ · 235 lines · 166 code · 50 blank · 19 comment · 19 complexity · a05358ea5d31beea11d3a8a41a42e6ca MD5 · raw file

  1. /*
  2. ** Copyright (C) 2001 Albert L. Faber
  3. **
  4. ** This program is free software; you can redistribute it and/or modify
  5. ** it under the terms of the GNU General Public License as published by
  6. ** the Free Software Foundation; either version 2 of the License, or
  7. ** (at your option) any later version.
  8. **
  9. ** This program is distributed in the hope that it will be useful,
  10. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ** GNU General Public License for more details.
  13. **
  14. ** You should have received a copy of the GNU General Public License
  15. ** along with this program; if not, write to the Free Software
  16. ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17. */
  18. #include "StdAfx.h"
  19. #include "GenreTable.h"
  20. #include <ALGORITHM>
  21. #include "resource.h"
  22. // CONSTRUCTOR
  23. CGenreTable::CGenreTable()
  24. {
  25. }
  26. // DESTRUCTOR
  27. CGenreTable::~CGenreTable()
  28. {
  29. v_Entries.clear();
  30. }
  31. BYTE CGenreTable::GetID3V1ID( WORD wIdx ) const
  32. {
  33. ASSERT( wIdx < v_Entries.size() );
  34. return v_Entries[wIdx].btID3V1ID;
  35. }
  36. void CGenreTable::SetID3V1ID( WORD wIdx, BYTE btValue )
  37. {
  38. ASSERT( wIdx < v_Entries.size() );
  39. v_Entries[wIdx].btID3V1ID = btValue;
  40. }
  41. CString CGenreTable::GetGenre( WORD wIdx ) const
  42. {
  43. return v_Entries[wIdx].strGenre;
  44. }
  45. void CGenreTable::SetGenre( WORD wIdx, CString strValue )
  46. {
  47. v_Entries[wIdx].strGenre = strValue;
  48. }
  49. CString CGenreTable::GetCDDBGenre( WORD wIdx ) const
  50. {
  51. return v_Entries[wIdx].strCDDBGenre;
  52. }
  53. void CGenreTable::SetCDDBGenre( WORD wIdx, CString strValue )
  54. {
  55. v_Entries[wIdx].strCDDBGenre = strValue;
  56. }
  57. int CGenreTable::Load( CString strFileName )
  58. {
  59. FILE* pFile = NULL;
  60. CHAR lpszLine[255] = { '\0',};
  61. pFile = fopen( strFileName, "r" );
  62. int g_nNumGenres = 0;
  63. if ( pFile )
  64. {
  65. while ( NULL != fgets( lpszLine, sizeof( lpszLine ), pFile ) )
  66. {
  67. int i = 0;
  68. CHAR* lpszToken = NULL;
  69. int nIndex = -1;
  70. CString strCategory;
  71. CString strCDDBCategory;
  72. lpszToken = strtok( lpszLine, "\t" );
  73. GENRETABLEENTRY newEntry;
  74. newEntry.btID3V1ID = -1;
  75. while ( lpszToken != NULL )
  76. {
  77. switch ( i )
  78. {
  79. case 0:
  80. newEntry.btID3V1ID = atoi( lpszToken );
  81. i++;
  82. break;
  83. case 1:
  84. newEntry.strGenre = lpszToken;
  85. newEntry.strGenre.TrimLeft();
  86. newEntry.strGenre.TrimRight();
  87. i++;
  88. break;
  89. case 2:
  90. newEntry.strCDDBGenre = lpszToken;
  91. newEntry.strCDDBGenre.TrimLeft();
  92. newEntry.strCDDBGenre.TrimRight();
  93. i++;
  94. break;
  95. }
  96. lpszToken = strtok( NULL, "\t" );
  97. }
  98. if ( ( newEntry.btID3V1ID ) >= 0 && ( i >= 3 ) )
  99. {
  100. v_Entries.push_back( newEntry );
  101. }
  102. }
  103. fclose( pFile );
  104. }
  105. sort( v_Entries.begin(), v_Entries.end() );
  106. return v_Entries.size() ;
  107. }
  108. void CGenreTable::Save( CString strFileName )
  109. {
  110. FILE* pFile = NULL;
  111. CHAR lpszLine[255] = { '\0',};
  112. int i = 0;
  113. pFile = fopen( strFileName, "w" );
  114. if ( pFile )
  115. {
  116. for ( i = 0 ; i < v_Entries.size(); i++ )
  117. {
  118. CString strLine;
  119. GENRETABLEENTRY newEntry = v_Entries[i] ;
  120. strLine.Format( "%d\t%s\t%s\n",
  121. newEntry.btID3V1ID,
  122. newEntry.strGenre,
  123. newEntry.strCDDBGenre );
  124. fputs( strLine, pFile );
  125. }
  126. }
  127. fclose( pFile );
  128. }
  129. int CGenreTable::AddEntry( BYTE btID3V1ID, CString strGenre, CString strCDDBGenre )
  130. {
  131. GENRETABLEENTRY newEntry;
  132. newEntry.btID3V1ID = btID3V1ID;
  133. newEntry.strGenre = strGenre;
  134. newEntry.strGenre.TrimLeft();
  135. newEntry.strGenre.TrimRight();
  136. newEntry.strCDDBGenre = strCDDBGenre;
  137. newEntry.strCDDBGenre.TrimLeft();
  138. newEntry.strCDDBGenre.TrimRight();
  139. v_Entries.push_back( newEntry );
  140. return v_Entries.size() -1 ;
  141. }
  142. int CGenreTable::SearchGenre( CString strSearch ) const
  143. {
  144. int i;
  145. strSearch.TrimLeft();
  146. strSearch.TrimRight();
  147. for ( i = 0 ; i < v_Entries.size(); i++ )
  148. {
  149. if ( 0 == strSearch.CompareNoCase( GetGenre( i ) ) )
  150. {
  151. return i;
  152. }
  153. }
  154. return -1;
  155. }
  156. CString CGenreTable::GetID3V1GenreString( int nID3TagIdx ) const
  157. {
  158. int i;
  159. CString strRet;
  160. for ( i = 0 ; i < v_Entries.size(); i++ )
  161. {
  162. if ( nID3TagIdx == GetID3V1ID( i ) )
  163. {
  164. return GetGenre( i );
  165. }
  166. }
  167. strRet.LoadString( IDS_UNKNOWN );
  168. return strRet;
  169. }
  170. void CGenreTable::Sort( )
  171. {
  172. sort( v_Entries.begin(), v_Entries.end() );
  173. }
  174. int CGenreTable::SearchID3V1ID( int btSearch ) const
  175. {
  176. int i;
  177. for ( i = 0 ; i < v_Entries.size(); i++ )
  178. {
  179. if ( GetID3V1ID( i ) == btSearch )
  180. {
  181. return i;
  182. }
  183. }
  184. return -1;
  185. }