Files
itgmania212121/stepmania/src/IniFile.cpp
T

294 lines
6.9 KiB
C++
Raw Normal View History

2003-02-16 04:01:45 +00:00
#include "global.h"
/*
-----------------------------------------------------------------------------
2002-07-23 01:41:40 +00:00
Class: IniFile
2002-07-23 01:41:40 +00:00
Desc: See header.
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-03 10:52:42 +00:00
#include "IniFile.h"
2002-10-31 08:05:13 +00:00
#include "RageUtil.h"
#include "RageLog.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
{
LOG->Trace("INI: Reading '%s'",path.c_str() );
2002-10-31 03:54:50 +00:00
ifstream file(path);
if (!file.is_open())
2001-11-03 10:52:42 +00:00
{
LOG->Trace("INI: FAILED");
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
2003-05-22 19:35:57 +00:00
CString line, keyname;
2002-10-31 03:54:50 +00:00
while (getline(file, line))
2001-11-03 10:52:42 +00:00
{
LOG->Trace("Read line '%s'", line.c_str());
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-02-14 22:43:43 +00:00
StripCrnl(line);
LOG->Trace("Stripped: '%s'", line.c_str());
2003-02-14 22:43:43 +00:00
2003-01-05 08:36:33 +00:00
if (line.substr(0, 2) == "//" || line.substr(0) == "#")
continue; /* comment */
LOG->Trace("Not a comment");
2003-01-05 08:36:33 +00:00
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
{
2003-05-22 19:35:57 +00:00
keyname = line.substr(1, line.size()-2);
LOG->Trace("Key name '%s'", keyname.c_str());
2002-08-20 02:12:19 +00:00
}
else //if a value
{
int iEqualIndex = line.Find("=");
LOG->Trace("Val, %i", iEqualIndex );
2002-08-20 02:12:19 +00:00
if( iEqualIndex != -1 )
2001-11-03 10:52:42 +00:00
{
2003-05-22 19:35:57 +00:00
CString valuename = line.Left(iEqualIndex);
CString value = line.Right(line.GetLength()-valuename.GetLength()-1);
LOG->Trace("'%s' '%s' (key '%s')", valuename.c_str(), value.c_str(), keyname.c_str() );
2002-08-20 02:12:19 +00:00
SetValue(keyname,valuename,value);
2001-11-03 10:52:42 +00:00
}
}
}
LOG->Trace("INI: done");
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" );
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;
2003-04-25 00:27:30 +00:00
fprintf( fp, "[%s]\n", k->first.c_str() );
2002-10-24 23:08:18 +00:00
for (key::const_iterator i = k->second.begin(); i != k->second.end(); ++i)
2003-04-25 00:27:30 +00:00
fprintf( fp, "%s=%s\n", i->first.c_str(), i->second.c_str() );
2002-10-24 23:08:18 +00:00
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()
{
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 =
2003-05-22 19:35:57 +00:00
bool IniFile::GetValue(const CString &keyname, const CString &valuename, CString& value) 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())
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 =
2003-05-22 19:35:57 +00:00
bool IniFile::GetValueI(const CString &keyname, const CString &valuename, int& value) const
2002-05-20 08:59:37 +00:00
{
CString sValue;
2003-05-22 19:35:57 +00:00
if( !GetValue(keyname,valuename,sValue) )
2002-05-20 08:59:37 +00:00
return false;
2003-05-22 19:35:57 +00:00
sscanf( sValue.c_str(), "%d", &value );
return true;
}
bool IniFile::GetValueU(const CString &keyname, const CString &valuename, unsigned &value) const
{
CString sValue;
if( !GetValue(keyname,valuename,sValue) )
return false;
sscanf( sValue.c_str(), "%u", &value );
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 =
2003-05-22 19:35:57 +00:00
bool IniFile::GetValueF(const CString &keyname, const CString &valuename, float& value) const
2001-11-03 10:52:42 +00:00
{
2002-05-20 08:59:37 +00:00
CString sValue;
2003-05-22 19:35:57 +00:00
if( !GetValue(keyname,valuename,sValue) )
2002-05-20 08:59:37 +00:00
return false;
2003-05-22 19:35:57 +00:00
sscanf( sValue.c_str(), "%f", &value );
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 =
2003-05-22 19:35:57 +00:00
bool IniFile::GetValueB(const CString &keyname, const CString &valuename, bool& value) const
2001-11-03 10:52:42 +00:00
{
2002-05-20 08:59:37 +00:00
CString sValue;
2003-05-22 19:35:57 +00:00
if( !GetValue(keyname,valuename,sValue) )
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
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
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
}
2003-05-22 19:35:57 +00:00
bool IniFile::SetValueU(const CString &keyname, const CString &valuename, unsigned value, bool create)
{
return SetValue(keyname, valuename, ssprintf("%u",value), create);
}
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-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
}