2002-07-23 01:41:40 +00:00
|
|
|
#include "stdafx.h"
|
2001-11-04 19:34:28 +00:00
|
|
|
/*
|
|
|
|
|
-----------------------------------------------------------------------------
|
2002-07-23 01:41:40 +00:00
|
|
|
Class: IniFile
|
2001-11-04 19:34:28 +00:00
|
|
|
|
2002-07-23 01:41:40 +00:00
|
|
|
Desc: See header.
|
2001-11-04 19:34:28 +00:00
|
|
|
|
2002-05-19 01:59:48 +00:00
|
|
|
Copyright (c) 2001-2002 by the person(s) listed below. All rights reserved.
|
2002-07-23 01:41:40 +00:00
|
|
|
Adam Clauss
|
|
|
|
|
Chris Danford
|
2001-11-04 19:34:28 +00:00
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
|
*/
|
2001-11-03 10:52:42 +00:00
|
|
|
|
|
|
|
|
#include "IniFile.h"
|
2002-10-31 08:05:13 +00:00
|
|
|
#include "RageUtil.h"
|
2002-10-31 03:54:50 +00:00
|
|
|
#include <fstream>
|
|
|
|
|
using namespace std;
|
2001-11-03 10:52:42 +00:00
|
|
|
|
|
|
|
|
//constructor, can specify pathname here instead of using SetPath later
|
|
|
|
|
IniFile::IniFile(CString inipath)
|
|
|
|
|
{
|
|
|
|
|
path = inipath;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//default destructor
|
|
|
|
|
IniFile::~IniFile()
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2002-10-31 03:54:50 +00:00
|
|
|
// sets path of ini file to read and write from
|
2001-11-03 10:52:42 +00:00
|
|
|
void IniFile::SetPath(CString newpath)
|
|
|
|
|
{
|
|
|
|
|
path = newpath;
|
|
|
|
|
}
|
|
|
|
|
|
2002-10-31 03:54:50 +00:00
|
|
|
// reads ini file specified using IniFile::SetPath()
|
|
|
|
|
// returns true if successful, false otherwise
|
2002-07-23 01:41:40 +00:00
|
|
|
bool IniFile::ReadFile()
|
2001-11-03 10:52:42 +00:00
|
|
|
{
|
2002-10-31 03:54:50 +00:00
|
|
|
ifstream file(path);
|
|
|
|
|
|
|
|
|
|
if (file.bad())
|
2001-11-03 10:52:42 +00:00
|
|
|
{
|
|
|
|
|
error = "Unable to open ini file.";
|
2002-07-23 01:41:40 +00:00
|
|
|
return 0;
|
2001-11-03 10:52:42 +00:00
|
|
|
}
|
2002-10-31 03:54:50 +00:00
|
|
|
|
2001-11-03 10:52:42 +00:00
|
|
|
CString keyname, valuename, value;
|
2002-07-23 01:41:40 +00:00
|
|
|
CString temp;
|
|
|
|
|
CString line;
|
2002-10-31 03:54:50 +00:00
|
|
|
while (getline(file, line))
|
2001-11-03 10:52:42 +00:00
|
|
|
{
|
2003-01-05 08:36:33 +00:00
|
|
|
|
|
|
|
|
if(line.size() >= 3 &&
|
|
|
|
|
line[0] == '\xef' &&
|
|
|
|
|
line[1] == '\xbb' &&
|
|
|
|
|
line[2] == '\xbf'
|
|
|
|
|
)
|
|
|
|
|
{
|
|
|
|
|
/* Obnoxious NT marker for UTF-8. Remove it. */
|
|
|
|
|
line.erase(0, 3);
|
|
|
|
|
}
|
|
|
|
|
|
2002-08-20 02:12:19 +00:00
|
|
|
if (line == "")
|
|
|
|
|
continue;
|
|
|
|
|
|
2003-01-05 08:36:33 +00:00
|
|
|
if (line.substr(0, 2) == "//" || line.substr(0) == "#")
|
|
|
|
|
continue; /* comment */
|
|
|
|
|
|
2002-08-20 02:12:19 +00:00
|
|
|
if (line[0] == '[' && line[line.GetLength()-1] == ']') //if a section heading
|
2001-11-03 10:52:42 +00:00
|
|
|
{
|
2002-08-20 02:12:19 +00:00
|
|
|
keyname = line;
|
2002-10-31 08:05:13 +00:00
|
|
|
TrimLeft(keyname, "[");
|
|
|
|
|
TrimRight(keyname, "]");
|
2002-08-20 02:12:19 +00:00
|
|
|
}
|
|
|
|
|
else //if a value
|
|
|
|
|
{
|
|
|
|
|
int iEqualIndex = line.Find("=");
|
|
|
|
|
if( iEqualIndex != -1 )
|
2001-11-03 10:52:42 +00:00
|
|
|
{
|
2002-08-20 02:12:19 +00:00
|
|
|
valuename = line.Left(iEqualIndex);
|
|
|
|
|
value = line.Right(line.GetLength()-valuename.GetLength()-1);
|
|
|
|
|
SetValue(keyname,valuename,value);
|
2001-11-03 10:52:42 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
2002-10-31 03:54:50 +00:00
|
|
|
// writes data stored in class to ini file
|
2001-11-03 10:52:42 +00:00
|
|
|
void IniFile::WriteFile()
|
|
|
|
|
{
|
2002-07-23 01:41:40 +00:00
|
|
|
FILE* fp = fopen( path, "w" );
|
2003-01-11 19:11:55 +00:00
|
|
|
|
|
|
|
|
if( fp == NULL )
|
|
|
|
|
return;
|
|
|
|
|
|
2002-10-24 23:08:18 +00:00
|
|
|
for (keymap::const_iterator k = keys.begin(); k != keys.end(); ++k)
|
2001-11-03 10:52:42 +00:00
|
|
|
{
|
2002-10-24 23:08:18 +00:00
|
|
|
if (k->second.empty())
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
fprintf( fp, "[%s]\n", k->first.GetString() );
|
|
|
|
|
|
|
|
|
|
for (key::const_iterator i = k->second.begin(); i != k->second.end(); ++i)
|
|
|
|
|
fprintf( fp, "%s=%s\n", i->first.GetString(), i->second.GetString() );
|
|
|
|
|
|
|
|
|
|
fprintf( fp, "\n" );
|
2001-11-03 10:52:42 +00:00
|
|
|
}
|
2002-07-23 01:41:40 +00:00
|
|
|
fclose( fp );
|
2001-11-03 10:52:42 +00:00
|
|
|
}
|
|
|
|
|
|
2002-10-31 03:54:50 +00:00
|
|
|
// deletes all stored ini data
|
2001-11-03 10:52:42 +00:00
|
|
|
void IniFile::Reset()
|
|
|
|
|
{
|
2002-10-24 19:53:13 +00:00
|
|
|
keys.clear();
|
2001-11-03 10:52:42 +00:00
|
|
|
}
|
|
|
|
|
|
2002-10-31 03:54:50 +00:00
|
|
|
// returns number of keys currently in the ini
|
2002-10-24 23:08:18 +00:00
|
|
|
int IniFile::GetNumKeys() const
|
2001-11-03 10:52:42 +00:00
|
|
|
{
|
2002-10-24 23:08:18 +00:00
|
|
|
return keys.size();
|
2001-11-03 10:52:42 +00:00
|
|
|
}
|
|
|
|
|
|
2002-10-31 03:54:50 +00:00
|
|
|
// returns number of values stored for specified key, or -1 if key not found
|
2002-10-24 23:08:18 +00:00
|
|
|
int IniFile::GetNumValues(const CString &keyname) const
|
2001-11-03 10:52:42 +00:00
|
|
|
{
|
2002-10-24 23:08:18 +00:00
|
|
|
keymap::const_iterator k = keys.find(keyname);
|
|
|
|
|
if (k == keys.end())
|
2001-11-03 10:52:42 +00:00
|
|
|
return -1;
|
2002-10-24 23:08:18 +00:00
|
|
|
|
|
|
|
|
return k->second.size();
|
2001-11-03 10:52:42 +00:00
|
|
|
}
|
|
|
|
|
|
2002-10-31 03:54:50 +00:00
|
|
|
// gets value of [keyname] valuename =
|
|
|
|
|
// overloaded to return CString, int, and double
|
2002-08-22 22:12:29 +00:00
|
|
|
bool IniFile::GetValue(const CString &keyname, const CString &valuename, CString& value)
|
2001-11-03 10:52:42 +00:00
|
|
|
{
|
2002-10-24 23:08:18 +00:00
|
|
|
keymap::const_iterator k = keys.find(keyname);
|
|
|
|
|
if (k == keys.end())
|
2002-07-23 01:41:40 +00:00
|
|
|
{
|
|
|
|
|
error = "Unable to locate specified key.";
|
2002-05-20 08:59:37 +00:00
|
|
|
return false;
|
2002-07-23 01:41:40 +00:00
|
|
|
}
|
2001-11-03 10:52:42 +00:00
|
|
|
|
2002-10-24 23:08:18 +00:00
|
|
|
key::const_iterator i = k->second.find(valuename);
|
|
|
|
|
|
|
|
|
|
if (i == k->second.end())
|
2002-07-23 01:41:40 +00:00
|
|
|
{
|
|
|
|
|
error = "Unable to locate specified value.";
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2002-10-24 23:08:18 +00:00
|
|
|
value = i->second;
|
2002-07-23 01:41:40 +00:00
|
|
|
return true;
|
2002-05-20 08:59:37 +00:00
|
|
|
}
|
|
|
|
|
|
2002-10-31 03:54:50 +00:00
|
|
|
// gets value of [keyname] valuename =
|
|
|
|
|
// overloaded to return CString, int, and double
|
2002-08-22 22:12:29 +00:00
|
|
|
bool IniFile::GetValueI(const CString &keyname, const CString &valuename, int& value)
|
2002-05-20 08:59:37 +00:00
|
|
|
{
|
|
|
|
|
CString sValue;
|
2002-07-23 01:41:40 +00:00
|
|
|
bool bSuccess = GetValue(keyname,valuename,sValue);
|
|
|
|
|
if( !bSuccess )
|
2002-05-20 08:59:37 +00:00
|
|
|
return false;
|
2002-07-23 01:41:40 +00:00
|
|
|
value = atoi(sValue);
|
2002-05-20 08:59:37 +00:00
|
|
|
return true;
|
2001-11-03 10:52:42 +00:00
|
|
|
}
|
|
|
|
|
|
2002-10-31 03:54:50 +00:00
|
|
|
// gets value of [keyname] valuename =
|
|
|
|
|
// overloaded to return CString, int, and double
|
2002-08-22 22:12:29 +00:00
|
|
|
bool IniFile::GetValueF(const CString &keyname, const CString &valuename, float& value)
|
2001-11-03 10:52:42 +00:00
|
|
|
{
|
2002-05-20 08:59:37 +00:00
|
|
|
CString sValue;
|
2002-07-23 01:41:40 +00:00
|
|
|
bool bSuccess = GetValue(keyname,valuename,sValue);
|
|
|
|
|
if( !bSuccess )
|
2002-05-20 08:59:37 +00:00
|
|
|
return false;
|
2002-07-23 01:41:40 +00:00
|
|
|
value = (float)atof(sValue);
|
2002-05-20 08:59:37 +00:00
|
|
|
return true;
|
2001-11-03 10:52:42 +00:00
|
|
|
}
|
|
|
|
|
|
2002-10-31 03:54:50 +00:00
|
|
|
// gets value of [keyname] valuename =
|
|
|
|
|
// overloaded to return CString, int, and double
|
2002-08-22 22:12:29 +00:00
|
|
|
bool IniFile::GetValueB(const CString &keyname, const CString &valuename, bool& value)
|
2001-11-03 10:52:42 +00:00
|
|
|
{
|
2002-05-20 08:59:37 +00:00
|
|
|
CString sValue;
|
2002-07-23 01:41:40 +00:00
|
|
|
bool bSuccess = GetValue(keyname,valuename,sValue);
|
|
|
|
|
if( !bSuccess )
|
2002-05-20 08:59:37 +00:00
|
|
|
return false;
|
2002-07-23 01:41:40 +00:00
|
|
|
value = atoi(sValue) != 0;
|
2002-05-20 08:59:37 +00:00
|
|
|
return true;
|
2001-11-03 10:52:42 +00:00
|
|
|
}
|
|
|
|
|
|
2002-10-31 03:54:50 +00:00
|
|
|
// sets value of [keyname] valuename =.
|
|
|
|
|
// specify the optional paramter as false (0) if you do not want it to create
|
|
|
|
|
// the key if it doesn't exist. Returns true if data entered, false otherwise
|
|
|
|
|
// overloaded to accept CString, int, and double
|
2002-08-22 22:12:29 +00:00
|
|
|
bool IniFile::SetValue(const CString &keyname, const CString &valuename, const CString &value, bool create)
|
2001-11-03 10:52:42 +00:00
|
|
|
{
|
2002-10-24 23:08:18 +00:00
|
|
|
if (!create && keys.find(keyname) == keys.end()) //if key doesn't exist
|
|
|
|
|
return false; // stop entering this key
|
2001-11-03 10:52:42 +00:00
|
|
|
|
2002-10-24 23:08:18 +00:00
|
|
|
// find value
|
|
|
|
|
if (!create && keys[keyname].find(valuename) == keys[keyname].end())
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
keys[keyname][valuename] = value;
|
|
|
|
|
return true;
|
2001-11-03 10:52:42 +00:00
|
|
|
}
|
|
|
|
|
|
2002-10-31 03:54:50 +00:00
|
|
|
// sets value of [keyname] valuename =.
|
|
|
|
|
// specify the optional paramter as false (0) if you do not want it to create
|
|
|
|
|
// the key if it doesn't exist. Returns true if data entered, false otherwise
|
|
|
|
|
// overloaded to accept CString, int, and double
|
2002-08-22 22:12:29 +00:00
|
|
|
bool IniFile::SetValueI(const CString &keyname, const CString &valuename, int value, bool create)
|
2001-11-03 10:52:42 +00:00
|
|
|
{
|
2002-11-02 21:45:27 +00:00
|
|
|
return SetValue(keyname, valuename, ssprintf("%d",value), create);
|
2001-11-03 10:52:42 +00:00
|
|
|
}
|
|
|
|
|
|
2002-10-31 03:54:50 +00:00
|
|
|
// sets value of [keyname] valuename =.
|
|
|
|
|
// specify the optional paramter as false (0) if you do not want it to create
|
|
|
|
|
// the key if it doesn't exist. Returns true if data entered, false otherwise
|
|
|
|
|
// overloaded to accept CString, int, and double
|
2002-08-22 22:12:29 +00:00
|
|
|
bool IniFile::SetValueF(const CString &keyname, const CString &valuename, float value, bool create)
|
2001-11-03 10:52:42 +00:00
|
|
|
{
|
2002-11-02 21:45:27 +00:00
|
|
|
return SetValue(keyname, valuename, ssprintf("%f",value), create);
|
2002-05-20 08:59:37 +00:00
|
|
|
}
|
|
|
|
|
|
2002-10-31 03:54:50 +00:00
|
|
|
// sets value of [keyname] valuename =.
|
|
|
|
|
// specify the optional paramter as false (0) if you do not want it to create
|
|
|
|
|
// the key if it doesn't exist. Returns true if data entered, false otherwise
|
|
|
|
|
// overloaded to accept CString, int, and double
|
2002-08-22 22:12:29 +00:00
|
|
|
bool IniFile::SetValueB(const CString &keyname, const CString &valuename, bool value, bool create)
|
2002-05-20 08:59:37 +00:00
|
|
|
{
|
2002-11-02 21:45:27 +00:00
|
|
|
return SetValue(keyname, valuename, ssprintf("%d",value), create);
|
2001-11-03 10:52:42 +00:00
|
|
|
}
|
|
|
|
|
|
2002-10-31 03:54:50 +00:00
|
|
|
// deletes specified value
|
|
|
|
|
// returns true if value existed and deleted, false otherwise
|
2002-08-22 22:12:29 +00:00
|
|
|
bool IniFile::DeleteValue(const CString &keyname, const CString &valuename)
|
2001-11-03 10:52:42 +00:00
|
|
|
{
|
2002-10-24 23:08:18 +00:00
|
|
|
keymap::iterator k = keys.find(keyname);
|
|
|
|
|
if (k == keys.end())
|
|
|
|
|
return false;
|
2001-11-03 10:52:42 +00:00
|
|
|
|
2002-10-24 23:08:18 +00:00
|
|
|
key::iterator i = k->second.find(valuename);
|
|
|
|
|
if(i == k->second.end())
|
|
|
|
|
return false;
|
2001-11-03 10:52:42 +00:00
|
|
|
|
2002-10-24 23:08:18 +00:00
|
|
|
k->second.erase(i);
|
|
|
|
|
return true;
|
2001-11-03 10:52:42 +00:00
|
|
|
}
|
|
|
|
|
|
2002-10-31 03:54:50 +00:00
|
|
|
// deletes specified key and all values contained within
|
|
|
|
|
// returns true if key existed and deleted, false otherwise
|
2002-10-24 23:08:18 +00:00
|
|
|
bool IniFile::DeleteKey(const CString &keyname)
|
2002-07-23 01:41:40 +00:00
|
|
|
{
|
2002-10-24 23:08:18 +00:00
|
|
|
keymap::iterator k = keys.find(keyname);
|
|
|
|
|
if (k == keys.end())
|
|
|
|
|
return false;
|
2001-11-03 10:52:42 +00:00
|
|
|
|
2002-10-24 23:08:18 +00:00
|
|
|
keys.erase(k);
|
|
|
|
|
return true;
|
2001-11-03 10:52:42 +00:00
|
|
|
}
|
|
|
|
|
|
2003-01-03 22:37:44 +00:00
|
|
|
const IniFile::key *IniFile::GetKey(const CString &keyname) const
|
2002-07-23 01:41:40 +00:00
|
|
|
{
|
2003-01-03 22:37:44 +00:00
|
|
|
keymap::const_iterator i = keys.find(keyname);
|
|
|
|
|
if(i == keys.end()) return NULL;
|
|
|
|
|
return &i->second;
|
|
|
|
|
}
|
|
|
|
|
|
2003-02-09 00:46:35 +00:00
|
|
|
void IniFile::SetValue(const CString &keyname, const key &key)
|
|
|
|
|
{
|
|
|
|
|
keys[keyname]=key;
|
|
|
|
|
}
|
|
|
|
|
|
2003-01-03 22:37:44 +00:00
|
|
|
void IniFile::RenameKey(const CString &from, const CString &to)
|
|
|
|
|
{
|
|
|
|
|
if(keys.find(from) == keys.end())
|
|
|
|
|
return;
|
|
|
|
|
if(keys.find(to) != keys.end())
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
keys[to] = keys[from];
|
|
|
|
|
keys.erase(from);
|
2002-07-23 01:41:40 +00:00
|
|
|
}
|