亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

C++讀取配置文件的示例代碼

 更新時(shí)間:2020年08月14日 08:37:50   作者:gongluck  
這篇文章主要介紹了C++讀取配置文件的示例代碼,幫助大家更好的理解和學(xué)習(xí)C++開發(fā),感興趣的朋友可以了解下

代碼地址

https://github.com/gongluck/Code-snippet/tree/master/cpp/config

需求

開發(fā)中,讀取配置文件信息必不可少。Windows平臺(tái)有現(xiàn)成的API可用,也很方便。但是一旦項(xiàng)目遷移到Linux平臺(tái)下,原先在Windows平臺(tái)下的代碼就全部作廢。所以,實(shí)現(xiàn)一套跨平臺(tái)的配置文件讀取功能代碼可以節(jié)省不少的勞動(dòng)力。

實(shí)現(xiàn)

依賴于boost的ini_parser,可以實(shí)現(xiàn)跨平臺(tái)讀取ini格式的配置文件。

// config.h
/*
 * @Author: gongluck 
 * @Date: 2020-03-23 15:11:50 
 * @Last Modified by: gongluck
 * @Last Modified time: 2020-03-23 15:17:58
 */

// Profile read, dependent on boost

#pragma once

#include <iostream>
#include <vector>
#include <boost/property_tree/ptree.hpp>

namespace gconf
{
class config
{
public:
  int open(const char *configfile);
  template <typename T>
  int read(const char *session, const char *key, T &value, const char *configfile = nullptr)
  {
    if (configfile != nullptr && open(configfile) != 0)
    {
      return -1;
    }

    try
    {
      auto lvbtItems = lvptProperties_.get_child(session);
      value = lvbtItems.get<T>(key);
    }
    catch (std::exception &e)
    {
      std::cerr << __FILE__ << " : " << __LINE__ << " : " << e.what() << std::endl;
      return -1;
    }

    return 0;
  }
  int readall(const char *session,
        std::vector<std::pair<std::string, std::string>> &results,
        const char *configfile = nullptr);

private:
  boost::property_tree::ptree lvptProperties_;
};
} // namespace gconf
// config.cpp
/*
 * @Author: gongluck 
 * @Date: 2020-03-23 15:13:13 
 * @Last Modified by: gongluck
 * @Last Modified time: 2020-03-23 15:17:56
 */

#include "config.h"
#include <boost/property_tree/ini_parser.hpp>

namespace gconf
{
int config::open(const char *configfile)
{
  if (configfile == nullptr)
  {
    return -1;
  }

  try
  {
    boost::property_tree::ini_parser::read_ini(configfile, lvptProperties_);
  }
  catch (std::exception &e)
  {
    std::cerr << __FILE__ << " : " << __LINE__ << " : " << e.what() << std::endl;
    return -1;
  }

  return 0;
}

int config::readall(const char *session,
          std::vector<std::pair<std::string, std::string>> &results,
          const char *configfile /*= nullptr*/)
{
  if (configfile != nullptr && open(configfile) != 0)
  {
    std::cerr << __FILE__ << " : " << __LINE__ << " : "
         << " can not open " << configfile << std::endl;
    return -1;
  }

  try
  {
    auto lvbtItems = lvptProperties_.get_child(session);
    for (const auto &i : lvbtItems)
    {
      results.push_back(std::make_pair(i.first.data(), i.second.data()));
    }
  }
  catch (std::exception &e)
  {
    std::cerr << __FILE__ << " : " << __LINE__ << " : " << e.what() << std::endl;
    return -1;
  }

  return 0;
}
} // namespace gconf
// testcode
#include <iostream>

#include "../config/config.h"

#define CHECKRET(ret)\
if(ret != 0)\
{\
  std::cin.get();\
  return ret;\
}

int main()
{
  gconf::config conf;
  auto ret = conf.open("./config.ini");
  CHECKRET(ret);
  int file = 0;
  ret = conf.read<int>("log", "file", file);
  CHECKRET(ret);
  std::vector<std::pair<std::string, std::string>>kvs;
  ret = conf.readall("log", kvs);
  CHECKRET(ret);
  return 0;
}

以上就是C++讀取配置文件的示例代碼的詳細(xì)內(nèi)容,更多關(guān)于C++讀取配置文件的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評(píng)論