引言
C++玩了这么久,也该存点有用的东西
简介
操作系统:Window 7 x64
编程IDE:VS2015
编程语言:C/C++
作 者:陈鲁勇
网络相关
网络在生活中可谓是无处不在了,在提高程序健壮性的时候经常会用到相关的函数。
IP
内网IP识别
识别当前IP是否在内网,内网IP的特征是在这个三个区段
10.0.0.0:10.0.0.0~10.255.255.255
172.16.0.0:172.16.0.0~172.31.255.255
192.168.0.0:192.168.0.0~192.168.255.255
#define _WINSOCK_DEPRECATED_NO_WARNINGS
#include <iostream>
#include <winsock2.h>
#pragma comment(lib, "ws2_32.lib")
bool IsInternalIP(const uint32_t ip)
{
if ((ip >= 0x0A000000 && ip <= 0x0AFFFFFF) ||
(ip >= 0xAC100000 && ip <= 0xAC1FFFFF) ||
(ip >= 0xC0A80000 && ip <= 0xC0A8FFFF) ||
ip == 0x7F000001 || ip == 0)
return true;
else
return false;
}
int main(void) {
uint32_t ip = ntohl(::inet_addr("10.255.255.255"));
if (IsInternalIP(ip)) {
std::cout << "内网ip." << std::endl;
}
else {
std::cout << "非内网ip." << std::endl;
}
getchar();
return 0;
}
鉴别IP格式
/*
* 功能:鉴别ip有效性
*/
bool isIp(const char* const ip) {
do {
if (ip == nullptr) {
break;
}
int tmp1, tmp2, tmp3, tmp4, i;
i = sscanf_s(ip, "%d.%d.%d.%d", &tmp1, &tmp2, &tmp3, &tmp4);
if (i != 4 || (tmp1 > 255) || (tmp2 > 255)
|| (tmp3 > 255) || (tmp4 > 255)) {
break;
}
for (const char *temp_char = ip; *temp_char != 0; temp_char++) {
if ((*temp_char != '.')
&& ((*temp_char < '0') || (*temp_char > '9'))) {
break;
}
}
return true;
} while (false);
return false;
}
类型相关
字符串
字符串切割
模版来解决字符串切割的问题, 支持宽字符分割
#include <iostream>
#include <list>
template<typename CharType>
int StringTokenizeT(const std::basic_string<CharType> &input,
const std::basic_string<CharType> &delimitor,
std::list<std::basic_string<CharType> > &output)
{
size_t token_begin;
size_t token_end;
output.clear();
for (token_begin = token_end = 0; token_end != std::basic_string<CharType>::npos;) {
token_begin = input.find_first_not_of(delimitor, token_begin);
if (token_begin == std::basic_string<CharType>::npos)
break;
token_end = input.find_first_of(delimitor, token_begin + 1);
output.push_back(input.substr(token_begin, token_end - token_begin));
token_begin = token_end + 1;
}
return static_cast<int>(output.size());
}
int main(void) {
std::list<std::string> list;
const std::string &address = "http://blog.csdn.net/CSND_Ayo/article/details/69808210";
StringTokenizeT<char>(address, "/", list);
for (auto iter : list)
std::cout << iter.c_str() << std::endl;
std::list<std::wstring> wlist;
const std::wstring &waddress = L"http://blog.csdn.net/CSND_Ayo/article/details/69808210";
StringTokenizeT<wchar_t>(waddress, L"/", wlist);
for (auto iter : wlist)
std::wcout << iter.c_str() << std::endl;
getchar();
return 0;
}
获得文件类型
/**
* @brief:获得文件后缀名
* @param:_fileName 文件名 / 带有文件的路径名
* @return:成功 返回后缀名(_fileName的后缀名起始地址)
失败 返回空
**/
const char* const CommonHelper::getFileType(const char* const _fileName) {
/* 判断非法 */
if (_fileName == nullptr) {
return nullptr;
}
/* 计算字符串长度 */
int lenth = strlen(_fileName);
/* 从尾部开始检索文件类型 */
for (int pos = lenth - 1; pos > 0; --pos) {
/* 检查是否为 . 分隔符 */
if (_fileName[pos] == '.') {
/* 返回 . 之后的文件类型名 */
return &_fileName[pos + 1];
}
}
return nullptr;
}
接口相关
- bing 高清大图
/*
* @param : idx 图片下标
* @param : n 图片数量
*/
http://cn.bing.com/HPImageArchive.aspx?format=xml&idx=0&n=1
/*
* 可以通过截取xml的url部分与bing官网进行拼接,已达到获取高清图片的目的。
* 例如:http://www.bing.com/az/hprichbg/rb/Dipper_ZH-CN11205462091_1366x768.jpg
* 后缀的分辨率可以改成常用的显示屏分辨率,已达到修改照片分辨率的目的。
*/