scummvm/engine/registry.cpp

110 lines
2.9 KiB
C++
Raw Normal View History

2006-04-02 14:20:45 +00:00
/* Residual - Virtual machine to run LucasArts' 3D adventure games
*
* Residual is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the AUTHORS
* file distributed with this source distribution.
2006-04-02 14:20:45 +00:00
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
* This library 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
* Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*
* $URL$
* $Id$
*
*/
2003-08-15 18:00:22 +00:00
#include "common/sys.h"
#include "common/debug.h"
#include "engine/registry.h"
2005-01-01 12:27:57 +00:00
2003-08-15 18:00:22 +00:00
#include <cstdlib>
#include <cstring>
2003-08-15 18:00:22 +00:00
2004-12-31 22:05:26 +00:00
Registry *g_registry = NULL;
2004-12-09 23:55:43 +00:00
Registry::Registry() : _dirty(false) {
#ifndef __DC__
#ifdef WIN32
std::string filename = "residual.ini";
#else
std::string filename = std::string(std::getenv("HOME")) + "/.residualrc";
#endif
2003-08-15 18:00:22 +00:00
std::FILE *f = fopen(filename.c_str(), "r");
if (f != NULL) {
char line[1024];
while (!feof(f) && fgets(line, sizeof(line), f) != NULL) {
char *equals = std::strchr(line, '=');
char *newline = std::strchr(line, '\n');
if (newline != NULL)
*newline = '\0';
if (equals != NULL) {
std::string key = std::string(line, equals - line);
std::string val = std::string(equals + 1);
2004-12-09 23:55:43 +00:00
_settings[key] = val;
}
}
std::fclose(f);
}
#endif
2003-08-15 18:00:22 +00:00
}
const char *Registry::get(const char *key, const char *defval) const {
// GrimDataDir is an alias for DataDir for our purposes
if (!strcmp(key, "GrimDataDir"))
key = "DataDir";
2004-12-10 07:26:03 +00:00
Group::const_iterator i = _settings.find(key);
2004-12-09 23:55:43 +00:00
if (i == _settings.end())
return defval;
else
return i->second.c_str();
2003-08-15 18:00:22 +00:00
}
void Registry::set(const char *key, const char *val) {
// Hack: Don't save these, so we can run in good_times mode
// without new games being bogus.
if (strstr(key, "GrimLastSet") || strstr(key, "GrimMannyState"))
return;
2004-12-09 23:55:43 +00:00
_settings[key] = val;
_dirty = true;
2003-08-15 18:00:22 +00:00
}
void Registry::save() {
2004-12-09 23:55:43 +00:00
if (!_dirty)
return;
#ifndef __DC__
#ifdef WIN32
std::string filename = "residual.ini";
#else
std::string filename = std::string(std::getenv("HOME")) + "/.residualrc";
#endif
2003-08-15 18:00:22 +00:00
std::FILE *f = std::fopen(filename.c_str(), "w");
if (f == NULL) {
warning("Could not open registry file %s for writing\n",
filename.c_str());
return;
}
2003-08-15 18:00:22 +00:00
2004-12-10 07:26:03 +00:00
for (Group::iterator i = _settings.begin(); i != _settings.end(); i++)
std::fprintf(f, "%s=%s\n", i->first.c_str(), i->second.c_str());
2003-08-15 18:00:22 +00:00
std::fclose(f);
#endif
2004-12-09 23:55:43 +00:00
_dirty = false;
2003-08-15 18:00:22 +00:00
}