using System.Runtime.InteropServices;
using System.Text;
namespace IgniteEngine.IO
{
///
/// Class to read and manipulate .ini files.
///
public class INIFile
{
///
/// The file's path.
///
private readonly string path;
///
/// The section to read from.
///
private readonly string section;
///
/// Creates a new instance of the class.
///
/// The path of the file.
/// The section to read from.
public INIFile(string path, string section = null)
{
this.path = path;
this.section = section;
}
///
/// Gets information from the file.
///
[DllImport("kernel32")]
private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
///
/// Writes information to the file.
///
[DllImport("kernel32")]
private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
///
/// Gets a byte from the file.
///
public byte GetByte(string key)
{
return GetByte(section, key);
}
///
/// Gets a byte from the file.
///
public byte GetByte(string section, string key)
{
return byte.Parse(GetValue(section, key));
}
///
/// Gets a 16-bit integer from the file.
///
public short GetInt16(string key)
{
return GetInt16(section, key);
}
///
/// Gets a 16-bit integer from the file.
///
public short GetInt16(string section, string key)
{
return short.Parse(GetValue(section, key));
}
///
/// Gets a 32-bit integer from the file.
///
public int GetInt32(string key)
{
return GetInt32(section, key);
}
///
/// Gets a 32-bit integer from the file.
///
public int GetInt32(string section, string key)
{
return int.Parse(GetValue(section, key));
}
///
/// Gets a 64-bit integer from the file.
///
public long GetInt64(string key)
{
return GetInt64(section, key);
}
///
/// Gets a 64-bit integer from the file.
///
public long GetInt64(string section, string key)
{
return long.Parse(GetValue(section, key));
}
///
/// Gets a string from the file.
///
public string GetString(string key)
{
return GetString(section, key);
}
///
/// Gets a string from the file.
///
public string GetString(string section, string key)
{
return GetValue(section, key);
}
///
/// Sets a value in the file.
///
/// The value's section.
/// The value's key.
/// The value.
public void SetValue(string section, string key, string value)
{
WritePrivateProfileString(section, key, value, path);
}
///
/// Gets a value from the file.
///
/// The value's section.
/// The value's key.
private string GetValue(string section, string key)
{
var builder = new StringBuilder(256);
GetPrivateProfileString(section, key, "" , builder, 256, path);
return builder.ToString();
}
}
}