一个读取参数(配置)文件的C语言开发库

我的一个 Open Source 的项目,适合嵌入式程序的应用。

http://sourceforge.net/projects/readparameter

许多应用程序需要读取一些配置文件, Windows 下的常用解决办法 就是读取注册表,这种办法不是很好:

  1. 不能跨平台
  2. 不是所谓的绿色软件,需要特别的安装程序。
  3. 需要专门的界面编辑,regedit 很容易出错。

Linux 下的软件大多数读取 $HOME 目录下的点文件,例如 .emacs , .vimrc, .screenrc 等等。 ReadParameter 就提供了一种读取配置 参数的方法。

例如:

   /*************************************
   this is multiline  comments
   ----------------------------
    this is a parameter file contains
    the complex expression.
   ----------------------------
   *************************************/
   /*this is comment*/
   //single line comments
Charles = {
     first_name = "Charles";  //simple field
     last_name = "Lee";
     age = 20;
     company = {                   // structure field
          name = "Beijing University of Post and Telemunication";
          address = "Beijing China";
     }
     brothers = [    // array field
          {
               name = "Brown";   // elements of array are structures also.
               age = 23;
          },
          {
               name = "Tom"; // elements are not necessary to have same type.
               age = 25;
          }]
}
Charles = {
    sex = "male";
}
Charles = {
    company = {
        tel = true;
        test = false;
        contact = {
            tel = "123456";
            fax = "654321";
        }
    }
    height=163;
}

这样一种结构化的参数格式,可以通过下面的方式读取:

     int i,count;
     pt_array_t p;
     parameter_t root;
     parameter_t * ps;
     printf("charles first name is %s\n",readconfig_get_text("Charles.first_name"));
     printf("charles last name is %s\n",readconfig_get_text("Charles.last_name"));
     printf("charles age is %ld\n",readconfig_get_int("Charles.age"));
     printf("Charles company name is %s.\n",readconfig_get_text("Charles.company.name"));
     printf("Charles company address is %s.\n",readconfig_get_text("Charles.company.address"));
     p = readconfig_get_array("Charles.brothers");
     count = readconfig_get_array_size(p);
     ps = readconfig_get_array_base(p);
     printf("Charles brothers:\n");
     for(i=0;i<count;i++){
          printf("\tbrother[%d]:\n",i);
          root = readconfig_set_root(ps[i]);
          printf("\t\tname %s, age %ld.\n",readconfig_get_text("name"),readconfig_get_int("age"));
          readconfig_set_root(root);
     }
     return 0;

同时也支持 C++ 的方式 :

{
   RPParam Charles = RPParam("Charles");
   RPParam::With(Charles);
   cout <<"First Name: " << (const char *)RPParam("first_name") << endl
        <<"Last Name : " << (const char *) RPParam("last_name") << endl
        << "age:" << (long) RPParam("age") << endl
        <<endl;
   RPParam Brothers = RPParam("brothers");
   for(size_t i=0 ; i < Brothers.size() ; i++){
      cout << "Brother " << i << ":" << (const char*)(Brothers[i]("name")) <<endl
           << "age " << (long)Brothers[i]("age") << endl;
   }
   cout << endl;
   RPParam::EndWith();
   return 0;
}

可以看到,这种简单的结构支持 int, double, string, struct, array 几种最常用的数据类型,这样就可以作为一种 XML 的替代品, 在某些场合作为一种通用的数据交换格式.

这个库设计用于通用平台,核心完全用 C 语言完成,速度快,可移植性 好,可以在任何支持标准 C 的平台上运行. 对于嵌入式系统,用于读 取参数,网络交换协议数据,等等,都是一个极好解决方案.