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

📄 mfccddb.cpp

📁 MfcCDDB v1.11 A freeware MFC class to support access to CDDB servers Welcome to MfcCDDB, a collectio
💻 CPP
📖 第 1 页 / 共 5 页
字号:
  LPSTR pszNextLine = strstr(pszLine, "\r\n");
  if (pszNextLine)
  {
    LPSTR pszEnd = pszNextLine;  
    pszNextLine = pszNextLine + 2;
    pszEnd[0] = '\0';
  }
  else
  {
    //Failed to get a DOS EOL, try to get a Unix EOL
    pszNextLine = strstr(pszLine, "\n");
    if (pszNextLine)
    {
      LPSTR pszEnd = pszNextLine;  
      pszNextLine = pszNextLine + 1;
      pszEnd[0] = '\0';
    }
  }

  return pszNextLine;
}

int CCDDB::GetCDDBReponseCode(LPSTR pszBody)
{
  //Validate out parameters
  ASSERT(pszBody);
  ASSERT(strlen(pszBody));

  int nResponse = -1;
  int nLength = strlen(pszBody);
  if (nLength >= 3)
  {
    char sNum[4];
    strncpy(sNum, pszBody, 3);
    sNum[3] = '\0';
    nResponse = atoi(sNum);
  }
  return nResponse;
}

BOOL CCDDB::ParseCommentLine(LPCSTR pszLine, CString& sValue)
{
  //Validate out parameters
  ASSERT(pszLine);

  //Duplicate the string into a local variable 
  int nLength = strlen(pszLine);
  LPSTR pszData = new char[nLength + 1];
  strcpy(pszData, pszLine);
  
  //Do the actual parsing
  BOOL bSuccess = FALSE;
  if (pszData[0] == '#')
  {
    sValue.Empty();
    bSuccess = TRUE;

    char* pszValue = pszData+1;
    if (strlen(pszValue) > 1)
    {
      sValue = pszValue;

      //Remove unwanted spaces 
      sValue.TrimLeft();
      sValue.TrimRight();
    }
  }

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

  return bSuccess;
}

BOOL CCDDB::SplitSemiColonLine(LPCSTR pszLine, CString& sField, CString& sValue)
{
  //Validate out parameters
  ASSERT(pszLine);
  
  //Duplicate the string into a local variable 
  int nLength = strlen(pszLine);
  LPSTR pszData = new char[nLength + 1];
  strcpy(pszData, pszLine);

  //Do the actual parsing
  BOOL bSuccess = FALSE;
  LPSTR pszColon = strstr(pszData, ":");
  if (pszColon)
  {
    pszColon[0] = '\0';
    if (strlen(pszData) && strlen(pszColon+1) > 1)
    {
      sField = pszData;
      sValue = pszColon+1;

      //Remove unwanted spaces 
      sField.TrimLeft();
      sField.TrimRight();
      sValue.TrimLeft();
      sValue.TrimRight();
      bSuccess = TRUE;
    }
  }

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

  return bSuccess;
}

BOOL CCDDB::Sites(CArray<CCDDBSite, CCDDBSite&>& sites, const CCDDBSite& server)
{
	//For correct operation of the T2A macro, see MFC Tech Note 59
	USES_CONVERSION;

  BOOL bSuccess = FALSE;

  //Create the socket
  CHTTPSocket socket;
  if (!socket.Create())
  {
    m_dwLastError = 0;
    TRACE(_T("Failed to create client socket for sites command, GetLastError:%d, %s\n"), GetLastError(), GetErrorMessage());
    return FALSE;
  }

  //Connect to the host on the specified port
  if (!socket.Connect(server.m_sSite, server.m_nPort))
  {
    m_dwLastError = 0;
    TRACE(_T("Could not connect to the CDDB HTTP server %s, GetLastError:%d, %s\n"), server.m_sSite, GetLastError(), GetErrorMessage());
    return FALSE;
  }
  else
  {
    //Form the specialized HTTP request to retrieve the sites data
    CString sRequest;
    sRequest.Format(_T("GET %s?cmd=sites&%s HTTP/1.0\r\n\r\n"), server.m_sAddress, GetHelloCommand());

    //Send the request through the socket
    LPCSTR pszRequest = T2A((LPTSTR) (LPCTSTR) sRequest);
    int nCmdLength = strlen(pszRequest);
    if (!socket.Send(pszRequest, nCmdLength))
    {
      m_dwLastError = 0;
      TRACE(_T("An unexpected error occurred while sending the HTTP sites request, GetLastError:%d, %s\n"), GetLastError(), GetErrorMessage());
      return FALSE;
    }
    
    //Read the response back through the socket
    LPSTR pszOverFlowBuffer = NULL;
    if (ReadResponse(socket, NULL, 0, "\r\n.\r\n", &pszOverFlowBuffer))
    {
      ASSERT(pszOverFlowBuffer);

      //Extract the HTTP body from the response;
      LPSTR pszBody = FindHTTPBody(pszOverFlowBuffer);

      //Parse the sites info from the response
      bSuccess = ParseSitesBody(pszBody, sites);
    }
    else
    {
      //Reponse could not be retrieved
      m_dwLastError = 0;
      SetLastError(WSAEPROTONOSUPPORT);
    	TRACE(_T("CDDB server did not respond correctly to the sites command\n"));
    }

    //Don't forget to delete the allocated buffer
    if (pszOverFlowBuffer)
      delete [] pszOverFlowBuffer;

    return bSuccess;
  }
}

