chat

#undef _WIN32_WINNT
#define _WIN32_WINNT 0x0601
#undef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN 0x0002
//#pragma cpp_std c++11 
//你可以换成指令: -std=gnu++11
//#pragma comment(lib, "winmm.lib")
//你可以换成 -lwinmm
#include <bits/stdc++.h>
#include <Windows.h>
#include <mmsystem.h>
#include <string>
#include <algorithm>
#include <stdexcept>
#include <cctype>
#include <mutex>
#include <fstream>
#include <chrono>
#include <conio.h>
#include <limits>    // 用于清除输入缓冲区
#include <wininet.h>
#include <shlobj.h>
#include <powrprof.h>
#include <lmaccess.h>
#include <tlhelp32.h>
#include <commctrl.h>
#include <shellapi.h>
#include <mmsystem.h>
#include <psapi.h>
#include <thread>
#include <vector>
#include <map>
//#include <unistd.h> linux/macOS
using namespace std;
#define INI_MOD_DIF 0x31
#define INI_GOOD 0x32
#define INI_READ_BAD 0x33
#define INI_NOT_FOUND 0x34
#define INI_WRITE_BAD 0x35
#define INI_WRITE_OK 0x36
#define INI_READ_OK 0x37
#define INI_UNKOWN_ERROR 0x38
#define INI_NPOS "ini_log_or_othor_sring_get_error"
#define INI_NO_THIS_TITLE "ini_lost_this_title_error"
#define INI_NO_THIS_KEY "ini_lost_this_key_error"
#define INI_INT_NPOS 0x39
#ifndef _iniagree_
#error "you need define _iniagree_ to agree this";
#endif
struct ifinistream
{
private:
	stringstream log;
	string path="program.ini";
	map<string,map<string,string> > ans;
public:
	int set_ini_reader_path(const string& new_path)
	{
		if(new_path.empty()||new_path.substr(int(new_path.size())-4,4)!=".ini")
		{
			log<<"error[INI_MOD_DIF] in set_ini_reader_path("<<new_path<<");\n";
			return INI_MOD_DIF;
		}
		else
		{
			path=new_path;
			log<<"info[INI_GOOD]set_ini_reader_path(); maken path in"<<new_path<<" \n";
			return INI_GOOD;
		}
	}
	int read_ini()
	{
		ifstream fini;
		fini.open(path,ios::in);
		if(!fini.good())
		{
			fini.close();
			log<<"error[INI_READ_BAD] unknow error make ini bad\n";
			return INI_READ_BAD;
		}
		int line=0;
		string now="system";
		while(fini.good())
		{
			string l;
			getline(fini,l);
			line++;
			if(!l.empty())
			{
				if(l[0]=='['&&l[int(l.size())-1]==']')
				{
					string title=l.substr(1,int(l.size())-2);
					log<<"info[] take title(\'"<<title<<"\') in \'"<<l<<"\' \n";
					//[title]
					now=title;
					ans[now]["self_ini_title_exc"]="1";
				}
				else if(l.substr(0,4)=="vol ")
				{
					size_t a1=l.find('\"',3);
					size_t a2=l.find('\"',a1+1);
					size_t b1=l.find('\"',a2+1);
					size_t b2=l.find('\"',b1+1);
					if(a1==string::npos||a2==string::npos||b1==string::npos||b2==string::npos)
					{
						log<<"error[] find npos in vol["<<l<<"]"<<endl;
					}
					else
					{
						string a=l.substr(a1+1,a2-a1-1);
						string b=l.substr(b1+1,b2-b1-1);
						ans[now][a]=b;
						log<<"info[] get vol[\'"<<a<<"\',\'"<<b<<"\'] \n";
					}
				}
				else if(l=="title_finish")
				{
					log<<"info[] title back to system\n";
					now="system";
				}
				else if(l=="end")
				{
					log<<"info[] ini vol_self end\n";
					return INI_READ_OK;
				}
				else
				{
					log<<"error[] unkown vol[\'"<<l<<"\'] please check your ini\n";
				}
			}
			else log<<"warn[] line "<<line<<" empty\n";
		}
		if(fini.eof()) return INI_READ_OK;
		else return INI_UNKOWN_ERROR;
	}
	string ini_get_log()
	{
		if(!log.str().empty()) return log.str();
		else return INI_NPOS;
	}
	string ini_get_vol(const string& title,const string& key_name)
	{
		if(ans.count(title)==0) return INI_NO_THIS_TITLE;
		else if(ans[title].count(key_name)==0) return INI_NO_THIS_KEY;
		else return ans[title][key_name];
		return INI_NPOS;
	}
};



