📄 bcampropertybag.cpp
字号:
//-----------------------------------------------------------------------------
// (c) 2002 by Basler Vision Technologies
// Section: Vision Components
// Project: BCAM
// $Header: BcamPropertyBag.cpp, 8, 30.09.2003 19:01:34, Nebelung, H.$
//-----------------------------------------------------------------------------
/**
\file BcamPropertyBag.cpp
*
\brief Implementation of CPropertyBag, CIniFilePropertyBag, and CRegistryPropertyBag
classes
*
* The property bag classes are used to load / store a Bcam device's configuration
*/
//-----------------------------------------------------------------------------
#include "stdafx.h"
#include "BcamPropertyBag.h"
#include "bcamerror.h"
using namespace Bcam;
#include <atlbase.h> // CRegKey
//////////////////////////////////////////////////////////////////////////////////
///
/// Implementation of CPropertyBag
///
//////////////////////////////////////////////////////////////////////////////////
void CPropertyBag::WriteBool(const CString& name, const bool value)
{
DWORD error = WriteProperty(name, value ? _T("true") : _T("false") );
if ( error != 0 )
throw BcamException(error, _T( "CPropertyBag::WriteBool()" ) );
}
bool CPropertyBag::ReadBool(const CString& name)
{
CString buffer;
DWORD error;
if ( error = ReadProperty(name, buffer), error )
{
throw BcamException(error, _T( "CPropertyBag::ReadBool()" ) );
}
if ( buffer.CompareNoCase( _T( "false" ) ) == 0 )
return false;
else if ( buffer.CompareNoCase( _T( "true" ) ) == 0 )
return true;
else
throw BcamException(BCAM_E_INVALID_VALUE, _T( "CPropertyBag::ReadBool()" ));
}
void CPropertyBag::WriteLong(const CString& name, const long value)
{
DWORD error;
TCHAR buffer[256];
_ltot(value, buffer, 10);
if ( error = WriteProperty(name, buffer) )
throw BcamException(error, _T( "CPropertyBag::WriteLong()" ) );
}
long CPropertyBag::ReadLong(const CString& name)
{
CString buffer;
DWORD error;
if ( error = ReadProperty(name, buffer), error )
{
throw BcamException(error, _T( "CPropertyBag::ReadLong()" ) );
}
long res;
if ( ! _stscanf(buffer, _T( "%ld" ), &res) )
throw BcamException(BCAM_E_INVALID_VALUE, _T( "CPropertyBag::ReadLong()" ) );
return res;
}
void CPropertyBag::WriteFloat(const CString& name, const float value)
{
TCHAR buffer[256];
_stprintf(buffer, _T( "%f" ), value);
DWORD error;
if ( error = WriteProperty(name, buffer ))
throw BcamException(error, _T( "CPropertyBag::WriteFloat()" ) );
}
float CPropertyBag::ReadFloat(const CString& name)
{
CString buffer;
DWORD error;
if ( error = ReadProperty(name, buffer), error )
{
throw BcamException(error, _T( "CPropertyBag::ReadFloat()" ) );
}
float res;
if ( ! _stscanf(buffer, _T( "%f" ), &res) )
throw BcamException(BCAM_E_INVALID_VALUE, _T( "CPropertyBag::ReadFloat()" ) );
return res;
}
void CPropertyBag::WriteString(const CString& name, const CString value)
{
DWORD error;
if ( error = WriteProperty(name, value), error )
throw BcamException(error, _T( "CPropertyBag::WriteString()" ) );
}
CString CPropertyBag::ReadString(const CString& name)
{
CString buffer;
DWORD error;
if ( error = ReadProperty(name, buffer), error )
throw BcamException(error, _T( "CPropertyBag::ReadFloat()" ) );
return buffer;
}
//////////////////////////////////////////////////////////////////////////////////
///
/// Implementation of CIniFilePropertyBag
///
//////////////////////////////////////////////////////////////////////////////////
//------------------------------------------------------------------------------
// CIniFilePropertyBag::CIniFilePropertyBag(const CString& fileName, const CString& sectionName,const bool create) : m_strFileName(fileName), m_strSectionName(sectionName)
// Author:
// Date: 04.09.2002
//------------------------------------------------------------------------------
/**
* Protected constructor. To create a property bag, use one of the static factory methods
*
* \param fileName name of the .ini file
* \param sectionName name of the .ini file's section the created bag is bound to
* \param create true: a new .ini file will be created, false: an existing one will be used
*/
//------------------------------------------------------------------------------
CIniFilePropertyBag::CIniFilePropertyBag(const CString& fileName, const CString& sectionName,const bool create) :
m_strFileName(fileName),
m_strSectionName(sectionName)
{
bool exist = false;
TCHAR lpszReturnBuffer[1024];
// check if already an section named entryName exists
if ( GetPrivateProfileSectionNames(lpszReturnBuffer, 1024, m_strFileName) )
{
TCHAR* p = lpszReturnBuffer;
while ( *p != 0 )
{
if ( sectionName.CompareNoCase(p) == 0 )
{
exist = true;
break;
}
while ( *p++);
}
}
if ( create )
{
if ( exist )
throw BcamException( BCAM_E_BAG_ALREADY_EXISTS, _T( "CIniFilePropertyBag::CIniFilePropertyBag()" ) );
// write an empty section for the BAG
if ( ! WritePrivateProfileSection(m_strSectionName, _T( "\0\0" ), m_strFileName) )
throw BcamException( GetLastError(), _T( "CIniFilePropertyBag::Create()" ) );
}
else
{
if ( ! exist )
throw BcamException( BCAM_E_BAG_DOES_NOT_EXIST, _T( "CIniFilePropertyBag::CIniFilePropertyBag()" ) );
}
}
//------------------------------------------------------------------------------
// CPropertyBagPtr CIniFilePropertyBag::Create(const CString& fileName, const CString& bagName)
// Author:
// Date: 04.09.2002
//------------------------------------------------------------------------------
/**
*
*
* Static factory method to create a new .ini file. An existing .ini file will be
* deleted.
*
* \param fileName name of the ini.file
* \param bagName name of the section the bag is bound to
* \return
*
* A property bag bound to a section named bagName
*
*/
//------------------------------------------------------------------------------
CPropertyBagPtr CIniFilePropertyBag::Create(const CString& fileName, const CString& bagName)
{
DWORD numCharsCopied;
TCHAR pszFilePath[MAX_PATH];
LPTSTR *lpFilePart = NULL;
// WritePrivateProfileString works relative to the windows directory if no fully qualified
// file name is used. So we will use SearchPath to work relative to the current directory
numCharsCopied = SearchPath(
NULL, // search path
(LPCTSTR) fileName, // file name
NULL, // file extension
MAX_PATH, // size of buffer
pszFilePath, // found file name buffer
lpFilePart ); // file component
if( ! numCharsCopied )
{
// file doesn't exist, create a dummy one
HANDLE hFile = ::CreateFile( (LPCTSTR) fileName, GENERIC_WRITE, 0, NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL );
if (INVALID_HANDLE_VALUE == hFile)
throw BcamException( ERROR_CANNOT_MAKE, _T( "CIniFilePropertyBag::Create()" ) );
::CloseHandle( hFile );
numCharsCopied = SearchPath(
NULL, // search path
(LPCTSTR) fileName, // file name
NULL, // file extension
MAX_PATH, // size of buffer
pszFilePath, // found file name buffer
lpFilePart ); // file component
if ( ! numCharsCopied )
throw BcamException( GetLastError(), _T( "CIniFilePropertyBag::Create()" ) );
}
else
{
// There already exists such an file, delete it
DeleteFile(pszFilePath);
}
return CPropertyBagPtr(new CIniFilePropertyBag(pszFilePath, bagName, true));
}
//------------------------------------------------------------------------------
// CPropertyBagPtr CIniFilePropertyBag::Open(const CString& fileName, const CString& bagName)
// Author:
// Date: 04.09.2002
//------------------------------------------------------------------------------
/**
* Static factory method. The function returns a property bag which is bound to a specified
* section of an existing .ini file
*
* \param fileName name of the .ini file
* \param bagName name of the section the bag is to be bound to
*
* \return A property bag bound smart pointer
*
*/
//------------------------------------------------------------------------------
CPropertyBagPtr CIniFilePropertyBag::Open(const CString& fileName, const CString& bagName)
{
DWORD numCharsCopied;
TCHAR pszFilePath[MAX_PATH];
LPTSTR *lpFilePart = NULL;
numCharsCopied = SearchPath(
NULL, // search path
(LPCTSTR) fileName, // file name
NULL, // file extension
MAX_PATH, // size of buffer
pszFilePath, // found file name buffer
lpFilePart ); // file component
if( ! numCharsCopied )
{
throw BcamException( GetLastError(), _T( "CIniFilePropertyBag::Open()" ) );
}
return CPropertyBagPtr(new CIniFilePropertyBag(pszFilePath, bagName, false));
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -