From 7033314f386778b42bc4e0c388d07252364e7ae3 Mon Sep 17 00:00:00 2001 From: daleclack Date: Wed, 11 Aug 2021 10:45:11 +0800 Subject: [PATCH] Add cfgfile2 --- cpp/cfgfile2.cpp | 158 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 158 insertions(+) create mode 100644 cpp/cfgfile2.cpp diff --git a/cpp/cfgfile2.cpp b/cpp/cfgfile2.cpp new file mode 100644 index 0000000..09f2b30 --- /dev/null +++ b/cpp/cfgfile2.cpp @@ -0,0 +1,158 @@ +// A Test to read Config files +// This is a modified version +// The Config File Should be a text file +// and the content as follows +// key=value + +#include +#include +#include +#include + +typedef void (*pfun)();//The Function Pointer +typedef std::map conf_map;//Container of configs + +void Trim(std::string &str){ + if(str.empty()){//String is empty + return; + } + int i; + size_t start_pos,end_pos; + + for(i=0;i=0;i--){//Get End Position + if(!isspace(str[i])){ + break; + } + } + + end_pos=i; + + str=str.substr(start_pos,end_pos+1); +} + +bool readCfgFile(const char * cfgfilePath,conf_map &configs){ + //Open The Config File and load config to map + std::fstream cfgfile; + cfgfile.open(cfgfilePath); + if(!cfgfile.is_open()){ + std::cout<<"Failed to open the file!"<