Feedback

C# - Einfache Ini Klasse

Veröffentlicht von am 4/15/2011
(1 Bewertungen)
Heute habe ich für euch eine kleine aber feine klasse für Ini-Dateien mit Hilfe von Dictionaries, welche ich vor ein paar Minuten soweit erstmal fertiggestellt habe. Grundlegendem Fehlerabfang ist soweit implementiert, diesmal per Exceptions. Ein paar Erweiterungen werden sich dafür aber bestimmt noch finden lassen, wie z.b. Kommentierte Zeilen mitnehmen (natürlich mit der korrekten Position in der Inidatei), oder Export/Import per Xml.
Verbesserungsvorschläge und so weiter, sind wie immer willkommen.
    /// <summary>
    /// 
    /// </summary>
    public sealed class Ini
    {
        /// <summary>
        /// 
        /// </summary>
        public Ini()
        {
            this._Categories = new Dictionary<String,IniCategory>();
            this._General = new IniCategory(String.Empty);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="File"></param>
        public Ini(String File) : this()
        {
            this.Read(File);
        }
        /// <summary>
        /// 
        /// </summary>
        public String Name
        {
            get
            {
                return this._Name;
            }
            set
            {
                this._Name = value;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="File"></param>
        public void Read(String File)
        {
            if (!System.IO.File.Exists(File)) throw new System.IO.FileNotFoundException("The specified path doesn't exist");

            this._Name = System.IO.Path.GetFileNameWithoutExtension(File);

            using (System.IO.StreamReader Input = new System.IO.StreamReader(File))
            {
                String Line = String.Empty;
                String Temp = String.Empty;
                IniCategory TCategory = this._General;

                while (!Input.EndOfStream)
                {
                    Line = Input.ReadLine();
                    //Skip comment lines and empty lines
                    if (Line.StartsWith(";") || String.IsNullOrEmpty(Line)) continue;

                    //Detect categories
                    if (Line.StartsWith("[") && Line.EndsWith("]"))
                    {
                        TCategory = this.CreateCategory(Line.Substring(1, Line.Length - 2));
                        continue;
                    }
                    //Detect invalid entries
                    if (!Line.Contains('=')) continue;
                    Temp = Line.Split('=')[0];
                    //Detect duplicate entries
                    if (TCategory._Entries.ContainsKey(Temp))
                    {
                        //Something to do
                    }
                    else
                    {
                        TCategory.CreateEntry(Temp, Line.Substring(Temp.Length + 1));
                    }
                }
            }       
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="File"></param>
        public void Save(String File)
        {
            using (System.IO.StreamWriter Output = new System.IO.StreamWriter(File, false))
            {
                foreach (IniEntry Entry in this._General._Entries.Values)
                {
                    Output.WriteLine(Entry.Name + "=" + Entry.Value);
                }
                foreach (IniCategory Category in this._Categories.Values)
                {
                    Output.WriteLine("[" + Category.Name + "]");
                    foreach (IniEntry Entry in Category._Entries.Values)
                    {
                        Output.WriteLine(Entry.Name + "=" + Entry.Value);
                    }
                }
            }            
        }
        /// <summary>
        /// 
        /// </summary>
        public IniCategory[] Categories
        {
            get
            {
                return this._Categories.Values.ToArray();
            }
        }
        /// <summary>
        /// 
        /// </summary>
        public IniEntry[] Entries
        {
            get
            {
                List<IniEntry> TEntries = new List<IniEntry>();
                TEntries.AddRange(this._General.Entries);
                foreach (IniCategory TCategory in this._Categories.Values)
                {
                    TEntries.AddRange(TCategory.Entries);
                }
                return TEntries.ToArray();
            }
        }
        /// <summary>
        /// 
        /// </summary>
        public IniCategory General
        {
            get
            {
                return this._General;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="Category"></param>
        /// <returns></returns>
        public IniCategory this[String Category]
        {
            get
            {
                if (!this._Categories.ContainsKey(Category)) throw new InvalidOperationException("The category doesn't exists");
                else return this._Categories[Category];
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="Category"></param>
        /// <param name="Entry"></param>
        /// <returns></returns>
        public IniEntry this[String Category, String Entry]
        {
            get
            {
                try
                {
                    if (!this._Categories.ContainsKey(Category)) throw new InvalidOperationException("The category doesn't exists");
                    else if (!this._Categories[Category]._Entries.ContainsKey(Entry)) throw new InvalidOperationException("The entry doesn't exists");
                    else return this._Categories[Category]._Entries[Entry];
                }
                catch
                {
                    return null;
                }
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="Name"></param>
        public IniCategory CreateCategory(String Name)
        {
            if (String.IsNullOrEmpty(Name)) throw new InvalidOperationException("The name of a category cant be empty");
            else if (this._Categories.ContainsKey(Name)) throw new InvalidOperationException("The category you tried to create already exists");

            return this._Categories[Name] = new IniCategory(Name);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="Name"></param>
        public IniEntry CreateEntry(String Name)
        {
            if (Name.Contains("=")) throw new InvalidOperationException("The name can't contain =");

            if (this._General._Entries.ContainsKey(Name)) throw new InvalidOperationException("The entry you tried to create already exists");
            return this._General._Entries[Name] = new IniEntry(Name, String.Empty);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="Category"></param>
        /// <param name="Name"></param>
        public IniEntry CreateEntry(String Category, String Name)
        {
            return this.CreateEntry(Category, Name, String.Empty);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="Category"></param>
        /// <param name="Name"></param>
        /// <param name="Value"></param>
        public IniEntry CreateEntry(String Category, String Name, String Value)
        {
            if (!this._Categories.ContainsKey(Category)) throw new InvalidOperationException("The category doesn't exists");
            else if (this._Categories[Category]._Entries.ContainsKey(Name)) throw new InvalidOperationException("The entry you tried to create already exists");
            else if (Name.Contains("=")) throw new InvalidOperationException("The name can't contain =");
            else return this._Categories[Category]._Entries[Name] = new IniEntry(Name, Value);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="Name"></param>
        /// <param name="Value"></param>
        public void SetEntry(String Name, String Value)
        {
            if (Name.Contains("=")) throw new InvalidOperationException("The name can't contain =");

            if (this._General._Entries.ContainsKey(Name)) this._General._Entries[Name].Value = Value;
            else this._General._Entries[Name] = new IniEntry(Name, Value);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="Category"></param>
        /// <param name="Name"></param>
        /// <param name="Value"></param>
        public void SetEntry(String Category, String Name, String Value)
        {
            if (String.IsNullOrEmpty(Category)) throw new InvalidOperationException("The name of a category can't be empty");
            else if (String.IsNullOrEmpty(Name)) throw new InvalidOperationException("The name of an entry can't be empty");
            else if (Name.Contains("=")) throw new InvalidOperationException("The name can't contain =");
            
            
            IniCategory TCategory = null;

            if (!this._Categories.ContainsKey(Category)) TCategory = this._Categories[Category] = new IniCategory(Category);
            else TCategory = this._Categories[Category];

            if (!TCategory._Entries.ContainsKey(Name)) TCategory._Entries[Name] = new IniEntry(Name, Value);
            else TCategory._Entries[Name].Value = Value;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="Name"></param>
        /// <returns></returns>
        public bool CategoryExists(String Name)
        {
            return this._Categories.ContainsKey(Name);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="Name"></param>
        /// <returns></returns>
        public bool EntryExists(String Name)
        {
            return this._General._Entries.ContainsKey(Name);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="Category"></param>
        /// <param name="Name"></param>
        /// <returns></returns>
        public bool EntryExists(String Category, String Name)
        {
            return this._Categories[Category]._Entries.ContainsKey(Name);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="Name"></param>
        public void RemoveCategorie(String Name)
        {
            if (String.IsNullOrEmpty(Name)) throw new InvalidOperationException("The name of a category can't be empty");
            else this._Categories.Remove(Name);
        }
        /// <summary>
        /// Removes an entries von the categorized-entries
        /// </summary>
        /// <param name="Category"></param>
        /// <param name="Name"></param>
        public void RemoveEntry(String Category, String Name)
        {
            if (this._Categories.ContainsKey(Category)) this._Categories[Category]._Entries.Remove(Name);
        }
        /// <summary>
        /// Removes an entry from the uncategorized-entries
        /// </summary>
        /// <param name="Name"></param>
        public void RemoveEntry(String Name)
        {
            this._General.RemoveEntry(Name);
        }
        /// <summary>
        /// Represent the categories
        /// </summary>
        internal Dictionary<String, IniCategory> _Categories;
        /// <summary>
        /// Represent the Entries without category
        /// </summary>
        internal IniCategory _General;
        /// <summary>
        /// 
        /// </summary>
        private String _Name;
    }
    /// <summary>
    /// 
    /// </summary>
    public sealed class IniCategory
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="Ini"></param>
        /// <param name="Name"></param>
        /// <returns></returns>
        public static IniCategory Create(Ini Ini, String Name)
        {
            if (Ini == null) throw new ArgumentNullException("The ini object can't be null");
            else if (Ini._Categories.ContainsKey(Name)) throw new InvalidOperationException("The category you tried to create already exists");
            else if (String.IsNullOrEmpty(Name)) throw new InvalidOperationException("The name of a category can't be empty");
            return Ini.CreateCategory(Name);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="Name"></param>
        internal IniCategory(String Name)
        {
            this._Name = Name;
            this._Entries = new Dictionary<String, IniEntry>();
        }
        /// <summary>
        /// 
        /// </summary>
        public String Name
        {
            get
            {
                return this._Name;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="Entry"></param>
        /// <returns></returns>
        public IniEntry this[String Entry]
        {
            get
            {
                try
                {
                    return this._Entries[Entry];
                }
                catch
                {
                    return null;
                }
            }
        }
        /// <summary>
        /// 
        /// </summary>
        public IniEntry[] Entries
        {
            get
            {
                return this._Entries.Values.ToArray();
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="Name"></param>
        public IniEntry CreateEntry(String Name)
        {
            return this.CreateEntry(Name, String.Empty);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="Name"></param>
        /// <param name="Value"></param>
        public IniEntry CreateEntry(String Name, String Value)
        {
            if(this._Entries.ContainsKey(Name)) throw new InvalidOperationException("The entry you tried to create already exists");
            else if (String.IsNullOrEmpty(Name)) throw new InvalidOperationException("The name of an entry can't be empty");
            else if (Name.Contains("=")) throw new InvalidOperationException("The name can't contain =");
            return (this._Entries[Name] = new IniEntry(Name, Value));
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="Name"></param>
        public void RemoveEntry(String Name)
        {
            this._Entries.Remove(Name);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="Name"></param>
        /// <param name="Value"></param>
        public void SetEntry(String Name, String Value)
        {
            if (Name.Contains("=")) throw new InvalidOperationException("The name can't contain =");

            if(!this._Entries.ContainsKey(Name)) this._Entries[Name] = new IniEntry(Name, Value);
            else this._Entries[Name].Value = Value;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="Name"></param>
        /// <returns></returns>
        public bool EntryExists(String Name)
        {
            return this._Entries.ContainsKey(Name);
        }
        /// <summary>
        /// 
        /// </summary>
        public bool HasEntries
        {
            get
            {
                return (this._Entries.Count > 0);
            }
        }
        /// <summary>
        /// 
        /// </summary>
        private String _Name;
        /// <summary>
        /// 
        /// </summary>
        internal Dictionary<String, IniEntry> _Entries;
    }
    /// <summary>
    /// 
    /// </summary>
    public sealed class IniEntry
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="Category"></param>
        /// <param name="Name"></param>
        /// <param name="Value"></param>
        public static IniEntry Create (IniCategory Category, String Name, String Value)
        {
            if (Category == null) throw new ArgumentNullException("The category object can't be null");
            else if (Category._Entries.ContainsKey(Name)) throw new InvalidOperationException("The entry you tried to create already exists");
            else if (String.IsNullOrEmpty(Name)) throw new InvalidOperationException("The name of an entry can't be empty");
            else if (Name.Contains("=")) throw new InvalidOperationException("The name can't contain =");
            
            return Category.CreateEntry(Name, Value);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="Name"></param>
        /// <param name="Value"></param>
        internal IniEntry(String Name, String Value)
        {
            this._Name = String.Intern(Name);
            this._Value = String.Intern(Value);
        }
        /// <summary>
        /// 
        /// </summary>
        public String Name
        {
            get
            {
                return this._Name;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        public String Value
        {
            get
            {
                return this._Value;
            }
            set
            {
                this._Value = value;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        public bool HasValue
        {
            get
            {
                return !String.IsNullOrEmpty(this._Value);
            }
        }
        /// <summary>
        /// 
        /// </summary>
        private String _Name;
        /// <summary>
        /// 
        /// </summary>
        private String _Value;
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            return this._Value;
        }
    }
Abgelegt unter Ini, Dictionary, C#, IniEntry, IniCategory.

Kommentare zum Snippet

 

Logge dich ein, um hier zu kommentieren!