现在的位置: 首页 > 综合 > 正文

对读取的配置字符串批量处理的可变参数函数

2013年10月15日 ⁄ 综合 ⁄ 共 1475字 ⁄ 字号 评论关闭

 

代码很清楚了,本来是每个地方都用CString作临时变量再Replace,后面改成用一个函数集中处理。

使用这个函数要注意越界的问题。

记录如下

 

TCHAR* replaceCStyleChar(TCHAR* strSrc, TCHAR const * strHolder, TCHAR const * strValue)
 {
  if (NULL == strSrc || NULL == strHolder || NULL == strValue)
  {
   return NULL;
  }

  //记录是否需要修改,因为后面有一个插入结束符的操作,若不记录,某些情况导致字符串变短。

  bool bChange = false;

  //get length
  int iSrcLen = lstrlen(strSrc);
  int iHolderLen = lstrlen(strHolder);
  int iDesLen = lstrlen(strValue);

  //if src string is less than holder string, return it
  if (iSrcLen < iHolderLen)
  {
   return strSrc;
  }

  int iPos = 0;
  TCHAR sTmp[255];
  while (iPos < iSrcLen)
  {
   //if left length of src string, then break it
   if (iSrcLen - iPos < iHolderLen)
   {
    break;
   }

   if (strSrc[iPos] == strHolder[0])
   {
    memcpy(sTmp, strSrc + iPos, iHolderLen*sizeof(TCHAR));
    sTmp[iHolderLen] = 0;
    if (lstrcmp(sTmp, strHolder) == 0)
    {
     bChange = true;
     //movie移位
     memcpy(strSrc + iPos + iDesLen, strSrc + iPos + iHolderLen, (iSrcLen - iPos - iHolderLen)*sizeof(TCHAR));
     //fill填充空位
     memcpy(strSrc + iPos, strValue, iDesLen*sizeof(TCHAR));
     //change iPos
     iPos += iHolderLen;
     continue;
    }
   }
   iPos++;
  }

  if (bChange)
  {
   strSrc[iSrcLen + iDesLen - iHolderLen] = 0;
  }
  else
  {}

  return strSrc;
 }

 

 

void ChangeTCharContentForPhoto(const int iTCharCount, ...)

{

va_list argp;

va_start(argp,iTCharCount);

for (int i = 0; i<iTCharCount; i++)

{

replaceCStyleChar(va_arg(argp,TCHAR*), VIRTUAL_NETPATH, _T(""));

}

//指针重新移动到参数列表开头

va_start(argp,iTCharCount);

for (int i = 0; i<iTCharCount; i++)

{

replaceCStyleChar(va_arg(argp,TCHAR*), _T("/"), _T("//"));

}

va_end(argp);

}

抱歉!评论已关闭.