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"
|
2003-07-10 16:51:15 +00:00
|
|
|
#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
|
|
|
|
2005-01-26 09:13:24 +00:00
|
|
|
IniFile::IniFile()
|
|
|
|
|
{
|
|
|
|
|
m_sName = "IniFile";
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
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
|
|
|
{
|
2003-07-10 17:45:00 +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
|
|
|
|
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 */
|
|
|
|
|
|
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
|
|
|
}
|
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. */
|
2002-08-20 02:12:19 +00:00
|
|
|
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);
|
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
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2005-01-07 14:28:00 +00:00
|
|
|
bool IniFile::WriteFile( const CString &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
|
|
|
}
|
2003-01-11 19:11:55 +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 )
|
|
|
|
|
{
|
2005-01-08 00:16:11 +00:00
|
|
|
if( f.PutLine( ssprintf("%s=%s", pAttr->m_sName.c_str(), pAttr->m_sValue.c_str()) ) == -1 )
|
|
|
|
|
{
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
2005-01-07 14:28:00 +00:00
|
|
|
bool IniFile::GetValue( const CString &keyname, const CString &valuename, CString& 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
|
|
|
}
|
|
|
|
|
|
2005-01-07 14:28:00 +00:00
|
|
|
void IniFile::SetValue( const CString &keyname, const CString &valuename, const CString &value )
|
2004-12-27 10:24:54 +00:00
|
|
|
{
|
2005-01-07 14:28:00 +00:00
|
|
|
XNode* pNode = GetChild( keyname );
|
|
|
|
|
if( pNode == NULL )
|
|
|
|
|
pNode = AppendChild( keyname );
|
|
|
|
|
pNode->SetAttrValue( valuename, value );
|
2004-12-27 10:24:54 +00:00
|
|
|
}
|
|
|
|
|
|
2005-01-07 14:28:00 +00:00
|
|
|
bool IniFile::DeleteValue(const CString &keyname, const CString &valuename)
|
2004-12-27 10:24:54 +00:00
|
|
|
{
|
2005-01-07 14:28:00 +00:00
|
|
|
XNode* pNode = GetChild( keyname );
|
|
|
|
|
if( pNode == NULL )
|
|
|
|
|
return false;
|
|
|
|
|
XAttr* pAttr = pNode->GetAttr( valuename );
|
|
|
|
|
if( pAttr == NULL )
|
2004-12-27 10:24:54 +00:00
|
|
|
return false;
|
2005-01-07 14:28:00 +00:00
|
|
|
return pNode->RemoveAttr( pAttr );
|
2004-12-27 10:24:54 +00:00
|
|
|
}
|
|
|
|
|
|
2001-11-03 10:52:42 +00:00
|
|
|
|
2004-12-07 01:43:06 +00:00
|
|
|
#define TYPE(T) \
|
|
|
|
|
bool IniFile::GetValue( const CString &keyname, const CString &valuename, T &value ) const \
|
|
|
|
|
{ \
|
|
|
|
|
CString sValue; \
|
|
|
|
|
if( !GetValue(keyname,valuename,sValue) ) \
|
|
|
|
|
return false; \
|
|
|
|
|
return FromString( sValue, value ); \
|
|
|
|
|
} \
|
2005-01-07 14:28:00 +00:00
|
|
|
void IniFile::SetValue( const CString &keyname, const CString &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
|
|
|
|
|
|
|
|
|
2005-01-07 14:28:00 +00:00
|
|
|
bool IniFile::DeleteKey(const CString &keyname)
|
|
|
|
|
{
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
2005-01-07 14:28:00 +00:00
|
|
|
bool IniFile::RenameKey(const CString &from, const CString &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
|
|
|
|
2005-01-07 14:28:00 +00:00
|
|
|
multimap<CString, XNode*>::iterator it = m_childs.find( from );
|
|
|
|
|
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-11-06 19:47:23 +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.
|
|
|
|
|
*/
|