Files
gjm 164968b62e chore
把非utf8-bom编码的cpp/h文件改为 utf8 bom 编码, msvc识别utf8编码时,如果不是bom格式的,会使用当前cp_oem来解码.
2026-10-04 00:04:20 +08:00

269 lines
7.1 KiB
C++

#include "StdAfx.h"
#include "DataLoadAndSave.h"
DataLoadAndSave::DataLoadAndSave()
{
}
DataLoadAndSave::~DataLoadAndSave()
{
}
bool DataLoadAndSave::readRecordDataInt( CString sKey, int& iValue, CString sTermName )
{
CString sValue(_T(""));
sValue.Format(_T("%d"), iValue);
bool bReturn = CompleteRecordDataCString(sKey, sValue, sTermName);
iValue = _ttoi(sValue);
return bReturn;
}
bool DataLoadAndSave::writeRecordDataInt( CString sKey, int& iValue, CString sTermName )
{
CString sValue(_T(""));
sValue.Format(_T("%d"), iValue);
return CompleteRecordDataCString(sKey, sValue, sTermName, false);
}
bool DataLoadAndSave::readRecordDataUINT( CString sKey, UINT& iValue, CString sTermName )
{
int iReturn = iValue;
bool bReturn = readRecordDataInt(sKey, iReturn, sTermName);
iValue = iReturn;
return bReturn;
}
bool DataLoadAndSave::writeRecordDataUINT( CString sKey, UINT& iValue, CString sTermName )
{
int iReturn = iValue;
return writeRecordDataInt(sKey, iReturn, sTermName);
}
bool DataLoadAndSave::readRecordDataDouble( CString sKey, double& dValue, CString sTermName )
{
CString sValue(_T(""));
sValue.Format(_T("%lf"), dValue);
bool bReturn = CompleteRecordDataCString(sKey, sValue, sTermName);
dValue = _tcstod(sValue, NULL);
return bReturn;
}
bool DataLoadAndSave::writeRecordDataDouble( CString sKey, double& dValue, CString sTermName )
{
CString sValue(_T(""));
sValue.Format(_T("%lf"), dValue);
return CompleteRecordDataCString(sKey, sValue, sTermName, false);
}
bool DataLoadAndSave::readRecordDataDWORD( CString sKey, DWORD& dwValue, CString sTermName )
{
return CompleteRecordDataDWORD(sKey, dwValue, sTermName);
}
bool DataLoadAndSave::writeRecordDataDWORD( CString sKey, DWORD& dwValue, CString sTermName )
{
return CompleteRecordDataDWORD(sKey, dwValue, sTermName, false);
}
bool DataLoadAndSave::readRecordDataCString( CString sKey, CString& sValue, CString sTermName )
{
return CompleteRecordDataCString(sKey, sValue, sTermName);
}
bool DataLoadAndSave::writeRecordDataCString( CString sKey, CString& sValue, CString sTermName )
{
return CompleteRecordDataCString(sKey, sValue, sTermName, false);
}
bool DataLoadAndSave::readRecordDataBool( CString sKey, bool& bValue, CString sTermName )
{
CString sValue(_T(""));
int iValue = bValue ? 1 : 0;
sValue.Format(_T("%d"), iValue);
bool bReturn = CompleteRecordDataCString(sKey, sValue, sTermName);
bValue = _ttoi(sValue) ? true : false;
return bReturn;
}
bool DataLoadAndSave::writeRecordDataBool( CString sKey, bool& bValue, CString sTermName )
{
CString sValue(_T(""));
int iValue = bValue ? 1 : 0;
sValue.Format(_T("%d"), iValue);
return CompleteRecordDataCString(sKey, sValue, sTermName, false);
}
bool DataLoadAndSave::readRecordDataBytes( CString sKey, byte* pValue, CString sTermName )
{
return CompleteRecordDataBytes(sKey, pValue, sTermName);
}
bool DataLoadAndSave::writeRecordDataBytes( CString sKey, byte* pValue, CString sTermName )
{
return CompleteRecordDataBytes(sKey, pValue, sTermName, false);
}
bool DataLoadAndSave::CompleteRecordDataCString( CString sKey, CString& sValue, CString sTermName, bool bRead /*= true*/, EDataStyle dataStyle /*= ECString*/ )
{
DWORD dwNull = NULL;
byte* pNull = NULL;
return CompleteRecordData(sKey, sValue, dwNull, pNull, sTermName, bRead, dataStyle);
}
bool DataLoadAndSave::CompleteRecordDataDWORD( CString sKey, DWORD& dwValue, CString sTermName, bool bRead /*= true*/, EDataStyle dataStyle /*= EDWORD*/ )
{
CString sNull(_T(""));
byte* pNull = NULL;
return CompleteRecordData(sKey, sNull, dwValue, pNull, sTermName, bRead, dataStyle);
}
bool DataLoadAndSave::CompleteRecordDataBytes( CString sKey, byte* pValue, CString sTermName, bool bRead /*= true*/, EDataStyle dataStyle /*= EBytes*/ )
{
CString sNull(_T(""));
DWORD dwNull = NULL;
return CompleteRecordData(sKey, sNull, dwNull, pValue, sTermName, bRead, dataStyle);
}
bool DataLoadAndSave::CompleteRecordData( CString sKey, CString& sValue, DWORD& dwValue, byte* pValue, CString sTermName, bool bRead, EDataStyle dataStyle )
{
HKEY hKEY;
long lDispos = REG_CREATED_NEW_KEY;
LPBYTE pParametervValue(NULL);
DWORD cbDataStyle(NULL);
unsigned long cbDataLength(MAX_PATH), cbDataLength2(MAX_PATH);
switch (dataStyle)
{
case ECString:
{
cbDataStyle = REG_SZ;
pParametervValue = (LPBYTE)(LPCTSTR)sValue;
cbDataLength2 = (_tcslen(sValue) + 1) * sizeof(TCHAR);
break;
}
case EDWORD:
{
cbDataStyle = REG_DWORD;
pParametervValue = (LPBYTE)&dwValue;
cbDataLength2 = sizeof(DWORD);
break;
}
case EBytes:
{
cbDataStyle = REG_BINARY;
pParametervValue = (LPBYTE)pValue;
cbDataLength2 = (_tcslen(reinterpret_cast<wchar_t*>((LPBYTE)pValue)) + 1) * sizeof(TCHAR);
break;
}
default:
break;
}
bool bReturn(FALSE);
CString sLocation = m_sLocation;
sLocation.Format(_T("%s\\Settings\\%s"), sLocation, sTermName);
long rError = ::RegOpenKeyEx(HKEY_CURRENT_USER, sLocation, 0, KEY_ALL_ACCESS, &hKEY);
if(rError == ERROR_SUCCESS)
{
if (bRead)
{
LPBYTE pGetReturnValue = new BYTE[MAX_PATH];
rError = ::RegQueryValueEx(hKEY, sKey, NULL, &cbDataStyle, pGetReturnValue, &cbDataLength);
if (rError == ERROR_SUCCESS)
{
switch (dataStyle)
{
case ECString:
{
sValue.Format(_T("%s"), pGetReturnValue);
break;
}
case EDWORD:
{
dwValue = (DWORD)(*pGetReturnValue);
break;
}
case EBytes:
{
pValue = (byte*)pGetReturnValue;
break;
}
default:
break;
}
bReturn = TRUE;
}
else
{
CompleteRegSetValueEx(hKEY, sKey, pParametervValue, cbDataLength2, dataStyle);
bReturn = FALSE;
}
delete pGetReturnValue;
pGetReturnValue = NULL;
}
else
{
rError = CompleteRegSetValueEx(hKEY, sKey, pParametervValue, cbDataLength2, dataStyle);
if (rError == ERROR_SUCCESS)
bReturn = TRUE;
else
bReturn = FALSE;
}
}
else
{
if(ERROR_SUCCESS == ::RegCreateKeyEx(HKEY_CURRENT_USER, sLocation, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS,
NULL, &hKEY, (unsigned long *)&lDispos))
{
rError = CompleteRegSetValueEx(hKEY, sKey, pParametervValue, cbDataLength2, dataStyle);
if (rError == ERROR_SUCCESS && !bRead)
bReturn = TRUE;
else
bReturn = FALSE;
}
else
bReturn = FALSE;
}
if(hKEY)
RegCloseKey(hKEY);
return bReturn;
}
long DataLoadAndSave::CompleteRegSetValueEx(HKEY hKEY, CString sKey, LPBYTE pParametervValue, DWORD cbDataLength, EDataStyle dataStyle)
{
long rError(1);
switch (dataStyle)
{
case ECString:
{
rError = ::RegSetValueEx(hKEY, sKey, NULL, REG_SZ, pParametervValue, cbDataLength);
break;
}
case EDWORD:
{
rError = ::RegSetValueEx(hKEY, sKey, NULL, REG_DWORD, pParametervValue, cbDataLength);
break;
}
case EBytes:
{
rError = ::RegSetValueEx(hKEY, sKey, NULL, REG_BINARY, pParametervValue, cbDataLength);
break;
}
default:
break;
}
return rError;
}
bool DataLoadAndSave::JudgeDataWhetherOutOfRange(int x, int y)
{
CRect CADcet;
acedGetAcadFrame()->GetWindowRect(CADcet);
if (CADcet.PtInRect(CPoint(x, y)))
return true;
else
return false;
}
CString DataLoadAndSave::m_sLocation = GetRegLocation();