?? lsutil.cpp
字號:
/*********************************************************************
* 版權所有 (C)2006, 深圳市中興通訊股份有限公司。
*
* 文件名稱: LSUtil.cpp
* 文件標識:
* 內(nèi)容摘要: 系統(tǒng)日志服務工具函數(shù)實現(xiàn)
* 其它說明:
* 當前版本: V1.00
* 作 者: 張 帆
* 完成日期: 2006-07-20
*
* 修改記錄1:// 修改歷史記錄,包括修改日期、修改者及修改內(nèi)容
* 修改日期:
* 版 本 號:
* 修 改 人:
* 修改內(nèi)容:
* 修改記錄2:…
**********************************************************************/
#include "LSUtil.h"
#include <windows.h>
#include <io.h>
#include <fstream>
#include "NOPConst.h"
using namespace std;
using namespace NOP;
/**************************************************************************
* 類CLSUtil的靜態(tài)數(shù)據(jù)成員 *
**************************************************************************/
map<string, TLogLevel> CLSUtil::m_LogLevelMap; // 日志服務工具類的單實例對象
/**************************************************************************
* 類CLSUtil實現(xiàn) *
**************************************************************************/
/**************************************************************************
* 函數(shù)名稱: GetDefaultPath()
* 功能描述: 取得執(zhí)行文件路徑
* 輸入?yún)?shù): 無
* 輸出參數(shù): 無
* 返 回 值: 得到本模塊自己的可執(zhí)行文件路徑
* 其它說明: 無
* 修改日期 版本號 修改人 修改內(nèi)容
* -----------------------------------------------
* 2006/07/03 V1.0 張 帆 創(chuàng)建
**************************************************************************/
string CLSUtil::GetDefaultPath(void)
{
char chFileName[MAX_PATH+1];
GetModuleFileName(NULL, chFileName, MAX_PATH);
string strFileName = chFileName;
string strPath = strFileName.substr(0, strFileName.find_last_of('\\')+1);
return strPath;
}
/**************************************************************************
* 函數(shù)名稱: FullPath()
* 功能描述: 根據(jù)相對路徑獲取全路徑
* 輸入?yún)?shù): const string& strPartialPath : 相對路徑
* 輸出參數(shù): 無
* 返 回 值: 該文件的全路徑
* 其它說明: 無
* 修改日期 版本號 修改人 修改內(nèi)容
* -----------------------------------------------
* 2006/07/03 V1.0 張 帆 創(chuàng)建
**************************************************************************/
const string CLSUtil::FullPath(const string& strPartialPath)
{
char full[_MAX_PATH];
if( _fullpath( full, strPartialPath.c_str(), _MAX_PATH ) != NULL )
{
return string(full);
}
else
{
return "";
}
}
/**************************************************************************
* 函數(shù)名稱: make_sure_path_exists()
* 功能描述: 創(chuàng)建路徑
* 輸入?yún)?shù): const char *iPath : 待確定的目標路徑
* bool FilenameIncluded : 該路徑中是否包含文件名稱
* 輸出參數(shù): 無
* 返 回 值: true : 指定的目標路徑可以創(chuàng)建
* false : 指定的目標路徑不可以創(chuàng)建
* 其它說明: 無
* 修改日期 版本號 修改人 修改內(nèi)容
* -----------------------------------------------
* 2006/07/03 V1.0 張 帆 創(chuàng)建
**************************************************************************/
int CLSUtil::make_sure_path_exists( const char *iPath,
bool FilenameIncluded /*=false*/)
{
char *Path=(char*)iPath,
*TmpPath=Path,
TmpSmb=0,
*LastDPtr=NULL;
while((TmpPath=strpbrk(TmpPath+1,"\\/")))
{
TmpSmb=Path[TmpPath-Path];
Path[TmpPath-Path]=0;
CreateDirectory(Path,NULL);
Path[TmpPath-Path]=TmpSmb;
LastDPtr=TmpPath;
}
int Res=1;
if(!FilenameIncluded)
{
CreateDirectory(iPath,NULL);
Res=!_access(iPath,0);
}
else
{
if(LastDPtr)
{
Path=(char*)iPath;
TmpSmb=Path[LastDPtr-Path];
Path[LastDPtr-Path]=0;
Res=!_access(Path,0);
Path[LastDPtr-Path]=TmpSmb;
}
}
return Res;
}
//
//File
//
/**************************************************************************
* 函數(shù)名稱: IsAllowedFileName()
* 功能描述: 檢查文件名非法
* 輸入?yún)?shù): const string& strSrc : 文件名稱
* 輸出參數(shù): 無
* 返 回 值: true : 文件名合法
* false : 文件名非法
* 其它說明: 無
* 修改日期 版本號 修改人 修改內(nèi)容
* -----------------------------------------------
* 2006/07/04 V1.0 張 帆 創(chuàng)建
**************************************************************************/
bool CLSUtil::IsAllowedFileName(const string& strSrc)
{
static char chUnAllowed[] =
{'\\', '/', ':', '*', '?', '\"', '<','>', '|', NULL};
string::const_iterator iter = strSrc.begin();
for (; iter != strSrc.end(); iter++)
{
int i= 0;
while (NULL != chUnAllowed[i])
{
if (*iter == chUnAllowed[i])
{
return false;
}
i++;
}
}
return true;
}
/**************************************************************************
* 函數(shù)名稱: GetFileLineNumber()
* 功能描述: 獲得文件的行數(shù)
* 輸入?yún)?shù): const string& strFile : 文件名稱
* 輸出參數(shù): 無
* 返 回 值: iNumber : 文件的行數(shù)
* 其它說明: 無
* 修改日期 版本號 修改人 修改內(nèi)容
* -----------------------------------------------
* 2006/07/04 V1.0 張 帆 創(chuàng)建
**************************************************************************/
int CLSUtil::GetFileLineNumber(const string& strFile)
{
int iNumber = 0;
char temp;
ifstream f;
f.open(strFile.c_str());
if (!f.is_open())
{
return INVALID_LINENUM;
}
while(!f.eof())
{
temp = f.get();
if (temp == '\n')
{
iNumber++;
}
}
f.close();
return iNumber;
}
/**************************************************************************
* 函數(shù)名稱: MoveFile()
* 功能描述: 文件更名
* 輸入?yún)?shù): const string& strFileSrc : 源文件名
* const string& strFileDest : 目標文件名
* 輸出參數(shù): 無
* 返 回 值: true : 文件更名成功
* false : 文件更名失敗
* 其它說明: 無
* 修改日期 版本號 修改人 修改內(nèi)容
* -----------------------------------------------
* 2006/07/04 V1.0 張 帆 創(chuàng)建
**************************************************************************/
bool CLSUtil::MoveFileabc(const string& strFileSrc,
const string& strFileDest)
{
if (!IsFileExists(strFileSrc))
{
return true;
}
bool bRet = false;
BOOL bMoved = MoveFileEx(strFileSrc.c_str(),
strFileDest.c_str(),
MOVEFILE_REPLACE_EXISTING);
if (bMoved != 0 ) //重命名成功
{
bRet = true;
}
return bRet;
}
/**************************************************************************
* 函數(shù)名稱: IsFileExists()
* 功能描述: 檢查文件是否存在
* 輸入?yún)?shù): const string& strFileSrc : 文件名稱
* 輸出參數(shù): 無
* 返 回 值: true : 文件存在
* false : 文件不存在
* 其它說明: 無
* 修改日期 版本號 修改人 修改內(nèi)容
* -----------------------------------------------
* 2006/07/04 V1.0 張 帆 創(chuàng)建
**************************************************************************/
bool CLSUtil::IsFileExists(const string& strFile)
{
return ( (_access( strFile.c_str(), 0 )) != -1 );
}
/**************************************************************************
* 函數(shù)名稱: MakeSureFileExists()
* 功能描述: 確認文件存在, 如果文件不存在則創(chuàng)建
* 輸入?yún)?shù): const string& strFileSrc : 文件名稱
* 輸出參數(shù): 無
* 返 回 值: true : 成功
* false : 失敗
* 其它說明: 無
* 修改日期 版本號 修改人 修改內(nèi)容
* -----------------------------------------------
* 2006/07/04 V1.0 張 帆 創(chuàng)建
**************************************************************************/
bool CLSUtil::MakeSureFileExists(const string& strFile)
{
if(!IsFileExists(strFile))
{
fstream f;
f.open(strFile.c_str(), ios::out | ios::trunc);
f.close();
}
return true;
}
//
//No case compare
//
/**************************************************************************
* 函數(shù)名稱: IsNoCaseStrEqual()
* 功能描述: 不區(qū)分大小比較字符串
* 輸入?yún)?shù): const string& s1 : 源字符串
* const string& s2 : 目標字符串
* 輸出參數(shù): 無
* 返 回 值: true : 字符串相同
* false : 字符串不同
* 其它說明: 無
* 修改日期 版本號 修改人 修改內(nèi)容
* -----------------------------------------------
* 2006/07/04 V1.0 張 帆 創(chuàng)建
**************************************************************************/
bool CLSUtil::IsNoCaseStrEqual(const string& s1, const string& s2)
{
bool bRet = false;
if (s1.size() == s2.size() //ensure same size
&& equal(s1.begin(), s1.end(), //first source string
s2.begin(), //second source string
IsNoCaseCharEqual)) //comparison criterion
{
bRet = true;
}
return bRet;
}
/**************************************************************************
* 函數(shù)名稱: IsNoCaseCharEqual()
* 功能描述: 不區(qū)分大小比較字符
* 輸入?yún)?shù): char c1 : 源字符
* char c2 : 目標字符
* 輸出參數(shù): 無
* 返 回 值: true : 字符相同
* false : 字符不同
* 其它說明: 無
* 修改日期 版本號 修改人 修改內(nèi)容
* -----------------------------------------------
* 2006/07/04 V1.0 張 帆 創(chuàng)建
**************************************************************************/
bool CLSUtil::IsNoCaseCharEqual(char c1, char c2)
{
return (toupper(c1) == toupper(c2));
}
//
//duplicate
//
/**************************************************************************
* 函數(shù)名稱: IsExist()
* 功能描述: 查找字符串向量中是否有指定的字符串
* 輸入?yún)?shù): const vector<string>& vstrSrc : 待查找的字符串向量
* const string& strChk : 指定的字符串
* char c2 : 目標字符
* 輸出參數(shù): 無
* 返 回 值: true : 存在
* false : 不存在
* 其它說明: 無
* 修改日期 版本號 修改人 修改內(nèi)容
* -----------------------------------------------
* 2006/07/04 V1.0 張 帆 創(chuàng)建
**************************************************************************/
bool CLSUtil::IsExist(const vector<string>& vstrSrc, const string& strChk)
{
bool bRet = false;
//檢查重復
vector<string>::const_iterator iter = vstrSrc.begin();
for (; iter != vstrSrc.end(); iter++)
{
if (IsNoCaseStrEqual(strChk, *iter))
{
bRet = true;
break;
}
}
return bRet;
}
//
//Log Level
//
/**************************************************************************
* 函數(shù)名稱: GetLevelString()
* 功能描述: 獲取日志級別的文字表示
* 輸入?yún)?shù): const TLogLevel& Level : 日志級別項
* 輸出參數(shù): 無
* 返 回 值: const string : 日志級別的名稱
* 其它說明: 無
* 修改日期 版本號 修改人 修改內(nèi)容
* -----------------------------------------------
* 2006/07/04 V1.0 張 帆 創(chuàng)建
**************************************************************************/
const string CLSUtil::GetLevelString(const TLogLevel& Level)
{
InitLogLevel();
map<string, TLogLevel>::const_iterator iter = m_LogLevelMap.begin();
for (; iter != m_LogLevelMap.end(); iter++)
{
if (Level == iter->second)
{
return iter->first;
}
}
return "";
}
/**************************************************************************
* 函數(shù)名稱: IsAllowedLogLevel()
* 功能描述: 檢查日志級別是否非法
* 輸入?yún)?shù): const string& strSrc : 待判斷的日志級別名稱
* 輸出參數(shù): TLogLevel& Level : 日志級別項
* 返 回 值: true : 日志級別合法
* false : 日志級別非法
* 其它說明: 無
* 修改日期 版本號 修改人 修改內(nèi)容
* -----------------------------------------------
* 2006/07/04 V1.0 張 帆 創(chuàng)建
**************************************************************************/
bool CLSUtil::IsAllowedLogLevel(const string& strSrc, NOP::TLogLevel& Level)
{
InitLogLevel();
bool bRet = false;
map<string, TLogLevel>::const_iterator iter = m_LogLevelMap.begin();
for (; iter != m_LogLevelMap.end(); iter++)
{
if (strSrc == iter->first)
{
Level = iter->second;
bRet = true;
break;
}
}
return bRet;
}
/**************************************************************************
* 函數(shù)名稱: InitLogLevel()
* 功能描述: 初始化,日志級別對應表
* 輸入?yún)?shù): 無
* 輸出參數(shù): 無
* 返 回 值: 無
* 其它說明: 無
* 修改日期 版本號 修改人 修改內(nèi)容
* -----------------------------------------------
* 2006/07/04 V1.0 張 帆 創(chuàng)建
**************************************************************************/
void CLSUtil::InitLogLevel(void)
{
//
//初始化,日志級別對應表
//
#define LVLINSERT(LVL) \
m_LogLevelMap.insert(map<string, TLogLevel>::value_type((#LVL), (LOG##LVL)))
static bool sbInit = false;
if (!sbInit)
{
LVLINSERT(ALL);
LVLINSERT(DEBUG);
LVLINSERT(INFO);
LVLINSERT(WARN);
LVLINSERT(ERROR);
LVLINSERT(FATAL);
sbInit = true;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -