Benjamin

静以修身,俭以养德,非澹薄无以明志,非宁静无以致远。
随笔 - 386, 文章 - 0, 评论 - 196, 引用 - 0
数据加载中……

根据特定分割符拆分字符串、判断字符串是否为IP

////////////////////////////////////////////////////////////
//函数名: splitEx
//功能:    根据特定的分隔符拆分字符串
//输入参数:string& src-------------需要拆分的原字符串
//          char ch-----------------分隔符
//返回值:  string类型的动态数组(vector)
/////////////////////////////////////////////////////////////  
vector<string> Zone::splitEx(string& src, char ch)  
{  
  vector<string> strs; 
 string last(&ch);
 string pro_str;
 char c_temp;

  //去掉字符串中的双引号
 std::string::size_type First_pos = src.find("\"");
 std::string::size_type Last_pos = src.find_last_of("\"");
 if (First_pos == 0  && Last_pos !=std::string::npos)
 {
  src[Last_pos]='\0';
  string t_sTemp;
  for (int i=0;i<(int)src.size();++i)
  {
   if(i>0)
   {
    t_sTemp += src[i];
   }
  }
  strs.insert(strs.end(),t_sTemp);
  return strs;
 }

  //将字符串中多个连续的分隔符压缩成一个
 for (int i=0;i<(int)src.size();++i)
 {
  if(i>0)
  {
      if (src[i] == ch && src[i-1]==ch)
      {
         continue;
      }
   }
    c_temp=src[i];
    pro_str += c_temp;
 }
   
 src=pro_str;

 int separate_characterLen = 1;//分割字符串的长度
    int lastPosition = 0,index = -1;  
    while (-1 != (index = src.find(ch,lastPosition)))  
    {  
       string Temp= src.substr(lastPosition,index - lastPosition);
       if(!Temp.empty())
       {
            strs.push_back(Temp));  
        }
        lastPosition = index + separate_characterLen;  

    }  
    string lastString = src.substr(lastPosition);//截取最后一个分隔符后的内容   
    if (!lastString.empty() && lastString != last)
    {
      //cout<<"push_back_end"<<endl;  
       strs.push_back(lastString);//如果最后一个分隔符后还有内容就入队 
     }

    return strs;  
}  
////////////////////////////////////////////////////////////
//函数名: IsIp
//功能:    判断字符串是否为IP
//输入参数:string In-------字符串
//返回值:  true-------是IP,false----------不是IP
//修改记录:暂无
/////////////////////////////////////////////////////////////
bool Zone::IsIp(string In)
{
  int ip[4]={-1,-1,-1,-1};
  sscanf(In.c_str(),"%d.%d.%d.%d",&(ip[0]),&(ip[1]),&(ip[2]),&(ip[3]));
  if (ip[0]>-1 && ip[1]>-1 && ip[2]>-1 && ip[3]>-1)//是IP
  {
       return true;
  }
  else
  {
      return false;
  }
}

判断是否为IP,也可以用inet_pton函数
 unsigned char buf[sizeof(struct in6_addr)];  
    int domain, s;  
    char str[INET6_ADDRSTRLEN];  
 
    //IP字符串 ——》网络字节流  
    s = inet_pton(AF_INET, In.c_str(), buf);  
    if(s<=0)
    {  
        return false;
    }
   
    return true;
In是string对象,如果是IPV6,将 inet_pton的第一个参数改成AF_INET6,但是这个函数只在
linux下,windows下是没有的。在liunx下要包含sys/types.h、sys/socket.h、arpa/inet.h
原型:int inet_pton(int af, const char *src, void *dst); 这个函数转换字符串到网络地址,第一个参数af是地址族,转换后存在dst中
返回值如果小于等于0就表示ip不合法。
在windows平台下,在windows的SDKs\v6.0A\Include\ws2tcpip.h, inet_pton was
defined when NTDDI_VERSION >= NTDDI_LONGHORN with the following lines:

#if (NTDDI_VERSION >= NTDDI_LONGHORN)
WINSOCK_API_LINKAGE
INT
WSAAPI
inet_pton(
    __in                                INT             Family,
    __in                                PCSTR           pszAddrString,
    __out_bcount(sizeof(IN6_ADDR))      PVOID           pAddrBuf
    );
但是要注意windows版本(NTDDI_LONGHORN的windows版本是Windows Server 2008)

posted on 2011-04-26 20:38 Benjamin 阅读(1638) 评论(0)  编辑 收藏 引用 所属分类: C/C++


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理