函数的作用:
这个函数主要的作用是获取特定格式的字符串的地址和数据
比如 CString strValue = "0x1000:0x2540;0x1001:0x1234;";
地址是0x1000和0x1001,分别需要获取0x2540和0x1234
函数的实现:
int GetCurrentProjectOption(WORD* pwOption, int iMaxCount)
{
int iCount = 0;
int iCopFirstAddr = 0x1000;
CString strValue = "0x1000:0x2540;0x1001:0x1234;";
CString strItem;
int iEndIndex = 0;
memset(pwOption, 0 ,iMaxCount*sizeof(WORD));
while (strValue.GetLength() > 0)
{
iEndIndex = strValue.Find(_T(";"));
if(iEndIndex < 0) iEndIndex = strValue.GetLength();
strItem = strValue.Left(iEndIndex + 1);
strItem.Remove(_T(';'));
int iAddress = 0;
TCHAR tszData[100] = {0};
TCHAR tszAddress[100] = {0};
//格式的转换,通过这个函数截取不同的字符串
_stscanf(strItem, _T("0x%[^:]:0x%s"), tszAddress, tszData);
//字符串转为long型
iAddress = _tcstol(tszAddress, NULL, 16) - iCopFirstAddr;
if(iAddress >= 0 && iAddress < (int)(iMaxCount * sizeof(WORD)))
{
pwOption[iAddress] = (WORD)_tcstol(tszData, NULL, 16);
}
if((iEndIndex + 1) >= strValue.GetLength()) break;
//通过之前的分号截取后面的字符串
strValue = strValue.Mid(iEndIndex + 1);
}
return iCount;
}