- 其中的 TakeOutStringByChar 函数 查看我共享的代码
- /*
- 是不是一个 ip 的字符串形式 例如 127.000.000.001 1.1.1.1
- BY: SinCOder
- */
- bool is_ip_string(const char *str)
- {
- const char *pstr = str;
- uint len = 0;
- uint dot_count = 0; // . 计数
- while(*str)
- {
- if(!IS_CHAR_NUM(*str))
- {
- if(*str == '.')
- {
- ++dot_count;
- if(dot_count > 3)
- return false;
- }
- else
- return false;
- }
- ++str;
- ++len;
- if(len > 16)
- return false;
- }
- if(dot_count != 3 || len < 7)
- return false;
- // 下一步就是过滤掉 127..222.222这样的字符 。。。
- char buff[16] = {0};
- int str_block_count = 0;
- while((pstr = TakeOutStringByChar(pstr,'.',buff,16)))
- {
- int i = atoi(buff);
- if(i<0 || i>255)
- return false;
- ++str_block_count;
- }
- if(str_block_count!=4)
- return false;
- return true;
- }
- //该片段来自于http://www.codesnippet.cn/detail/170620134058.html
来源: http://www.codesnippet.cn/detail/170620134058.html