有这样的函数const std::string *my_names(){static const std::string priority_names[10] ={"FATAL","ALERT","CRIT","ERROR","WARN","NOTICE","INFO","DEBUG","NOTSET","UNKNOWN"};return priority_names;}这样调用的 my_names()[0],my_names()[1],my_names()[2];
/*!******************************************************************************** @brief 枚举类型的宏定义********************************************************************************/#define DECLARE_ENUM(E) \ struct E \ { \ public: \ E(int value = 0) : _value((__Enum)value) \ { \ } \ E& operator=(int value) \ { \ this->_value = (__Enum)value; \ return *this; \ } \ operator int() const \ { \ return this->_value; \ } \ \ enum __Enum \ {#define END_ENUM \ }; \ \ private: \ __Enum _value; \ };/*!******************************************************************************** @brief 常用函数********************************************************************************/class CCommon{public: CCommon(); ~CCommon();public: /** * @brief 将字符串转为大写 * @param [IN] [OUT] 字符串 * @return [OUT] 大写字符串 */ static std::string ToUpper(std::string &s); /** * @brief 将字符串转为小写 * @param [IN] [OUT] 字符串 * @return [OUT] 小写字符串 */ static std::string ToLower(std::string &s); /** * @brief 去除字符串空白符 * @param 要去除空白符的字符串("\r\n\t\x20") * @return 去除空白符后的字符串 */ static std::string Trim(const std::string &str); /** * @brief 去除字符串左边空白符 * @param 要去除空白符的字符串 * @return 去除空白符后的字符串 */ static std::string TrimLeft(const std::string &s); /** * @brief 去除字符串右边空白符 * @param 要去除空白符的字符串 * @return 去除空白符后的字符串 */ static std::string TrimRight(const std::string &s); /** * @brief CreateDir 创建多级目录 * @param pDirpath [IN] 目录路径 * @return int32_t [OUT] 0--成功,其他--失败 */ static int32_t CreateDir(const char * pDirpath); /** * @brief 把字符串根据分隔符sep分解,放到vector中 * @param [IN] s 欲分解字符串 * @param [OUT] v 存放地方 * @param [IN] sep 分隔符 * @return 无 */ static void ListToVector(const char *s, std::vector<std::string>& v, int sep); static void ListToVector(std::string s, std::vector<std::string>& v, const std::string sep); static void ListToVector(std::string s, std::vector<std::string>& v, const char* sep);};/*!******************************************************************************** @brief 时间类,获取时间等操作********************************************************************************/class COsTime{public: COsTime(); ~COsTime();public: /** * @brief 获取现在时间 格式为:YYYY-MM-DD HH24:MM:SS * @param 无 * @return 格式为YYYY-MM-DD HH24:MM:SS的字符串 */ static std::string now(); /** * @brief 时间格式化 * @param [IN] format 格式输入 * @return [OUT] 格式化后字符串 */ static std::string Format(const char* format); /** * @brief 获取现在时间 格式为:YYYYMMDDHH24MMSS * @param 无 * @return 格式为YYYYMMDDHH24MMSS的字符串 */ static std::string GetCurrentTime(); static std::string GetYear(); static std::string GetMonth(); static std::string GetWeekDay();};
/*!******************************************************************************** @brief 常用函数********************************************************************************/CCommon::CCommon(){}CCommon::~CCommon(){}string CCommon::ToUpper(string& s){ if (s.empty()) return s; for (string::iterator it=s.begin(); it!=s.end(); ++it) { if (isalpha(*it)) *it = toupper(*it); } return s;}string CCommon::ToLower(string& s){ if (s.empty()) return s; for (string::iterator it=s.begin(); it!=s.end(); ++it) { if (isalpha(*it)) *it = tolower(*it); } return s;}string CCommon::Trim(const string& str){ static const char* blank = "\r\n\t\x20";// \f\v if (str.empty()) return str; string::size_type a = str.find_first_not_of(blank); string::size_type b = str.find_last_not_of(blank); return string(str,a,b-a+1);}string CCommon::TrimLeft(const string &s){ string::const_iterator it; if (s.empty()) return s; for (it = s.begin(); it != s.end(); ++it) { if (!isspace(*it)) break; } return string(it,s.end());}string CCommon::TrimRight(const string &s){ string::difference_type dt; string::const_reverse_iterator it; if (s.empty()) return s; for (it = s.rbegin(); it != s.rend(); ++it) { if (!isspace(*it)) break; } dt = s.rend() - it; return string(s.begin(),s.begin() + dt);}int32_t CCommon::CreateDir(const char * pDirpath){ assert(NULL != pDirpath); int32_t nRet = 0; char * p = new char[strlen(pDirpath)+1];//char * p = strdup(pDirpath); memset(p,0x00,strlen(pDirpath)+1); memcpy(p,pDirpath,strlen(pDirpath)); int32_t length = strlen(p); for (int32_t i=0;i<length;++i){ if (i == 0 && (p[0] == '\\' || p[i] == '/'))//根目录不要创建 continue; if (p[i] == '\\' || p[i] == '/'){ p[i] = '\0'; nRet = access(p,0);//不存在,则创建 if (0 != nRet){ nRet = mkdir(p,0777); if (0 != nRet) return nRet; } p[i] = '/'; } } nRet = access(p,0); if (0 != nRet) nRet = mkdir(p,0777); delete [] p;//free(p); return nRet;}void CCommon::ListToVector(const char* s, std::vector<string>& v, int sep){ assert(NULL != s); char *index = NULL; char *pos = NULL; char *pCopy = new char[strlen(s)+1]; memset(pCopy,0x00,strlen(s)+1); char *pBuf = new char[strlen(s)+1]; memset(pBuf,0x00,strlen(s)+1); memcpy(pBuf,s,strlen(s)); pos = pBuf; while(NULL != (index = strchr(pos,sep))) { memset(pCopy,0x00,strlen(s)+1); strncpy(pCopy,pos,index-pos); string str(pCopy); v.push_back(str); pos = index+1; } if (NULL == index) { memset(pCopy,0x00,strlen(s)+1); strcpy(pCopy,pos); string str(pCopy); v.push_back(str); } delete [] pBuf; delete [] pCopy;}void CCommon::ListToVector(string s, std::vector<string>& v, const string sep){ string::size_type nIndex = 0; string::size_type nPos = 0; v.clear(); while(string::npos != (nIndex=s.find(sep,nPos))) { string m = s.substr(nPos,nIndex-nPos); v.push_back(m); nPos = nIndex+1; }}void CCommon::ListToVector(string s, std::vector<string>& v, const char* sep){ v.clear(); char* buffer = new char[s.length() + 1]; memcpy(buffer, s.c_str(), s.length()); buffer[s.length()] = 0; char* token = strtok(buffer, sep); while (token) { string sToken = token; v.push_back(sToken); token = strtok(NULL, sep); } delete [] buffer;}/*!******************************************************************************** @brief 时间类********************************************************************************/COsTime::COsTime(){}COsTime::~COsTime(){}string COsTime::now(){ const char * format = "%Y-%m-%d %H:%M:%S"; return Format(format);}string COsTime::Format(const char* format){ string s; time_t lVal;tm *plocaltime = NULL;char buffer[128] = {0x00};time(&lVal); if ((plocaltime = localtime(&lVal)) != 0 ) if (strftime(buffer, 128, format, plocaltime) > 0) s = buffer;return s;}string COsTime::GetCurrentTime(){ const char * format = "%Y%m%d%H%M%S"; return Format(format);}std::string COsTime::GetYear(){ const char * format = "%Y"; return Format(format);}std::string COsTime::GetMonth(){ const char * format = "%m"; return Format(format);}std::string COsTime::GetWeekDay(){ const char * format = "%A"; return Format(format);}
#include #include #include #include #include #include #include #include