配置文件中包含了项目中的软件配置项,软件配置项从配置文件中读取更加灵活,比如IP、Port等配置项更改方便。
配置文件land.config如下:
LocalServerIP = 192.168.1.108
LocalServerPort = 8888
LocalClientPort = 2000
LandServerAddress = 183.61.39.156
LandServerPort = 6049
GetConfig.h文件如下
/****************************************************************************
* 目的: 读取配置文件的信息,以map的形式存入
* 要求: 配置文件的格式,以#作为行注释,配置的形式是key = value,中间可有空
格,也可没有空格
*****************************************************************************/
#pragma once
#ifndef _GET_CONFIG_H_
#define _GET_CONFIG_H_
#define COMMENT_CHAR '#'
#include <string>
#include <map>
using namespace std;
bool ReadConfig(const string & filename, map<string, string> & m);
void PrintConfig(const map<string, string> & m);
#endif
GetConfig.cpp文件如下:
#include "GetConfig.h"
#include <fstream>
#include <iostream>
using namespace std;
//判断是否为空格
bool IsSpace(char c)
{
if (' ' == c || '\t' == c)
return true;
return false;
}
//判断配置文件内容是否为#开头注释
bool IsCommentChar(char c)
{
if (c == COMMENT_CHAR) {
return true;
}
else {
return false;
}
}
//检查key = value 间是否有空格并去除
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()) {
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) { // 行的第一个字符就是注释字符
return false;
}
end_pos = pos - 1;
}
string new_line = line.substr(start_pos, start_pos + 1 - end_pos); // 预处理,删除注释部分
if ((pos = new_line.find('=')) == -1)
return false; // 没有=号
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)
{
//ofstream fout("e:\\Config.txt");
map<string, string>::const_iterator mite = m.begin();
for (; mite != m.end(); ++mite) {
cout << mite->first << "=" << mite->second << endl;
/*// 把内容输出到新的文件里面去
fout << mite->first << "=" << mite->second << endl;*/
}
//fout.close();
}
编写测试文件,将GetConfig.h和.cpp文件放到项目中,即可得到IP和Port,测试结果如下:
测试案例
#define _CRT_SECURE_NO_WARNINGS
#include"GetConfig.h"
#include<iostream>
using namespace std;
int main()
{
string m_sPath = "D:\\land.config";
map<string, string> m_mapConfig;
ReadConfig(m_sPath, m_mapConfig);
PrintConfig(m_mapConfig);
system("pause");
return EXIT_SUCCESS;
}
结果为: