Files
itgmania212121/stepmania/src/IniFile.cpp
T

212 lines
5.1 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"
2005-01-07 14:28:00 +00:00
#include "Foreach.h"
2001-11-03 10:52:42 +00:00
IniFile::IniFile()
{
m_sName = "IniFile";
}
bool IniFile::ReadFile( const RString &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
2004-12-10 09:46:09 +00:00
return ReadFile( f );
}
2004-12-11 02:25:38 +00:00
bool IniFile::ReadFile( RageFileBasic &f )
2004-12-10 09:46:09 +00:00
{
RString keyname;
2004-04-18 23:06:56 +00:00
while( 1 )
2001-11-03 10:52:42 +00:00
{
RString line;
2004-04-18 23:06:56 +00:00
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
2005-03-22 23:04:03 +00:00
if( line.size() == 0 )
2002-08-20 02:12:19 +00:00
continue;
2005-03-22 23:04:03 +00:00
if( line[0] == '#' )
continue; /* comment */
if( line.size() > 1 && line[0] == '/' && line[1] == '/' )
2003-01-05 08:36:33 +00:00
continue; /* comment */
2005-12-21 07:52:23 +00:00
if( line[0] == '[' && line[line.size()-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
}
2005-09-05 03:24:14 +00:00
else
2002-08-20 02:12:19 +00:00
{
2005-09-05 03:24:14 +00:00
/* New value. */
2005-12-21 08:33:30 +00:00
size_t iEqualIndex = line.find("=");
if( iEqualIndex != string::npos )
2001-11-03 10:52:42 +00:00
{
2005-12-21 08:33:30 +00:00
RString valuename = line.Left( (int) iEqualIndex );
2005-12-21 07:52:23 +00:00
RString value = line.Right( line.size()-valuename.size()-1 );
2005-01-07 14:37:21 +00:00
if( keyname.size() && valuename.size() )
SetValue(keyname,valuename,value);
2001-11-03 10:52:42 +00:00
}
}
}
}
bool IniFile::WriteFile( const RString &sPath ) const
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-12-10 09:46:09 +00:00
return IniFile::WriteFile( f );
}
2005-01-07 14:28:00 +00:00
bool IniFile::WriteFile( RageFileBasic &f ) const
2004-12-10 09:46:09 +00:00
{
2005-01-07 14:28:00 +00:00
FOREACH_CONST_Child( this, pKey )
2001-11-03 10:52:42 +00:00
{
2005-01-07 14:28:00 +00:00
if( f.PutLine( ssprintf("[%s]", pKey->m_sName.c_str()) ) == -1 )
2004-06-06 20:01:19 +00:00
{
m_sError = f.GetError();
return false;
}
2005-01-07 14:28:00 +00:00
FOREACH_CONST_Attr( pKey, pAttr )
{
if( f.PutLine( ssprintf("%s=%s", pAttr->first.c_str(), pAttr->second.c_str()) ) == -1 )
2005-01-08 00:16:11 +00:00
{
m_sError = f.GetError();
return false;
}
2005-01-07 14:28:00 +00:00
}
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
}
bool IniFile::GetValue( const RString &keyname, const RString &valuename, RString& value ) const
2001-11-03 10:52:42 +00:00
{
2005-01-07 14:28:00 +00:00
const XNode* pNode = GetChild( keyname );
if( pNode == NULL )
2002-07-23 01:41:40 +00:00
return false;
2005-01-07 14:28:00 +00:00
return pNode->GetAttrValue( valuename, value );
2002-05-20 08:59:37 +00:00
}
void IniFile::SetValue( const RString &keyname, const RString &valuename, const RString &value )
{
2005-01-07 14:28:00 +00:00
XNode* pNode = GetChild( keyname );
if( pNode == NULL )
pNode = AppendChild( keyname );
pNode->AppendAttr( valuename, value );
}
bool IniFile::DeleteValue(const RString &keyname, const RString &valuename)
{
2005-01-07 14:28:00 +00:00
XNode* pNode = GetChild( keyname );
if( pNode == NULL )
return false;
return pNode->RemoveAttr( valuename );
}
2001-11-03 10:52:42 +00:00
2004-12-07 01:43:06 +00:00
#define TYPE(T) \
bool IniFile::GetValue( const RString &keyname, const RString &valuename, T &value ) const \
2004-12-07 01:43:06 +00:00
{ \
RString sValue; \
2004-12-07 01:43:06 +00:00
if( !GetValue(keyname,valuename,sValue) ) \
return false; \
return FromString( sValue, value ); \
} \
void IniFile::SetValue( const RString &keyname, const RString &valuename, T value ) \
2004-12-07 01:43:06 +00:00
{ \
2005-01-07 14:28:00 +00:00
SetValue( keyname, valuename, ToString(value) ); \
2004-12-07 01:43:06 +00:00
}
2002-05-20 08:59:37 +00:00
2004-12-07 01:43:06 +00:00
TYPE(int);
TYPE(unsigned);
TYPE(float);
TYPE(bool);
2001-11-03 10:52:42 +00:00
bool IniFile::DeleteKey(const RString &keyname)
2005-01-07 14:28:00 +00:00
{
XNode* pNode = GetChild( keyname );
if( pNode == NULL )
2002-10-24 23:08:18 +00:00
return false;
2005-01-07 14:28:00 +00:00
return RemoveChild( pNode );
2001-11-03 10:52:42 +00:00
}
bool IniFile::RenameKey(const RString &from, const RString &to)
2002-07-23 01:41:40 +00:00
{
2005-01-07 14:28:00 +00:00
/* If to already exists, do nothing. */
XNode* pTo = GetChild( to );
if( pTo )
2002-10-24 23:08:18 +00:00
return false;
2001-11-03 10:52:42 +00:00
multimap<RString, XNode*>::iterator it = m_childs.find( from );
2005-01-07 14:28:00 +00:00
if( it == m_childs.end() )
return false;
2001-11-03 10:52:42 +00:00
2005-01-07 14:28:00 +00:00
XNode* pNode = it->second;
2003-01-03 22:37:44 +00:00
2005-01-07 14:28:00 +00:00
m_childs.erase( it );
2003-02-09 00:46:35 +00:00
2005-01-07 14:28:00 +00:00
pNode->m_sName = to;
2005-06-27 05:24:24 +00:00
m_childs.insert( make_pair(pNode->m_sName, pNode) );
2003-01-03 22:37:44 +00:00
2005-01-07 14:28:00 +00:00
return true;
2002-07-23 01:41:40 +00:00
}
2004-05-23 02:27:51 +00:00
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.
*/