// one line to give the program's name and an idea of what it does.
// Copyright (C) 2005 name of Borja Snchez Zamorano
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
namespace BorSanZaNET{
using System;
using System.Collections;
using System.Xml;
using System.IO;
using System.Runtime.CompilerServices;
public class MiniGConf {
string file;
public struct DataStruct {
public string name, type;
public object value;
public DataStruct(string name, string type, object value)
{
this.name = name;
this.type = type;
this.value = value;
}
}
public ArrayList DataList;
public MiniGConf(string file)
{
DataList = new ArrayList();
LoadFile(file);
}
public void LoadFile(string file)
{
this.file = file;
DataList.Clear();
if (File.Exists (file)) {
LoadXML();
newEvent("loadfile", "true");
}
}
void LoadXML()
{
XmlTextReader reader = null;
try {
reader = new XmlTextReader(file);
reader.WhitespaceHandling = WhitespaceHandling.None;
string name, type, value;
name = type = value = null;
while (reader.Read()) {
switch (reader.NodeType) {
case XmlNodeType.Element:
name = reader.Name;
type = reader.GetAttribute("type");
break;
case XmlNodeType.Text:
value = reader.Value;
break;
case XmlNodeType.EndElement:
if ((name != null)&&(type != null)&&(value != null)) {
switch (type) {
case "System.Boolean":
Set ( name, System.Boolean.Parse(value), false, false );
break;
case "System.String":
Set ( name, value, false, false );
break;
case "System.Int16":
Set ( name, System.Int16.Parse( value ), false, false );
break;
case "System.Int32":
Set ( name, System.Int32.Parse( value ), false, false );
break;
case "System.Int64":
Set ( name, System.Int64.Parse( value ), false, false );
break;
case "System.UInt16":
Set ( name, System.UInt16.Parse( value ), false, false );
break;
case "System.UInt32":
Set ( name, System.UInt32.Parse( value ), false, false );
break;
case "System.UInt64":
Set ( name, System.UInt64.Parse( value ), false, false );
break;
}
}
name = type = value = null;
break;
}
}
} finally {
if (reader != null)
reader.Close();
}
}
void SaveXML()
{
XmlTextWriter xWriter = new XmlTextWriter(file, null);
xWriter.Formatting = System.Xml.Formatting.Indented;
xWriter.WriteStartElement(null, "config", null);
for (int i = 0; i < DataList.Count; i++) {
xWriter.WriteStartElement(null, ((DataStruct) DataList [i]).name, null);
xWriter.WriteStartAttribute(null, "type", null);
xWriter.WriteString( ((DataStruct) DataList [i]).type);
xWriter.WriteEndAttribute();
xWriter.WriteString(((DataStruct) DataList [i]).value.ToString());
xWriter.WriteEndElement ();
}
xWriter.WriteEndElement ();
xWriter.Close();
}
public object Get(string key, object valuedefault)
{
for (int i = 0; i < DataList.Count; i++) {
if (((DataStruct) DataList [i]).name == key)
return ((DataStruct) DataList [i]).value;
}
Set(key, valuedefault);
return valuedefault;
}
public string GetType(string key, string valuedefault)
{
for (int i = 0; i < DataList.Count; i++) {
if (((DataStruct) DataList [i]).name == key)
return ((DataStruct) DataList [i]).type;
}
return valuedefault;
}
public void Set(string key, object value)
{
Set(key, value, true, true);
}
public void Set(string key, object value, bool savexml, bool runevent)
{
int pos = -1;
for (int i = 0; i < DataList.Count; i++) {
if (((DataStruct) DataList [i]).name == key) {
pos = i;
break;
}
}
if (pos != -1) {
if ( ((DataStruct) DataList [pos]).value == value)
return;
DataList[pos] = new DataStruct(key, value.GetType().ToString(), value);
} else
DataList.Add(new DataStruct(key, value.GetType().ToString(), value));
if (runevent)
newEvent(key, value);
if (savexml)
SaveXML();
}
public void Del(string key)
{
Del(key, true, true);
}
public void Del(string key, bool savexml, bool runevent)
{
int pos = -1;
for (int i = 0; i < DataList.Count; i++) {
if (((DataStruct) DataList [i]).name == key) {
pos = i;
break;
}
}
if (pos != -1)
DataList.RemoveAt(pos);
if (runevent)
newEvent(key, null);
if (savexml)
SaveXML();
}
public static int HexToInt(String hexstr)
{
int hexint = 0;
String hexarr = hexstr.ToUpper();
for (int counter = hexarr.Length - 1; counter >= 0; counter--) {
if ((hexarr[counter] >= '0') && (hexarr[counter] <= '9')) {
hexint += (hexarr[counter] - 48) * ((int)(Math.Pow(16, hexarr.Length - 1 - counter)));
continue;
}
if ((hexarr[counter] >= 'A') && (hexarr[counter] <= 'F')) {
hexint += (hexarr[counter] - 55) * ((int)(Math.Pow(16, hexarr.Length - 1 - counter)));
continue;
}
return 0;
}
return hexint;
}
public void newEvent(string key, object value)
{
if (OnChange != null) OnChange(key, value);
}
public delegate void KeyHandler(string key, object val);
public event KeyHandler OnChange;
}
}
|