CCmdStr类用于拆分有分隔符的字符串。
字符串的格式为:字段内容1+分隔符+字段内容2+分隔符+字段内容3+分隔符+...+字段内容n。
例如:"messi,10,striker,30,1.72,68.5,Barcelona",这是足球运动员梅西的资料,
包括姓名、球衣号码、场上位置、年龄、身高、体重和效力的俱乐部,字段之间用半角的逗号分隔。
class CCmdStr
{
public:
vector<string> m_vCmdStr;
CCmdStr();
CCmdStr(const string &buffer,const char *sepstr,const bool bdelspace=false);
void SplitToCmd(const string &buffer,const char *sepstr,const bool bdelspace=false);
int CmdCount();
bool GetValue(const int inum,char *value,const int ilen=0);
bool GetValue(const int inum,int *value);
bool GetValue(const int inum,unsigned int *value);
bool GetValue(const int inum,long *value);
bool GetValue(const int inum,unsigned long *value);
bool GetValue(const int inum,double *value);
bool GetValue(const int inum,bool *value);
~CCmdStr();
};
----cpp
CCmdStr::CCmdStr()
{
m_vCmdStr.clear();
}
CCmdStr::CCmdStr(const string &buffer,const char *sepstr,const bool bdelspace)
{
m_vCmdStr.clear();
SplitToCmd(buffer,sepstr,bdelspace);
}
void CCmdStr::SplitToCmd(const string &buffer,const char *sepstr,const bool bdelspace)
{
m_vCmdStr.clear();
int iPOS=0;
string srcstr,substr;
srcstr=buffer;
while ( (iPOS=srcstr.find(sepstr)) >= 0)
{
substr=srcstr.substr(0,iPOS);
if (bdelspace == true)
{
char str[substr.length()+1];
STRCPY(str,sizeof(str),substr.c_str());
DeleteLRChar(str,' ');
substr=str;
}
m_vCmdStr.push_back(substr);
iPOS=iPOS+strlen(sepstr);
srcstr=srcstr.substr(iPOS,srcstr.size()-iPOS);
}
substr=srcstr;
if (bdelspace == true)
{
char str[substr.length()+1];
STRCPY(str,sizeof(str),substr.c_str());
DeleteLRChar(str,' ');
substr=str;
}
m_vCmdStr.push_back(substr);
return;
}
int CCmdStr::CmdCount()
{
return m_vCmdStr.size();
}
bool CCmdStr::GetValue(const int inum,char *value,const int ilen)
{
if ( (inum>=(int)m_vCmdStr.size()) || (value==0) ) return false;
if (ilen>0) memset(value,0,ilen+1);
if ( (m_vCmdStr[inum].length()<=(unsigned int)ilen) || (ilen==0) )
{
strcpy(value,m_vCmdStr[inum].c_str());
}
else
{
strncpy(value,m_vCmdStr[inum].c_str(),ilen); value[ilen]=0;
}
return true;
}
bool CCmdStr::GetValue(const int inum,int *value)
{
if ( (inum>=(int)m_vCmdStr.size()) || (value==0) ) return false;
(*value) = 0;
if (inum >= (int)m_vCmdStr.size()) return false;
(*value) = atoi(m_vCmdStr[inum].c_str());
return true;
}
bool CCmdStr::GetValue(const int inum,unsigned int *value)
{
if ( (inum>=(int)m_vCmdStr.size()) || (value==0) ) return false;
(*value) = 0;
if (inum >= (int)m_vCmdStr.size()) return false;
(*value) = atoi(m_vCmdStr[inum].c_str());
return true;
}
bool CCmdStr::GetValue(const int inum,long *value)
{
if ( (inum>=(int)m_vCmdStr.size()) || (value==0) ) return false;
(*value) = 0;
if (inum >= (int)m_vCmdStr.size()) return false;
(*value) = atol(m_vCmdStr[inum].c_str());
return true;
}
bool CCmdStr::GetValue(const int inum,unsigned long *value)
{
if ( (inum>=(int)m_vCmdStr.size()) || (value==0) ) return false;
(*value) = 0;
if (inum >= (int)m_vCmdStr.size()) return false;
(*value) = atol(m_vCmdStr[inum].c_str());
return true;
}
bool CCmdStr::GetValue(const int inum,double *value)
{
if ( (inum>=(int)m_vCmdStr.size()) || (value==0) ) return false;
(*value) = 0;
if (inum >= (int)m_vCmdStr.size()) return false;
(*value) = (double)atof(m_vCmdStr[inum].c_str());
return true;
}
bool CCmdStr::GetValue(const int inum,bool *value)
{
if ( (inum>=(int)m_vCmdStr.size()) || (value==0) ) return false;
(*value) = 0;
if (inum >= (int)m_vCmdStr.size()) return false;
char strTemp[11];
memset(strTemp,0,sizeof(strTemp));
strncpy(strTemp,m_vCmdStr[inum].c_str(),10);
ToUpper(strTemp);
if (strcmp(strTemp,"TRUE")==0) (*value)=true;
return true;
}
CCmdStr::~CCmdStr()
{
m_vCmdStr.clear();
}
-----测试
struct st_player
{
char name[51];
char no[6];
bool striker;
int age;
double weight;
long sal;
char club[51];
}stplayer;
int main()
{
memset(&stplayer,0,sizeof(struct st_player));
char buffer[301];
STRCPY(buffer,sizeof(buffer),"messi ,10,true,30,68.5,2100000,Barcelona");
CCmdStr CmdStr;
CmdStr.SplitToCmd(buffer,",");
CmdStr.GetValue(0, stplayer.name,50);
CmdStr.GetValue(1, stplayer.no,5);
CmdStr.GetValue(2,&stplayer.striker);
CmdStr.GetValue(3,&stplayer.age);
CmdStr.GetValue(4,&stplayer.weight);
CmdStr.GetValue(5,&stplayer.sal);
CmdStr.GetValue(6, stplayer.club,50);
printf("name=%s,no=%s,striker=%d,age=%d,weight=%.1f,sal=%ld,club=%s\n",\
stplayer.name,stplayer.no,stplayer.striker,stplayer.age,\
stplayer.weight,stplayer.sal,stplayer.club);
}