testresource.cpp

来自「基于ecos的redboot」· C++ 代码 · 共 544 行 · 第 1/2 页

CPP
544
字号
//####COPYRIGHTBEGIN####
//                                                                          
// ----------------------------------------------------------------------------
// Copyright (C) 1998, 1999, 2000 Red Hat, Inc.
//
// This program is part of the eCos host tools.
//
// 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.
//
// ----------------------------------------------------------------------------
//                                                                          
//####COPYRIGHTEND####
//=================================================================
//
//        TestResource.cpp
//
//        Resource test class
//
//=================================================================
//=================================================================
//#####DESCRIPTIONBEGIN####
//
// Author(s):     sdf
// Contributors:  sdf
// Date:          1999-04-01
// Description:   This class abstracts a test resource for use in the testing infrastructure
// Usage:
//
//####DESCRIPTIONEND####
#include "eCosStd.h"
#include "eCosTestUtils.h"
#include "eCosTrace.h"
#include "Subprocess.h"

#include "TestResource.h"

CTestResource *CTestResource::pFirstInstance=0;
unsigned int CTestResource::nCount=0;

String CTestResource::strResourceHostPort;

CTestResource::CTestResource(LPCTSTR pszHostPort, LPCTSTR target, LPCTSTR  pszDownloadPort, int nBaud, LPCTSTR pszResetString):
  m_strReset(pszResetString),
  m_bInUse(false),
  m_nBaud(nBaud),
  m_strPort(pszDownloadPort),
  m_bLocked(false),
  m_Target(target)
{
  CeCosSocket::ParseHostPort(pszHostPort,m_strHost,m_nPort);
  VTRACE(_T("@@@ Created resource %08x %s\n"),(unsigned int)this,(LPCTSTR)Image());
  Chain();
}

CTestResource::~CTestResource()
{
  ENTERCRITICAL;
  VTRACE(_T("@@@ Destroy resource %08x %s\n"),this,(LPCTSTR)Image());
  if(m_pPrevInstance || m_pNextInstance){
    nCount--;
  }
  if(pFirstInstance==this){
    pFirstInstance=m_pNextInstance;
  }
  if(m_pPrevInstance){
    m_pPrevInstance->m_pNextInstance=m_pNextInstance;
  }
  if(m_pNextInstance){
    m_pNextInstance->m_pPrevInstance=m_pPrevInstance;
  }
  LEAVECRITICAL;
}

// Find the resource matching the given host:port specification
// Returns 0 if no such host:port found
CTestResource * CTestResource::Lookup(LPCTSTR pszHostPort)
{
  CTestResource *pResource=NULL;
  ENTERCRITICAL;
  String strHost;
  int nPort;
  if(CeCosSocket::ParseHostPort(pszHostPort,strHost,nPort)){
    for(pResource=pFirstInstance;pResource;pResource=pResource->m_pNextInstance){
      if(nPort==pResource->TcpIPPort() && CeCosSocket::SameHost(strHost,pResource->Host())){
        break;
      }
    }
  }
  LEAVECRITICAL;
  return pResource;
}

unsigned int CTestResource::GetMatchCount (const CeCosTest::ExecutionParameters &e,bool bIgnoreLocking)
{
  unsigned int i=0;
  ENTERCRITICAL;
  for(const CTestResource *pResource=pFirstInstance;pResource;pResource=pResource->m_pNextInstance){
    if(pResource->Matches(e,bIgnoreLocking)){
      i++;
    }
  }
  LEAVECRITICAL;
  return i;
}

bool CTestResource::GetMatches (const CeCosTest::ExecutionParameters &e, StringArray &arstr, bool bIgnoreLocking)
{
  bool rc=false;
  arstr.clear();
  if(Load()){
    ENTERCRITICAL;
    for(CTestResource *pResource=pFirstInstance;pResource;pResource=pResource->m_pNextInstance){
		  if(pResource->Matches(e,bIgnoreLocking)){
        arstr.push_back(pResource->HostPort());
      }
    }
    LEAVECRITICAL;
    rc=true;
  }
  return rc;
}

void CTestResource::DeleteAllInstances()
{
  ENTERCRITICAL;
  while(pFirstInstance){
    delete pFirstInstance;
  }
  LEAVECRITICAL;
}

bool CTestResource::LoadFromDirectory (LPCTSTR psz)
{
  bool rc=true;
  ENTERCRITICAL;
  DeleteAllInstances();
  // Find all the files in directory "psz" and load from each of them
  TCHAR szOrigDir[256];
  _tgetcwd(szOrigDir,sizeof szOrigDir-1);
  if(0==_tchdir(psz)){
    String strFile;
    void *pHandle;
    for(bool b=CeCosTestUtils::StartSearch(pHandle,strFile);b;b=CeCosTestUtils::NextFile(pHandle,strFile)){
      if(CeCosTestUtils::IsFile(strFile)){
        CTestResource *pResource=new CTestResource(_T(""),_T(""));
        CTestResourceProperties prop(pResource);
        prop.LoadFromFile(strFile);
      }
    }
    CeCosTestUtils::EndSearch(pHandle);
  } else {
    TRACE(_T("Failed to change to %s from %s\n"),psz,szOrigDir);
  }
  _tchdir(szOrigDir);
  LEAVECRITICAL;
  
  return rc;
}

