mini gconf.cs :  » Business-Application » MonoCalendar » BorSanZaNET » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Business Application » MonoCalendar 
MonoCalendar » BorSanZaNET » mini-gconf.cs
// 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;
  }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.