(17)读取ini文件的键值对

来源:互联网 发布:五轴联动数控机床编程 编辑:程序博客网 时间:2024/05/17 13:45

拿到一个新需求,是读取ini文件中键值对,因此用了一个单例模式:

头文件如下:

#pragma once#include "stdafx.h"#include<iostream>#include<fstream>#include<windows.h>#include<string>#include<map>class QueryTraderIni{public:QueryTraderIni();~QueryTraderIni();public:/** * @brief 单例模式,获取该类实例* @return QueryTraderIni*,返回该实例指针*/static QueryTraderIni* getInstance();/*** @brief 传入key,得到对应的value* @param[in] int iKey, 查询的key* @param[out] wchar_t* sDes,查询到的value * @param[in]  int len sDes为首的内存最大长度* @return bool, 成功查找到则返回TRUE, 否则返回FALSE*/bool GetValue(int iKey, wchar_t* sDes, int len);/*** @brief 初始化成员变量指针* @return void*/static void Init();/*** @brief delete成员变量指针* @return void*/static void UnInit();private:bool LoadFile();std::map<int, std::wstring> m_map;static QueryTraderIni* m_pInstance;};
实现文件如下:

#include"stdafx.h"#include"QueryTraderIni.h"#include <codecvt>QueryTraderIni* QueryTraderIni::m_pInstance = NULL;QueryTraderIni* QueryTraderIni::getInstance() {if (m_pInstance == NULL) {m_pInstance = new QueryTraderIni();}return m_pInstance;}void QueryTraderIni::Init(){if (m_pInstance == NULL) {m_pInstance = new QueryTraderIni();}}void QueryTraderIni::UnInit(){if (m_pInstance != NULL) {delete m_pInstance;}}bool QueryTraderIni::LoadFile(){std::ifstream fin;LPCTSTR path = L"test.ini";bool seeEqu = false;wchar_t readChar, equalChar = L'=', nLineChar = L'\n';fin.open(path, std::ios::binary);fin.read((char *)(&readChar), 2);// 跳过unicode文本开头有两个字节0xFFFE(称作BOM,用于标识unicode编码)//fin.seekg(2, std::ios::beg);if (fin.good()){/*if (std::stoi(readChar) != 0xFFFE) //不是unicode文本{return false; }*///std::cout <<readChar<<" "<< _wtoi(&readChar) << std::endl;//std::cout << readChar << std::endl;//std::cout << readChar << std::endl;}else{return false;}std::wstring sKey, sValue;while (!fin.eof()){fin.read((char *)(&readChar), 2);if (fin.good()){if (readChar != equalChar && readChar != nLineChar)//正常情况{if (!seeEqu){sKey = sKey + readChar;}else{sValue = sValue + readChar;}}else if (readChar == equalChar)//找到 '='了,前面的key完整了{seeEqu = TRUE;}else if (readChar == nLineChar)//换行了{if (seeEqu){m_map.insert(std::make_pair(std::stoi(sKey), sValue));}sKey = L"";sValue = L"";seeEqu = FALSE;}}else  // fin.bad()  , 避免EOF时读取到末尾字符2次的情况{if (seeEqu){m_map.insert(std::make_pair(std::stoi(sKey), sValue));}sKey = L"";sValue = L"";seeEqu = FALSE;}}fin.close();//打开完文件记得关闭return FALSE;}bool QueryTraderIni::GetValue(int iKey, wchar_t* sDes, int len){std::map<int, std::wstring>::iterator it = m_map.find(iKey);if (it == m_map.end()){return FALSE;                             //不存在}else{std::wstring sValue = it->second;for (int i = 0; i < sValue.length(); i++){*(sDes + i) = sValue[i];}//查找到了,传入des指针return TRUE;}}QueryTraderIni::QueryTraderIni(){LoadFile();}QueryTraderIni::~QueryTraderIni(){}

提供接口如下:

std::wstring GetValue(int iKey){QueryTraderIni* queryTrader = QueryTraderIni::getInstance();std::wstring value = L"";//wchar_t* sDes = new wchar_t[];wchar_t sDes[100] = { 0 };if (queryTrader->GetValue(iKey, sDes, 100))   //TRUE,得到查询结果{for (int i = 0; i < wcslen(sDes); i++){value = value + *(sDes + i);}return value;}else{return L"";                       //返回空字符串}}