一个有用的工具类(功能会慢慢添加)Util
来源:互联网 发布:淘宝直通车管用吗 编辑:程序博客网 时间:2024/06/06 08:24
头文件Util.h
#ifndef _UTIL_H_#define _UTIL_H_#include <string.h>#include <time.h>#include <stdio.h>#include <stdarg.h>#include <iostream>#include <fstream>#include <map>#include <deque>#include <vector>#include <set>#include <list>#include <string>#include <ctime>#include <fstream>#include <sstream>#include <exception>#include <stdlib.h>#include <errno.h>#include <assert.h>#if (defined(WIN32) || defined(WIN64))#include <direct.h>#include <process.h>#include <tchar.h>#include <WINSOCK2.H>#include <Mswsock.h>#include <windows.h>#ifdef _DEBUG#include <assert.h>#endif#include <io.h>#include <winsock.h>#include <wininet.h>#include <sql.h>#include <sqlext.h>#include <sqltypes.h>#pragma comment(lib,"WS2_32")#pragma comment(lib,"odbc32")#else#include<pthread.h>#include <sys/socket.h>#include <arpa/inet.h>#include <netinet/in.h>#include <netdb.h>#include <sys/types.h>/* some systems still require this */#include <sys/stat.h>#include <sys/termios.h>/* for winsize */#ifndef TIOCGWINSZ#include <sys/ioctl.h>#endif#include <stdio.h>/* for convenience */#include <stdlib.h>/* for convenience */#include <stddef.h>/* for offsetof */#include <string.h>/* for convenience */#include <unistd.h>/* for convenience */#include <signal.h>/* for SIG_ERR */#endifusing namespace std;#ifndef SZ_LENGTH#define SZ_LENGTH 128#endif#ifndef SZ_S_LENGTH#define SZ_S_LENGTH 64#endif#ifndef SZ_L_LENGTH#define SZ_L_LENGTH 512#endif#ifndef SZ_8_LENGTH#define SZ_8_LENGTH 8#endif#ifndef SZ_16_LENGTH#define SZ_16_LENGTH 16#endif#ifndef SZ_32_LENGTH#define SZ_32_LENGTH 32#endif#ifndef SZ_1024_LENGTH#define SZ_1024_LENGTH 1024#endif#ifndef SZ_2048_LENGTH#define SZ_2048_LENGTH 2048#endif#ifndef SZ_4096_LENGTH#define SZ_4096_LENGTH 4096#endif// 定义类中属性的访问方法(可以减少编写重复的代码)#ifndef PROPERTY_INIT#define PROPERTY_INIT(xtype,xname,proper)\voidSet##xname(xtype val)\{\proper = val;\}\xtype Get##xname()\{\return proper;\}\#endif/************************************************************************* 工具类 ************************************************************************/class Util{public:// 获取区间[a,b)内的一个随机数static intGetRandomNum(int a,int b);// 获取一个随机概率static floatGetProbability();// 把int转换成stringstatic stringConvertToStr(int value);// 把double转换成stringstatic stringConvertToStr(double value);// 从sockaddr_in中获取IP地址static stringGetIPString(sockaddr_in& addr);// 从sockaddr_in获取端口static u_shortGetPort(sockaddr_in& addr);// 根据spilit_char切割字符串,结果放在vector中返回static vector<string>Spilit(const char* str,int len,char spilit_char);// 日志记录器static voidLog(const char* format,...);static stringGetRandomString(int len);};//Util::Log("[%s] [%s] [Line:%d]\t",__TIME__,__FILE__,__LINE__);#ifndef LOG_DEBUG#define LOG_DEBUG \Util::Log#endif#endif //_UTIL_H_
源文件Util.cpp
#include "Util.h"#include <time.h>#include <limits>// 获取区间[a,b)内的随机数int Util::GetRandomNum(int a,int b){static int initFlag = 0;if (initFlag == 0){srand((unsigned)time(NULL)); initFlag = 1;}return rand()%(b-a)+a;}// 得到一个0~1的随机数float Util::GetProbability(){int n = GetRandomNum(1,101);return (n / 100.0);}// 把int转换成stringstring Util::ConvertToStr(int value){char strBuffer[16] = {0};sprintf(strBuffer,"%d",value);return strBuffer;}// 把double转换成stringstring Util::ConvertToStr(double value){char strBuffer[16] = {0};sprintf(strBuffer,"%lf",value);return strBuffer;}// 从sockaddr_in中获取IP地址string Util::GetIPString(sockaddr_in& addr){return inet_ntoa(addr.sin_addr);}// 从sockaddr_in获取端口u_short Util::GetPort(sockaddr_in& addr){return ntohs(addr.sin_port);}// 根据spilit_char切割字符串,结果放在vector中返回vector<string> Util::Spilit(const char* str,int len,char spilit_char){vector<string>stringList;const intnstrBufferLen = 32;intnstrBufferIndex = 0;charstrBuffer[nstrBufferLen] = {0};for (int i = 0; i < len; ++i){if (str[i] != spilit_char){strBuffer[nstrBufferIndex++] = str[i];}else{stringList.push_back(strBuffer);memset(strBuffer,0,nstrBufferLen);nstrBufferIndex = 0;}}stringList.push_back(strBuffer);return stringList;}// 日志记录器void Util::Log(const char* format,...){// 简单的日志类class CreateLogFile{public:CreateLogFile(){// 创建一个可写文件m_pFile = 0;m_strFileName = "";time_t ntime;tm* ptimeinfo;time(&ntime);ptimeinfo = localtime(&ntime);char szBuffer[1024] = {0};sprintf(szBuffer,"%4d-%02d-%02d",1900+ptimeinfo->tm_year, 1+ptimeinfo->tm_mon,ptimeinfo->tm_mday);m_strFileName = szBuffer;int index = GetRandomNum(0,100);string strIndex = ConvertToStr(index);//m_strFileName += "_";//m_strFileName += strIndex;m_strFileName += ".log";m_pFile = fopen(m_strFileName.c_str(),"w+");}voidRecord(const string& str){// 将信息写入到文件中if (m_pFile == 0){return;}fwrite(str.c_str(),str.size(),1,m_pFile);fflush(m_pFile);}~CreateLogFile(){// 关闭文件if (m_pFile != 0){fflush(m_pFile);fclose(m_pFile);}}private:string m_strFileName;// 日志文件名字FILE* m_pFile;// 文件指针};static CreateLogFile logfile; // static保证只初始化一次time_t ntime;tm* ptimeinfo;time(&ntime);// 获取时间ptimeinfo = localtime(&ntime);// 转换为本地时间char szBuffer[SZ_1024_LENGTH] = {0};// 在记录信息前面打上时间标记sprintf(szBuffer,"%4d-%02d-%02d %02d:%02d:%02d",1900+ptimeinfo->tm_year, 1+ptimeinfo->tm_mon,ptimeinfo->tm_mday,ptimeinfo->tm_hour,ptimeinfo->tm_min,ptimeinfo->tm_sec);string temp = "";temp = "[";temp += szBuffer;va_list info;va_start(info,format);vsnprintf(szBuffer,SZ_1024_LENGTH,format,info);//printf("%s",szBuffer);#if (defined(WIN32) || defined(WIN64))strcat(szBuffer,"\r\n");#elsestrcat(szBuffer,"\n");#endif temp += "] ";temp += szBuffer;logfile.Record(temp);}// 得到一个随机字符串stringUtil::GetRandomString(int len){if (len <= 0){return "";}string strRet = "";for (int i = 0; i < len; ++i){int x = GetRandomNum(0,128);if ((x >= '0' && x <= '9') || (x >= 'a' && x <= 'z') || (x >= 'A' && x <= 'Z')){strRet += (char)(x);}}return strRet;}
0 0
- 一个有用的工具类(功能会慢慢添加)Util
- 一些常用的JS正则表达式--会慢慢添加
- 看《c++ primer》的过程中原来没遇到的内容的总结(会慢慢添加的)
- 有用的工具类
- 有用的工具类
- 数组(慢慢添加)
- 一个有用的NHibernate工具类NHibernateUtil(第一版)
- 一个有用的NHibernate工具类NHibernateUtil (其它版本)
- 一个有用的NHibernate工具类NHibernateUtil (第二版)
- 一个有用的NHibernate工具类NHibernateUtil (第三版)
- 一个有用的正则表达式判定工具类
- 一个PHP的数组工具UTIL
- 动画,一个方框会慢慢变小变白
- SQLite的一点学习心得(以后会慢慢补充)
- 可能对大家有用的工具一个
- 可能对大家有用的工具一个
- 推荐一个有用的工具--Aargo
- android 的Util工具类
- 液体壁纸价格 湖南硅藻泥 壁纸漆价格 液体墙纸绿色环保材料 转载▼
- shrio 权限管理filterChainDefinitions过滤器配置
- swift 点击cell没反应,点击后应该跳到指定页面
- 使用升级脚本进行数据库版本管理及发布
- 【swift】15-0601 枚举类型
- 一个有用的工具类(功能会慢慢添加)Util
- 张念萍,我喜欢你 yilong 栏甘
- 张念萍,我喜欢你 yilong 队曝
- Android NDK开发Crash错误定位
- Dubbo入门
- 2015最新扑克分析仪【加威-信13535353835】2015最新扑克分析仪
- 扑克牌分析仪价格【加威-信13535353835】扑克牌分析仪价格
- 2015最新扑克分析工具【加威-信13535353835】2015最新扑克分析工具
- Java程序员也应该知道的系统知识系列之内存