吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 1458|回复: 0
收起左侧

[C&C++ 转载] 读取配置文件

[复制链接]
古月不傲 发表于 2020-12-31 16:53
本帖最后由 古月不傲 于 2020-12-31 17:06 编辑

[C++] 纯文本查看 复制代码
#ifndef __WIND_CONFIG_H__
#define __WIND_CONFIG_H__

#include <iostream>
#include <string>
#include <map>
#include <fstream>
#include <cstring>
#include <utility>

#include "../_include/wind_log.h"
#include "../_include/wind_misc.h"

// 配置文件相关
namespace wind_config
{
	class CConfig
	{
		public:
			CConfig() = default;
			CConfig(const CConfig &) = delete;
			CConfig &operator =(const CConfig &) = delete;

		public:
			static CConfig &get_instance()
			{
				static CConfig obj;
				return obj;
			}

			~CConfig()
			{
			}

		public:
			// 加载配置文件
			int load_config(const std::string &fileName)
			{
				if (fileName.size() == 0)
				{
					wind_log::CLog::get_instance().write_log("fileName.size() failed, errno:%d, at %s:%s.%d", 
							errno, __FILE__, __func__, __LINE__);
					return -1;
				}

				std::ifstream inFile;
				inFile.open(fileName.c_str(), std::ios::binary | std::ios::in);

				// 如果打开文件失败
				if (inFile.is_open() == false)
				{
					wind_log::CLog::get_instance().write_log("inFile.is_open() failed, errno:%d, at %s:%s.%d", 
							errno, __FILE__, __func__, __LINE__);
					return -1;
				}

				char buf[512]{};

				// 逐行解析
				while (inFile.eof() == false)
				{
					inFile.getline(buf, 512);

					// 空行不处理
					if (strlen(buf) == 0)
						continue;

					// 开头是特殊字符不处理
					if (buf[0] == '\0' || buf[0] == '#' || buf[0] == '['
							|| buf[0] == ' ' || buf[0] == '\t' || buf[0] == '\n')
						continue;

					// 截取键值对
					char *pEqual = strchr(buf, '=');

					// 无效行不处理
					if (pEqual == nullptr)
						continue;

					char tempKey[256]{};
					char tempValue[256]{};

					strncpy(tempKey, buf, pEqual - buf);
					strcpy(tempValue, pEqual+1);

					// 去掉左右空格
					wind_misc::lTrim(tempKey);
					wind_misc::rTrim(tempKey);
					wind_misc::lTrim(tempValue);
					wind_misc::rTrim(tempValue);

					// 插入map
					this->m_cfgItem.insert(std::pair<std::string, std::string>(tempKey, tempValue));
				}
				inFile.close();

				return 0;
			}

			// 获取值
			int get_value(const std::string &str)
			{
				if (str.size() == 0)
				{
					wind_log::CLog::get_instance().write_log("str_size() failed, errno:%d, at %s:%s.%d", 
							errno, __FILE__, __func__, __LINE__);
					return -1;
				}

				auto it = this->m_cfgItem.find(str.c_str());
				if (it != this->m_cfgItem.end())
					return std::atoi(it->second.c_str());

				return -1;
			}

		private:
			std::map<std::string, std::string> m_cfgItem;
	};
}

#endif

[Asm] 纯文本查看 复制代码
[Proc]
# 子进程的个数
child_process_num = 2

[Net]
# 需要监听端口的个数
listen_port_count = 2
# 需要监听的端口
listenPort0 = 80
listenPort1 = 5578
# 允许连接连入的数量
allow_connections_num = 1024

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-5-6 01:40

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表