C++編寫Config類讀取配置檔案
C++程式碼
- //Config.h
- #pragma once
- #include <string>
- #include <map>
- #include <iostream>
- #include <fstream>
- #include <sstream>
- /*
- * \brief Generic configuration Class
- *
- */
- class Config {
- // Data
- protected:
-
std::string m_Delimiter; //!< separator between key and value
- std::string m_Comment; //!< separator between value and comments
- std::map<std::string,std::string> m_Contents; //!< extracted keys and values
- typedef std::map<std::string,std::string>::iterator mapi;
-
typedef std::map<std::string,std::string>::const_iterator mapci;
- // Methods
- public:
- Config( std::string filename,std::string delimiter = "=",std::string comment = "#" );
- Config();
- template<class T> T Read( const std::string& in_key ) const; //!<Search for key and read value or optional default value, call as read<T>
-
template
- template<class T> bool ReadInto( T& out_var, const std::string& in_key ) const;
- template<class T>
- bool ReadInto( T& out_var, const std::string& in_key, const T& in_value ) const;
- bool FileExist(std::string filename);
- void ReadFile(std::string filename,std::string delimiter = "=",std::string comment = "#" );
- // Check whether key exists in configuration
- bool KeyExists( const std::string& in_key ) const;
- // Modify keys and values
- template<class T> void Add( const std::string& in_key, const T& in_value );
- void Remove( const std::string& in_key );
- // Check or change configuration syntax
- std::string GetDelimiter() const { return m_Delimiter; }
- std::string GetComment() const { return m_Comment; }
- std::string SetDelimiter( const std::string& in_s )
- { std::string old = m_Delimiter; m_Delimiter = in_s; return old; }
- std::string SetComment( const std::string& in_s )
- { std::string old = m_Comment; m_Comment = in_s; return old; }
- // Write or read configuration
- friend std::ostream& operator<<( std::ostream& os, const Config& cf );
- friend std::istream& operator>>( std::istream& is, Config& cf );
- protected:
- template<class T> static std::string T_as_string( const T& t );
- template<class T> static T string_as_T( const std::string& s );
- static void Trim( std::string& inout_s );
- // Exception types
- public:
- struct File_not_found {
- std::string filename;
- File_not_found( const std::string& filename_ = std::string() )
- : filename(filename_) {} };
- struct Key_not_found { // thrown only by T read(key) variant of read()
- std::string key;
- Key_not_found( const std::string& key_ = std::string() )
- : key(key_) {} };
- };
- /* static */
- template<class T>
- std::string Config::T_as_string( const T& t )
- {
- // Convert from a T to a string
- // Type T must support << operator
- std::ostringstream ost;
- ost << t;
- return ost.str();
- }
- /* static */
- template<class T>
- T Config::string_as_T( const std::string& s )
- {
- // Convert from a string to a T
- // Type T must support >> operator
- T t;
- std::istringstream ist(s);
- ist >> t;
- return t;
- }
- /* static */
- template<>
- inline std::string Config::string_as_T<std::string>( const std::string& s )
- {
- // Convert from a string to a string
- // In other words, do nothing
- return s;
- }
- /* static */
- template<>
- inline bool Config::string_as_T<bool>( const std::string& s )
- {
- // Convert from a string to a bool
- // Interpret "false", "F", "no", "n", "0" as false
- // Interpret "true", "T", "yes", "y", "1", "-1", or anything else as true
- bool b = true;
- std::string sup = s;
- for( std::string::iterator p = sup.begin(); p != sup.end(); ++p )
- *p = toupper(*p); // make string all caps
- if( sup==std::string("FALSE") || sup==std::string("F") ||
- sup==std::string("NO") || sup==std::string("N") ||
- sup==std::string("0") || sup==std::string("NONE") )
- b = false;
- return b;
- }
- template<class T>
- T Config::Read( const std::string& key ) const
- {
- // Read the value corresponding to key
- mapci p = m_Contents.find(key);
- if( p == m_Contents.end() ) throw Key_not_found(key);
- return string_as_T<T>( p->second );
- }
- template<class T>
- T Config::Read( const std::string& key, const T& value ) const
- {
- // Return the value corresponding to key or given default value
- // if key is not found
- mapci p = m_Contents.find(key);
- if( p == m_Contents.end() ) return value;
- return string_as_T<T>( p->second );
- }
- template<class T>
- bool Config::ReadInto( T& var, const std::string& key ) const
- {
- // Get the value corresponding to key and store in var
- // Return true if key is found
- // Otherwise leave var untouched
- mapci p = m_Contents.find(key);
- bool found = ( p != m_Contents.end() );
- if( found ) var = string_as_T<T>( p->second );
- return found;
- }
- template<class T>
- bool Config::ReadInto( T& var, const std::string& key, const T& value ) const
- {
- // Get the value corresponding to key and store in var
- // Return true if key is found
- // Otherwise set var to given default
- mapci p = m_Contents.find(key);
- bool found = ( p != m_Contents.end() );
- if( found )
- var = string_as_T<T>( p->second );
- else
- var = value;
- return found;
- }
- template<class T>
- void Config::Add( const std::string& in_key, const T& value )
- {
- // Add a key with given value
- std::string v = T_as_string( value );
- std::string key=in_key;
- trim(key);
- trim(v);
- m_Contents[key] = v;
- return;
- }
C++程式碼
- // Config.cpp
- #include "Config.h"
- using namespace std;
- Config::Config( string filename, string delimiter,
- string comment )
- : m_Delimiter(delimiter), m_Comment(comment)
- {
- // Construct a Config, getting keys and values from given file
- std::ifstream in( filename.c_str() );
- if( !in ) throw File_not_found( filename );
- in >> (*this);
- }
- Config::Config()
- : m_Delimiter( string(1,'=') ), m_Comment( string(1,'#') )
- {
- // Construct a Config without a file; empty
- }
- bool Config::KeyExists( const string& key ) const
- {
- // Indicate whether key is found
- mapci p = m_Contents.find( key );
- return ( p != m_Contents.end() );
- }
- /* static */
- void Config::Trim( string& inout_s )
- {
- // Remove leading and trailing whitespace
- static const char whitespace[] = " \n\t\v\r\f";
- inout_s.erase( 0, inout_s.find_first_not_of(whitespace) );
- inout_s.erase( inout_s.find_last_not_of(whitespace) + 1U );
- }
- std::ostream& operator<<( std::ostream& os, const Config& cf )
- {
- // Save a Config to os
- for( Config::mapci p = cf.m_Contents.begin();
- p != cf.m_Contents.end();
- ++p )
-
相關推薦
C++編寫Config類讀取配置檔案
老外寫的一段程式碼,在Server中編寫這個類讀取配置檔案比較實用 C++程式碼 //Config.h #pragma once #include <string> #include <map> #include <
C++builder利用GetPrivateProfileString讀取配置檔案
GetPrivateProfileString配置檔案中經常用到ini檔案,在C++builder或者Delphi中其函式分別為: 寫入.ini檔案:bool WritePrivateProfileString(LPCTSTR lpAppName,LPC
Properties類讀取配置檔案資訊
一、Java Properties類 Java中有個比較重要的類Properties(Java.util.Properties),主要用於讀取Java的配置檔案,各種語言都有自己所支援的配置檔案,配置檔案中很多變數是經常改變的,這樣做也是為了方便使用者,讓使
C#使用ConfigurationManager類操作配置檔案
.net1.1中如果需要靈活的操作和讀寫配置檔案並不是十分方便,一般都會在專案中封裝一個配置 檔案管理類來進行讀寫操作。而在.net2.0中使用configurationmanager 和webconfigurationmanager 類可以很好的管理配置檔案,configurationmanager類在s
C# winForm讀取配置檔案 App.config
//讀取配置窗體中使用者輸入的配置 string server = txtServer.Text.Trim();string database = txtDatabase.Text.Trim();string uid = txtUid.Text.Trim();string pwd = txtPwd.T
MySQL-讀取配置檔案的工具類與測試
package JDBCUtil; import java.io.IOException; import java.io.InputStream; import java.sql.Connection; import java.sql.DriverManager; import java.ut
讀取配置檔案的工具類實現
/** * 讀取配置檔案的工具類 * */ public class ConfigManager { // 第一步:構建私有的靜態的例項 private static ConfigManager configManager; private static Properties pr
C++讀取配置檔案.txt連線資料庫
mysql=mysql_init((MYSQL*)0); ifstream file; string path="D:/data.txt"; file.open(path.c_str()); string port1; string url; string name; str
自定義讀取配置檔案類
#include<iostream> #include<string.h> #include<vector> #include<map> #include<set> #include <fstream> #include<
Asp.net Core 和類庫讀取配置檔案資訊
Asp.net Core 和類庫讀取配置檔案資訊 看乾貨請移步至.net core 讀取配置檔案公共類 首先開一個腦洞,Asp.net core 被使用這麼長時間了,但是關於配置檔案(json)的讀取,微軟官方似乎並沒有給出像.net framework讀取web.config那樣簡單且完美。嚴重懷
在ServletContextListener 的實現類中(使用Spring @Value 註解的方式讀取配置檔案、或者注入Spring bean)
在ServletContextListener 的實現類中 使用Spring @Value 註解的方式讀取配置檔案 我想向ServletContextListener中通過Spring @value 的方法讀取 properties 配置檔案資訊,但是我開始的方法不行 public class MyLi
讀取配置檔案config.properties的方法
引入的jar包: 這兩個jar包版本是配套的,建議使用maven 使用的jar版本需要與spring整體版本適應 commons-configuration-1.8.jar commons-lang-2.6.jar(commons-configuration的依賴
.NET CORE 類庫讀取配置檔案繫結到類
前言,好記性不如爛筆頭 1,NuGet管理器新增 三個引用Microsoft.Extensions.Configuration,Microsoft.Extensions.Configuration.Binder,Microsoft.Extensions.Configurat
讀取配置檔案 properties類工具
import java.io.BufferedInputStream; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import
Spring Cloud入門教程-Config Server從github 遠端讀取配置檔案
接上一篇文章,這裡記錄一下Config Server從github 遠端讀取配置檔案。 Spring cloud Config支援從遠端Git倉庫讀取配置檔案,即 Config Server可以不從本地的倉庫讀取,而是從遠端Git倉庫讀取。這
springboot 專案框架搭建(三):工具類中讀取配置檔案
一.原因 編寫一個服務類的工具類,想做成一個靈活的配置,各種唯一code想從配置檔案中讀取,便有了這個坑。 二.使用@value獲取值為null, 這是因為這個工具類沒有交給spring boot 來管理,導致每次都是new 一個新的,所以每次取出來的
【java】 一個讀取配置檔案的類
/** * <p>Title:InitConfig.java</p> * <p>Description:</p> * @author songrongkai * @date 2018年7月29日 * @version 1.0 */ p
自己編寫讀取配置檔案的資訊的方法
結果: 程式碼: public static void main(String[] args) { //自己編寫的讀取配置檔案的方法 InputStream inputStream = FtpConfig.class.getResourceAsStream("/
Spring Cloud學習筆記(十)-配置中心Config元件從GitHub讀取配置檔案
說明:本文僅作為本人學習<<深入理解Spring Cloud與微服務構建>>一書的學習筆記,所有程式碼案例及文字描述均參考該書,不足之處,請留言指正,不勝感激. 一.為什麼要使用Config元件? 我覺得主要有兩點,方便配置統一
c++:讀取配置檔案
namespace fs = boost::filesystem; fs::path objPath = strFilePath; // strFilePath--目錄 objPath/=strFileName;// strFileName--檔名 if(o