struct ofinistream
{
private:
	stringstream log;
	string path="program.ini";
	map<string,map<string,string> > ans;
	string now_title="system";
	int read_for_write_with_mod_added()
	{
		ifstream fini;
		fini.open(path,ios::in);
		if(!fini.good())
		{
			fini.close();
			log<<"error[INI_READ_BAD] unknow error make ini bad\n";
			return INI_READ_BAD;
		}
		int line=0;
		string now="system";
		while(fini.good())
		{
			string l;
			getline(fini,l);
			line++;
			if(!l.empty())
			{
				if(l[0]=='['&&l[int(l.size())-1]==']')
				{
					string title=l.substr(1,int(l.size())-2);
					log<<"info[] take title(\'"<<title<<"\') in \'"<<l<<"\' \n";
					//[title]
					now=title;
					ans[now]["self_ini_title_exc"]="1";
				}
				else if(l.substr(0,4)=="vol ")
				{
					size_t a1=l.find('\"',3);
					size_t a2=l.find('\"',a1+1);
					size_t b1=l.find('\"',a2+1);
					size_t b2=l.find('\"',b1+1);
					if(a1==string::npos||a2==string::npos||b1==string::npos||b2==string::npos)
					{
						log<<"error[] find npos in vol["<<l<<"]"<<endl;
					}
					else
					{
						string a=l.substr(a1+1,a2-a1-1);
						string b=l.substr(b1+1,b2-b1-1);
						ans[now][a]=b;
						log<<"info[] get vol[\'"<<a<<"\',\'"<<b<<"\'] \n";
					}
				}
				else if(l=="title_finish")
				{
					log<<"info[] title back to system\n";
					now="system";
				}
				else if(l=="end")
				{
					log<<"info[] ini vol_self end\n";
				}
				else
				{
					log<<"error[] unkown vol[\'"<<l<<"\'] please check your ini\n";
				}
			}
			else log<<"warn[] line "<<line<<" empty\n";
		}
		if(fini.eof()) return INI_READ_OK;
		else return INI_UNKOWN_ERROR;
	}
	int rewrite_ini_volum_with_what_mod()
	{
		ofstream fini;
		fini.open(path);
		if(!fini.good()) return INI_WRITE_BAD;
		for(map<string,map<string,string> >::iterator it=ans.begin();it!=ans.end();++it)
		{
			fini<<"["<<it->first<<"]\n";
			for(map<string,string>::iterator iter=it->second.begin();iter!=it->second.end();++iter)
			{
				fini<<"vol \""<<iter->first<<"\" = \""<<iter->second<<"\"\n";
			}
			fini<<"title_finish\n\n";
		}
		fini<<"\nend\n";
		fini.close();
		return INI_WRITE_OK;
	}
public:
	int ini_reset()
	{
		return read_for_write_with_mod_added();
	}
	int set_ini_WRITER_path(const string& new_path)
	{
		if(new_path.empty()||new_path.substr(int(new_path.size())-4,4)!=".ini")
		{
			log<<"error[INI_MOD_DIF] in set_ini_reader_path("<<new_path<<");\n";
			return INI_MOD_DIF;
		}
		else
		{
			path=new_path;
			log<<"info[INI_GOOD]set_ini_reader_path(); maken path in"<<new_path<<" \n";
			return INI_GOOD;
		}
	}
	int ini_write()
	{
		return rewrite_ini_volum_with_what_mod();
	}
	string ini_get_log()
	{
		if(!log.str().empty()) return log.str();
		else return INI_NPOS;
	}
	int ini_set_title(const string& title)
	{
		if(!title.empty()) now_title=title;
		else return INI_INT_NPOS;
		return INI_GOOD;
	}
	int ini_add_vol(const string& key,const string& vol)
	{
		if(key.empty()||vol.empty())
		{
			log<<"error[INI_NPOS] key|vol is empty\n";
			return INI_INT_NPOS;
		}
		else ans[now_title][key]=vol;
		log<<"info[] added new volum["<<key<<":"<<vol<<"] \n";
		return INI_GOOD;
	}
};
posted @ 2025-10-30 12:42  [GuiHua]  阅读(11)  评论(0)    收藏  举报

GuiHua博客 by GuiHua is licensed under CC BY-NC-ND 4.0