- 论坛徽章:
- 0
|
小弟出来工作一年,一直做UNIX后台.看过几个模块的UNIX程序,它们有很多地方有相同的地方,我想大家能不能一起总结一下,比方程序设计常用到:检测UNIX进程是否正在进行,打印日志文件,建立SOCKET连接,读配置文件等等.
我目前写了个读配置文件的类,大家看看有那些还需要修改的.发表一下意见.
config.hpp
#ifndef __CONFIG_HPP
#define __CONFIG_HPP
#define MAX_LINELEN 200 //定义读配置文件的最大长度
class TConfigBase
{
public:
TConfigBase();
~TConfigBase();
public:
void CloseFile();
void DeleteNote(char *pString);
void DeleteSpace(char *pString);
void GetLineFromFile(char *pString);
int SetFileName(char *pFile);
int OpenFile();
int GetMsgFromConfig(char *pSection, char *pKey, char *pResult);
int GetKeyResult(char *pString, char *pKey, char *pResult);
int FindSection(char *pString, char *pSection);
int FindKey(char *pString, char *pKey);
protected:
//预打打开的文件名
char *m_pFileName;
//文件指针
FILE *fp;
};
#endif
config.cpp
/***********************************************************************
File name:config.cpp
Author:liaoweijun Version:v1.01 Date: 2007/09/21
Description:读取配置文件
Others: // 其它内容的说明
History: // 修改历史记录列表,每条修改记录应包括修改日期、修改
// 者及修改内容简述
1. Date:
Author:
Modification:
2. ...
************************************************************************/
#include <stdio.h>
#include <string.h>
#include "config.hpp"
/*************************************************
Function: TConfigBase()
Description: 构造函数
*************************************************/
TConfigBase::TConfigBase()
{
m_pFileName = NULL;
fp = NULL;
}
/*************************************************
Function: ~TConfigBase()
Description: 析构函数
*************************************************/
TConfigBase::~TConfigBase()
{
if (m_pFileName != NULL )
{
delete [] m_pFileName;
}
closeFile();
}
/*************************************************
Function: closeFile()
Description: 关闭打开的文件描述符
*************************************************/
void TConfigBase::CloseFile()
{
if (NULL != fp)
{
fclose(fp);
}
}
/*************************************************
Function: setFileName()
Description: 设置配置文件名字
Input: 配置文件名字
Return: -1:失败, 0:正确
*************************************************/
int TConfigBase::SetFileName(char *pFile)
{
CloseFile();
if (m_pFileName != NULL )
{
delete [] m_pFileName;
}
int iNameLen = strlen(pFile); //配置文件名的长度
m_pFileName = new char[iNameLen + 1];
strcpy(m_pFileName, pFile);
return OpenFile();
}
/*************************************************
Function: openFile()
Description: 打开配置文件
Return: -1:打开文件失败, 0:打开文件成功
*************************************************/
int TConfigBase::OpenFile()
{
fp == fopen(m_pFileName, "r"
if (NULL == fp)
{
printf("OpenFile()::ERROR open file:%s\n", m_pFileName);
return -1;
}
return 0;
}
/************************************************************
Function: FindSection()
Description: 在配置文件中寻找相关域的位置
Input: pString:寻找字符串,pSection:被寻找的域名
Return: 1:没找到相关域, 0:找到相关域
************************************************************/
int TConfigBase::FindSection(char *pString, char *pSection)
{
int iTempLen = 0; //字符串长度
char *pTempStr = NULL;
iTempLen = strlen(pString);
if ('[' == pString[0] || ']' == pString[iTempLen - 1])
{
pTempStr = new char[iTempLen + 1];
memset(pTempStr, 0, iTempLen + 1);
memcpy(pTempStr, pString);
DeleteNote(pTempsStr); //删除字符串里的注释
DeleteSpace(pTempsStr); //删除字符串里的空格
//比较是否是所要找的域
if (0 == memcmp(pSection, pTempsStr + 1, strlen(pSection)))
{
delete [] pTempStr;
return 0;
}
else
{
delete [] pTempStr;
return 1;
}
}
else
{
return 1;
}
}
/************************************************************
Function: FindKey()
Description: 在域中查找相关配置项
Input: pString:寻找字符串,pKey:被寻找的配置项
Return: 1:没找到相关域, 0:找到相关域
************************************************************/
int TConfigBase::FindKey(char *pString, char *pKey)
{
int iTempLen = 0;
char *pTempStr == NULL;
iTempLen = strlen(pString);
pTempStr = new char[iTempLen + 1];
DeleteNote(pTempsStr); //删除字符串里的注释
DeleteSpace(pTempsStr); //删除字符串里的空格
//比较是否是所要找的域
if (0 == memcmp(pKey, pTempsStr + 1, strlen(pKey)))
{
delete [] pTempStr;
return 0;
}
else
{
delete [] pTempStr;
return 1;
}
}
/************************************************************
Function: DeleteNote()
Description: 删除字符串里的注释
Input: pString:被删除注释的字符串
************************************************************/
void TConfigBase:eleteNote(char *pString)
{
char *pTemp = NULL;
int iTemp = 0;
pTemp = strstr(pString, "//" //查找带'//'的注释
if (NULL == pTemp)
{
pTemp = strstr(pString, "/*" //查找带'/*'的注释
if (NULL == pTemp)
{
pTemp = strstr(pString, "--" //查找带'--'的注释
if (NULL != pTemp)
{
strset(pTemp, 0);
}
else
{
return;
}
}
else
{
strset(pTemp, 0);
}
}
else
{
strset(pTemp, 0);
}
}
/************************************************************
Function: DeleteSpace()
Description: 删除字符串里的空格
Input: pString:被删除空格的字符串
************************************************************/
void TConfigBase:eleteSpace(char *pString)
{
char szResult[MAX_LINELEN + 1];
int iTempLen = strlen(pString);
int iValueCount = 0;
//去掉空格
for(int i =0; i< iTempLen; i++)
{
if ((unsigned char)pString <= 0x20 )
{
continue;
}
szResult[iValueCount++] = pString;
}
szResult[iValueCount] = '\0';
strcpy(pString, szResult);
}
/**************************************************************************
Function: GetMsgFromConfig()
Description: 从配置文件中读取相关信息
Input: pSection:域名, pKey:域中的关键字, pResult:读取出的信息
Return: 1--读取成功, 0--读取失败
**************************************************************************/
int TConfigBase::GetMsgFromConfig(char *pSection, char *pKey, char *pResult)
{
char szTempstr[MAX_LINELEN + 1];
int iFindSection = 1;
int iFindKey = 1;
if (NULL == fp)
{
printf("GetMsgFromConfig()::The fp is NULL, please check the fp"
return 0;
}
rewind(fp);
//查找相关的域名
while(!feof(fp))
{
GetLineFromFile(szTempstr);
iFindSection = FindSection(szTempstr, pSection);
if (!iFindSection)
{
break;
}
}
if(iFindSection)
{
printf("GetMsgFromConfig()::没有找到相关的域名..."
return 0;
}
//在域中查找相关的配置项
while(!feof(fp))
{
memset(szTempstr, 0, sizeof(szTempstr));
GetLineFromFile(szTempstr);
DeleteSpace(szTempstr);
//如果找到下个域,跳出
if ('[' == szTempStr[0])
{
break;
}
iFindKey = FindKey(szTempstr, pKey);
if (!iFindKey)
{
break;
}
}
if (iFindKey)
{
printf("GetMsgFromConfig()::在域中没有找到相关配置项..."
return 0;
}
if (GetKeyResult(szTempstr, pResult))
{
return 1;
}
else
{
return 0;
}
}
/**************************************************************************
Function: GetKeyResult()
Description: 从配置文件中读取相关信息
Input: pString:配置项, pResult:读取出的信息
Return: 1--读取成功, 0--读取失败
**************************************************************************/
int TConfigBase::GetKeyResult(char *pString, char *pResult)
{
char *pTempStr = NULL;
int iTempLen = 0;
DeleteSpace(pString);
DeleteNote(pString);
iTempLen = strlen(pString);
//判断配置项是否已经配置了值
pTempStr = strchr(pString, '=');
if (NULL == pTempStr)
{
printf("GetKeyResult()::配置项配置错误..."
return 0;
}
else if (0 == strcmp("=", pString[pTempStr - 1]))
{
printf("GetKeyResult()::配置项配置为空值..."
return 0;
}
else
{
strcpy(pResult, pTempStr);
return 1;
}
}
/**************************************************************************
Function: GetLineFromFile()
Description: 从配置文件中读取一行相关信息
Input: pString:读取出的信息
**************************************************************************/
void TConfigBase::GetLineFromFile(char *pString)
{
memset(pString, 0, MAX_LINELEN);
fgets(pString, MAX_LINELEN, fp);
return;
} |
|