/* ----------------------------------------------------------------------------- File: IniFile.h Desc: Wrapper for reading and writing an .ini file. Copyright (c) 2001-2002 by the persons listed below. All rights reserved. ----------------------------------------------------------------------------- */ #include "stdafx.h" #include "IniFile.h" #include "RageUtil.h" ///////////////////////////////////////////////////////////////////// // Construction/Destruction ///////////////////////////////////////////////////////////////////// //default constructor IniFile::IniFile() { } //constructor, can specify pathname here instead of using SetPath later IniFile::IniFile(CString inipath) { path = inipath; } //default destructor IniFile::~IniFile() { } ///////////////////////////////////////////////////////////////////// // Public Functions ///////////////////////////////////////////////////////////////////// //sets path of ini file to read and write from void IniFile::SetPath(CString newpath) { path = newpath; } //reads ini file specified using IniFile::SetPath() //returns true if successful, false otherwise BOOL IniFile::ReadFile() { CStdioFile file; if( !file.Open(path, CFile::modeRead) ) { error = "Unable to open ini file."; return FALSE; } CString line; int curkey = -1, curval = -1; CString keyname, valuename, value; while( file.ReadString(line) ) { if( line != "" ) { if( line[0] == '[' && line[line.GetLength()-1] == ']' ) //if a section heading { keyname = line; keyname.TrimLeft('['); keyname.TrimRight(']'); } else //if a value { int iIndexOfEqual = line.Find("="); if( iIndexOfEqual == -1 ) // this is a malformed line continue; valuename = line.Left( iIndexOfEqual ); value = line.Right(line.GetLength()-valuename.GetLength()-1); SetValue(keyname,valuename,value); } } } file.Close(); return 1; } //writes data stored in class to ini file void IniFile::WriteFile() { CStdioFile file; if( !file.Open(path, CFile::modeCreate | CFile::modeWrite ) ) { error = "Unable to open ini for writing."; return; } // foreach key for( int keynum = 0; keynum <= names.GetUpperBound(); keynum++ ) { CString sTemp; sTemp.Format( "[%s]\n", names[keynum] ); file.WriteString( sTemp ); CMapStringToString &map = keys[keynum]; CStringArray as; // for each value_name/value pair for( POSITION pos = map.GetStartPosition(); pos != NULL; ) { CString value_name; CString value; map.GetNextAssoc( pos, value_name, value ); sTemp.Format( "%s=%s\n", value_name, value ); as.Add( sTemp ); } SortCStringArray( as ); for( int i=0; i