C++ ConfigParser

本文介绍了使用C++实现配置文件解析的方法,重点是通过链表操作读写配置文件,并且能够处理文件中的注释。提供了ConfigParser头文件和源文件的详细说明,以及在main函数中的调用示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

运用链表读写配置文件。

可以保存配置文件中的注解。

ConfigParser.h

#ifndef linux
#pragma once
#else
#include <string.h>
#define _stricmp strcasecmp
#endif // linux

#include <string>

using std::string;

typedef struct Options
{
	string comment;	// save option notes.
	string name;	// save option name.
	string value;	// save option value.
	Options* next;
}*pOption, Option;

typedef struct Sections
{
	string comment;	// save section notes.
	string name;	// section name.
	Option *options;
	Sections* next;
}*pSection, Section;

class ConfigParser
{
private:
	string _ini;
	Section *_section_list_head;
	size_t section_count = 0;

	string line_break = "\n";

public:

	ConfigParser(string path);
	~ConfigParser();

	// Init Section struct.
	Section* InitSectionList();

	// read part
	bool ReadIni();

	string GetString(const string& root, const string& key, const string& def = "");
	int GetInt(const string& root, const string& key, int def = 0);
	bool GetBool(const string& root, const string& key, bool def = false);
	double GetDouble(const string& root, const string& key, double def = 0.00);

	// set ini
	void SetValue(const string& root, const string& key, const string& value);
	// void SetString(const string& root, const string& key, const string& value);
	void SetInt(const string& root, const string& key, int value);
	void SetDouble(const string& root, const string& key, double value);
	void SetBool(const string& root, const string& key, bool value);

	// write ini
	bool WriteIni(string path);

	// Is notes
	bool IsNotes(string line);
	bool IsSection(string line);

	bool IsExistSection(string root);
	bool IsExistOption(string root, string option);

	string TrimLeadTrailSpecStr(string src, string keyword);
	string TrimLeadSpecStr(string src, string keyword);
	string TrimTrailSpecStr(string src, string keyword);
	// delete '\r' & '\n' at the end of the string.
	string TrimTrialLineBreak(string data);

	void AddSection(string section, string note);
	void AddOption(string section, string option, string value, string note);

	void ViewSections();
};

ConfigParser.cpp

#include "ConfigParser.h"

#include <iostream>
#include <fstream>
#include <sstream>

using std::fstream;
using std::stringstream;
using std::istringstream;
using std::to_string;
using std::ios;
using std::cout;
using std::endl;

ConfigParser::ConfigParser(string path)
{
	_ini = path;
	ReadIni();
}

ConfigParser::~ConfigParser()
{
	for (size_t index = 0; index < section_count; index++)
	{
		Section* node = _section_list_head;
		_section_list_head = _section_list_head->next;

		Option* op_head = node->options;
		size_t j = 0;
		while (op_head != nullptr)
		{
			Option* child = op_head;
			op_head = op_head->next;
			// cout << child->name <<"  free." << j++ << endl;
			delete child;
		}
		delete node;
		// cout << "delete
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值