Header file: get_config.h
/*************************************** * *********************************** Author: jasitzhang (Zhang Tao) * Date: 2011-10-2 * objective: to read the information of the configuration file and store it in map format * requirement: Format of the configuration file, with # As the line comment, the configuration format is key = value, with spaces in the middle, there can also be no space ************************************ **************************************** */# ifndef _ GET_CONFIG_H _ # define _ GET_CONFIG_H _ # include <string >#include <map> using namespace std; # define COMMENT_CHAR '# 'bool ReadConfig (const string & filename, map <string, string> & m); void PrintConfig (const map <string, string> & m); # endif
Source File: get_config.cpp
# Include "get_config.h"
# Include <fstream>
# Include <iostream>
Using namespace std;
Bool IsSpace (char c)
{
If (''= c | '\ t' = c)
Return true;
Return false;
}
Bool IsCommentChar (char c)
{
Switch (c ){
Case COMMENT_CHAR:
Return true;
Default:
Return false;
}
}
Void Trim (string & str)
{
If (str. empty ()){
Return;
}
Int I, start_pos, end_pos;
For (I = 0; I <str. size (); ++ I ){
If (! IsSpace (str [I]) {
Break;
}
}
If (I = str. size () {// All are blank strings
Str = "";
Return;
}
Start_pos = I;
For (I = str. size ()-1; I> = 0; -- I ){
If (! IsSpace (str [I]) {
Break;
}
}
End_pos = I;
Str = str. substr (start_pos, end_pos-start_pos + 1 );
}
Bool AnalyseLine (const string & line, string & key, string & value)
{
If (line. empty ())
Return false;
Int start_pos = 0, end_pos = line. size ()-1, pos;
If (pos = line. find (COMMENT_CHAR ))! =-1 ){
If (0 = pos) {// the first character of the row is the comment character
Return false;
}
End_pos = pos-1;
}
String new_line = line. substr (start_pos, start_pos + 1-end_pos); // preprocessing, delete comments
If (pos = new_line.find ('=') =-1)
Return false; // No =
Key = new_line.substr (0, pos );
Value = new_line.substr (pos + 1, end_pos + 1-(pos + 1 ));
Trim (key );
If (key. empty ()){
Return false;
}
Trim (value );
Return true;
}
Bool ReadConfig (const string & filename, map <string, string> & m)
{
M. clear ();
Ifstream infile (filename. c_str ());
If (! Infile ){
Cout <"file open error" <endl;
Return false;
}
String line, key, value;
While (getline (infile, line )){
If (AnalyseLine (line, key, value )){
M [key] = value;
}
}
Infile. close ();
Return true;
}
Void PrintConfig (const map <string, string> & m)
{
Map <string, string >:: const_iterator mite = m. begin ();
For (; mite! = M. End (); ++ mite ){
Cout <Mite-> first <"=" <Mite-> second <Endl;
}
}
Test data: Test. cfg
# Added by jasit
Key1 = value1
Key2 = value2
Key3 = value3 + value3
SDF
Test source code: Test. cpp
#include "get_config.h"int main(){map<string, string> m;ReadConfig("test2.cfg", m);PrintConfig(m);return 0;}
Makefile:
BIN := test
CFLAGS := -g -static -Wall
CC := g++
SRCFILE := $(wildcard *.cpp)
OBJFILE := $(patsubst %.cpp,%.o,$(SRCFILE))
$(BIN): $(OBJFILE)
$(CC) $(CFLAGS) -o $(BIN) $(OBJFILE)
%.o:%.cpp
$(CC) $(CFLAGS) -c $< -o $@
clean :
rm -rf $(OBJFILE) ${BIN}