Mercurial > vba-clojure
diff src/gtk/configfile.cpp @ 1:f9f4f1b99eed
importing src directory
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Sat, 03 Mar 2012 10:31:27 -0600 |
parents | |
children |
line wrap: on
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/gtk/configfile.cpp Sat Mar 03 10:31:27 2012 -0600 1.3 @@ -0,0 +1,262 @@ 1.4 +// VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. 1.5 +// Copyright (C) 1999-2003 Forgotten 1.6 +// Copyright (C) 2004 Forgotten and the VBA development team 1.7 + 1.8 +// This program is free software; you can redistribute it and/or modify 1.9 +// it under the terms of the GNU General Public License as published by 1.10 +// the Free Software Foundation; either version 2, or(at your option) 1.11 +// any later version. 1.12 +// 1.13 +// This program is distributed in the hope that it will be useful, 1.14 +// but WITHOUT ANY WARRANTY; without even the implied warranty of 1.15 +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 1.16 +// GNU General Public License for more details. 1.17 +// 1.18 +// You should have received a copy of the GNU General Public License 1.19 +// along with this program; if not, write to the Free Software Foundation, 1.20 +// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 1.21 + 1.22 +#include "configfile.h" 1.23 + 1.24 +#include <string.h> 1.25 +#include <glib.h> 1.26 + 1.27 +#include <glibmm/fileutils.h> 1.28 +#include <glibmm/iochannel.h> 1.29 + 1.30 +namespace VBA 1.31 +{ 1.32 +namespace Config 1.33 +{ 1.34 + 1.35 +using std::string; 1.36 +using Glib::IOChannel; 1.37 + 1.38 +Line::Line(const string & _rsKey, const string & _rsValue) : 1.39 + m_sKey(_rsKey), 1.40 + m_sValue(_rsValue) 1.41 +{ 1.42 +} 1.43 + 1.44 +Section::Section(const string & _rsName) : 1.45 + m_sName(_rsName) 1.46 +{ 1.47 +} 1.48 + 1.49 +bool Section::bKeyExists(const string & _rsKey) 1.50 +{ 1.51 + for (iterator it = begin(); it != end(); it++) 1.52 + { 1.53 + if (it->m_sKey == _rsKey) 1.54 + { 1.55 + return true; 1.56 + } 1.57 + } 1.58 + return false; 1.59 +} 1.60 + 1.61 +void Section::vSetKey(const string & _rsKey, const string & _rsValue) 1.62 +{ 1.63 + for (iterator it = begin(); it != end(); it++) 1.64 + { 1.65 + if (it->m_sKey == _rsKey) 1.66 + { 1.67 + it->m_sValue = _rsValue; 1.68 + return; 1.69 + } 1.70 + } 1.71 + push_back(Line(_rsKey, _rsValue)); 1.72 +} 1.73 + 1.74 +string Section::sGetKey(const string & _rsKey) const 1.75 +{ 1.76 + for (const_iterator it = begin(); it != end(); it++) 1.77 + { 1.78 + if (it->m_sKey == _rsKey) 1.79 + { 1.80 + return it->m_sValue; 1.81 + } 1.82 + } 1.83 + throw KeyNotFound(m_sName, _rsKey); 1.84 +} 1.85 + 1.86 +void Section::vRemoveKey(const string & _rsKey) 1.87 +{ 1.88 + for (iterator it = begin(); it != end(); it++) 1.89 + { 1.90 + if (it->m_sKey == _rsKey) 1.91 + { 1.92 + erase(it); 1.93 + return; 1.94 + } 1.95 + } 1.96 +} 1.97 + 1.98 +File::File() 1.99 +{ 1.100 +} 1.101 + 1.102 +File::File(const string & _rsFile) 1.103 +{ 1.104 + vLoad(_rsFile); 1.105 +} 1.106 + 1.107 +File::~File() 1.108 +{ 1.109 +} 1.110 + 1.111 +bool File::bSectionExists(const string & _rsName) 1.112 +{ 1.113 + for (iterator it = begin(); it != end(); it++) 1.114 + { 1.115 + if (it->sGetName() == _rsName) 1.116 + { 1.117 + return true; 1.118 + } 1.119 + } 1.120 + return false; 1.121 +} 1.122 + 1.123 +Section * File::poAddSection(const string & _rsName) 1.124 +{ 1.125 + Section * poSection = NULL; 1.126 + for (iterator it = begin(); it != end(); it++) 1.127 + { 1.128 + if (it->sGetName() == _rsName) 1.129 + { 1.130 + poSection = &(*it); 1.131 + } 1.132 + } 1.133 + if (poSection == NULL) 1.134 + { 1.135 + push_back(Section(_rsName)); 1.136 + poSection = &back(); 1.137 + } 1.138 + return poSection; 1.139 +} 1.140 + 1.141 +Section * File::poGetSection(const string & _rsName) 1.142 +{ 1.143 + for (iterator it = begin(); it != end(); it++) 1.144 + { 1.145 + if (it->sGetName() == _rsName) 1.146 + { 1.147 + return &(*it); 1.148 + } 1.149 + } 1.150 + throw SectionNotFound(_rsName); 1.151 +} 1.152 + 1.153 +void File::vRemoveSection(const string & _rsName) 1.154 +{ 1.155 + for (iterator it = begin(); it != end(); it++) 1.156 + { 1.157 + if (it->sGetName() == _rsName) 1.158 + { 1.159 + erase(it); 1.160 + return; 1.161 + } 1.162 + } 1.163 +} 1.164 + 1.165 +void File::vLoad(const string & _rsFile, 1.166 + bool _bAddSection, 1.167 + bool _bAddKey) 1.168 +{ 1.169 + string sBuffer = Glib::file_get_contents(_rsFile); 1.170 + Section * poSection = NULL; 1.171 + char ** lines = g_strsplit(sBuffer.c_str(), "\n", 0); 1.172 + char * tmp; 1.173 + int i = 0; 1.174 + while (lines[i]) 1.175 + { 1.176 + if (lines[i][0] == '[') 1.177 + { 1.178 + if ((tmp = strchr(lines[i], ']'))) 1.179 + { 1.180 + *tmp = '\0'; 1.181 + if (_bAddSection) 1.182 + { 1.183 + poSection = poAddSection(&lines[i][1]); 1.184 + } 1.185 + else 1.186 + { 1.187 + try 1.188 + { 1.189 + poSection = poGetSection(&lines[i][1]); 1.190 + } 1.191 + catch (...) 1.192 + { 1.193 + poSection = NULL; 1.194 + } 1.195 + } 1.196 + } 1.197 + } 1.198 + else if (lines[i][0] != '#' && poSection != NULL) 1.199 + { 1.200 + if ((tmp = strchr(lines[i], '='))) 1.201 + { 1.202 + *tmp = '\0'; 1.203 + tmp++; 1.204 + if (_bAddKey || poSection->bKeyExists(lines[i])) 1.205 + { 1.206 + poSection->vSetKey(lines[i], tmp); 1.207 + } 1.208 + } 1.209 + } 1.210 + i++; 1.211 + } 1.212 + g_strfreev(lines); 1.213 +} 1.214 + 1.215 +void File::vSave(const string & _rsFile) 1.216 +{ 1.217 + Glib::RefPtr<IOChannel> poFile = IOChannel::create_from_file(_rsFile, "w"); 1.218 + poFile->set_encoding(""); 1.219 + 1.220 + for (const_iterator poSection = begin(); 1.221 + poSection != end(); 1.222 + poSection++) 1.223 + { 1.224 + string sName = "[" + poSection->sGetName() + "]\n"; 1.225 + poFile->write(sName); 1.226 + 1.227 + for (Section::const_iterator poLine = poSection->begin(); 1.228 + poLine != poSection->end(); 1.229 + poLine++) 1.230 + { 1.231 + string sLine = poLine->m_sKey + "=" + poLine->m_sValue + "\n"; 1.232 + poFile->write(sLine); 1.233 + } 1.234 + poFile->write("\n"); 1.235 + } 1.236 +} 1.237 + 1.238 +void File::vClear() 1.239 +{ 1.240 + clear(); 1.241 +} 1.242 + 1.243 +std::ostream & operator<<(std::ostream & _roOut, const File & _roFile) 1.244 +{ 1.245 + for (File::const_iterator poSection = _roFile.begin(); 1.246 + poSection != _roFile.end(); 1.247 + poSection++) 1.248 + { 1.249 + string sName = "[" + poSection->sGetName() + "]\n"; 1.250 + _roOut << sName; 1.251 + 1.252 + for (Section::const_iterator poLine = poSection->begin(); 1.253 + poLine != poSection->end(); 1.254 + poLine++) 1.255 + { 1.256 + string sLine = poLine->m_sKey + "=" + poLine->m_sValue + "\n"; 1.257 + _roOut << sLine; 1.258 + } 1.259 + _roOut << "\n"; 1.260 + } 1.261 + return _roOut; 1.262 +} 1.263 + 1.264 +} // namespace Config 1.265 +} // namespace VBA