Chinaunix

标题: 大家一起来完善UNIX大型程序常用到的一些代码 [打印本页]

作者: liaoweijun    时间: 2007-10-10 14:42
标题: 大家一起来完善UNIX大型程序常用到的一些代码
小弟出来工作一年,一直做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;
   
}
作者: cugb_cat    时间: 2007-10-10 14:45
代码乱七八糟了,加个[code][/code]标签吧
作者: bluster    时间: 2007-10-10 15:07
除了读配置文件那个,其他的都有现成的了,可以看看unp(or apue?) 附带的代码。
作者: flw    时间: 2007-10-10 15:28
看来用 class{ FILE *fp } 的人真的很多啊。
作者: net_robber    时间: 2007-10-10 17:18
原帖由 flw 于 2007-10-10 15:28 发表
看来用 class{ FILE *fp } 的人真的很多啊。

这样是不妥
但是应该不会引起什么严重后或吧??
作者: xiaomiao    时间: 2007-10-10 20:27
提示: 作者被禁止或删除 内容自动屏蔽
作者: angelanpan    时间: 2007-10-10 20:35
这种组件,几乎每个IT软件公司都有一套吧
作者: evaspring    时间: 2007-10-11 10:53
为什么不用C?
作者: ChinaOK    时间: 2007-10-11 11:44
俺用的是 C。
作者: 醉卧水云间    时间: 2007-10-11 11:46
这样的程序已经很完善了,楼主大可不必操心。
作者: liaoweijun    时间: 2007-10-11 13:00
原帖由 醉卧水云间 于 2007-10-11 11:46 发表
这样的程序已经很完善了,楼主大可不必操心。

这里是做个引子,看能不能搞些,不是很完善的,一起完善下。
作者: yecheng_110    时间: 2007-10-11 13:13
标题: 回复 #11 liaoweijun 的帖子
LZ能不能写个C版本的
作者: alazer    时间: 2007-10-11 16:44
把代码加个CODE标签把,要不实在不好看
作者: liaoweijun    时间: 2007-10-12 09:03
原帖由 yecheng_110 于 2007-10-11 13:13 发表
LZ能不能写个C版本的

C版本的把类换成结构体,稍做修改就搞定了呀
作者: liaoweijun    时间: 2007-10-12 09:04
原帖由 alazer 于 2007-10-11 16:44 发表
把代码加个CODE标签把,要不实在不好看

你指的是代码注释吗?
作者: liaoweijun    时间: 2007-10-12 09:13
标题: 一个检查进程的shell程序
#!/bin/sh

result=`ps -u $LOGNAME | awk '{if($NF == "casfep")print \
$1 }' | wc -l`
if[ $result -gt 1 ]; then
    exit 1
fi
exit 0



casfep是进程的名字,也就是编译出来可运行程序名字。
$LOGNAME是环境的名字。
然后在C代码中调用此SHELL程序来检测进程是否在运行。
各位看看有需要改善的没?
作者: lanneret_sky    时间: 2007-10-12 14:16
lz的热脸贴到一些人的冷屁股上了
作者: liaoweijun    时间: 2007-10-12 22:46
原帖由 lanneret_sky 于 2007-10-12 14:16 发表
lz的热脸贴到一些人的冷屁股上了

呵呵。......
没办法了,走自己的路,让别说去吧.
作者: alaulong    时间: 2007-10-13 18:34
走自己的路,说别人去吧.




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2