BOOL CCDDB::ParseSitesBody(LPSTR pszBody, CArray<CCDDBSite, CCDDBSite&>& sites)
{
  //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 site data line by line
  if (nResponseCode >= 210 && nResponseCode <= 219)
  {
    //Remove any entries which are in the sites array
    sites.RemoveAll();

    //Get the site data and then iterate through all the lines
    LPSTR pszLine = SkipToNextLine(pszBody);
    while (pszLine)
    {
      //Get the next line before we parse the current line
      LPSTR pszNextLine = GetNextLine(pszLine);

      //If we can parse the current line then add it 
      //to the sites array
      CCDDBSite site;
      if (site.Parse(pszLine))
        sites.Add(site);
    
      //Move on to the next line  
      pszLine = pszNextLine;
    }

    //Everything is successful, empty out the last command reponse string
    m_sLastCommandResponse.Empty();
    bSuccess = TRUE;
  }
  else
  {
    //An unexpected response was retrieved
    m_dwLastError = 0;
    SetLastError(WSAEPROTONOSUPPORT);
    TRACE(_T("CDDB server failed to return a valid sites response code\n"));
  }

  return bSuccess;
}

BOOL CCDDB::Sites(CArray<CCDDBSite, CCDDBSite&>& sites, const CString& sServer, const CString& sAddress, int nPort)
{
  //Form the site which we will be querying
  CCDDBSite server;
  server.m_sSite = sServer;
  server.m_nPort = nPort;
  server.m_sAddress = sAddress;
  
  //Let the other version 
  return Sites(sites, server);
}

BOOL CCDDB::Categories(const CCDDBSite& server, CStringArray& categories)
{
	//For correct operation of the T2A macro, see MFC Tech Note 59
	USES_CONVERSION;

  BOOL bSuccess = FALSE;

  //Create the socket
  CHTTPSocket socket;
  if (!socket.Create())
  {
    m_dwLastError = 0;
    TRACE(_T("Failed to create client socket for categories command, GetLastError:%d, %s\n"), GetLastError(), GetErrorMessage());
    return FALSE;
  }

  //Connect to the host on the specified port
  if (!socket.Connect(server.m_sSite, server.m_nPort))
  {
    m_dwLastError = 0;
    TRACE(_T("Could not connect to the CDDB HTTP server %s, GetLastError:%d, %s\n"), server.m_sSite, GetLastError(), GetErrorMessage());
    return FALSE;
  }
  else
  {
    //Form the specialized HTTP request to retreive the sites data
    CString sRequest;
    sRequest.Format(_T("GET %s?cmd=cddb+lscat&%s HTTP/1.0\r\n\r\n"), server.m_sAddress, GetHelloCommand());

    //Send the request through the socket
    LPCSTR pszRequest = T2A((LPTSTR) (LPCTSTR) sRequest);
    int nCmdLength = strlen(pszRequest);
    if (!socket.Send(pszRequest, nCmdLength))
    {
      m_dwLastError = 0;
      TRACE(_T("An unexpected error occurred while sending the HTTP categories request, GetLastError:%d, %s\n"), GetLastError(), GetErrorMessage());
      return FALSE;
    }
    
    //Read the response back through the socket
    LPSTR pszOverFlowBuffer = NULL;
    if (ReadResponse(socket, NULL, 0, "\r\n.\r\n", &pszOverFlowBuffer))
    {
      ASSERT(pszOverFlowBuffer);

      //Extract the HTTP body from the response;
      LPSTR pszBody = FindHTTPBody(pszOverFlowBuffer);

      //Parse the categories info from the response
      bSuccess = ParseCategoriesBody(pszBody, categories);
    }
    else
    {
      //Reponse could not be Retrieved
      m_dwLastError = 0;
      SetLastError(WSAEPROTONOSUPPORT);
    	TRACE(_T("CDDB server did not respond correctly to the categories command\n"));
    }

    //Don't forget to delete the allocated buffer
    if (pszOverFlowBuffer)
      delete [] pszOverFlowBuffer;

    return bSuccess;
  }
}

