Advertisement
Guest User

Ini Reader/Writer

a guest
Oct 25th, 2017
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.13 KB | None | 0 0
  1. // .h
  2.  
  3. class Ini
  4. {
  5. private:
  6. Ini();
  7. ~Ini();
  8.  
  9. public:
  10. bool Load(std::string path);
  11.  
  12. void Load(istream& input);
  13.  
  14. bool Save(std::string path);
  15.  
  16. void Save(ostream& output);
  17.  
  18. void Create(std::string path);
  19.  
  20. class IniSection
  21. {
  22. friend class Ini;
  23.  
  24. Ini& pIni;
  25. std::string sectionName;
  26.  
  27. private:
  28. IniSection(Ini& ini, const std::string& section);
  29. ~IniSection();
  30.  
  31. friend bool operator == (const IniSection& _this, const IniSection& _other)
  32. {
  33. return _this.sectionName == _other.sectionName;
  34. }
  35.  
  36. public:
  37. std::string GetSectionName();
  38.  
  39. class IniKey
  40. {
  41. friend class IniSection;
  42.  
  43. IniSection *pSection;
  44. std::string keyName;
  45. std::string keyValue;
  46. std::string commentValue;
  47.  
  48. private:
  49. IniKey(IniSection& section, const std::string& key);
  50. ~IniKey();
  51.  
  52. friend bool operator == (const IniKey& _this, const IniKey& _other)
  53. {
  54. return _this.keyName == _other.keyName;
  55. }
  56.  
  57. public:
  58. void SetValue(std::string value);
  59.  
  60. std::string GetValue();
  61.  
  62. std::string GetKeyName();
  63.  
  64. void AddComment(std::string comment);
  65.  
  66. std::string GetComment();
  67. };
  68.  
  69. void RemoveAllKeys();
  70.  
  71. vector<IniKey>::iterator IniSection::FindKey(const std::string& key);
  72.  
  73. vector<IniKey>::iterator AddKey(const std::string& key);
  74.  
  75. IniKey *GetKey(std::string key);
  76.  
  77. std::string GetKeyValue(std::string key);
  78.  
  79. void SetKeyValue(std::string key, std::string value);
  80.  
  81. private:
  82. std::vector<IniKey> Keys;
  83. };
  84.  
  85. void RemoveAllSections();
  86.  
  87. std::vector<IniSection>::iterator FindSection(const std::string& section);
  88.  
  89. std::vector<IniSection>::iterator AddSection(const std::string& section);
  90.  
  91. IniSection *GetSection(std::string section);
  92.  
  93. std::string GetKeyValue(std::string section, std::string key);
  94.  
  95. void SetKeyValue(std::string section, std::string key, std::string value);
  96.  
  97. private:
  98. std::vector<IniSection> Sections;
  99. };
  100.  
  101. typedef Ini::IniSection IniSection;
  102. typedef IniSection::IniKey IniKey;
  103.  
  104.  
  105.  
  106.  
  107.  
  108.  
  109. // .cpp
  110. Ini::Ini()
  111. {
  112.  
  113. }
  114.  
  115. Ini::~Ini()
  116. {
  117. RemoveAllSections();
  118. }
  119.  
  120. bool Ini::Load(std::string path)
  121. {
  122. ifstream input;
  123.  
  124. input.open(path.c_str(), ios::binary);
  125.  
  126. if (!input.is_open())
  127. return false;
  128.  
  129. Load(input);
  130.  
  131. input.close();
  132.  
  133. return true;
  134. }
  135.  
  136. void Ini::Load(istream& input)
  137. {
  138. IniSection *section = NULL;
  139. std::string lineValue;
  140.  
  141. enum
  142. {
  143. KEY,
  144. SECTION,
  145. COMMENT,
  146. OTHER
  147. };
  148.  
  149. while (getline(input, lineValue))
  150. {
  151. TrimLeft(lineValue);
  152. TrimRight(lineValue, "\n\r");
  153.  
  154. if (!lineValue.empty())
  155. {
  156. unsigned int type = OTHER;
  157.  
  158. type = (lineValue.find_first_of("[") == 0 && (lineValue[lineValue.find_last_not_of(" \t\r\n")] == ']')) ? SECTION : OTHER;
  159. type = ((type == OTHER) && (lineValue.find_first_of("=") != std::string::npos && lineValue.find_first_of("=") > 0)) ? KEY : type;
  160. type = ((type == OTHER) && (lineValue.find_first_of("#") == 0)) ? COMMENT : type;
  161.  
  162. switch (type)
  163. {
  164. case SECTION:
  165. section = AddSection(lineValue.substr(1, lineValue.size() - 2));
  166. break;
  167. case KEY:
  168. if (section)
  169. {
  170. size_t equalSpot = lineValue.find_first_of("=");
  171. std::string keyName = lineValue.substr(0, equalSpot);
  172. std::string keyValue = lineValue.substr(equalSpot + 1);
  173. IniKey *key = section->AddKey(keyName);
  174.  
  175. if (key)
  176. key->SetValue(keyValue);
  177. }
  178. break;
  179. default:
  180. break;
  181. }
  182. }
  183. }
  184. }
  185.  
  186. void Ini::Create(std::string path)
  187. {
  188. fstream file;
  189.  
  190. file.open(path, fstream::out);
  191.  
  192. file.close();
  193. }
  194.  
  195. bool Ini::Save(std::string path)
  196. {
  197. ofstream output;
  198.  
  199. output.open(path.c_str(), ios::binary);
  200.  
  201. if (!output.is_open())
  202. {
  203. output.close();
  204.  
  205. return false;
  206. }
  207.  
  208. Save(output);
  209.  
  210. output.close();
  211.  
  212. return true;
  213. }
  214.  
  215. void Ini::Save(ostream& output)
  216. {
  217. std::string section;
  218. vector<IniSection *>::iterator iter1;
  219.  
  220. for (iter1 = Sections.begin(); iter1 != Sections.end(); iter1++)
  221. {
  222. section = "[" + (*iter1)->GetSectionName() + "]";
  223.  
  224. output << section << "\r\n";
  225.  
  226. vector<IniKey *>::iterator iter2;
  227.  
  228. for (iter2 = (*iter1)->Keys.begin(); iter2 != (*iter1)->Keys.end(); iter2++)
  229. {
  230. std::string comment = "# " + (*iter2)->GetComment();
  231.  
  232. if (comment != "# ")
  233. output << comment << "\r\n";
  234.  
  235. std::string key = (*iter2)->GetKeyName() + "=" + (*iter2)->GetValue();
  236.  
  237. output << key << "\r\n";
  238. }
  239.  
  240. output << "\r\n";
  241. }
  242. }
  243.  
  244. std::string Ini::GetKeyValue(std::string section, std::string key)
  245. {
  246. std::string value;
  247. IniSection *tempSection = GetSection(section);
  248.  
  249. if (tempSection)
  250. {
  251. IniKey *tempKey = tempSection->GetKey(key);
  252. if (tempKey)
  253. value = tempKey->GetValue();
  254. }
  255.  
  256. return value;
  257. }
  258.  
  259. void Ini::SetKeyValue(std::string section, std::string key, std::string value)
  260. {
  261. IniSection *tempSection = AddSection(section);
  262.  
  263. if (tempSection)
  264. {
  265. IniKey *tempKey = tempSection->AddKey(key);
  266. if (tempKey)
  267. tempKey->SetValue(value);
  268. }
  269. }
  270.  
  271. // IniSection -----------------------------------------------------------------------------------
  272.  
  273. IniSection::IniSection(Ini& ini, const std::string& section) : pIni(ini), sectionName(section)
  274. {
  275.  
  276. }
  277.  
  278. IniSection::~IniSection()
  279. {
  280. RemoveAllKeys();
  281. }
  282.  
  283. void Ini::RemoveAllSections()
  284. {
  285. vector<IniSection *>::iterator iter;
  286.  
  287. for (iter = Sections.begin(); iter != Sections.end(); iter++)
  288. delete *iter;
  289.  
  290. Sections.clear();
  291. }
  292.  
  293. vector<IniSection>::iterator Ini::FindSection(const std::string& section)
  294. {
  295. IniSection tempSection(*this, section);
  296.  
  297. return std::find(Sections.begin(), Sections.end(), tempSection);
  298. }
  299.  
  300. vector<IniSection>::iterator Ini::AddSection(const std::string& section)
  301. {
  302. std::vector<IniSection>::iterator iter = FindSection(section);
  303.  
  304. if (iter == Sections.end())
  305. {
  306. Sections.emplace_back(*this, section);
  307.  
  308. return Sections.end() - 1;
  309. }
  310. else
  311. return iter;
  312. }
  313.  
  314. IniSection *Ini::GetSection(std::string section)
  315. {
  316. vector<IniSection *>::iterator iter = FindSection(section);
  317.  
  318. if (iter != Sections.end())
  319. return *iter;
  320.  
  321. return NULL;
  322. }
  323.  
  324. std::string IniSection::GetSectionName()
  325. {
  326. return sectionName;
  327. }
  328.  
  329. std::string IniSection::GetKeyValue(std::string key)
  330. {
  331. std::string value;
  332. IniKey *tempKey = GetKey(key);
  333.  
  334. if (tempKey)
  335. value = tempKey->GetValue();
  336.  
  337. return value;
  338. }
  339.  
  340. void IniSection::SetKeyValue(std::string key, std::string value)
  341. {
  342. IniKey *tempKey = AddKey(key);
  343.  
  344. if (tempKey)
  345. tempKey->SetValue(value);
  346. }
  347.  
  348. // IniKey -----------------------------------------------------------------------------------
  349.  
  350. IniKey::IniKey(IniSection& section, const std::string& key) : pSection(section), keyName(key)
  351. {
  352.  
  353. }
  354.  
  355. IniKey::~IniKey()
  356. {
  357.  
  358. }
  359.  
  360. void IniSection::RemoveAllKeys()
  361. {
  362. vector<IniKey *>::iterator iter;
  363.  
  364. for (iter = Keys.begin(); iter != Keys.end(); iter++)
  365. delete *iter;
  366.  
  367. Keys.clear();
  368. }
  369.  
  370. vector<IniKey>::iterator IniSection::FindKey(const std::string& key)
  371. {
  372. IniKey tempKey(*this, key);
  373.  
  374. return std::find(Keys.begin(), Keys.end(), tempKey);
  375. }
  376.  
  377. vector<IniKey>::iterator IniSection::AddKey(const std::string& key)
  378. {
  379. std::vector<IniKey>::iterator iter = FindKey(key);
  380.  
  381. if (iter == Keys.end())
  382. {
  383. Keys.emplace_back(*this, key);
  384.  
  385. return Keys.end() - 1;
  386. }
  387. else
  388. return iter;
  389. }
  390.  
  391. void IniKey::SetValue(std::string value)
  392. {
  393. keyValue = value;
  394. }
  395.  
  396. std::string IniKey::GetValue()
  397. {
  398. return keyValue;
  399. }
  400.  
  401. std::string IniKey::GetKeyName()
  402. {
  403. return keyName;
  404. }
  405.  
  406. IniKey *IniSection::GetKey(std::string key)
  407. {
  408. vector<IniKey *>::iterator iter = FindKey(key);
  409.  
  410. if (iter != Keys.end())
  411. return *iter;
  412.  
  413. return NULL;
  414. }
  415.  
  416. void IniKey::AddComment(std::string comment)
  417. {
  418. commentValue = comment;
  419. }
  420.  
  421. std::string IniKey::GetComment()
  422. {
  423. return commentValue;
  424. }
  425.  
  426.  
  427.  
  428.  
  429.  
  430.  
  431.  
  432. // How i want to use it:
  433. // Write:
  434. Ini ini;
  435.  
  436. ini.Create(IniPath);
  437.  
  438. ini.Load(IniPath);
  439.  
  440. ini.AddSection("Test");
  441.  
  442. IniSection *Section = ini.GetSection("Test");
  443.  
  444. if (Section)
  445. {
  446. Section->AddKey("Key1")->SetValue("KeyValue1")->AddComment("This is a Test");
  447. }
  448.  
  449. ini.Save(IniPath);
  450.  
  451. // Read:
  452. Ini ini;
  453.  
  454. ini.Load(IniPath);
  455.  
  456. IniSection *Section = ini.GetSection("Test");
  457.  
  458. if (Section)
  459. {
  460. std::string keyValue = Section->GetKeyValue("Key1");
  461. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement