📄 lsutil.cpp
字号:
/*********************************************************************
* 版权所有 (C)2006, 深圳市中兴通讯股份有限公司。
*
* 文件名称: LSUtil.cpp
* 文件标识:
* 内容摘要: 系统日志服务工具函数实现
* 其它说明:
* 当前版本: V1.00
* 作 者: 张 帆
* 完成日期: 2006-07-20
*
* 修改记录1:// 修改历史记录,包括修改日期、修改者及修改内容
* 修改日期:
* 版 本 号:
* 修 改 人:
* 修改内容:
* 修改记录2:…
**********************************************************************/
#include "LSUtil.h"
#include <windows.h>
#include <io.h>
#include <fstream>
#include "NOPConst.h"
using namespace std;
using namespace NOP;
/**************************************************************************
* 类CLSUtil的静态数据成员 *
**************************************************************************/
map<string, TLogLevel> CLSUtil::m_LogLevelMap; // 日志服务工具类的单实例对象
/**************************************************************************
* 类CLSUtil实现 *
**************************************************************************/
/**************************************************************************
* 函数名称: GetDefaultPath()
* 功能描述: 取得执行文件路径
* 输入参数: 无
* 输出参数: 无
* 返 回 值: 得到本模块自己的可执行文件路径
* 其它说明: 无
* 修改日期 版本号 修改人 修改内容
* -----------------------------------------------
* 2006/07/03 V1.0 张 帆 创建
**************************************************************************/
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;
}
/**************************************************************************
* 函数名称: FullPath()
* 功能描述: 根据相对路径获取全路径
* 输入参数: const string& strPartialPath : 相对路径
* 输出参数: 无
* 返 回 值: 该文件的全路径
* 其它说明: 无
* 修改日期 版本号 修改人 修改内容
* -----------------------------------------------
* 2006/07/03 V1.0 张 帆 创建
**************************************************************************/
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 "";
}
}
/**************************************************************************
* 函数名称: make_sure_path_exists()
* 功能描述: 创建路径
* 输入参数: const char *iPath : 待确定的目标路径
* bool FilenameIncluded : 该路径中是否包含文件名称
* 输出参数: 无
* 返 回 值: true : 指定的目标路径可以创建
* false : 指定的目标路径不可以创建
* 其它说明: 无
* 修改日期 版本号 修改人 修改内容
* -----------------------------------------------
* 2006/07/03 V1.0 张 帆 创建
**************************************************************************/
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
//
/**************************************************************************
* 函数名称: IsAllowedFileName()
* 功能描述: 检查文件名非法
* 输入参数: const string& strSrc : 文件名称
* 输出参数: 无
* 返 回 值: true : 文件名合法
* false : 文件名非法
* 其它说明: 无
* 修改日期 版本号 修改人 修改内容
* -----------------------------------------------
* 2006/07/04 V1.0 张 帆 创建
**************************************************************************/
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;
}
/**************************************************************************
* 函数名称: GetFileLineNumber()
* 功能描述: 获得文件的行数
* 输入参数: const string& strFile : 文件名称
* 输出参数: 无
* 返 回 值: iNumber : 文件的行数
* 其它说明: 无
* 修改日期 版本号 修改人 修改内容
* -----------------------------------------------
* 2006/07/04 V1.0 张 帆 创建
**************************************************************************/
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;
}
/**************************************************************************
* 函数名称: MoveFile()
* 功能描述: 文件更名
* 输入参数: const string& strFileSrc : 源文件名
* const string& strFileDest : 目标文件名
* 输出参数: 无
* 返 回 值: true : 文件更名成功
* false : 文件更名失败
* 其它说明: 无
* 修改日期 版本号 修改人 修改内容
* -----------------------------------------------
* 2006/07/04 V1.0 张 帆 创建
**************************************************************************/
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;
}
/**************************************************************************
* 函数名称: IsFileExists()
* 功能描述: 检查文件是否存在
* 输入参数: const string& strFileSrc : 文件名称
* 输出参数: 无
* 返 回 值: true : 文件存在
* false : 文件不存在
* 其它说明: 无
* 修改日期 版本号 修改人 修改内容
* -----------------------------------------------
* 2006/07/04 V1.0 张 帆 创建
**************************************************************************/
bool CLSUtil::IsFileExists(const string& strFile)
{
return ( (_access( strFile.c_str(), 0 )) != -1 );
}
/**************************************************************************
* 函数名称: MakeSureFileExists()
* 功能描述: 确认文件存在, 如果文件不存在则创建
* 输入参数: const string& strFileSrc : 文件名称
* 输出参数: 无
* 返 回 值: true : 成功
* false : 失败
* 其它说明: 无
* 修改日期 版本号 修改人 修改内容
* -----------------------------------------------
* 2006/07/04 V1.0 张 帆 创建
**************************************************************************/
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
//
/**************************************************************************
* 函数名称: IsNoCaseStrEqual()
* 功能描述: 不区分大小比较字符串
* 输入参数: const string& s1 : 源字符串
* const string& s2 : 目标字符串
* 输出参数: 无
* 返 回 值: true : 字符串相同
* false : 字符串不同
* 其它说明: 无
* 修改日期 版本号 修改人 修改内容
* -----------------------------------------------
* 2006/07/04 V1.0 张 帆 创建
**************************************************************************/
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;
}
/**************************************************************************
* 函数名称: IsNoCaseCharEqual()
* 功能描述: 不区分大小比较字符
* 输入参数: char c1 : 源字符
* char c2 : 目标字符
* 输出参数: 无
* 返 回 值: true : 字符相同
* false : 字符不同
* 其它说明: 无
* 修改日期 版本号 修改人 修改内容
* -----------------------------------------------
* 2006/07/04 V1.0 张 帆 创建
**************************************************************************/
bool CLSUtil::IsNoCaseCharEqual(char c1, char c2)
{
return (toupper(c1) == toupper(c2));
}
//
//duplicate
//
/**************************************************************************
* 函数名称: IsExist()
* 功能描述: 查找字符串向量中是否有指定的字符串
* 输入参数: const vector<string>& vstrSrc : 待查找的字符串向量
* const string& strChk : 指定的字符串
* char c2 : 目标字符
* 输出参数: 无
* 返 回 值: true : 存在
* false : 不存在
* 其它说明: 无
* 修改日期 版本号 修改人 修改内容
* -----------------------------------------------
* 2006/07/04 V1.0 张 帆 创建
**************************************************************************/
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
//
/**************************************************************************
* 函数名称: GetLevelString()
* 功能描述: 获取日志级别的文字表示
* 输入参数: const TLogLevel& Level : 日志级别项
* 输出参数: 无
* 返 回 值: const string : 日志级别的名称
* 其它说明: 无
* 修改日期 版本号 修改人 修改内容
* -----------------------------------------------
* 2006/07/04 V1.0 张 帆 创建
**************************************************************************/
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 "";
}
/**************************************************************************
* 函数名称: IsAllowedLogLevel()
* 功能描述: 检查日志级别是否非法
* 输入参数: const string& strSrc : 待判断的日志级别名称
* 输出参数: TLogLevel& Level : 日志级别项
* 返 回 值: true : 日志级别合法
* false : 日志级别非法
* 其它说明: 无
* 修改日期 版本号 修改人 修改内容
* -----------------------------------------------
* 2006/07/04 V1.0 张 帆 创建
**************************************************************************/
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;
}
/**************************************************************************
* 函数名称: InitLogLevel()
* 功能描述: 初始化,日志级别对应表
* 输入参数: 无
* 输出参数: 无
* 返 回 值: 无
* 其它说明: 无
* 修改日期 版本号 修改人 修改内容
* -----------------------------------------------
* 2006/07/04 V1.0 张 帆 创建
**************************************************************************/
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 + -