| 
									
										
										
										
											2009-07-28 21:32:10 +00:00
										 |  |  | // Copyright (C) 2003 Dolphin Project.
 | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | // This program is free software: you can redistribute it and/or modify
 | 
					
						
							|  |  |  | // it under the terms of the GNU General Public License as published by
 | 
					
						
							|  |  |  | // the Free Software Foundation, version 2.0.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // This program is distributed in the hope that it will be useful,
 | 
					
						
							|  |  |  | // but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
					
						
							|  |  |  | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
					
						
							|  |  |  | // GNU General Public License 2.0 for more details.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // A copy of the GPL 2.0 should have been included with the program.
 | 
					
						
							|  |  |  | // If not, see http://www.gnu.org/licenses/
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Official SVN repository and contact information can be found at
 | 
					
						
							|  |  |  | // http://code.google.com/p/dolphin-emu/
 | 
					
						
							|  |  |  | // see IniFile.h
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <stdlib.h>
 | 
					
						
							|  |  |  | #include <stdio.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <string>
 | 
					
						
							|  |  |  | #include <vector>
 | 
					
						
							|  |  |  | #include <iostream>
 | 
					
						
							|  |  |  | #include <fstream>
 | 
					
						
							|  |  |  | #include <algorithm>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "StringUtil.h"
 | 
					
						
							|  |  |  | #include "IniFile.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | IniFile::IniFile() | 
					
						
							|  |  |  | {} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | IniFile::~IniFile() | 
					
						
							|  |  |  | {} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Section::Section() | 
					
						
							|  |  |  | 	: lines(), name(""), comment("") {} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Section::Section(const std::string& _name) | 
					
						
							|  |  |  | 	: lines(), name(_name), comment("") {} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Section::Section(const Section& other) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	name = other.name; | 
					
						
							|  |  |  | 	comment = other.comment; | 
					
						
							|  |  |  | 	lines = other.lines; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const Section* IniFile::GetSection(const char* sectionName) const | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	for (std::vector<Section>::const_iterator iter = sections.begin(); iter != sections.end(); ++iter) | 
					
						
							| 
									
										
										
										
											2009-03-20 12:23:05 +00:00
										 |  |  | 		if (!strcasecmp(iter->name.c_str(), sectionName)) | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 			return (&(*iter)); | 
					
						
							|  |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Section* IniFile::GetSection(const char* sectionName) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	for (std::vector<Section>::iterator iter = sections.begin(); iter != sections.end(); ++iter) | 
					
						
							| 
									
										
										
										
											2009-03-20 15:01:49 +00:00
										 |  |  | 		if (!strcasecmp(iter->name.c_str(), sectionName)) | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 			return (&(*iter)); | 
					
						
							|  |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Section* IniFile::GetOrCreateSection(const char* sectionName) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	Section* section = GetSection(sectionName); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (!section) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		sections.push_back(Section(sectionName)); | 
					
						
							|  |  |  | 		section = §ions[sections.size() - 1]; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return(section); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 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) | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			sections.erase(iter); | 
					
						
							|  |  |  | 			return true; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return false; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void IniFile::ParseLine(const std::string& line, std::string* keyOut, std::string* valueOut, std::string* commentOut) const | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2009-08-30 19:44:42 +00:00
										 |  |  | 	//
 | 
					
						
							|  |  |  | 	int FirstEquals = (int)line.find("=", 0); | 
					
						
							|  |  |  | 	int FirstCommentChar = -1; | 
					
						
							|  |  |  | 	// Comments
 | 
					
						
							|  |  |  | 	//if (FirstCommentChar < 0) {FirstCommentChar = (int)line.find(";", FirstEquals > 0 ? FirstEquals : 0);}
 | 
					
						
							|  |  |  | 	if (FirstCommentChar < 0) {FirstCommentChar = (int)line.find("#", FirstEquals > 0 ? FirstEquals : 0);} | 
					
						
							|  |  |  | 	if (FirstCommentChar < 0) {FirstCommentChar = (int)line.find("//", FirstEquals > 0 ? FirstEquals : 0);} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Allow preservation of spacing before comment
 | 
					
						
							|  |  |  | 	if (FirstCommentChar > 0) | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2009-08-30 19:44:42 +00:00
										 |  |  | 		while (line[FirstCommentChar - 1] == ' ' || line[FirstCommentChar - 1] == 9) // 9 == tab
 | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2009-08-30 19:44:42 +00:00
										 |  |  | 			FirstCommentChar--; | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-30 19:44:42 +00:00
										 |  |  | 	if ((FirstEquals >= 0) && ((FirstCommentChar < 0) || (FirstEquals < FirstCommentChar))) | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 	{ | 
					
						
							|  |  |  | 		// Yes, a valid line!
 | 
					
						
							| 
									
										
										
										
											2009-08-30 19:44:42 +00:00
										 |  |  | 		*keyOut = StripSpaces(line.substr(0, FirstEquals)); | 
					
						
							|  |  |  | 		if (commentOut) *commentOut = FirstCommentChar > 0 ? line.substr(FirstCommentChar) : std::string(""); | 
					
						
							|  |  |  | 		if (valueOut) *valueOut = StripQuotes(StripSpaces(line.substr(FirstEquals + 1, FirstCommentChar - FirstEquals - 1))); | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | std::string* IniFile::GetLine(Section* section, const char* key, std::string* valueOut, std::string* commentOut) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	for (std::vector<std::string>::iterator iter = section->lines.begin(); iter != section->lines.end(); ++iter) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		std::string& line = *iter; | 
					
						
							|  |  |  | 		std::string lineKey; | 
					
						
							|  |  |  | 		ParseLine(line, &lineKey, valueOut, commentOut); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-21 23:44:40 +00:00
										 |  |  | 		if (!strcasecmp(lineKey.c_str(), key)) | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 		{ | 
					
						
							|  |  |  | 			return &line; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-03-20 15:01:49 +00:00
										 |  |  | bool IniFile::Exists(const char* const sectionName, const char* key) const | 
					
						
							| 
									
										
										
										
											2009-03-20 11:51:22 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-03-20 15:01:49 +00:00
										 |  |  | 	const Section* const section = GetSection(sectionName); | 
					
						
							| 
									
										
										
										
											2009-03-20 11:51:22 +00:00
										 |  |  | 	if (!section) | 
					
						
							|  |  |  | 		return false; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for (std::vector<std::string>::const_iterator iter = section->lines.begin(); iter != section->lines.end(); ++iter) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		std::string lineKey; | 
					
						
							|  |  |  | 		ParseLine(*iter, &lineKey, NULL, NULL); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if (!strcasecmp(lineKey.c_str(), key)) | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			return true; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return false; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | void IniFile::SetLines(const char* sectionName, const std::vector<std::string> &lines) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	Section* section = GetOrCreateSection(sectionName); | 
					
						
							|  |  |  | 	section->lines.clear(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for (std::vector<std::string>::const_iterator iter = lines.begin(); iter != lines.end(); ++iter) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		section->lines.push_back(*iter); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2009-01-28 16:09:08 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | bool IniFile::DeleteKey(const char* sectionName, const char* key) | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	Section* section = GetSection(sectionName); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (!section) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		return false; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-28 16:09:08 +00:00
										 |  |  | 	std::string* line = GetLine(section, key, 0, 0); | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-28 16:09:08 +00:00
										 |  |  | 	for (std::vector<std::string>::iterator liter = section->lines.begin(); liter != section->lines.end(); ++liter) | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2009-01-28 16:09:08 +00:00
										 |  |  | 		if (line == &(*liter)) | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2009-01-28 16:09:08 +00:00
										 |  |  | 			section->lines.erase(liter); | 
					
						
							|  |  |  | 			return true; | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-28 16:09:08 +00:00
										 |  |  | 	return false; //shouldn't happen
 | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-20 03:13:22 +00:00
										 |  |  | // Return a list of all keys in a section
 | 
					
						
							| 
									
										
										
										
											2009-01-28 16:09:08 +00:00
										 |  |  | bool IniFile::GetKeys(const char* sectionName, std::vector<std::string>& keys) const | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2009-01-28 16:09:08 +00:00
										 |  |  | 	const Section* section = GetSection(sectionName); | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-28 16:09:08 +00:00
										 |  |  | 	if (!section) | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2009-01-28 16:09:08 +00:00
										 |  |  | 		return false; | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-28 16:09:08 +00:00
										 |  |  | 	keys.clear(); | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-28 16:09:08 +00:00
										 |  |  | 	for (std::vector<std::string>::const_iterator liter = section->lines.begin(); liter != section->lines.end(); ++liter) | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2009-01-28 16:09:08 +00:00
										 |  |  | 		std::string key; | 
					
						
							|  |  |  | 		ParseLine(*liter, &key, 0, 0); | 
					
						
							|  |  |  | 		keys.push_back(key); | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-28 16:09:08 +00:00
										 |  |  | 	return true; | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-20 03:13:22 +00:00
										 |  |  | // Return a list of all lines in a section
 | 
					
						
							| 
									
										
										
										
											2009-01-28 16:09:08 +00:00
										 |  |  | bool IniFile::GetLines(const char* sectionName, std::vector<std::string>& lines) const | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2009-01-28 16:09:08 +00:00
										 |  |  | 	const Section* section = GetSection(sectionName); | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 	if (!section) | 
					
						
							|  |  |  | 		return false; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-28 16:09:08 +00:00
										 |  |  | 	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
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2009-01-28 16:09:08 +00:00
										 |  |  | 		std::string line = StripSpaces(*iter); | 
					
						
							|  |  |  | 		int commentPos = (int)line.find('#'); | 
					
						
							|  |  |  | 		if (commentPos == 0) | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2009-01-28 16:09:08 +00:00
										 |  |  | 			continue; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if (commentPos != (int)std::string::npos) | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			line = StripSpaces(line.substr(0, commentPos)); | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2009-01-28 16:09:08 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		lines.push_back(line); | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-28 16:09:08 +00:00
										 |  |  | 	return true; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void IniFile::SortSections() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	std::sort(sections.begin(), sections.end()); | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | bool IniFile::Load(const char* filename) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2009-01-28 16:09:08 +00:00
										 |  |  | 	// Maximum number of letters in a line
 | 
					
						
							| 
									
										
										
										
											2009-08-30 19:44:42 +00:00
										 |  |  | 	static const int MAX_BYTES = 1024*32; | 
					
						
							| 
									
										
										
										
											2009-01-28 16:09:08 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 	sections.clear(); | 
					
						
							|  |  |  | 	sections.push_back(Section("")); | 
					
						
							| 
									
										
										
										
											2009-02-23 18:08:03 +00:00
										 |  |  | 	// first section consists of the comments before the first real section
 | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-28 16:09:08 +00:00
										 |  |  | 	// Open file
 | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 	std::ifstream in; | 
					
						
							|  |  |  | 	in.open(filename, std::ios::in); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-30 19:44:42 +00:00
										 |  |  | 	if (in.fail()) return false; | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	while (!in.eof()) | 
					
						
							|  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2009-01-28 16:09:08 +00:00
										 |  |  | 		char templine[MAX_BYTES]; | 
					
						
							|  |  |  | 		in.getline(templine, MAX_BYTES); | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 		std::string line = templine; | 
					
						
							| 
									
										
										
										
											2009-02-23 18:08:03 +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
 | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-30 19:44:42 +00:00
										 |  |  | 		if (in.eof()) break; | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00: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); | 
					
						
							|  |  |  | 					sections.push_back(Section(sub)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 					if (endpos + 1 < line.size()) | 
					
						
							|  |  |  | 					{ | 
					
						
							|  |  |  | 						sections[sections.size() - 1].comment = line.substr(endpos + 1); | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			else | 
					
						
							|  |  |  | 			{ | 
					
						
							|  |  |  | 				sections[sections.size() - 1].lines.push_back(line); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	in.close(); | 
					
						
							|  |  |  | 	return true; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | bool IniFile::Save(const char* filename) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	std::ofstream out; | 
					
						
							|  |  |  | 	out.open(filename, std::ios::out); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (out.fail()) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		return false; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for (std::vector<Section>::const_iterator iter = sections.begin(); iter != sections.end(); ++iter) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		const Section& section = *iter; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if (section.name != "") | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			out << "[" << section.name << "]" << section.comment << std::endl; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		for (std::vector<std::string>::const_iterator liter = section.lines.begin(); liter != section.lines.end(); ++liter) | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			std::string s = *liter; | 
					
						
							|  |  |  | 			out << s << std::endl; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	out.close(); | 
					
						
							|  |  |  | 	return true; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-28 16:09:08 +00:00
										 |  |  | void IniFile::Set(const char* sectionName, const char* key, const char* newValue) | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2009-01-28 16:09:08 +00:00
										 |  |  | 	Section* section = GetOrCreateSection(sectionName); | 
					
						
							|  |  |  | 	std::string value, comment; | 
					
						
							|  |  |  | 	std::string* line = GetLine(section, key, &value, &comment); | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-28 16:09:08 +00:00
										 |  |  | 	if (line) | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2009-01-28 16:09:08 +00:00
										 |  |  | 		// Change the value - keep the key and comment
 | 
					
						
							|  |  |  | 		*line = StripSpaces(key) + " = " + newValue + comment; | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2009-01-28 16:09:08 +00:00
										 |  |  | 	else | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2009-01-28 16:09:08 +00:00
										 |  |  | 		// The key did not already exist in this section - let's add it.
 | 
					
						
							|  |  |  | 		section->lines.push_back(std::string(key) + " = " + newValue); | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2009-01-28 16:09:08 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-25 10:33:09 +00:00
										 |  |  | void IniFile::Set(const char* sectionName, const char* key, const std::vector<std::string>& newValues)  | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	std::string temp; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Join the strings with , 
 | 
					
						
							|  |  |  | 	std::vector<std::string>::const_iterator it; | 
					
						
							|  |  |  | 	for (it = newValues.begin(); it != newValues.end(); ++it) { | 
					
						
							|  |  |  | 	 | 
					
						
							|  |  |  | 		temp = (*it) + ","; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// remove last ,
 | 
					
						
							|  |  |  | 	temp.resize(temp.length() - 1); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	Set(sectionName, key, temp.c_str()); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2009-01-28 16:09:08 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | void IniFile::Set(const char* sectionName, const char* key, u32 newValue) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	Set(sectionName, key, StringFromFormat("0x%08x", newValue).c_str()); | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-28 16:09:08 +00:00
										 |  |  | void IniFile::Set(const char* sectionName, const char* key, int newValue) | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2009-01-28 16:09:08 +00:00
										 |  |  | 	Set(sectionName, key, StringFromInt(newValue).c_str()); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-28 16:09:08 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | void IniFile::Set(const char* sectionName, const char* key, bool newValue) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	Set(sectionName, key, StringFromBool(newValue).c_str()); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | bool IniFile::Get(const char* sectionName, const char* key, std::string* value, const char* defaultValue) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	Section* section = GetSection(sectionName); | 
					
						
							| 
									
										
										
										
											2009-04-25 16:47:45 +00:00
										 |  |  | 	 | 
					
						
							| 
									
										
										
										
											2009-01-28 16:09:08 +00:00
										 |  |  | 	if (!section) | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2009-01-28 16:09:08 +00:00
										 |  |  | 		if (defaultValue) | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2009-01-28 16:09:08 +00:00
										 |  |  | 			*value = defaultValue; | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2009-01-28 16:09:08 +00:00
										 |  |  | 		return false; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	std::string* line = GetLine(section, key, value, 0); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (!line) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		if (defaultValue) | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2009-01-28 16:09:08 +00:00
										 |  |  | 			*value = defaultValue; | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2009-01-28 16:09:08 +00:00
										 |  |  | 		return false; | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return true; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-25 10:33:09 +00:00
										 |  |  | bool IniFile::Get(const char* sectionName, const char* key, std::vector<std::string>& values)  | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	std::string temp; | 
					
						
							|  |  |  | 	bool retval = Get(sectionName, key, &temp, 0); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (! retval || temp.empty()) { | 
					
						
							|  |  |  | 		return false; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// ignore starting , if any
 | 
					
						
							| 
									
										
										
										
											2009-02-28 01:26:56 +00:00
										 |  |  | 	size_t subStart = temp.find_first_not_of(","); | 
					
						
							|  |  |  | 	size_t subEnd; | 
					
						
							| 
									
										
										
										
											2009-02-25 10:33:09 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// split by , 
 | 
					
						
							|  |  |  | 	while (subStart != std::string::npos) { | 
					
						
							|  |  |  | 		 | 
					
						
							|  |  |  | 		// Find next , 
 | 
					
						
							|  |  |  | 		subEnd = temp.find_first_of(",", subStart); | 
					
						
							|  |  |  | 		if (subStart != subEnd)  | 
					
						
							|  |  |  | 			// take from first char until next , 
 | 
					
						
							|  |  |  | 			values.push_back(StripSpaces(temp.substr(subStart, subEnd - subStart))); | 
					
						
							|  |  |  | 	 | 
					
						
							|  |  |  | 		// Find the next non , char
 | 
					
						
							|  |  |  | 		subStart = temp.find_first_not_of(",", subEnd); | 
					
						
							|  |  |  | 	}  | 
					
						
							|  |  |  | 	 | 
					
						
							|  |  |  | 	return true; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-28 16:09:08 +00:00
										 |  |  | bool IniFile::Get(const char* sectionName, const char* key, int* value, int defaultValue) | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2009-01-28 16:09:08 +00:00
										 |  |  | 	std::string temp; | 
					
						
							|  |  |  | 	bool retval = Get(sectionName, key, &temp, 0); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (retval && TryParseInt(temp.c_str(), value)) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		return true; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	*value = defaultValue; | 
					
						
							|  |  |  | 	return false; | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-28 16:09:08 +00:00
										 |  |  | bool IniFile::Get(const char* sectionName, const char* key, u32* value, u32 defaultValue) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	std::string temp; | 
					
						
							|  |  |  | 	bool retval = Get(sectionName, key, &temp, 0); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (retval && TryParseUInt(temp.c_str(), value)) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		return true; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	*value = defaultValue; | 
					
						
							|  |  |  | 	return false; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | bool IniFile::Get(const char* sectionName, const char* key, bool* value, bool defaultValue) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	std::string temp; | 
					
						
							|  |  |  | 	bool retval = Get(sectionName, key, &temp, 0); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (retval && TryParseBool(temp.c_str(), value)) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		return true; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	*value = defaultValue; | 
					
						
							|  |  |  | 	return false; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-03-28 08:57:34 +00:00
										 |  |  | // TODO: Keep this code below?
 | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +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; | 
					
						
							|  |  |  |    } | 
					
						
							|  |  |  |  */ |