Files
itgmania212121/stepmania/src/IniFile.cpp
T

311 lines
8.1 KiB
C++
Raw Normal View History

2002-07-23 01:41:40 +00:00
#include "stdafx.h"
/*
-----------------------------------------------------------------------------
2002-07-23 01:41:40 +00:00
Class: IniFile
2002-07-23 01:41:40 +00:00
Desc: See header.
2002-05-19 01:59:48 +00:00
Copyright (c) 2001-2002 by the person(s) listed below. All rights reserved.
2002-07-23 01:41:40 +00:00
Adam Clauss
Chris Danford
-----------------------------------------------------------------------------
*/
2001-11-03 10:52:42 +00:00
#include "IniFile.h"
/////////////////////////////////////////////////////////////////////
// Construction/Destruction
/////////////////////////////////////////////////////////////////////
//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
2002-07-23 01:41:40 +00:00
bool IniFile::ReadFile()
2001-11-03 10:52:42 +00:00
{
CStdioFile file;
2002-07-23 01:41:40 +00:00
CFileStatus status;
if (!file.GetStatus(path,status))
return 0;
if (!file.Open(path, CFile::modeRead))
2001-11-03 10:52:42 +00:00
{
error = "Unable to open ini file.";
2002-07-23 01:41:40 +00:00
return 0;
2001-11-03 10:52:42 +00:00
}
CString keyname, valuename, value;
2002-07-23 01:41:40 +00:00
CString temp;
CString line;
while (file.ReadString(line))
2001-11-03 10:52:42 +00:00
{
2002-08-20 02:12:19 +00:00
if (line == "")
continue;
if (line[0] == '[' && line[line.GetLength()-1] == ']') //if a section heading
2001-11-03 10:52:42 +00:00
{
2002-08-20 02:12:19 +00:00
keyname = line;
keyname.TrimLeft('[');
keyname.TrimRight(']');
}
else //if a value
{
int iEqualIndex = line.Find("=");
if( iEqualIndex != -1 )
2001-11-03 10:52:42 +00:00
{
2002-08-20 02:12:19 +00:00
valuename = line.Left(iEqualIndex);
value = line.Right(line.GetLength()-valuename.GetLength()-1);
SetValue(keyname,valuename,value);
2001-11-03 10:52:42 +00:00
}
}
}
file.Close();
2001-11-03 10:52:42 +00:00
return 1;
}
//writes data stored in class to ini file
void IniFile::WriteFile()
{
2002-07-23 01:41:40 +00:00
FILE* fp = fopen( path, "w" );
for (unsigned keynum = 0; keynum < names.size(); keynum++)
2001-11-03 10:52:42 +00:00
{
2002-07-23 01:41:40 +00:00
if (keys[keynum].names.GetSize() != 0)
2001-11-03 10:52:42 +00:00
{
2002-07-23 01:41:40 +00:00
fprintf( fp, "[%s]\n", names[keynum] );
for (unsigned valuenum = 0; valuenum < keys[keynum].names.size(); valuenum++)
2002-07-23 01:41:40 +00:00
fprintf( fp, "%s=%s\n", keys[keynum].names[valuenum], keys[keynum].values[valuenum] );
fprintf( fp, "\n" );
2001-11-03 10:52:42 +00:00
}
}
2002-07-23 01:41:40 +00:00
fclose( fp );
2001-11-03 10:52:42 +00:00
}
//deletes all stored ini data
void IniFile::Reset()
{
keys.clear();
names.clear();
2001-11-03 10:52:42 +00:00
}
//returns number of keys currently in the ini
int IniFile::GetNumKeys()
{
return keys.GetSize();
}
2002-07-23 01:41:40 +00:00
//returns number of values stored for specified key, or -1 if key found
2002-08-22 22:12:29 +00:00
int IniFile::GetNumValues(const CString &keyname)
2001-11-03 10:52:42 +00:00
{
int keynum = FindKey(keyname);
if (keynum == -1)
return -1;
else
2002-07-23 01:41:40 +00:00
return keys[keynum].names.GetSize();
2001-11-03 10:52:42 +00:00
}
//gets value of [keyname] valuename =
//overloaded to return CString, int, and double
2002-08-22 22:12:29 +00:00
bool IniFile::GetValue(const CString &keyname, const CString &valuename, CString& value)
2001-11-03 10:52:42 +00:00
{
2002-07-23 01:41:40 +00:00
int keynum = FindKey(keyname), valuenum = FindValue(keynum,valuename);
if (keynum == -1)
{
error = "Unable to locate specified key.";
2002-05-20 08:59:37 +00:00
return false;
2002-07-23 01:41:40 +00:00
}
2001-11-03 10:52:42 +00:00
2002-07-23 01:41:40 +00:00
if (valuenum == -1)
{
error = "Unable to locate specified value.";
return false;
}
value = keys[keynum].values[valuenum];
return true;
2002-05-20 08:59:37 +00:00
}
//gets value of [keyname] valuename =
//overloaded to return CString, int, and double
2002-08-22 22:12:29 +00:00
bool IniFile::GetValueI(const CString &keyname, const CString &valuename, int& value)
2002-05-20 08:59:37 +00:00
{
CString sValue;
2002-07-23 01:41:40 +00:00
bool bSuccess = GetValue(keyname,valuename,sValue);
if( !bSuccess )
2002-05-20 08:59:37 +00:00
return false;
2002-07-23 01:41:40 +00:00
value = atoi(sValue);
2002-05-20 08:59:37 +00:00
return true;
2001-11-03 10:52:42 +00:00
}
//gets value of [keyname] valuename =
//overloaded to return CString, int, and double
2002-08-22 22:12:29 +00:00
bool IniFile::GetValueF(const CString &keyname, const CString &valuename, float& value)
2001-11-03 10:52:42 +00:00
{
2002-05-20 08:59:37 +00:00
CString sValue;
2002-07-23 01:41:40 +00:00
bool bSuccess = GetValue(keyname,valuename,sValue);
if( !bSuccess )
2002-05-20 08:59:37 +00:00
return false;
2002-07-23 01:41:40 +00:00
value = (float)atof(sValue);
2002-05-20 08:59:37 +00:00
return true;
2001-11-03 10:52:42 +00:00
}
//gets value of [keyname] valuename =
//overloaded to return CString, int, and double
2002-08-22 22:12:29 +00:00
bool IniFile::GetValueB(const CString &keyname, const CString &valuename, bool& value)
2001-11-03 10:52:42 +00:00
{
2002-05-20 08:59:37 +00:00
CString sValue;
2002-07-23 01:41:40 +00:00
bool bSuccess = GetValue(keyname,valuename,sValue);
if( !bSuccess )
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
}
//sets value of [keyname] valuename =.
//specify the optional paramter as false (0) if you do not want it to create
//the key if it doesn't exist. Returns true if data entered, false otherwise
//overloaded to accept CString, int, and double
2002-08-22 22:12:29 +00:00
bool IniFile::SetValue(const CString &keyname, const CString &valuename, const CString &value, bool create)
2001-11-03 10:52:42 +00:00
{
2002-07-23 01:41:40 +00:00
int keynum = FindKey(keyname), valuenum = 0;
//find key
if (keynum == -1) //if key doesn't exist
2001-11-03 10:52:42 +00:00
{
2002-07-23 01:41:40 +00:00
if (!create) //and user does not want to create it,
return 0; //stop entering this key
2001-11-03 10:52:42 +00:00
names.SetSize(names.GetSize()+1);
keys.SetSize(keys.GetSize()+1);
keynum = names.GetSize()-1;
names[keynum] = keyname;
}
2002-07-23 01:41:40 +00:00
//find value
valuenum = FindValue(keynum,valuename);
if (valuenum == -1)
{
if (!create)
return 0;
keys[keynum].names.SetSize(keys[keynum].names.GetSize()+1);
keys[keynum].values.SetSize(keys[keynum].names.GetSize()+1);
valuenum = keys[keynum].names.GetSize()-1;
keys[keynum].names[valuenum] = valuename;
}
keys[keynum].values[valuenum] = value;
return 1;
2001-11-03 10:52:42 +00:00
}
//sets value of [keyname] valuename =.
//specify the optional paramter as false (0) if you do not want it to create
//the key if it doesn't exist. Returns true if data entered, false otherwise
//overloaded to accept CString, int, and double
2002-08-22 22:12:29 +00:00
bool IniFile::SetValueI(const CString &keyname, const CString &valuename, int value, bool create)
2001-11-03 10:52:42 +00:00
{
CString temp;
temp.Format("%d",value);
return SetValue(keyname, valuename, temp, create);
}
//sets value of [keyname] valuename =.
//specify the optional paramter as false (0) if you do not want it to create
//the key if it doesn't exist. Returns true if data entered, false otherwise
//overloaded to accept CString, int, and double
2002-08-22 22:12:29 +00:00
bool IniFile::SetValueF(const CString &keyname, const CString &valuename, float value, bool create)
2001-11-03 10:52:42 +00:00
{
CString temp;
temp.Format("%f",value);
2001-11-03 10:52:42 +00:00
return SetValue(keyname, valuename, temp, create);
2002-05-20 08:59:37 +00:00
}
//sets value of [keyname] valuename =.
//specify the optional paramter as false (0) if you do not want it to create
//the key if it doesn't exist. Returns true if data entered, false otherwise
//overloaded to accept CString, int, and double
2002-08-22 22:12:29 +00:00
bool IniFile::SetValueB(const CString &keyname, const CString &valuename, bool value, bool create)
2002-05-20 08:59:37 +00:00
{
CString temp;
temp.Format("%d",value);
return SetValue(keyname, valuename, temp, create);
2001-11-03 10:52:42 +00:00
}
//deletes specified value
//returns true if value existed and deleted, false otherwise
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-07-23 01:41:40 +00:00
int keynum = FindKey(keyname), valuenum = FindValue(keynum,valuename);
if (keynum == -1 || valuenum == -1)
return 0;
2001-11-03 10:52:42 +00:00
2002-07-23 01:41:40 +00:00
keys[keynum].names.RemoveAt(valuenum);
keys[keynum].values.RemoveAt(valuenum);
return 1;
2001-11-03 10:52:42 +00:00
}
//deletes specified key and all values contained within
//returns true if key existed and deleted, false otherwise
2002-08-22 22:12:29 +00:00
bool IniFile::DeleteKey(const CString &keyname)
2001-11-03 10:52:42 +00:00
{
int keynum = FindKey(keyname);
if (keynum == -1)
return 0;
keys.RemoveAt(keynum);
names.RemoveAt(keynum);
return 1;
}
2002-07-23 01:41:40 +00:00
//deletes specified key and all values contained within
//returns true if key existed and deleted, false otherwise
2002-08-22 22:12:29 +00:00
IniFile::key* IniFile::GetKey(const CString &keyname)
2002-07-23 01:41:40 +00:00
{
int keynum = FindKey(keyname);
if (keynum == -1)
return NULL;
return &keys[keynum];
}
2001-11-03 10:52:42 +00:00
/////////////////////////////////////////////////////////////////////
// Private Functions
/////////////////////////////////////////////////////////////////////
//returns index of specified key, or -1 if not found
2002-08-22 22:12:29 +00:00
int IniFile::FindKey(const CString &keyname)
2001-11-03 10:52:42 +00:00
{
int keynum = 0;
while ( keynum < keys.GetSize() && names[keynum] != keyname)
keynum++;
if (keynum == keys.GetSize())
return -1;
return keynum;
}
2002-07-23 01:41:40 +00:00
//returns index of specified value, in the specified key, or -1 if not found
2002-08-22 22:12:29 +00:00
int IniFile::FindValue(int keynum, const CString &valuename)
2002-07-23 01:41:40 +00:00
{
if (keynum == -1)
return -1;
int valuenum = 0;
while (valuenum < keys[keynum].names.GetSize() && keys[keynum].names[valuenum] != valuename)
valuenum++;
if (valuenum == keys[keynum].names.GetSize())
return -1;
return valuenum;
}