コード例 #1
0
bool CKaraokeLyricsTextLRC::Load()
{
  XFILE::CFile file;

  // Clear the lyrics array
  clearLyrics();

  if ( !file.Open( m_lyricsFile ) )
    return false;

  unsigned int lyricSize = (unsigned int) file.GetLength();

  if ( !lyricSize )
  {
    CLog::Log( LOGERROR, "LRC lyric loader: lyric file %s has zero length", m_lyricsFile.c_str() );
    return false;
  }

  // Read the file into memory array
  std::vector<char> lyricData( lyricSize );

  file.Seek( 0, SEEK_SET );

  // Read the whole file
  if ( file.Read( &lyricData[0], lyricSize) != lyricSize )
    return false; // disk error?

  file.Close();

  // Parse the correction value
  int timing_correction = MathUtils::round_int( g_advancedSettings.m_karaokeSyncDelayLRC * 10 );

  unsigned int offset = 0;

  CStdString ext, songfilename = getSongFile();
  URIUtils::GetExtension( songfilename, ext );

  // Skip windoze UTF8 file prefix, if any, and reject UTF16 files
  if ( lyricSize > 3 )
  {
    if ( (unsigned char)lyricData[0] == 0xFF && (unsigned char)lyricData[1] == 0xFE )
    {
      CLog::Log( LOGERROR, "LRC lyric loader: lyrics file is in UTF16 encoding, must be in UTF8" );
      return false;
    }

    // UTF8 prefix added by some windoze apps
    if ( (unsigned char)lyricData[0] == 0xEF && (unsigned char)lyricData[1] == 0xBB && (unsigned char)lyricData[2] == 0xBF )
      offset = 3;
  }

  if (checkMultiTime(&lyricData[offset], lyricSize - offset))
    return ParserMultiTime(&lyricData[offset], lyricSize - offset, timing_correction);
  else
    return ParserNormal(&lyricData[offset], lyricSize - offset, timing_correction);
}
コード例 #2
0
std::vector< CStdString > CKaraokeLyricsTextUStar::readFile(const CStdString & lyricsFile, bool report_errors )
{
  std::vector< CStdString > lines;

  XFILE::CFile file;

  if ( !file.Open( lyricsFile ) )
    return std::vector< CStdString >();

  unsigned int lyricSize = (unsigned int) file.GetLength();

  if ( !lyricSize )
  {
    if ( report_errors )
      CLog::Log( LOGERROR, "UStar lyric loader: lyric file %s has zero length", lyricsFile.c_str() );

    return std::vector< CStdString >();
  }

  // Read the file into memory array
  std::vector<char> lyricData( lyricSize );

  file.Seek( 0, SEEK_SET );

  // Read the whole file
  if ( file.Read( &lyricData[0], lyricSize) != lyricSize )
    return std::vector< CStdString >(); // disk error?

  file.Close();

  // Parse into the string array
  unsigned int offset = 0;
  unsigned int lineoffset = 0;

  while ( offset < lyricSize )
  {
    // End of line?
    if ( lyricData[offset] == 0x0D || lyricData[offset] == 0x0A )
    {
      // An empty line?
      if ( lineoffset != offset )
      {
        lyricData[offset] = '\0';
        lines.push_back( &lyricData[lineoffset] );
      }

      // Point to the next symbol
      lineoffset = offset + 1;
    }

    offset++;
  }

  // Last line, if any
  if ( lineoffset < lyricSize )
  {
    lyricData[lyricSize-1] = '\0';
    lines.push_back( &lyricData[lineoffset] );
  }

  return lines;
}