Files
itgmania212121/stepmania/src/IniFile.cpp
T

408 lines
8.8 KiB
C++
Raw Normal View History

2003-02-16 04:01:45 +00:00
#include "global.h"
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"
2003-10-02 02:03:29 +00:00
#include "RageFile.h"
2001-11-03 10:52:42 +00:00
2004-05-23 02:27:51 +00:00
bool IniFile::ReadFile( const CString &sPath )
2001-11-03 10:52:42 +00:00
{
2004-05-23 02:27:51 +00:00
m_sPath = sPath;
CHECKPOINT_M( ssprintf("Reading '%s'",m_sPath.c_str()) );
2002-10-31 03:54:50 +00:00
2003-12-04 08:32:35 +00:00
RageFile f;
2004-05-23 02:27:51 +00:00
if( !f.Open( m_sPath ) )
2001-11-03 10:52:42 +00:00
{
2004-05-23 02:27:51 +00:00
LOG->Trace( "Reading '%s' failed: %s", m_sPath.c_str(), f.GetError().c_str() );
m_sError = f.GetError();
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
CString keyname;
2004-04-18 23:06:56 +00:00
while( 1 )
2001-11-03 10:52:42 +00:00
{
2004-04-18 23:06:56 +00:00
CString line;
switch( f.GetLine(line) )
{
case -1:
2004-05-23 02:27:51 +00:00
m_sError = f.GetError();
2004-04-18 23:06:56 +00:00
return false;
case 0:
return true; /* eof */
}
2004-12-03 01:04:31 +00:00
utf8_remove_bom( line );
2003-01-05 08:36:33 +00:00
2004-05-23 02:27:51 +00:00
if( line == "" )
2002-08-20 02:12:19 +00:00
continue;
2004-05-23 02:27:51 +00:00
if( line.substr(0, 2) == "//" || line.substr(0) == "#" )
2003-01-05 08:36:33 +00:00
continue; /* comment */
2004-05-23 02:27:51 +00:00
if( line[0] == '[' && line[line.GetLength()-1] == ']' )
2001-11-03 10:52:42 +00:00
{
2004-05-23 02:27:51 +00:00
/* New section. */
2003-05-22 19:35:57 +00:00
keyname = line.substr(1, line.size()-2);
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
{
2003-05-22 19:35:57 +00:00
CString valuename = line.Left(iEqualIndex);
CString value = line.Right(line.GetLength()-valuename.GetLength()-1);
2002-08-20 02:12:19 +00:00
SetValue(keyname,valuename,value);
2001-11-03 10:52:42 +00:00
}
}
}
}
2004-05-23 02:27:51 +00:00
bool IniFile::WriteFile( const CString &sPath )
2001-11-03 10:52:42 +00:00
{
2003-12-04 08:32:35 +00:00
RageFile f;
2004-05-23 02:27:51 +00:00
if( !f.Open( sPath, RageFile::WRITE ) )
2003-12-04 08:32:35 +00:00
{
2004-05-23 02:27:51 +00:00
LOG->Trace( "Writing '%s' failed: %s", sPath.c_str(), f.GetError().c_str() );
m_sError = f.GetError();
2004-02-10 09:42:01 +00:00
return false;
2003-12-04 08:32:35 +00:00
}
2004-05-23 02:27:51 +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;
2004-06-06 20:01:19 +00:00
if( f.PutLine( ssprintf("[%s]", k->first.c_str()) ) == -1 )
{
m_sError = f.GetError();
return false;
}
2002-10-24 23:08:18 +00:00
2004-05-23 02:27:51 +00:00
for( key::const_iterator i = k->second.begin(); i != k->second.end(); ++i )
2003-12-04 08:39:08 +00:00
f.PutLine( ssprintf("%s=%s", i->first.c_str(), i->second.c_str()) );
2002-10-24 23:08:18 +00:00
2004-06-06 20:01:19 +00:00
if( f.PutLine( "" ) == -1 )
{
m_sError = f.GetError();
return false;
}
2001-11-03 10:52:42 +00:00
}
2004-02-10 09:42:01 +00:00
return true;
2001-11-03 10:52:42 +00:00
}
void IniFile::Reset()
{
keys.clear();
2001-11-03 10:52:42 +00:00
}
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
}
2004-05-23 02:27:51 +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);
2004-05-23 02:27:51 +00:00
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
}
2004-05-23 02:27:51 +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-05-20 08:59:37 +00:00
return false;
2001-11-03 10:52:42 +00:00
2002-10-24 23:08:18 +00:00
key::const_iterator i = k->second.find(valuename);
2004-05-23 02:27:51 +00:00
if( i == k->second.end() )
2002-07-23 01:41:40 +00:00
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
}
2003-10-02 02:03:29 +00:00
bool IniFile::GetValue(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;
}
2003-10-02 02:03:29 +00:00
bool IniFile::GetValue(const CString &keyname, const CString &valuename, unsigned &value) const
2003-05-22 19:35:57 +00:00
{
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
}
2003-10-02 02:03:29 +00:00
bool IniFile::GetValue(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;
2004-08-10 22:19:06 +00:00
value = strtof( sValue, NULL );
2002-05-20 08:59:37 +00:00
return true;
2001-11-03 10:52:42 +00:00
}
2004-05-23 02:27:51 +00:00
bool IniFile::GetValue( 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
}
2004-05-23 02:27:51 +00:00
bool IniFile::SetValue( const CString &keyname, const CString &valuename, const CString &value )
2001-11-03 10:52:42 +00:00
{
2002-10-24 23:08:18 +00:00
keys[keyname][valuename] = value;
return true;
2001-11-03 10:52:42 +00:00
}
2004-05-23 02:27:51 +00:00
bool IniFile::SetValue( const CString &keyname, const CString &valuename, int value )
2001-11-03 10:52:42 +00:00
{
2004-05-23 02:27:51 +00:00
return SetValue( keyname, valuename, ssprintf("%d",value) );
2001-11-03 10:52:42 +00:00
}
2004-05-23 02:27:51 +00:00
bool IniFile::SetValue( const CString &keyname, const CString &valuename, unsigned value )
2003-05-22 19:35:57 +00:00
{
2004-05-23 02:27:51 +00:00
return SetValue( keyname, valuename, ssprintf("%u",value) );
2003-05-22 19:35:57 +00:00
}
2004-05-23 02:27:51 +00:00
bool IniFile::SetValue( const CString &keyname, const CString &valuename, float value )
2001-11-03 10:52:42 +00:00
{
2004-05-23 02:27:51 +00:00
return SetValue( keyname, valuename, ssprintf("%f",value) );
2002-05-20 08:59:37 +00:00
}
2004-05-23 02:27:51 +00:00
bool IniFile::SetValue( const CString &keyname, const CString &valuename, bool value )
2002-05-20 08:59:37 +00:00
{
2004-05-23 02:27:51 +00:00
return SetValue( keyname, valuename, ssprintf("%d",value) );
2001-11-03 10:52:42 +00:00
}
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);
2004-05-23 02:27:51 +00:00
if( k == keys.end() )
2002-10-24 23:08:18 +00:00
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);
2004-05-23 02:27:51 +00:00
if( i == k->second.end() )
2002-10-24 23:08:18 +00:00
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-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);
2004-05-23 02:27:51 +00:00
if( k == keys.end() )
2002-10-24 23:08:18 +00:00
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);
2004-05-23 02:27:51 +00:00
if( i == keys.end() )
return NULL;
2003-01-03 22:37:44 +00:00
return &i->second;
}
2003-02-09 00:46:35 +00:00
void IniFile::SetValue(const CString &keyname, const key &key)
{
2004-05-23 02:27:51 +00:00
keys[keyname] = key;
2003-02-09 00:46:35 +00:00
}
2003-01-03 22:37:44 +00:00
void IniFile::RenameKey(const CString &from, const CString &to)
{
2004-05-23 02:27:51 +00:00
if( keys.find(from) == keys.end() )
2003-01-03 22:37:44 +00:00
return;
2004-05-23 02:27:51 +00:00
if( keys.find(to) != keys.end() )
2003-01-03 22:37:44 +00:00
return;
keys[to] = keys[from];
keys.erase(from);
2002-07-23 01:41:40 +00:00
}
2004-05-23 02:27:51 +00:00
bool IniFilePreserveOrder::ReadFile( const CString &sPath )
{
m_sPath = sPath;
CHECKPOINT_M( ssprintf("Reading '%s'",m_sPath.c_str()) );
RageFile f;
if( !f.Open( m_sPath ) )
{
LOG->Trace( "Reading '%s' failed: %s", m_sPath.c_str(), f.GetError().c_str() );
m_sError = f.GetError();
return 0;
}
CString keyname;
while( 1 )
{
CString line;
switch( f.GetLine(line) )
{
case -1:
m_sError = f.GetError();
return false;
case 0:
return true; /* eof */
}
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);
}
if( line == "" )
continue;
if( line.substr(0, 2) == "//" || line.substr(0) == "#" )
continue; /* comment */
if( line[0] == '[' && line[line.GetLength()-1] == ']' )
{
/* New section. */
keyname = line.substr(1, line.size()-2);
}
else //if a value
{
int iEqualIndex = line.Find("=");
if( iEqualIndex != -1 )
{
CString valuename = line.Left(iEqualIndex);
CString value = line.Right(line.GetLength()-valuename.GetLength()-1);
SetValue(keyname,valuename,value);
}
}
}
}
bool IniFilePreserveOrder::WriteFile( const CString &sPath )
{
RageFile f;
if( !f.Open( sPath, RageFile::WRITE ) )
{
LOG->Trace( "Writing '%s' failed: %s", sPath.c_str(), f.GetError().c_str() );
m_sError = f.GetError();
return false;
}
for( keymap::const_iterator k = keys.begin(); k != keys.end(); ++k )
{
if (k->second.empty())
continue;
if( f.PutLine( ssprintf("[%s]", k->first.c_str()) ) == -1 )
{
m_sError = f.GetError();
return false;
}
for( key::const_iterator i = k->second.begin(); i != k->second.end(); ++i )
f.PutLine( ssprintf("%s=%s", i->first.c_str(), i->second.c_str()) );
if( f.PutLine( "" ) == -1 )
{
m_sError = f.GetError();
return false;
}
}
return true;
}
bool IniFilePreserveOrder::SetValue( const CString &keyname, const CString &valuename, const CString &value )
{
int iIndexOfKey = -1;
for( unsigned i=0; i<keys.size(); i++ )
{
if( keys[i].first == keyname )
{
iIndexOfKey = i;
break;
}
}
if( iIndexOfKey == -1 )
{
iIndexOfKey = keys.size();
keys.resize( keys.size()+1 );
keys.back().first = keyname;
}
key &k = keys[iIndexOfKey].second;
int iIndexOfValue = -1;
for( unsigned i=0; i<k.size(); i++ )
{
if( k[i].first == valuename )
{
iIndexOfValue = i;
break;
}
}
if( iIndexOfValue == -1 )
{
iIndexOfValue = k.size();
k.resize( k.size()+1 );
k.back().first = valuename;
}
k[iIndexOfValue].second = value;
return true;
}
2004-05-23 02:27:51 +00:00
/*
* (c) 2001-2004 Adam Clauss, Chris Danford
*
* All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, and/or sell copies of the Software, and to permit persons to
* whom the Software is furnished to do so, provided that the above
* copyright notice(s) and this permission notice appear in all copies of
* the Software and that both the above copyright notice(s) and this
* permission notice appear in supporting documentation.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF
* THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS
* INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT
* OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
* OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
* OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*/