BOOL CCDDB::ParseCategoriesBody(LPSTR pszBody, CStringArray& categories)
{
  //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 categories data line by line
  if (nResponseCode >= 210 && nResponseCode <= 219)
  {
    //Remove any entries which are in the sites array
    categories.RemoveAll();

    //Get the category data and then iterate through all the lines
    LPSTR pszLine = SkipToNextLine(pszBody);
    while (pszLine)
    {
      //Get the next line before we parse the current line
      LPSTR pszNextLine = GetNextLine(pszLine);

      //Add the current line to the categories array
      CString sCategory(pszLine);
      categories.Add(sCategory);
      
      //Move on to the next line  
      pszLine = pszNextLine;
    }

    //Everything is successful, empty out the last command reponse string
    m_sLastCommandResponse.Empty();
    bSuccess = TRUE;
  }
  else
  {
    //An unexpected response was Retrieved
    m_dwLastError = 0;
    SetLastError(WSAEPROTONOSUPPORT);
    TRACE(_T("CDDB server failed to return a valid response code to the categories command\n"));
  }

  return bSuccess;
}

BOOL CCDDB::Status(const CCDDBSite& server, CCDDBStatus& status)
{
	//For correct operation of the T2A macro, see MFC Tech Note 59
	USES_CONVERSION;

  BOOL bSuccess = FALSE;

  //Create the socket
  CHTTPSocket socket;
  if (!socket.Create())
  {
    m_dwLastError = 0;
    TRACE(_T("Failed to create client socket for status command, GetLastError:%d, %s\n"), GetLastError(), GetErrorMessage());
    return FALSE;
  }

  //Connect to the host on the specified port
  if (!socket.Connect(server.m_sSite, server.m_nPort))
  {
    m_dwLastError = 0;
    TRACE(_T("Could not connect to the CDDB HTTP server %s, GetLastError:%d, %s\n"), server.m_sSite, GetLastError(), GetErrorMessage());
    return FALSE;
  }
  else
  {
    //Form the specialized HTTP request to retreive the status data
    CString sRequest;
    sRequest.Format(_T("GET %s?cmd=stat&%s HTTP/1.0\r\n\r\n"), server.m_sAddress, GetHelloCommand());

    //Send the request through the socket
    LPCSTR pszRequest = T2A((LPTSTR) (LPCTSTR) sRequest);
    int nCmdLength = strlen(pszRequest);
    if (!socket.Send(pszRequest, nCmdLength))
    {
      m_dwLastError = 0;
      TRACE(_T("An unexpected error occurred while sending the HTTP stat request, GetLastError:%d, %s\n"), GetLastError(), GetErrorMessage());
      return FALSE;
    }

    //Read the response back through the socket
    LPSTR pszOverFlowBuffer = NULL;
    if (ReadResponse(socket, NULL, 0, "\r\n.\r\n", &pszOverFlowBuffer))
    {
      ASSERT(pszOverFlowBuffer);

      //Extract the HTTP body from the response;
      LPSTR pszBody = FindHTTPBody(pszOverFlowBuffer);

      //Parse out the status from the body
      bSuccess = ParseStatusBody(pszBody, status);
    }
    else
    {
      //Reponse could not be Retrieved
      m_dwLastError = 0;
      SetLastError(WSAEPROTONOSUPPORT);
    	TRACE(_T("CDDB server did not respond correctly to the stat command\n"));
    }

    //Don't forget to delete the allocated buffer
    if (pszOverFlowBuffer)
      delete [] pszOverFlowBuffer;

    return bSuccess;
  }
}

BOOL CCDDB::ParseStatusBody(LPSTR pszBody, CCDDBStatus& status)
{
  //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 status info line by line
  if (nResponseCode >= 210 && nResponseCode <= 219)
  {
    //Empty out all the arrays in case they already have something in them
    status.m_Categories.RemoveAll();
    status.m_CategoryEntries.RemoveAll();
    status.m_PendingSites.RemoveAll();
    status.m_PendingEntries.RemoveAll();

    //Get ready for parsing all the fields
    LPSTR pszNextLine;

    //Skip over the first two status lines
    LPSTR pszCurLine = SkipToNextLine(pszBody);
    if (pszCurLine)
      pszCurLine = SkipToNextLine(pszCurLine);

    //iterate through all the data  
    BOOL bContinue = TRUE;
    while (pszCurLine && bContinue)
    {
      //Get the next line before we parse the current one
      pszNextLine = GetNextLine(pszCurLine);
      if (pszNextLine == NULL)

⌨️ 快捷键说明

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