bool CTestResource::SaveToDirectory (LPCTSTR pszDir)
{
  bool rc=false;    
  ENTERCRITICAL;
  {
    // Delete all the files under directory "pszDir"
    void *pHandle;
    TCHAR szOrigDir[256];
    _tgetcwd(szOrigDir,sizeof szOrigDir-1);
    if(0==_tchdir(pszDir)){
      String strFile;
      for(bool b=CeCosTestUtils::StartSearch(pHandle,strFile);b;b=CeCosTestUtils::NextFile(pHandle,strFile)){
        if(CeCosTestUtils::IsFile(strFile)){
          _tunlink(strFile);
        }
      }
      CeCosTestUtils::EndSearch(pHandle);
      rc=true;
      for(CTestResource *pResource=pFirstInstance;pResource;pResource=pResource->m_pNextInstance){
        CTestResourceProperties prop(pResource);
        rc&=prop.SaveToFile(pResource->FileName());
      }
    } else {
      fprintf(stderr,"Failed to change to %s from %s\n",pszDir,szOrigDir);
    }
    _tchdir(szOrigDir);
  }
  
  LEAVECRITICAL;
  
  return rc;
}

#ifdef _WIN32
bool CTestResource::LoadFromRegistry(HKEY key,LPCTSTR psz)
{
  // Find all the keys under "psz" and load from each of them
  bool rc=false;    
  ENTERCRITICAL;
  HKEY hKey;
  if(ERROR_SUCCESS==RegOpenKeyEx ((HKEY)key, psz, 0L, KEY_ENUMERATE_SUB_KEYS, &hKey)){
		TCHAR szName[256];
    DWORD dwSizeName=sizeof szName;
    FILETIME ftLastWriteTime;
    for(DWORD dwIndex=0;ERROR_SUCCESS==RegEnumKeyEx(hKey, dwIndex, szName, &dwSizeName, NULL, NULL, NULL, &ftLastWriteTime); dwIndex++){
      CTestResource *pResource=new CTestResource(_T(""),_T(""));
      String strKey;
      strKey.Format(_T("%s\\%s"),psz,szName);
      CTestResourceProperties prop1(pResource);
      prop1.LoadFromRegistry(key,strKey);
      dwSizeName=sizeof szName;
    }
    RegCloseKey(hKey);
  }
  LEAVECRITICAL;
  return rc;
}

bool CTestResource::SaveToRegistry(HKEY key,LPCTSTR psz)
{
  bool rc=false;    
  ENTERCRITICAL;
  // Delete all the keys under "psz"
  HKEY hKey;
  if(ERROR_SUCCESS==RegOpenKeyEx ((HKEY)key, psz, 0L, KEY_ENUMERATE_SUB_KEYS, &hKey)){
    TCHAR szName[256];
    DWORD dwSizeName=sizeof szName;
    FILETIME ftLastWriteTime;
    DWORD dwIndex;
    if(ERROR_SUCCESS==RegQueryInfoKey(hKey,0,0,0,&dwIndex,0,0,0,0,0,0,0)){
      while((signed)--dwIndex>=0){
        if(ERROR_SUCCESS!=RegEnumKeyEx(hKey, dwIndex, szName, &dwSizeName, NULL, NULL, NULL, &ftLastWriteTime) ||
          ERROR_SUCCESS!=RegDeleteKey(hKey,szName)){
          rc=false;
        }
        dwSizeName=sizeof szName;
      }
    }
    RegCloseKey(hKey);
  }
  rc=true;
  for(CTestResource *pResource=pFirstInstance;pResource;pResource=pResource->m_pNextInstance){
    CTestResourceProperties prop1(pResource);
    rc&=prop1.SaveToRegistry(key,pResource->FileName());
  }

  LEAVECRITICAL;
  return rc;
}

#endif

CTestResource::CTestResourceProperties::CTestResourceProperties(CTestResource *pResource)
{
  Add(_T("Baud"),       pResource->m_nBaud);
  Add(_T("BoardId"),    pResource->m_strBoardID);
  Add(_T("Date"),       pResource->m_strDate);
  Add(_T("Email"),      pResource->m_strEmail);
  Add(_T("Host"),       pResource->m_strHost);
  Add(_T("Port"),       pResource->m_nPort);
  Add(_T("Locked"),     pResource->m_bLocked);

⌨️ 快捷键说明

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