| 
									
										
										
										
											2013-04-17 23:09:55 -04:00
										 |  |  | // Copyright 2013 Dolphin Emulator Project
 | 
					
						
							|  |  |  | // Licensed under GPLv2
 | 
					
						
							|  |  |  | // Refer to the license.txt file included.
 | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // see IniFile.h
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-06-03 18:05:08 +00:00
										 |  |  | #include <stdlib.h>
 | 
					
						
							|  |  |  | #include <stdio.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <string>
 | 
					
						
							|  |  |  | #include <vector>
 | 
					
						
							|  |  |  | #include <iostream>
 | 
					
						
							|  |  |  | #include <fstream>
 | 
					
						
							|  |  |  | #include <algorithm>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-02-28 19:33:39 -06:00
										 |  |  | #include "FileUtil.h"
 | 
					
						
							| 
									
										
										
										
											2010-06-03 18:05:08 +00:00
										 |  |  | #include "StringUtil.h"
 | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | #include "IniFile.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-06-04 19:56:34 +00:00
										 |  |  | namespace { | 
					
						
							| 
									
										
										
										
											2010-06-03 18:05:08 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-06 19:56:19 +02:00
										 |  |  | void ParseLine(const std::string& line, std::string* keyOut, std::string* valueOut) | 
					
						
							| 
									
										
										
										
											2010-06-04 19:56:34 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2013-08-11 09:19:46 -04:00
										 |  |  | 	if (line[0] == '#') | 
					
						
							|  |  |  | 		return; | 
					
						
							| 
									
										
										
										
											2010-06-04 19:56:34 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-11 09:19:46 -04:00
										 |  |  | 	int FirstEquals = (int)line.find("=", 0); | 
					
						
							| 
									
										
										
										
											2010-06-04 19:56:34 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-11 09:19:46 -04:00
										 |  |  | 	if (FirstEquals >= 0) | 
					
						
							| 
									
										
										
										
											2010-06-04 19:56:34 +00:00
										 |  |  | 	{ | 
					
						
							|  |  |  | 		// Yes, a valid line!
 | 
					
						
							|  |  |  | 		*keyOut = StripSpaces(line.substr(0, FirstEquals)); | 
					
						
							| 
									
										
										
										
											2013-08-11 09:19:46 -04: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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-06-04 19:56:34 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void IniFile::Section::Set(const char* key, const char* newValue) | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2013-09-06 19:56:19 +02:00
										 |  |  | 	auto it = values.find(key); | 
					
						
							|  |  |  | 	if (it != values.end()) | 
					
						
							|  |  |  | 		it->second = newValue; | 
					
						
							| 
									
										
										
										
											2010-06-04 19:56:34 +00:00
										 |  |  | 	else | 
					
						
							|  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2013-09-06 19:56:19 +02:00
										 |  |  | 		values[key] = newValue; | 
					
						
							|  |  |  | 		keys_order.push_back(key); | 
					
						
							| 
									
										
										
										
											2010-06-04 19:56:34 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2010-06-03 18:05:08 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-06-05 05:30:23 +00:00
										 |  |  | void IniFile::Section::Set(const char* key, const std::string& newValue, const std::string& defaultValue) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	if (newValue != defaultValue) | 
					
						
							|  |  |  | 		Set(key, newValue); | 
					
						
							|  |  |  | 	else | 
					
						
							|  |  |  | 		Delete(key); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void IniFile::Section::Set(const char* key, const float newValue, const float defaultValue) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	if (newValue != defaultValue) | 
					
						
							|  |  |  | 		Set(key, newValue); | 
					
						
							|  |  |  | 	else | 
					
						
							|  |  |  | 		Delete(key); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-07-19 10:36:30 +00:00
										 |  |  | void IniFile::Section::Set(const char* key, int newValue, int defaultValue) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	if (newValue != defaultValue) | 
					
						
							|  |  |  | 		Set(key, newValue); | 
					
						
							|  |  |  | 	else | 
					
						
							|  |  |  | 		Delete(key); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void IniFile::Section::Set(const char* key, bool newValue, bool defaultValue) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	if (newValue != defaultValue) | 
					
						
							|  |  |  | 		Set(key, newValue); | 
					
						
							|  |  |  | 	else | 
					
						
							|  |  |  | 		Delete(key); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-06-04 19:56:34 +00:00
										 |  |  | void IniFile::Section::Set(const char* key, const std::vector<std::string>& newValues)  | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2010-06-04 19:56:34 +00:00
										 |  |  | 	std::string temp; | 
					
						
							|  |  |  | 	// Join the strings with , 
 | 
					
						
							|  |  |  | 	std::vector<std::string>::const_iterator it; | 
					
						
							|  |  |  | 	for (it = newValues.begin(); it != newValues.end(); ++it) | 
					
						
							| 
									
										
										
										
											2010-06-03 18:05:08 +00:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2010-06-04 19:56:34 +00:00
										 |  |  | 		temp = (*it) + ","; | 
					
						
							| 
									
										
										
										
											2010-06-03 18:05:08 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2010-06-04 19:56:34 +00:00
										 |  |  | 	// remove last ,
 | 
					
						
							|  |  |  | 	temp.resize(temp.length() - 1); | 
					
						
							|  |  |  | 	Set(key, temp.c_str()); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2010-06-03 18:05:08 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-06 19:56:19 +02:00
										 |  |  | bool IniFile::Section::Get(const char* key, std::string* value, const char* defaultValue) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	auto it = values.find(key); | 
					
						
							|  |  |  | 	if (it != values.end()) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		*value = it->second; | 
					
						
							|  |  |  | 		return true; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	else if (defaultValue) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		*value = defaultValue; | 
					
						
							|  |  |  | 		return true; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	else | 
					
						
							|  |  |  | 		return false; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | bool IniFile::Section::Get(const char* key, std::vector<std::string>& out) | 
					
						
							| 
									
										
										
										
											2010-06-04 19:56:34 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	std::string temp; | 
					
						
							|  |  |  | 	bool retval = Get(key, &temp, 0); | 
					
						
							|  |  |  | 	if (!retval || temp.empty()) | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2010-06-04 19:56:34 +00:00
										 |  |  | 		return false; | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2010-06-04 19:56:34 +00:00
										 |  |  | 	// ignore starting , if any
 | 
					
						
							|  |  |  | 	size_t subStart = temp.find_first_not_of(","); | 
					
						
							|  |  |  | 	size_t subEnd; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// split by , 
 | 
					
						
							|  |  |  | 	while (subStart != std::string::npos) { | 
					
						
							|  |  |  | 		 | 
					
						
							|  |  |  | 		// Find next , 
 | 
					
						
							|  |  |  | 		subEnd = temp.find_first_of(",", subStart); | 
					
						
							|  |  |  | 		if (subStart != subEnd)  | 
					
						
							|  |  |  | 			// take from first char until next , 
 | 
					
						
							| 
									
										
										
										
											2013-09-06 19:56:19 +02:00
										 |  |  | 			out.push_back(StripSpaces(temp.substr(subStart, subEnd - subStart))); | 
					
						
							| 
									
										
										
										
											2010-06-04 19:56:34 +00:00
										 |  |  | 	 | 
					
						
							|  |  |  | 		// Find the next non , char
 | 
					
						
							|  |  |  | 		subStart = temp.find_first_not_of(",", subEnd); | 
					
						
							|  |  |  | 	}  | 
					
						
							|  |  |  | 	 | 
					
						
							|  |  |  | 	return true; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2010-06-03 18:05:08 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-06-04 19:56:34 +00:00
										 |  |  | bool IniFile::Section::Get(const char* key, int* value, int defaultValue) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	std::string temp; | 
					
						
							|  |  |  | 	bool retval = Get(key, &temp, 0); | 
					
						
							| 
									
										
										
										
											2010-11-10 04:12:31 +00:00
										 |  |  | 	if (retval && TryParse(temp.c_str(), value)) | 
					
						
							| 
									
										
										
										
											2010-06-04 19:56:34 +00:00
										 |  |  | 		return true; | 
					
						
							|  |  |  | 	*value = defaultValue; | 
					
						
							| 
									
										
										
										
											2010-06-03 18:05:08 +00:00
										 |  |  | 	return false; | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-06-04 19:56:34 +00:00
										 |  |  | bool IniFile::Section::Get(const char* key, u32* value, u32 defaultValue) | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2010-06-04 19:56:34 +00:00
										 |  |  | 	std::string temp; | 
					
						
							|  |  |  | 	bool retval = Get(key, &temp, 0); | 
					
						
							| 
									
										
										
										
											2010-11-10 04:12:31 +00:00
										 |  |  | 	if (retval && TryParse(temp, value)) | 
					
						
							| 
									
										
										
										
											2010-06-04 19:56:34 +00:00
										 |  |  | 		return true; | 
					
						
							|  |  |  | 	*value = defaultValue; | 
					
						
							|  |  |  | 	return false; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2010-06-03 18:05:08 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-06-04 19:56:34 +00:00
										 |  |  | bool IniFile::Section::Get(const char* key, bool* value, bool defaultValue) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	std::string temp; | 
					
						
							|  |  |  | 	bool retval = Get(key, &temp, 0); | 
					
						
							| 
									
										
										
										
											2010-11-10 04:12:31 +00:00
										 |  |  | 	if (retval && TryParse(temp.c_str(), value)) | 
					
						
							| 
									
										
										
										
											2010-06-04 19:56:34 +00:00
										 |  |  | 		return true; | 
					
						
							|  |  |  | 	*value = defaultValue; | 
					
						
							|  |  |  | 	return false; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2010-06-03 18:05:08 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-06-04 19:56:34 +00:00
										 |  |  | bool IniFile::Section::Get(const char* key, float* value, float defaultValue) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	std::string temp; | 
					
						
							|  |  |  | 	bool retval = Get(key, &temp, 0); | 
					
						
							| 
									
										
										
										
											2010-11-10 04:12:31 +00:00
										 |  |  | 	if (retval && TryParse(temp.c_str(), value)) | 
					
						
							| 
									
										
										
										
											2010-06-04 19:56:34 +00:00
										 |  |  | 		return true; | 
					
						
							|  |  |  | 	*value = defaultValue; | 
					
						
							|  |  |  | 	return false; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | bool IniFile::Section::Get(const char* key, double* value, double defaultValue) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	std::string temp; | 
					
						
							|  |  |  | 	bool retval = Get(key, &temp, 0); | 
					
						
							| 
									
										
										
										
											2010-11-10 04:12:31 +00:00
										 |  |  | 	if (retval && TryParse(temp.c_str(), value)) | 
					
						
							| 
									
										
										
										
											2010-06-04 19:56:34 +00:00
										 |  |  | 		return true; | 
					
						
							|  |  |  | 	*value = defaultValue; | 
					
						
							|  |  |  | 	return false; | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-06-04 19:56:34 +00:00
										 |  |  | bool IniFile::Section::Exists(const char *key) const | 
					
						
							| 
									
										
										
										
											2009-03-20 11:51:22 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2013-09-06 19:56:19 +02:00
										 |  |  | 	return values.find(key) != values.end(); | 
					
						
							| 
									
										
										
										
											2010-06-04 19:56:34 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-06-05 05:30:23 +00:00
										 |  |  | bool IniFile::Section::Delete(const char *key) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2013-09-06 19:56:19 +02:00
										 |  |  | 	auto it = values.find(key); | 
					
						
							|  |  |  | 	if (it == values.end()) | 
					
						
							|  |  |  | 		return false; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	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
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const IniFile::Section* IniFile::GetSection(const char* sectionName) const | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	for (std::vector<Section>::const_iterator iter = sections.begin(); iter != sections.end(); ++iter) | 
					
						
							|  |  |  | 		if (!strcasecmp(iter->name.c_str(), sectionName)) | 
					
						
							|  |  |  | 			return (&(*iter)); | 
					
						
							| 
									
										
										
										
											2010-06-03 18:05:08 +00:00
										 |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-06-04 19:56:34 +00:00
										 |  |  | IniFile::Section* IniFile::GetSection(const char* sectionName) | 
					
						
							| 
									
										
										
										
											2010-06-03 18:05:08 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2010-06-04 19:56:34 +00:00
										 |  |  | 	for (std::vector<Section>::iterator iter = sections.begin(); iter != sections.end(); ++iter) | 
					
						
							|  |  |  | 		if (!strcasecmp(iter->name.c_str(), sectionName)) | 
					
						
							|  |  |  | 			return (&(*iter)); | 
					
						
							|  |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2010-06-03 18:05:08 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-06-04 19:56:34 +00:00
										 |  |  | IniFile::Section* IniFile::GetOrCreateSection(const char* sectionName) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	Section* section = GetSection(sectionName); | 
					
						
							| 
									
										
										
										
											2010-06-03 18:05:08 +00:00
										 |  |  | 	if (!section) | 
					
						
							|  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2010-06-04 19:56:34 +00:00
										 |  |  | 		sections.push_back(Section(sectionName)); | 
					
						
							|  |  |  | 		section = §ions[sections.size() - 1]; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return section; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2010-06-03 18:05:08 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-06-04 19:56:34 +00:00
										 |  |  | bool IniFile::DeleteSection(const char* sectionName) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	Section* s = GetSection(sectionName); | 
					
						
							|  |  |  | 	if (!s) | 
					
						
							|  |  |  | 		return false; | 
					
						
							|  |  |  | 	for (std::vector<Section>::iterator iter = sections.begin(); iter != sections.end(); ++iter) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		if (&(*iter) == s) | 
					
						
							| 
									
										
										
										
											2010-06-03 18:05:08 +00:00
										 |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2010-06-04 19:56:34 +00:00
										 |  |  | 			sections.erase(iter); | 
					
						
							| 
									
										
										
										
											2010-06-03 18:05:08 +00:00
										 |  |  | 			return true; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return false; | 
					
						
							| 
									
										
										
										
											2009-03-20 11:51:22 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-06-04 19:56:34 +00:00
										 |  |  | bool IniFile::Exists(const char* sectionName, const char* key) const | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	const Section* section = GetSection(sectionName); | 
					
						
							|  |  |  | 	if (!section) | 
					
						
							|  |  |  | 		return false; | 
					
						
							|  |  |  | 	return section->Exists(key); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-06-03 18:05:08 +00:00
										 |  |  | void IniFile::SetLines(const char* sectionName, const std::vector<std::string> &lines) | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2010-06-03 18:05:08 +00:00
										 |  |  | 	Section* section = GetOrCreateSection(sectionName); | 
					
						
							| 
									
										
										
										
											2013-09-06 19:56:19 +02:00
										 |  |  | 	section->lines = lines; | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2009-01-28 16:09:08 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-06-03 18:05:08 +00:00
										 |  |  | bool IniFile::DeleteKey(const char* sectionName, const char* key) | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2010-06-03 18:05:08 +00:00
										 |  |  | 	Section* section = GetSection(sectionName); | 
					
						
							|  |  |  | 	if (!section) | 
					
						
							|  |  |  | 		return false; | 
					
						
							| 
									
										
										
										
											2013-09-06 19:56:19 +02:00
										 |  |  | 	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
 | 
					
						
							|  |  |  | bool IniFile::GetKeys(const char* sectionName, std::vector<std::string>& keys) const | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2010-06-03 18:05:08 +00:00
										 |  |  | 	const Section* section = GetSection(sectionName); | 
					
						
							|  |  |  | 	if (!section) | 
					
						
							|  |  |  | 		return false; | 
					
						
							| 
									
										
										
										
											2013-09-06 19:56:19 +02:00
										 |  |  | 	keys = section->keys_order; | 
					
						
							| 
									
										
										
										
											2010-06-03 18:05:08 +00:00
										 |  |  | 	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
 | 
					
						
							| 
									
										
										
										
											2010-07-23 05:22:12 +00:00
										 |  |  | bool IniFile::GetLines(const char* sectionName, std::vector<std::string>& lines, const bool remove_comments) const | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2010-06-03 18:05:08 +00:00
										 |  |  | 	const Section* section = GetSection(sectionName); | 
					
						
							|  |  |  | 	if (!section) | 
					
						
							|  |  |  | 		return false; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	lines.clear(); | 
					
						
							|  |  |  | 	for (std::vector<std::string>::const_iterator iter = section->lines.begin(); iter != section->lines.end(); ++iter) | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2010-06-03 18:05:08 +00:00
										 |  |  | 		std::string line = StripSpaces(*iter); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-07-23 05:22:12 +00:00
										 |  |  | 		if (remove_comments) | 
					
						
							| 
									
										
										
										
											2010-06-03 18:05:08 +00:00
										 |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2010-07-23 05:22:12 +00:00
										 |  |  | 			int commentPos = (int)line.find('#'); | 
					
						
							|  |  |  | 			if (commentPos == 0) | 
					
						
							|  |  |  | 			{ | 
					
						
							|  |  |  | 				continue; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			if (commentPos != (int)std::string::npos) | 
					
						
							|  |  |  | 			{ | 
					
						
							|  |  |  | 				line = StripSpaces(line.substr(0, commentPos)); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2010-06-03 18:05:08 +00:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		lines.push_back(line); | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2010-06-03 18:05:08 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	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
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2010-06-03 18:05:08 +00:00
										 |  |  | 	std::sort(sections.begin(), sections.end()); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-06 19:56:19 +02:00
										 |  |  | bool IniFile::Load(const char* filename, bool keep_current_data) | 
					
						
							| 
									
										
										
										
											2010-06-03 18:05:08 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	// Maximum number of letters in a line
 | 
					
						
							|  |  |  | 	static const int MAX_BYTES = 1024*32; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-06 19:56:19 +02:00
										 |  |  | 	if (!keep_current_data) | 
					
						
							|  |  |  | 		sections.clear(); | 
					
						
							| 
									
										
										
										
											2010-06-03 18:05:08 +00:00
										 |  |  | 	// first section consists of the comments before the first real section
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Open file
 | 
					
						
							|  |  |  | 	std::ifstream in; | 
					
						
							| 
									
										
										
										
											2013-02-28 19:33:39 -06:00
										 |  |  | 	OpenFStream(in, filename, std::ios::in); | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-06-03 18:05:08 +00:00
										 |  |  | 	if (in.fail()) return false; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-06 19:56:19 +02:00
										 |  |  | 	Section* current_section = NULL; | 
					
						
							| 
									
										
										
										
											2010-06-03 18:05:08 +00:00
										 |  |  | 	while (!in.eof()) | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2010-06-03 18:05:08 +00:00
										 |  |  | 		char templine[MAX_BYTES]; | 
					
						
							|  |  |  | 		in.getline(templine, MAX_BYTES); | 
					
						
							|  |  |  | 		std::string line = templine; | 
					
						
							| 
									
										
										
										
											2013-09-06 19:56:19 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-06-03 18:05:08 +00:00
										 |  |  | #ifndef _WIN32
 | 
					
						
							|  |  |  | 		// Check for CRLF eol and convert it to LF
 | 
					
						
							|  |  |  | 		if (!line.empty() && line.at(line.size()-1) == '\r') | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			line.erase(line.size()-1); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if (line.size() > 0) | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2010-06-03 18:05:08 +00:00
										 |  |  | 			if (line[0] == '[') | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 			{ | 
					
						
							| 
									
										
										
										
											2010-06-03 18:05:08 +00:00
										 |  |  | 				size_t endpos = line.find("]"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				if (endpos != std::string::npos) | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 				{ | 
					
						
							| 
									
										
										
										
											2010-06-03 18:05:08 +00:00
										 |  |  | 					// New section!
 | 
					
						
							|  |  |  | 					std::string sub = line.substr(1, endpos - 1); | 
					
						
							| 
									
										
										
										
											2013-09-06 19:56:19 +02:00
										 |  |  | 					current_section = GetOrCreateSection(sub.c_str()); | 
					
						
							| 
									
										
										
										
											2010-06-03 18:05:08 +00:00
										 |  |  | 				} | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			else | 
					
						
							|  |  |  | 			{ | 
					
						
							| 
									
										
										
										
											2013-09-06 19:56:19 +02:00
										 |  |  | 				if (current_section) | 
					
						
							|  |  |  | 				{ | 
					
						
							|  |  |  | 					std::string key, value; | 
					
						
							|  |  |  | 					ParseLine(line, &key, &value); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-17 16:31:51 +02:00
										 |  |  | 					// Lines starting with '$', '*' or '+' are kept verbatim.
 | 
					
						
							|  |  |  | 					// Kind of a hack, but the support for raw lines inside an
 | 
					
						
							|  |  |  | 					// INI is a hack anyway.
 | 
					
						
							| 
									
										
										
										
											2013-09-06 19:56:19 +02:00
										 |  |  | 					if ((key == "" && value == "") | 
					
						
							| 
									
										
										
										
											2013-09-17 16:31:51 +02:00
										 |  |  | 					        || (line.size() >= 1 && (line[0] == '$' || line[0] == '+' || line[0] == '*'))) | 
					
						
							| 
									
										
										
										
											2013-09-06 19:56:19 +02:00
										 |  |  | 						current_section->lines.push_back(line.c_str()); | 
					
						
							|  |  |  | 					else | 
					
						
							|  |  |  | 						current_section->Set(key, value.c_str()); | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2010-06-03 18:05:08 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	in.close(); | 
					
						
							|  |  |  | 	return true; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | bool IniFile::Save(const char* filename) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	std::ofstream out; | 
					
						
							| 
									
										
										
										
											2013-02-28 19:33:39 -06:00
										 |  |  | 	OpenFStream(out, filename, std::ios::out); | 
					
						
							| 
									
										
										
										
											2010-06-03 18:05:08 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if (out.fail()) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		return false; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-02-24 19:20:17 -06:00
										 |  |  | 	for (auto iter = sections.begin(); iter != sections.end(); ++iter) | 
					
						
							| 
									
										
										
										
											2010-06-03 18:05:08 +00:00
										 |  |  | 	{ | 
					
						
							|  |  |  | 		const Section& section = *iter; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-06 19:56:19 +02:00
										 |  |  | 		if (section.keys_order.size() != 0 || section.lines.size() != 0) | 
					
						
							| 
									
										
										
										
											2013-08-11 10:55:06 -04:00
										 |  |  | 			out << "[" << section.name << "]" << std::endl; | 
					
						
							| 
									
										
										
										
											2010-06-03 18:05:08 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-06 19:56:19 +02:00
										 |  |  | 		if (section.keys_order.size() == 0) | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			for (auto liter = section.lines.begin(); liter != section.lines.end(); ++liter) | 
					
						
							|  |  |  | 			{ | 
					
						
							|  |  |  | 				std::string s = *liter; | 
					
						
							|  |  |  | 				out << s << std::endl; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		else | 
					
						
							| 
									
										
										
										
											2010-06-03 18:05:08 +00:00
										 |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2013-09-06 19:56:19 +02:00
										 |  |  | 			for (auto kvit = section.keys_order.begin(); kvit != section.keys_order.end(); ++kvit) | 
					
						
							|  |  |  | 			{ | 
					
						
							|  |  |  | 				auto pair = section.values.find(*kvit); | 
					
						
							|  |  |  | 				out << pair->first << " = " << pair->second << std::endl; | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2010-06-03 18:05:08 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	out.close(); | 
					
						
							| 
									
										
										
										
											2013-09-06 19:56:19 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-06-03 18:05:08 +00:00
										 |  |  | 	return true; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-28 16:09:08 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-06-03 18:05:08 +00:00
										 |  |  | bool IniFile::Get(const char* sectionName, const char* key, std::string* value, const char* defaultValue) | 
					
						
							| 
									
										
										
										
											2009-01-28 16:09:08 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2010-06-03 18:05:08 +00:00
										 |  |  | 	Section* section = GetSection(sectionName); | 
					
						
							| 
									
										
										
										
											2010-06-04 19:56:34 +00:00
										 |  |  | 	if (!section) { | 
					
						
							|  |  |  | 		if (defaultValue) { | 
					
						
							| 
									
										
										
										
											2010-06-03 18:05:08 +00:00
										 |  |  | 			*value = defaultValue; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		return false; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2010-06-04 19:56:34 +00:00
										 |  |  | 	return section->Get(key, value, defaultValue); | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-06-04 19:56:34 +00:00
										 |  |  | bool IniFile::Get(const char *sectionName, const char* key, std::vector<std::string>& values)  | 
					
						
							| 
									
										
										
										
											2009-02-25 10:33:09 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2010-06-04 19:56:34 +00:00
										 |  |  | 	Section *section = GetSection(sectionName); | 
					
						
							|  |  |  | 	if (!section) | 
					
						
							| 
									
										
										
										
											2010-06-03 18:05:08 +00:00
										 |  |  | 		return false; | 
					
						
							| 
									
										
										
										
											2010-06-04 19:56:34 +00:00
										 |  |  | 	return section->Get(key, values); | 
					
						
							| 
									
										
										
										
											2009-02-25 10:33:09 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-06-03 18:05:08 +00:00
										 |  |  | bool IniFile::Get(const char* sectionName, const char* key, int* value, int defaultValue) | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2010-06-04 19:56:34 +00:00
										 |  |  | 	Section *section = GetSection(sectionName); | 
					
						
							|  |  |  | 	if (!section) { | 
					
						
							|  |  |  | 		*value = defaultValue; | 
					
						
							|  |  |  | 		return false; | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		return section->Get(key, value, defaultValue); | 
					
						
							| 
									
										
										
										
											2010-06-03 18:05:08 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-06-03 18:05:08 +00:00
										 |  |  | bool IniFile::Get(const char* sectionName, const char* key, u32* value, u32 defaultValue) | 
					
						
							| 
									
										
										
										
											2009-01-28 16:09:08 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2010-06-04 19:56:34 +00:00
										 |  |  | 	Section *section = GetSection(sectionName); | 
					
						
							|  |  |  | 	if (!section) { | 
					
						
							|  |  |  | 		*value = defaultValue; | 
					
						
							|  |  |  | 		return false; | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		return section->Get(key, value, defaultValue); | 
					
						
							| 
									
										
										
										
											2010-06-03 18:05:08 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2009-01-28 16:09:08 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-06-03 18:05:08 +00:00
										 |  |  | bool IniFile::Get(const char* sectionName, const char* key, bool* value, bool defaultValue) | 
					
						
							| 
									
										
										
										
											2009-01-28 16:09:08 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2010-06-04 19:56:34 +00:00
										 |  |  | 	Section *section = GetSection(sectionName); | 
					
						
							|  |  |  | 	if (!section) { | 
					
						
							|  |  |  | 		*value = defaultValue; | 
					
						
							|  |  |  | 		return false; | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		return section->Get(key, value, defaultValue); | 
					
						
							| 
									
										
										
										
											2010-06-03 18:05:08 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2009-01-28 16:09:08 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											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; | 
					
						
							|  |  |  |    } | 
					
						
							|  |  |  |  */ |