开发工具-xml配置文件解析类

121 阅读2分钟

参数文件操作类。

// CIniFile类操作的文件是xml格式的参数文件,例如:
/*
 * 文件名:hssms.xml
<?xml version="1.0" encoding="gbk" ?>
<root>
    <!-- 程序运行的日志文件名。 -->
    <logpath>/log/hssms</logpath>

    <!-- 数据库连接参数。 -->
    <connstr>hssms/smspwd@hssmszx</connstr>

    <!-- 数据文件存放的根目录。 -->
    <datapath>/data/hssms</datapath>

    <!-- 中心服务器的ip地址。 -->
    <serverip>192.168.1.1</serverip>

    <!-- 中心服务器的通讯端口。 -->
    <port>5058</port>

    <!-- 是否采用长连接,true-是;false-否。 -->
    <online>true</online>
</root>
*/

类声明

class CIniFile
{
public:
  string m_xmlbuffer; // 存放参数文件全部的内容,由LoadFile方法载入。

  CIniFile();

  // 把参数文件的内容载入到m_xmlbuffer成员变量中。
  bool LoadFile(const char *filename);
 
  // 获取参数的值。
  // fieldname:字段的标签名。
  // value:传入变量的地址,用于存放字段的值,支持bool、int、insigned int、long、unsigned long、double和char[]。
  // 注意,当value参数的数据类型为char []时,必须保证value的内存足够,否则可能发生内存溢出的问题,
  // 也可以用ilen参数限定获取字段内容的长度,ilen的缺省值为0,表示不限定获取字段内容的长度。
  // 返回值:true-成功;false-失败。
  bool GetValue(const char *fieldname,bool *value);
  bool GetValue(const char *fieldname,int  *value);
  bool GetValue(const char *fieldname,unsigned int *value);
  bool GetValue(const char *fieldname,long *value);
  bool GetValue(const char *fieldname,unsigned long *value);
  bool GetValue(const char *fieldname,double *value);
  bool GetValue(const char *fieldname,char *value,const int ilen=0);
};

实现类

CIniFile::CIniFile()
{
  
}

bool CIniFile::LoadFile(const char *filename)
{
  m_xmlbuffer.clear();
  CFile File;
  if ( File.Open(filename,"r") == false) return false;
  char strLine[501];
  while (true)
  {
    memset(strLine,0,sizeof(strLine));
    if (File.FFGETS(strLine,500) == false) break;
    m_xmlbuffer=m_xmlbuffer+strLine;
  }

  if (m_xmlbuffer.length() < 10) return false;
  return true;
}

bool CIniFile::GetValue(const char *fieldname,bool   *value)
{
  return GetXMLBuffer(m_xmlbuffer.c_str(),fieldname,value);
}

bool CIniFile::GetValue(const char *fieldname,char *value,int ilen)
{
  return GetXMLBuffer(m_xmlbuffer.c_str(),fieldname,value,ilen);
}

bool CIniFile::GetValue(const char *fieldname,int *value)
{
  return GetXMLBuffer(m_xmlbuffer.c_str(),fieldname,value);
}

bool CIniFile::GetValue(const char *fieldname,unsigned int *value)
{
  return GetXMLBuffer(m_xmlbuffer.c_str(),fieldname,value);
}

bool CIniFile::GetValue(const char *fieldname,long *value)
{
  return GetXMLBuffer(m_xmlbuffer.c_str(),fieldname,value);
}

bool CIniFile::GetValue(const char *fieldname,unsigned long *value)
{
  return GetXMLBuffer(m_xmlbuffer.c_str(),fieldname,value);
}

bool CIniFile::GetValue(const char *fieldname,double *value)
{
  return GetXMLBuffer(m_xmlbuffer.c_str(),fieldname,value);
}

测试

配置文件格式如下:
<?xml version="1.0" encoding="gbk" ?>
<root>
    <!-- 程序运行的日志文件名。 -->
    <logpath>/log/hssms</logpath>

    <!-- 数据库连接参数。 -->
    <connstr>hssms/smspwd@hssmszx</connstr>

    <!-- 数据文件存放的根目录。 -->
    <datapath>/data/hssms</datapath>

    <!-- 中心服务器的ip地址。 -->
    <serverip>192.168.1.1</serverip>

    <!-- 中心服务器的通信端口。 -->
    <port>5058</port>

    <!-- 是否采用长连接,true-是;false-否。 -->
    <online>true</online>
</root>

---测试代码
// 用于存放本程序运行参数的数据结构。
struct st_args
{
  char logpath[301];
  char connstr[101];
  char datapath[301];
  char serverip[51];
  int  port;
  bool online;
}stargs;

int main(int argc,char *argv[])
{
  // 如果执行程序时输入的参数不正确,给出帮助信息。
  if (argc != 2) 
  { 
    printf("\nusing:/project/public/demo/demo45 inifile\n"); 
    printf("samples:/project/public/demo/demo45 /project/public/ini/hssms.xml\n\n"); 
    return -1;
  }

  // 加载参数文件。
  CIniFile IniFile;
  if (IniFile.LoadFile(argv[1])==false)
  {
    printf("IniFile.LoadFile(%s) failed.\n",argv[1]); return -1;
  } 

  // 获取参数,存放在stargs结构中。
  memset(&stargs,0,sizeof(struct st_args));
  IniFile.GetValue("logpath",stargs.logpath,300);
  IniFile.GetValue("connstr",stargs.connstr,100);
  IniFile.GetValue("datapath",stargs.datapath,300);
  IniFile.GetValue("serverip",stargs.serverip,50);
  IniFile.GetValue("port",&stargs.port);
  IniFile.GetValue("online",&stargs.online);
  
  printf("logpath=%s\n",stargs.logpath);
  printf("connstr=%s\n",stargs.connstr);
  printf("datapath=%s\n",stargs.datapath);
  printf("serverip=%s\n",stargs.serverip);
  printf("port=%d\n",stargs.port);
  printf("online=%d\n",stargs.online);

  // 以下可以写更多的主程序的代码。
}