• libconfig C++ 学习笔记


    1. C++API 头文件 #include <libconfig.h++> ,命名空间:using namespace libconfig;

    2.多线程使用问题:

      (1)libconfig是完全可重入的,库中函数不使用全局变量和不保持成功调用间的状态。所以两个独立配置文件在两个不同线程间并发操作可以是安全的;

      (2)libconfig是线程不安全,libconfig不考虑系统线程模型。所以一个配置文件的实例被多个线程获取,必须使用同步机制(例如读写锁或互斥锁)来保护。

      (3)libconfig不是异步安全,不能在信号句柄中调用

      (4)libconfig不能保证取消安全。因为它不知道主机系统的线程模型,库不包含任何线程取消点。在大多数情况下这将不是多线程程序的问题。但是,请注意一些库中的例程(即那些从/到文件读取/写入配置的程序)流)使用可能会阻止的C库例程执行I/O;是否这些C库例程是取消安全的,取决于主机系统。

    3.C++API

      C++API使用Config类和Setting类,因为不提供公有的copy constructor or assignment operator,所以在函数参数中传递时使用引用或指针方式
    4.使用方法(使用官方example):

    配置文件:

    // An example configuration file that stores information about a store.
    
    // Basic store information:
    name = "Books, Movies & More";
    
    // Store inventory:
    inventory =
    {
      books = ( { title  = "Treasure Island";
                  author = "Robert Louis Stevenson";
                  price  = 29.99;
                  qty    = 5; },
                { title  = "Snow Crash";
                  author = "Neal Stephenson";
                  price  = 9.99;
                  qty    = 8; }
              );
    
      movies = ( { title = "Brazil";
                   media = "DVD";
                   price = 19.99;
                   qty = 11; },
                 { title = "The City of Lost Children";
                   media = "DVD";
                   price = 18.99;
                   qty = 5; },
                 { title = "Memento";
                   media = "Blu-Ray";
                   price = 24.99;
                   qty = 20;
                 },
                 { title = "Howard the Duck"; }
               );
    };
    
    // Store hours:
    hours =
    {
      mon = { open =  9; close = 18; };
      tue = { open =  9; close = 18; };
      wed = { open =  9; close = 18; };
      thu = { open =  9; close = 18; };
      fri = { open =  9; close = 20; };
      sat = { open =  9; close = 20; };
      sun = { open = 11; close = 16; };
    };

    C++读取配置程序:

    #include <cstdlib>
    #include <libconfig.h++>
    #include <iostream>
    #include <iomanip>
    
    using namespace std;
    using namespace libconfig;
    
    int main(){
        Config cfg;     //1.声明 Config对象
    
        cfg.readFile("example.cfg");    //读取配置文件
    
        // Get the store name.
        try
        {
            string name = cfg.lookup("name");
            cout << "Store name: " << name << endl << endl;
        }
        catch(const SettingNotFoundException &nfex)         //配置没找到会有SettingNotFoundException 异常
        {
            cerr << "No 'name' setting in configuration file." << endl;
        }
        // Get the store name.
      try
      {
        string name = cfg.lookup("name");
        cout << "Store name: " << name << endl << endl;
      }
      catch(const SettingNotFoundException &nfex)
      {
        cerr << "No 'name' setting in configuration file." << endl;
      }
    
      const Setting& root = cfg.getRoot();
    
      // Output a list of all books in the inventory.
      try
      {
        const Setting &books = root["inventory"]["books"];
        int count = books.getLength();
    
        cout << setw(30) << left << "TITLE" << "  "
             << setw(30) << left << "AUTHOR" << "   "
             << setw(6) << left << "PRICE" << "  "
             << "QTY"
             << endl;
    
        for(int i = 0; i < count; ++i)
        {
          const Setting &book = books[i];
    
          // Only output the record if all of the expected fields are present.
          string title, author;
          double price;
          int qty;
    
          if(!(book.lookupValue("title", title)
               && book.lookupValue("author", author)
               && book.lookupValue("price", price)
               && book.lookupValue("qty", qty)))
            continue;
    
          cout << setw(30) << left << title << "  "
               << setw(30) << left << author << "  "
               << '$' << setw(6) << right << price << "  "
               << qty
               << endl;
        }
        cout << endl;
      }
      catch(const SettingNotFoundException &nfex)
      {
        // Ignore.
      }
    
      // Output a list of all books in the inventory.
      try
      {
        const Setting &movies = root["inventory"]["movies"];
        int count = movies.getLength();
    
        cout << setw(30) << left << "TITLE" << "  "
             << setw(10) << left << "MEDIA" << "   "
             << setw(6) << left << "PRICE" << "  "
             << "QTY"
             << endl;
    
        for(int i = 0; i < count; ++i)
        {
          const Setting &movie = movies[i];
    
          // Only output the record if all of the expected fields are present.
          string title, media;
          double price;
          int qty;
    
          if(!(movie.lookupValue("title", title)
               && movie.lookupValue("media", media)
               && movie.lookupValue("price", price)
               && movie.lookupValue("qty", qty)))
            continue;
    
          cout << setw(30) << left << title << "  "
               << setw(10) << left << media << "  "
               << '$' << setw(6) << right << price << "  "
               << qty
               << endl;
        }
        cout << endl;
      }
      catch(const SettingNotFoundException &nfex)
      {
        // Ignore.
      }
        return 0;
    }

    5.常用Config类方法:

    (1)Method on Config: Setting & getRoot () const

    This method returns the root setting for the configuration, which is a group.

    (2)

    Method on Config: Setting & lookup (const std::string &path) constMethod on Config: Setting & lookup (const char * path) const

    These methods locate the setting specified by the path path. If the requested setting is not found, a SettingNotFoundException is thrown.

    (3)

    Method on Config: bool lookupValue (const char *path, bool &value) constMethod on Config: bool lookupValue (const std::string &path, bool &value) constMethod on Config: bool lookupValue (const char *path, int &value) constMethod on Config: bool lookupValue (const std::string &path, int &value) constMethod on Config: bool lookupValue (const char *path, unsigned int &value) constMethod on Config: bool lookupValue (const std::string &path, unsigned int &value) constMethod on Config: bool lookupValue (const char *path, long long &value) constMethod on Config: bool lookupValue (const std::string &path, long long &value) constMethod on Config: bool lookupValue (const char *path, float &value) constMethod on Config: bool lookupValue (const std::string &path, float &value) constMethod on Config: bool lookupValue (const char *path, double &value) constMethod on Config: bool lookupValue (const std::string &path, double &value) constMethod on Config: bool lookupValue (const char *path, const char *&value) constMethod on Config: bool lookupValue (const std::string &path, const char *&value) constMethod on Config: bool lookupValue (const char *path, std::string &value) constMethod on Config: bool lookupValue (const std::string &path, std::string &value) const

     

  • 相关阅读:
    javascript-事件
    javascript-数组简单的认识
    javascript-dom操作
    JavaScript入门
    python语言程序设计?
    4,14
    python第二周。2019.4.13
    python第二周
    python语言程序设计9
    python语言程序设计8
  • 原文地址:https://www.cnblogs.com/dj0325/p/9197215.html
Copyright © 2020-2023  润新知