⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mfccddb.cpp

📁 MfcCDDB v1.11 A freeware MFC class to support access to CDDB servers Welcome to MfcCDDB, a collectio
💻 CPP
📖 第 1 页 / 共 5 页
字号:

    //Replace all strings as specified in the cddb document
    ReplaceSubString(sValue, _T("\\n"), _T("\n"));
    ReplaceSubString(sValue, _T("\\t"), _T("\t"));
    ReplaceSubString(sValue, _T("\\\\"), _T("\\"));

    bSuccess = TRUE;
  }

  //Dont forget to delete our local string
  delete [] pszData;

  return bSuccess;
}

BOOL CCDDB::ParseRecordBody(LPSTR pszBody, CCDDBRecord& record)
{
  //setup return value
  BOOL bSuccess = FALSE;

  //Hive away the last reponse until everything is ok
  m_sLastCommandResponse = pszBody;

  //From the HTTP body get the CDDB response code
  int nResponseCode = GetCDDBReponseCode(pszBody);

  //If the response is in the expected range (21x) then parse the query data line by line
  if (nResponseCode >= 210 && nResponseCode <= 219)
  {
    //Get the query data and then iterate through all the lines
    LPSTR pszCurLine = SkipToNextLine(pszBody);
    if (pszCurLine == NULL)
    {
      m_dwLastError = 0;
      SetLastError(WSAEPROTONOSUPPORT);
      TRACE(_T("CDDB server failed to return a valid xmcd record\n"));
      return FALSE;
    }

    //Get the next line before we parse the current one
    LPSTR pszNextLine = GetNextLine(pszCurLine);

    //Parse the "# xmcd" line  
    CString sValue;
    if ((!ParseCommentLine(pszCurLine, sValue)) || (sValue.Find(_T("xmcd")) == -1))
    {
      m_dwLastError = 0;
      SetLastError(WSAEPROTONOSUPPORT);
      TRACE(_T("CDDB server failed to return a valid xmcd record\n"));
      return FALSE;
    }

    //Skip over any other comment lines until we reach the "# Track frame offsets:" line
    pszCurLine = pszNextLine;
    BOOL bFound = FALSE;
    BOOL bContinue;
    do
    {
      //Get the next line prior to parsing the current one
      pszNextLine = GetNextLine(pszCurLine);

      //Continue skipping lines until we reach the "# Track frame offsets:" line
      bContinue = pszCurLine && ParseCommentLine(pszCurLine, sValue);
      if (bContinue)
      {
        bFound = (sValue.Find(_T("Track frame offsets:")) != -1);
        bContinue = !bFound;
      }

      //Get ready for the next loop around
      if (bContinue)
        pszCurLine = pszNextLine;
    }
    while (bContinue);

    //If we could not find the "# Track frame offsets:" line
    if (!bFound)
    {
      m_dwLastError = 0;
      SetLastError(WSAEPROTONOSUPPORT);
      TRACE(_T("CDDB server failed to return a record with a Track frame offset line\n"));
      return FALSE;
    }

    //Next should come the track offsets
    pszCurLine = pszNextLine;
    do
    {
      //Get the next line prior to parsing the current one
      pszNextLine = GetNextLine(pszCurLine);

      //Continue skipping lines until we reach the "# Track frame offsets:" line
      bContinue = pszCurLine && ParseCommentLine(pszCurLine, sValue) && (!sValue.IsEmpty());

      //Get ready for the next loop around
      if (bContinue)
      {
        //Add the track offsets to the array in the record
        int nTrackOffset = ::_ttoi(sValue);
        record.m_TrackOffsets.Add(nTrackOffset);
        pszCurLine = pszNextLine;
      }
    }
    while (bContinue);

    //Skip over any other comment lines until we reach the "# Disc length:" line
    pszCurLine = pszNextLine;
    do
    {
      //Get the next line prior to parsing the current one
      pszNextLine = GetNextLine(pszCurLine);

      //Continue skipping lines until we reach the "# Track frame offsets:" line
      bContinue = pszCurLine && ParseCommentLine(pszCurLine, sValue);
      if (bContinue)
      {
        bFound = (sValue.Find(_T("Disc length:")) != -1);
        bContinue = !bFound;
      }

      //Get ready for the next loop around
      if (bContinue)
        pszCurLine = pszNextLine;
    }
    while (bContinue);

    int nLength = sValue.GetLength();
    if (!bFound || nLength < 13)
    {
      m_dwLastError = 0;
      SetLastError(WSAEPROTONOSUPPORT);
      TRACE(_T("CDDB server failed to return a record with a Disc Length line\n"));
      return FALSE;
    }
  
    //Parse out the "# Disc length" value
    sValue = sValue.Right(nLength - 13);
    record.m_nDiskLength = ::_ttoi(sValue);

    //Skip over any other comment lines until we reach the "# Revision:" line
    pszCurLine = pszNextLine;
    BOOL bFoundRevision = FALSE;
    BOOL bFoundSubmitted = FALSE;
    do
    {
      //Get the next line prior to parsing the current one
      pszNextLine = GetNextLine(pszCurLine);

      //Continue skipping lines until we reach the "# Revision:" line
      bContinue = pszCurLine && ParseCommentLine(pszCurLine, sValue);
      if (bContinue)
      {
        bFoundRevision = (sValue.Find(_T("Revision:")) != -1);
        bFoundSubmitted = (sValue.Find(_T("Submitted via:")) != -1);
        bContinue = !bFoundRevision && !bFoundSubmitted;
      }

      //Get ready for the next loop around
      if (bContinue)
        pszCurLine = pszNextLine;
    }
    while (bContinue);

    //Handle revision not being found
    nLength = sValue.GetLength();
    if (!bFoundRevision || (bFoundRevision && nLength < 11))
    {
      TRACE(_T("CDDB server failed to return a record with a Revision line, assuming revision level zero\n"));
      record.m_nDatabaseRevision = 0;
    }
    else
    {
      //Parse out the "# Revision" value
      sValue = sValue.Right(nLength - 9);
      record.m_nDatabaseRevision = ::_ttoi(sValue);
    }

    //Handle case where revision was not found neither was submitted
    if (!bFoundRevision && !bFoundSubmitted)
    {
      m_dwLastError = 0;
      SetLastError(WSAEPROTONOSUPPORT);
      TRACE(_T("CDDB server failed to return a record with a revision or a submitted line\n"));
      return FALSE;
    }

    //Skip over any other comment lines until we reach the "# Submitted via:" line
    if (!bFoundSubmitted)
    {
      pszCurLine = pszNextLine;
      do
      {
        //Get the next line prior to parsing the current one
        pszNextLine = GetNextLine(pszCurLine);

        //Continue skipping lines until we reach the "# Submitted via:" line
        bContinue = pszCurLine && ParseCommentLine(pszCurLine, sValue);
        if (bContinue)
        {
          bFoundSubmitted = (sValue.Find(_T("Submitted via:")) != -1);
          bContinue = !bFoundSubmitted;
        }

        //Get ready for the next loop around
        if (bContinue)
          pszCurLine = pszNextLine;
      }
      while (bContinue);
    }

    //Handle case of no submitted line
    nLength = sValue.GetLength();
    if (!bFoundSubmitted || nLength < 14)
    {
      m_dwLastError = 0;
      SetLastError(WSAEPROTONOSUPPORT);
      TRACE(_T("CDDB server failed to return a record with a valid submitted line\n"));
      return FALSE;
    }

    //Parse out the "Submitted via:" line
    //Pull out the client name
    TCHAR pszSeparators[] = _T(" ");
    sValue = sValue.Right(nLength - 14);
    TCHAR* pszValue = sValue.GetBuffer(sValue.GetLength());
    TCHAR* pszToken = _tcstok(pszValue, pszSeparators);
    if (pszToken)
      record.m_sClientName = pszToken;
    else
    {
      sValue.ReleaseBuffer();
      m_dwLastError = 0;
      SetLastError(WSAEPROTONOSUPPORT);
      TRACE(_T("CDDB server failed to return a record with a valid Submitted via: line\n"));
      return FALSE;
    }

    //Pull out the client version
    pszToken = _tcstok(NULL, pszSeparators);
    if (pszToken)
      record.m_sClientVersion = pszToken;
    else
    {
      sValue.ReleaseBuffer();
      m_dwLastError = 0;
      SetLastError(WSAEPROTONOSUPPORT);
      TRACE(_T("CDDB server failed to return a record with a valid Submitted via: line\n"));
      return FALSE;
    }

    //Pull out the client comments
    pszToken = _tcstok(NULL, pszSeparators);
    if (pszToken)
      record.m_sClientComments = pszToken;
    else
      record.m_sClientComments.Empty();

    sValue.ReleaseBuffer();

    //Skip over any other comment lines until we reach the "DISCID=" line
    pszCurLine = pszNextLine;
    BOOL bFoundDiscID = FALSE;
    do
    {
      //Get the next line prior to parsing the current one
      pszNextLine = GetNextLine(pszCurLine);

      //Continue skipping lines until we reach the "DISCID=" line
      bContinue = (pszCurLine != NULL);
      if (bContinue)
      {
        CString sLine(pszCurLine);
        bFoundDiscID = (sLine.Find(_T("DISCID=")) != -1);
        bContinue = !bFoundDiscID;
      }

      //Get ready for the next loop around
      if (bContinue)
        pszCurLine = pszNextLine;
    }
    while (bContinue);

    //Handle the DISCID line
    CString sKeyword;
    bFound = bFound && ParseKeywordLine(pszCurLine, sKeyword, sValue);
    if (!bFound || sKeyword != _T("DISCID") || sValue.GetLength() < 8)
    {
      //Handle the DISCID line not being found
      m_dwLastError = 0;
      SetLastError(WSAEPROTONOSUPPORT);
      TRACE(_T("CDDB server failed to return a record with a DISCID line\n"));
      return FALSE;
    }
  
    //Parse out the "# Disc length" value
    _stscanf(sValue, _T("%x"), &record.m_dwDiscID);

    //Handle the DTITLE line
    pszCurLine = pszNextLine;
    pszNextLine = GetNextLine(pszCurLine);
    bFound = ParseKeywordLine(pszCurLine, sKeyword, sValue);
    if (!bFound || sKeyword != _T("DTITLE"))
    {
      //Handle the DTITLE line not being found
      m_dwLastError = 0;
      SetLastError(WSAEPROTONOSUPPORT);
      TRACE(_T("CDDB server failed to return a record with a DTITLE line\n"));
      return FALSE;
    }
    int nFind = sValue.Find(_T('/'));
    if (nFind != -1)
    {
      record.m_sArtist = sValue.Left(nFind);
      record.m_sTitle = sValue.Right(sValue.GetLength() - nFind - 1);
    }
    else
    {
      record.m_sTitle = sValue;
      record.m_sArtist.Empty();
    }
    //Remove any leading and trailing spaces
    record.m_sArtist.TrimLeft();
    record.m_sArtist.TrimRight();
    record.m_sTitle.TrimLeft();
    record.m_sTitle.TrimRight();

    //Handle the TTITLE lines
    record.m_TrackTitles.RemoveAll();
    pszCurLine = pszNextLine;
    pszNextLine = GetNextLine(pszCurLine);
    bContinue = FALSE;  
    BOOL bError = FALSE;
    do
    {
      bContinue = (pszCurLine != NULL);
      if (bContinue)
      {
        CString sLine(pszCurLine);
        bContinue = (sLine.Find(_T("TTITLE")) != -1);
        if (bContinue)
        {
          bError = !ParseKeywordLine(pszCurLine, sKeyword, sValue);
          if (!bError)
            record.m_TrackTitles.Add(sValue);
          else
            bContinue = FALSE;
        }
      }

      //Get ready for the next loop around
      if (bContinue)
      {
        pszCurLine = pszNextLine;
        pszNextLine = GetNextLine(pszCurLine);
      }
    }
    while (bContinue);

    //Handle any TTITLE errors
    if (bError)
    {
      m_dwLastError = 0;
      SetLastError(WSAEPROTONOSUPPORT);
      TRACE(_T("CDDB server failed to return a record with some valid DTITLE lines\n"));
      return FALSE;
    }

    //Handle any EXTD lines
    bContinue = FALSE;  
    bError = FALSE;
    CString sExtd;
    do
    {
      bContinue = (pszCurLine != NULL);
      if (bContinue)
      {
        CString sLine(pszCurLine);
        bContinue = (sLine.Find(_T("EXTD=")) != -1);
        if (bContinue)
        {
          bError = !ParseKeywordLine(pszCurLine, sKeyword, sValue);
          if (!bError)
          {
            if (sValue.GetLength())
              sExtd += sValue;
          }
          else
            bContinue = FALSE;
        }
      }

      //Get ready for the next loop around
      if (bContinue)
      {
        pszCurLine = pszNextLine;
        pszNextLine = GetNextLine(pszCurLine);
      }
    }
    while (bContinue);

    //Handle any EXTD errors
    if (bError)
    {
      m_dwLastError = 0;
      SetLastError(WSAEPROTONOSUPPORT);
      TRACE(_T("CDDB server failed to return a record with some valid EXTD lines\n"));
      return FALSE;
    }
    else
    {
      //Process the EXTD string into the two string arrays in the record instance
      record.m_ExtendedData.RemoveAll();
      do
      {
        int nFind = sExtd.Find(_T("\n"));
        if (nFind != -1)
        {
          CString sValue(sExtd.Left(nFind));
          record.m_ExtendedData.Add(sValue);
          sExtd = sExtd.Right(sExtd.GetLength() - nFind - 1);
          bContinue = TRUE;
        }
        else
        {
          if (sExtd.GetLength())
            record.m_ExtendedData.Add(sExtd);
          bContinue = FALSE;
        }
      }
      while (bContinue);
    }

    //Handle the EXTTN lines
    bContinue = FALSE;  
    bError = FALSE;
    CString sPrevKeyword;
    do
    {
      bContinue = (pszCurLine != NULL);
      if (bContinue)
      {
        CString sLine(pszCurLine);
        bContinue = (sLine.Find(_T("EXTT")) != -1);
        if (bContinue)
        {
          bError = !ParseKeywordLine(pszCurLine, sKeyword, sValue);
          if (!bError)
          {
            if (sPrevKeyword != sKeyword)
            {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -