zoukankan      html  css  js  c++  java
  • libconfig第二篇----两个小例子

    本文只看粗体即可,太多catch语句。两个例子均来自libconfig包的example文件夹下面,.

    例子一:

    #include <iostream>
    #include <iomanip>
    #include <cstdlib>
    #include <libconfig.h++>
    using namespace std;
    using namespace libconfig;
    // This example reads the configuration file 'example.cfg' and display some of its contents.

    int main(int argc, char **argv)
    {
      Config cfg;

      try
      {
        cfg.readFile("example.cfg"); //读配置文件
      }
      catch(const FileIOException &fioex)
      {
        std::cerr << "I/O error while reading file." << std::endl;
        return(EXIT_FAILURE);
      }
      catch(const ParseException &pex)
      {
        std::cerr << "Parse error at " << pex.getFile() << ":" << pex.getLine()
                  << " - " << pex.getError() << std::endl;
        return(EXIT_FAILURE);
      }


      // Get the store name.
      try
      {
        string name = cfg.lookup("name"); // 查询某个路径“name”,得到setting,存储到name
        cout << "Store name: " << name << endl << endl;
      }
      catch(const SettingNotFoundException &nfex)
      {
        cerr << "No 'name' setting in configuration file." << endl;
      }

      const Setting& root = cfg.getRoot();//得到根setting
     // Output a list of all books in the inventory.
      try
      {
        const Setting &books = root["inventory"]["books"];
        int count = books.getLength();   //得到某个setting的长度,从而循环读取

        
       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)  //查询名字为title的setting存储到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.
      }
      return(EXIT_SUCCESS);
    }


    // eof

    例子二

    头文件省略

    // This example reads the configuration file 'example.cfg', adds a new
    // movie record to the movies list, and writes the updated configuration to
    // 'updated.cfg'.

    int main(int argc, char **argv)
    {
      static const char *output_file = "updated.cfg";

      Config cfg;
      // Read the file. If there is an error, report it and exit.
      try
      {
        cfg.readFile("example.cfg"); //读配置文件
      }
      catch(const FileIOException &fioex)
      {
        std::cerr << "I/O error while reading file." << std::endl;
        return(EXIT_FAILURE);
      }
      catch(const ParseException &pex)
      {
        std::cerr << "Parse error at " << pex.getFile() << ":" << pex.getLine()
                  << " - " << pex.getError() << std::endl;
        return(EXIT_FAILURE);
      }


    // Get the store name.
      try
      {
        string name = cfg.lookup("name");    //通过"name"这个路径查找 到某个setting(名字为 name) 存储到string name 中
        cout << "Store name: " << name << endl << endl;
      }
      catch(const SettingNotFoundException &nfex)
      {
        cerr << "No 'name' setting in configuration file." << endl;
      }


      // Find the 'movies' setting. Add intermediate settings if they don't yet
      // exist.
      Setting &root = cfg.getRoot(); //返回root setting
      if(! root.exists("inventory"))
        root.add("inventory", Setting::TypeGroup);
      Setting &inventory = root["inventory"];
      if(! inventory.exists("movies"))
        inventory.add("movies", Setting::TypeList);
      Setting &movies = inventory["movies"];


      // Create the new movie entry.
      Setting &movie = movies.add(Setting::TypeGroup); //增加一个Setting::TypeGroup类型的子setting
      movie.add("title", Setting::TypeString) = "Buckaroo Banzai"; //增加一个子setting
      movie.add("media", Setting::TypeString) = "DVD";
      movie.add("price", Setting::TypeFloat) = 12.99;
      movie.add("qty", Setting::TypeInt) = 20;

      // Write out the updated configuration.
      try
      {
        cfg.writeFile(output_file); //把配置写到一个文件中
        cerr << "Updated configuration successfully written to: " << output_file
             << endl;
      }
      catch(const FileIOException &fioex)
      {
        cerr << "I/O error while writing file: " << output_file << endl;
        return(EXIT_FAILURE);
      }
      return(EXIT_SUCCESS);
    }

    // eof

    配置文件:

    example.cfg:

    // 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; };
    };

  • 相关阅读:
    百度地图地址解析/逆地址解析
    Oracle表空间创建要点
    dubbo——providers
    dubbo——常用标签属性
    dubbo——spring初始化
    dubbo——RPC
    mybatis——datasource
    redis——再补充
    mybatis——缓存
    mybatis——Executor
  • 原文地址:https://www.cnblogs.com/catkins/p/5270611.html
Copyright © 2011-2022 走看看