Files
itgmania212121/stepmania/src/IniFile.cpp
T

182 lines
4.4 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
2006-10-01 14:51:50 +00:00
IniFile::IniFile(): XNode("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() )
2006-01-08 18:40:20 +00:00
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
}
2006-01-20 21:40:15 +00:00
bool bSuccess = IniFile::WriteFile( f );
2006-01-20 23:38:17 +00:00
int iFlush = f.Flush();
bSuccess &= (iFlush != -1);
return bSuccess;
2004-12-10 09:46:09 +00:00
}
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
{
2006-10-02 06:12:42 +00:00
if( f.PutLine( ssprintf("[%s]", pKey->GetName().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 )
{
const RString &sName = pAttr->first;
2006-10-02 22:52:57 +00:00
const RString &sValue = pAttr->second.GetValue<RString>();
// TODO: Are there esacpe rules for these?
DEBUG_ASSERT( sName.find('\n') == sName.npos );
DEBUG_ASSERT( sName.find('=') == sName.npos );
if( f.PutLine( ssprintf("%s=%s", sName.c_str(), sValue.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::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
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. */
2006-10-02 06:37:00 +00:00
if( GetChild(to) != NULL )
2002-10-24 23:08:18 +00:00
return false;
2001-11-03 10:52:42 +00:00
2006-10-02 06:37:00 +00:00
XNode* pNode = GetChild( from );
if( pNode == NULL )
2005-01-07 14:28:00 +00:00
return false;
2001-11-03 10:52:42 +00:00
2006-10-02 06:37:00 +00:00
RemoveChild( pNode, false );
pNode->SetName( to );
AppendChild( 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.
*/