| 
									
										
										
										
											2015-05-24 06:55:12 +02:00
										 |  |  | // Copyright 2008 Dolphin Emulator Project
 | 
					
						
							| 
									
										
										
										
											2015-05-18 01:08:10 +02:00
										 |  |  | // Licensed under GPLv2+
 | 
					
						
							| 
									
										
										
										
											2013-04-17 23:09:55 -04:00
										 |  |  | // Refer to the license.txt file included.
 | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | // see IniFile.h
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-17 05:18:15 -05:00
										 |  |  | #include <algorithm>
 | 
					
						
							| 
									
										
										
										
											2014-02-20 04:11:52 +01:00
										 |  |  | #include <cstddef>
 | 
					
						
							|  |  |  | #include <cstring>
 | 
					
						
							| 
									
										
										
										
											2014-02-17 05:18:15 -05:00
										 |  |  | #include <fstream>
 | 
					
						
							| 
									
										
										
										
											2014-02-20 04:11:52 +01:00
										 |  |  | #include <map>
 | 
					
						
							| 
									
										
										
										
											2010-06-03 18:05:08 +00:00
										 |  |  | #include <string>
 | 
					
						
							| 
									
										
										
										
											2014-02-20 04:11:52 +01:00
										 |  |  | #include <utility>
 | 
					
						
							| 
									
										
										
										
											2010-06-03 18:05:08 +00:00
										 |  |  | #include <vector>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-20 04:11:52 +01:00
										 |  |  | #include "Common/CommonTypes.h"
 | 
					
						
							| 
									
										
										
										
											2014-02-17 05:18:15 -05:00
										 |  |  | #include "Common/FileUtil.h"
 | 
					
						
							|  |  |  | #include "Common/IniFile.h"
 | 
					
						
							|  |  |  | #include "Common/StringUtil.h"
 | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-07-29 11:47:56 -05:00
										 |  |  | void IniFile::ParseLine(const std::string& line, std::string* keyOut, std::string* valueOut) | 
					
						
							| 
									
										
										
										
											2010-06-04 19:56:34 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   if (line[0] == '#') | 
					
						
							|  |  |  |     return; | 
					
						
							| 
									
										
										
										
											2010-06-04 19:56:34 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   size_t firstEquals = line.find("=", 0); | 
					
						
							| 
									
										
										
										
											2010-06-04 19:56:34 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   if (firstEquals != std::string::npos) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     // Yes, a valid line!
 | 
					
						
							|  |  |  |     *keyOut = StripSpaces(line.substr(0, firstEquals)); | 
					
						
							| 
									
										
										
										
											2014-05-14 20:36:57 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |     if (valueOut) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       *valueOut = StripQuotes(StripSpaces(line.substr(firstEquals + 1, std::string::npos))); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2010-06-04 19:56:34 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2010-06-03 18:05:08 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-08 18:50:37 +13:00
										 |  |  | const std::string& IniFile::NULL_STRING = ""; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void IniFile::Section::Set(const std::string& key, const std::string& newValue) | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   auto it = values.find(key); | 
					
						
							|  |  |  |   if (it != values.end()) | 
					
						
							|  |  |  |     it->second = newValue; | 
					
						
							|  |  |  |   else | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     values[key] = newValue; | 
					
						
							|  |  |  |     keys_order.push_back(key); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2010-06-03 18:05:08 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  | void IniFile::Section::Set(const std::string& key, const std::string& newValue, | 
					
						
							|  |  |  |                            const std::string& defaultValue) | 
					
						
							| 
									
										
										
										
											2010-06-05 05:30:23 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   if (newValue != defaultValue) | 
					
						
							|  |  |  |     Set(key, newValue); | 
					
						
							|  |  |  |   else | 
					
						
							|  |  |  |     Delete(key); | 
					
						
							| 
									
										
										
										
											2010-06-05 05:30:23 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-08 18:50:37 +13:00
										 |  |  | void IniFile::Section::Set(const std::string& key, const std::vector<std::string>& newValues) | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   std::string temp; | 
					
						
							|  |  |  |   // Join the strings with ,
 | 
					
						
							|  |  |  |   for (const std::string& value : newValues) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     temp = value + ","; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   // remove last ,
 | 
					
						
							|  |  |  |   temp.resize(temp.length() - 1); | 
					
						
							|  |  |  |   Set(key, temp); | 
					
						
							| 
									
										
										
										
											2010-06-04 19:56:34 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2010-06-03 18:05:08 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  | bool IniFile::Section::Get(const std::string& key, std::string* value, | 
					
						
							|  |  |  |                            const std::string& defaultValue) const | 
					
						
							| 
									
										
										
										
											2013-09-06 19:56:19 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   auto it = values.find(key); | 
					
						
							|  |  |  |   if (it != values.end()) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     *value = it->second; | 
					
						
							|  |  |  |     return true; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   else if (&defaultValue != &NULL_STRING) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     *value = defaultValue; | 
					
						
							|  |  |  |     return true; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return false; | 
					
						
							| 
									
										
										
										
											2013-09-06 19:56:19 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-19 22:22:28 -04:00
										 |  |  | bool IniFile::Section::Get(const std::string& key, std::vector<std::string>* out) const | 
					
						
							| 
									
										
										
										
											2010-06-04 19:56:34 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   std::string temp; | 
					
						
							|  |  |  |   bool retval = Get(key, &temp); | 
					
						
							|  |  |  |   if (!retval || temp.empty()) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     return false; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // ignore starting comma, if any
 | 
					
						
							|  |  |  |   size_t subStart = temp.find_first_not_of(","); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // split by comma
 | 
					
						
							|  |  |  |   while (subStart != std::string::npos) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     // Find next comma
 | 
					
						
							|  |  |  |     size_t subEnd = temp.find(',', subStart); | 
					
						
							|  |  |  |     if (subStart != subEnd) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       // take from first char until next comma
 | 
					
						
							|  |  |  |       out->push_back(StripSpaces(temp.substr(subStart, subEnd - subStart))); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // Find the next non-comma char
 | 
					
						
							|  |  |  |     subStart = temp.find_first_not_of(",", subEnd); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return true; | 
					
						
							| 
									
										
										
										
											2010-06-04 19:56:34 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2010-06-03 18:05:08 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-19 22:22:28 -04:00
										 |  |  | bool IniFile::Section::Get(const std::string& key, int* value, int defaultValue) const | 
					
						
							| 
									
										
										
										
											2010-06-04 19:56:34 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   std::string temp; | 
					
						
							|  |  |  |   bool retval = Get(key, &temp); | 
					
						
							| 
									
										
										
										
											2015-08-19 22:22:28 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   if (retval && TryParse(temp, value)) | 
					
						
							|  |  |  |     return true; | 
					
						
							| 
									
										
										
										
											2015-08-19 22:22:28 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   *value = defaultValue; | 
					
						
							|  |  |  |   return false; | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-19 22:22:28 -04:00
										 |  |  | bool IniFile::Section::Get(const std::string& key, u32* value, u32 defaultValue) const | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   std::string temp; | 
					
						
							|  |  |  |   bool retval = Get(key, &temp); | 
					
						
							| 
									
										
										
										
											2015-08-19 22:22:28 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   if (retval && TryParse(temp, value)) | 
					
						
							|  |  |  |     return true; | 
					
						
							| 
									
										
										
										
											2015-08-19 22:22:28 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   *value = defaultValue; | 
					
						
							|  |  |  |   return false; | 
					
						
							| 
									
										
										
										
											2010-06-04 19:56:34 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2010-06-03 18:05:08 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-19 22:22:28 -04:00
										 |  |  | bool IniFile::Section::Get(const std::string& key, bool* value, bool defaultValue) const | 
					
						
							| 
									
										
										
										
											2010-06-04 19:56:34 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   std::string temp; | 
					
						
							|  |  |  |   bool retval = Get(key, &temp); | 
					
						
							| 
									
										
										
										
											2015-08-19 22:22:28 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   if (retval && TryParse(temp, value)) | 
					
						
							|  |  |  |     return true; | 
					
						
							| 
									
										
										
										
											2015-08-19 22:22:28 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   *value = defaultValue; | 
					
						
							|  |  |  |   return false; | 
					
						
							| 
									
										
										
										
											2010-06-04 19:56:34 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2010-06-03 18:05:08 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-19 22:22:28 -04:00
										 |  |  | bool IniFile::Section::Get(const std::string& key, float* value, float defaultValue) const | 
					
						
							| 
									
										
										
										
											2010-06-04 19:56:34 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   std::string temp; | 
					
						
							|  |  |  |   bool retval = Get(key, &temp); | 
					
						
							| 
									
										
										
										
											2015-08-19 22:22:28 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   if (retval && TryParse(temp, value)) | 
					
						
							|  |  |  |     return true; | 
					
						
							| 
									
										
										
										
											2015-08-19 22:22:28 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   *value = defaultValue; | 
					
						
							|  |  |  |   return false; | 
					
						
							| 
									
										
										
										
											2010-06-04 19:56:34 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-19 22:22:28 -04:00
										 |  |  | bool IniFile::Section::Get(const std::string& key, double* value, double defaultValue) const | 
					
						
							| 
									
										
										
										
											2010-06-04 19:56:34 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   std::string temp; | 
					
						
							|  |  |  |   bool retval = Get(key, &temp); | 
					
						
							| 
									
										
										
										
											2015-08-19 22:22:28 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   if (retval && TryParse(temp, value)) | 
					
						
							|  |  |  |     return true; | 
					
						
							| 
									
										
										
										
											2015-08-19 22:22:28 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   *value = defaultValue; | 
					
						
							|  |  |  |   return false; | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-08 18:50:37 +13:00
										 |  |  | bool IniFile::Section::Exists(const std::string& key) const | 
					
						
							| 
									
										
										
										
											2009-03-20 11:51:22 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   return values.find(key) != values.end(); | 
					
						
							| 
									
										
										
										
											2010-06-04 19:56:34 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-08 18:50:37 +13:00
										 |  |  | bool IniFile::Section::Delete(const std::string& key) | 
					
						
							| 
									
										
										
										
											2010-06-05 05:30:23 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   auto it = values.find(key); | 
					
						
							|  |  |  |   if (it == values.end()) | 
					
						
							|  |  |  |     return false; | 
					
						
							| 
									
										
										
										
											2013-09-06 19:56:19 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   values.erase(it); | 
					
						
							|  |  |  |   keys_order.erase(std::find(keys_order.begin(), keys_order.end(), key)); | 
					
						
							|  |  |  |   return true; | 
					
						
							| 
									
										
										
										
											2010-06-05 05:30:23 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2010-06-03 18:05:08 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-06-04 19:56:34 +00:00
										 |  |  | // IniFile
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-08 18:50:37 +13:00
										 |  |  | const IniFile::Section* IniFile::GetSection(const std::string& sectionName) const | 
					
						
							| 
									
										
										
										
											2010-06-04 19:56:34 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   for (const Section& sect : sections) | 
					
						
							|  |  |  |     if (!strcasecmp(sect.name.c_str(), sectionName.c_str())) | 
					
						
							|  |  |  |       return (&(sect)); | 
					
						
							|  |  |  |   return nullptr; | 
					
						
							| 
									
										
										
										
											2010-06-03 18:05:08 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-08 18:50:37 +13:00
										 |  |  | IniFile::Section* IniFile::GetSection(const std::string& sectionName) | 
					
						
							| 
									
										
										
										
											2010-06-03 18:05:08 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   for (Section& sect : sections) | 
					
						
							|  |  |  |     if (!strcasecmp(sect.name.c_str(), sectionName.c_str())) | 
					
						
							|  |  |  |       return (&(sect)); | 
					
						
							|  |  |  |   return nullptr; | 
					
						
							| 
									
										
										
										
											2010-06-04 19:56:34 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2010-06-03 18:05:08 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-08 18:50:37 +13:00
										 |  |  | IniFile::Section* IniFile::GetOrCreateSection(const std::string& sectionName) | 
					
						
							| 
									
										
										
										
											2010-06-04 19:56:34 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   Section* section = GetSection(sectionName); | 
					
						
							|  |  |  |   if (!section) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     sections.emplace_back(sectionName); | 
					
						
							|  |  |  |     section = §ions.back(); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   return section; | 
					
						
							| 
									
										
										
										
											2010-06-04 19:56:34 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2010-06-03 18:05:08 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-08 18:50:37 +13:00
										 |  |  | bool IniFile::DeleteSection(const std::string& sectionName) | 
					
						
							| 
									
										
										
										
											2010-06-04 19:56:34 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   Section* s = GetSection(sectionName); | 
					
						
							|  |  |  |   if (!s) | 
					
						
							|  |  |  |     return false; | 
					
						
							|  |  |  |   for (auto iter = sections.begin(); iter != sections.end(); ++iter) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     if (&(*iter) == s) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       sections.erase(iter); | 
					
						
							|  |  |  |       return true; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   return false; | 
					
						
							| 
									
										
										
										
											2009-03-20 11:51:22 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-08 18:50:37 +13:00
										 |  |  | bool IniFile::Exists(const std::string& sectionName, const std::string& key) const | 
					
						
							| 
									
										
										
										
											2010-06-04 19:56:34 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   const Section* section = GetSection(sectionName); | 
					
						
							|  |  |  |   if (!section) | 
					
						
							|  |  |  |     return false; | 
					
						
							|  |  |  |   return section->Exists(key); | 
					
						
							| 
									
										
										
										
											2010-06-04 19:56:34 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  | void IniFile::SetLines(const std::string& sectionName, const std::vector<std::string>& lines) | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   Section* section = GetOrCreateSection(sectionName); | 
					
						
							|  |  |  |   section->lines = lines; | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2009-01-28 16:09:08 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-08 18:50:37 +13:00
										 |  |  | bool IniFile::DeleteKey(const std::string& sectionName, const std::string& key) | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   Section* section = GetSection(sectionName); | 
					
						
							|  |  |  |   if (!section) | 
					
						
							|  |  |  |     return false; | 
					
						
							|  |  |  |   return section->Delete(key); | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-06-03 18:05:08 +00:00
										 |  |  | // Return a list of all keys in a section
 | 
					
						
							| 
									
										
										
										
											2014-03-15 03:29:53 +01:00
										 |  |  | bool IniFile::GetKeys(const std::string& sectionName, std::vector<std::string>* keys) const | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   const Section* section = GetSection(sectionName); | 
					
						
							|  |  |  |   if (!section) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     return false; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   *keys = section->keys_order; | 
					
						
							|  |  |  |   return true; | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-06-03 18:05:08 +00:00
										 |  |  | // Return a list of all lines in a section
 | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  | bool IniFile::GetLines(const std::string& sectionName, std::vector<std::string>* lines, | 
					
						
							|  |  |  |                        const bool remove_comments) const | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   lines->clear(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const Section* section = GetSection(sectionName); | 
					
						
							|  |  |  |   if (!section) | 
					
						
							|  |  |  |     return false; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   for (std::string line : section->lines) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     line = StripSpaces(line); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (remove_comments) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       size_t commentPos = line.find('#'); | 
					
						
							|  |  |  |       if (commentPos == 0) | 
					
						
							|  |  |  |       { | 
					
						
							|  |  |  |         continue; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       if (commentPos != std::string::npos) | 
					
						
							|  |  |  |       { | 
					
						
							|  |  |  |         line = StripSpaces(line.substr(0, commentPos)); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     lines->push_back(line); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return true; | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-06-03 18:05:08 +00:00
										 |  |  | void IniFile::SortSections() | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   sections.sort(); | 
					
						
							| 
									
										
										
										
											2010-06-03 18:05:08 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-08 18:50:37 +13:00
										 |  |  | bool IniFile::Load(const std::string& filename, bool keep_current_data) | 
					
						
							| 
									
										
										
										
											2010-06-03 18:05:08 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   if (!keep_current_data) | 
					
						
							|  |  |  |     sections.clear(); | 
					
						
							|  |  |  |   // first section consists of the comments before the first real section
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // Open file
 | 
					
						
							|  |  |  |   std::ifstream in; | 
					
						
							|  |  |  |   OpenFStream(in, filename, std::ios::in); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (in.fail()) | 
					
						
							|  |  |  |     return false; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   Section* current_section = nullptr; | 
					
						
							|  |  |  |   bool first_line = true; | 
					
						
							|  |  |  |   while (!in.eof()) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     std::string line; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (!std::getline(in, line)) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       if (in.eof()) | 
					
						
							|  |  |  |         return true; | 
					
						
							|  |  |  |       else | 
					
						
							|  |  |  |         return false; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // Skips the UTF-8 BOM at the start of files. Notepad likes to add this.
 | 
					
						
							|  |  |  |     if (first_line && line.substr(0, 3) == "\xEF\xBB\xBF") | 
					
						
							|  |  |  |       line = line.substr(3); | 
					
						
							|  |  |  |     first_line = false; | 
					
						
							| 
									
										
										
										
											2015-06-05 18:32:58 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-06-03 18:05:08 +00:00
										 |  |  | #ifndef _WIN32
 | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |     // Check for CRLF eol and convert it to LF
 | 
					
						
							|  |  |  |     if (!line.empty() && line.at(line.size() - 1) == '\r') | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       line.erase(line.size() - 1); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2010-06-03 18:05:08 +00:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |     if (line.size() > 0) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       if (line[0] == '[') | 
					
						
							|  |  |  |       { | 
					
						
							|  |  |  |         size_t endpos = line.find("]"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (endpos != std::string::npos) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |           // New section!
 | 
					
						
							|  |  |  |           std::string sub = line.substr(1, endpos - 1); | 
					
						
							|  |  |  |           current_section = GetOrCreateSection(sub); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       else | 
					
						
							|  |  |  |       { | 
					
						
							|  |  |  |         if (current_section) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |           std::string key, value; | 
					
						
							|  |  |  |           ParseLine(line, &key, &value); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           // Lines starting with '$', '*' or '+' are kept verbatim.
 | 
					
						
							|  |  |  |           // Kind of a hack, but the support for raw lines inside an
 | 
					
						
							|  |  |  |           // INI is a hack anyway.
 | 
					
						
							|  |  |  |           if ((key == "" && value == "") || | 
					
						
							|  |  |  |               (line.size() >= 1 && (line[0] == '$' || line[0] == '+' || line[0] == '*'))) | 
					
						
							|  |  |  |             current_section->lines.push_back(line); | 
					
						
							|  |  |  |           else | 
					
						
							|  |  |  |             current_section->Set(key, value); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   in.close(); | 
					
						
							|  |  |  |   return true; | 
					
						
							| 
									
										
										
										
											2010-06-03 18:05:08 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-08 18:50:37 +13:00
										 |  |  | bool IniFile::Save(const std::string& filename) | 
					
						
							| 
									
										
										
										
											2010-06-03 18:05:08 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-06-24 10:43:46 +02:00
										 |  |  |   std::ofstream out; | 
					
						
							|  |  |  |   std::string temp = File::GetTempFilenameForAtomicWrite(filename); | 
					
						
							|  |  |  |   OpenFStream(out, temp, std::ios::out); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (out.fail()) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     return false; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   for (const Section& section : sections) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     if (section.keys_order.size() != 0 || section.lines.size() != 0) | 
					
						
							|  |  |  |       out << "[" << section.name << "]" << std::endl; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (section.keys_order.size() == 0) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       for (const std::string& s : section.lines) | 
					
						
							|  |  |  |         out << s << std::endl; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       for (const std::string& kvit : section.keys_order) | 
					
						
							|  |  |  |       { | 
					
						
							|  |  |  |         auto pair = section.values.find(kvit); | 
					
						
							|  |  |  |         out << pair->first << " = " << pair->second << std::endl; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   out.close(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return File::RenameSync(temp, filename); | 
					
						
							| 
									
										
										
										
											2010-06-03 18:05:08 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-06-04 19:56:34 +00:00
										 |  |  | // Unit test. TODO: Move to the real unit test framework.
 | 
					
						
							| 
									
										
										
										
											2010-06-03 18:05:08 +00:00
										 |  |  | /*
 | 
					
						
							|  |  |  |    int main() | 
					
						
							|  |  |  |    { | 
					
						
							|  |  |  |     IniFile ini; | 
					
						
							|  |  |  |     ini.Load("my.ini"); | 
					
						
							|  |  |  |     ini.Set("Hej", "A", "amaskdfl"); | 
					
						
							|  |  |  |     ini.Set("Mossa", "A", "amaskdfl"); | 
					
						
							|  |  |  |     ini.Set("Aissa", "A", "amaskdfl"); | 
					
						
							|  |  |  |     //ini.Read("my.ini");
 | 
					
						
							|  |  |  |     std::string x; | 
					
						
							|  |  |  |     ini.Get("Hej", "B", &x, "boo"); | 
					
						
							|  |  |  |     ini.DeleteKey("Mossa", "A"); | 
					
						
							|  |  |  |     ini.DeleteSection("Mossa"); | 
					
						
							|  |  |  |     ini.SortSections(); | 
					
						
							|  |  |  |     ini.Save("my.ini"); | 
					
						
							|  |  |  |     //UpdateVars(ini);
 | 
					
						
							|  |  |  |     return 0; | 
					
						
							|  |  |  |    } | 
					
						
							|  |  |  |  */ |