[Release] IniFile Reader!

02/25/2012 10:42 Mr_PoP#1
Greetings Guys,

I was bored so I made a dll to read your Ini files.
all you need is to add the dll as a reference to your project and start using it.

an example:
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IniFile;

namespace Test {

    internal class Program {

        private static void Main(string[] args) {
            var ini = new CIniFile("test.ini", "AccountServer");
            int port = ini.GetInt("ACCOUNT_SERVER_PORT");
            ini.SetSection("Game_DataBase");
            string test = ini.GetString("DATABASE_HOST");
            Console.WriteLine("values are {0} , {1}", port, test);
            Console.ReadKey();
        }
    }
}
test.ini

Code:
[AccountServer]
ACCOUNT_SERVER_IP=5.230.189.19
ACCOUNT_SERVER_PORT=9958

[Account_DataBase]
DATABASE_HOST=127.0.0.1
DATABASE_USER=root
DATABASE_PASSWORD=
DATABASE_DBNAME=account_zf
DATABASE_PORT=3306

[GameServer]
GAME_SERVER_NAME=Unkown
GAME_SERVER_IP=5.230.189.19
GAME_SERVER_PORT=5816

[Game_DataBase]
DATABASE_HOST=127.0.0.1 // this is the db ip
DATABASE_USER=root
DATABASE_PASSWORD=
DATABASE_DBNAME=account_zf
DATABASE_PORT=3306
output:
Code:
values are 9958 , 127.0.0.1
Features:
-Managed (no native calls);
-You can comment your fields.
-Works on anyCPU maybe any platform :P.
-Reads Multiple sections.

okey test it , and if there is any issue please let me know so I can fix it.
02/25/2012 11:12 Spirited#2
Code:
public class CIniFile
{
    // Fields
    private Dictionary<string, string> fields = new Dictionary<string, string>();
    private string[] lines;
    private string section;
    private List<string> temp = new List<string>();
    private string Wsection;

    // Methods
    public CIniFile(string filepath, string section)
    {
        try
        {
            this.lines = File.ReadAllLines(filepath);
            this.section = "[" + section + "]";
            this.GetFields();
        }
        catch (Exception exception)
        {
            Console.WriteLine(exception.Message);
        }
    }

    private void GetFields()
    {
        try
        {
            bool flag = false;
            for (int i = 0; i < this.lines.Length; i++)
            {
                if (string.IsNullOrWhiteSpace(this.lines[i]))
                {
                    continue;
                }
                char ch = this.lines[i][0];
                if (ch.Equals('[') && (this.lines[i] == this.section))
                {
                    for (int j = i + 1; j < this.lines.Length; j++)
                    {
                        if (!string.IsNullOrWhiteSpace(this.lines[j]))
                        {
                            ch = this.lines[j][0];
                            if (ch.Equals('['))
                            {
                                flag = true;
                                break;
                            }
                            this.temp.Add(this.lines[j]);
                        }
                    }
                }
                if (flag)
                {
                    break;
                }
            }
            foreach (string str in this.temp)
            {
                string[] strArray = str.Split("//".ToCharArray())[0].Replace(" ", "").Split(new char[] { '=' });
                this.fields.Add(strArray[0], strArray[1].Replace(";", ""));
            }
        }
        catch (Exception exception)
        {
            Console.WriteLine(exception.Message);
        }
    }

    public int GetInt(string key)
    {
        Func<KeyValuePair<string, string>, bool> predicate = null;
        try
        {
            if (predicate == null)
            {
                predicate = x => x.Key == key;
            }
            return int.Parse(this.fields.SingleOrDefault<KeyValuePair<string, string>>(predicate).Value);
        }
        catch (Exception exception)
        {
            Console.WriteLine(exception.Message);
            return 0;
        }
    }

    public string GetString(string key)
    {
        Func<KeyValuePair<string, string>, bool> predicate = null;
        try
        {
            if (predicate == null)
            {
                predicate = x => x.Key == key;
            }
            return this.fields.SingleOrDefault<KeyValuePair<string, string>>(predicate).Value;
        }
        catch (Exception exception)
        {
            Console.WriteLine(exception.Message);
            return "";
        }
    }

    public void SetSection(string section)
    {
        this.section = "[" + section + "]";
        this.fields.Clear();
        this.temp.Clear();
        this.GetFields();
    }
}
Why would you want to keep yourself away from Native calls? .-.
02/25/2012 11:59 JobvdH#3
Great work Mr_PoP.
Fang`s method is better tho.
02/25/2012 12:04 Mr_PoP#4
Quote:
Originally Posted by Fаng View Post
Why would you want to keep yourself away from Native calls? .-.
the question is why do I need to use it?

Quote:
Originally Posted by JobvdH View Post
Great work Mr_PoP.
Fang`s method is better tho.
what method lol? if your talking about the code he posted, it's the reflection of my dll.
02/25/2012 12:23 JobvdH#5
Aah, well then great job ;)
02/25/2012 16:23 CptSky#6
Quote:
Originally Posted by Fаng View Post
Why would you want to keep yourself away from Native calls? .-.
1. Native calls may be really slow...
2. Native calls aren't made for INI file, but for the Windows Registry.
3. The second point give another problem. Each time you call a function, the file is completely loaded for the call... Really not optimal.
02/25/2012 16:44 Korvacs#7
Of course a better question is why the hell are you still using .ini files at all?
02/25/2012 16:45 I don't have a username#8
Quote:
Originally Posted by Korvacs View Post
Of course a better question is why the hell are you still using .ini files at all?
Config?
02/25/2012 16:59 Korvacs#9
Quote:
Originally Posted by I don't have a username View Post
Config?
...xml? Basically the replacement for ini files?? ie. Welcome to the 21st century??
02/25/2012 17:31 I don't have a username#10
Quote:
Originally Posted by Korvacs View Post
...xml? Basically the replacement for ini files?? ie. Welcome to the 21st century??
Not necessary. Ini is far easier to read than reading a xml file, so if you have a lot of configurations, it would be much faster configuring an inifile than xml. However xml is faster parsing for programming and much easier to use at that part as well, but performance doesn't matter much at loading configs, unless you load configurations multiple times during your process.
02/25/2012 17:40 Korvacs#11
There's practically no difference in readability unless your a complete moron who doesn't understand simple syntax, if anything its clearer to read if your sensible about your syntax.

And as for performance its not even worth thinking about, at no point should you need to be worrying about how long it takes you to parse a .ini or .xml file, if your worrying about it then you need to rethink what your doing entirely.

Honestly do like the rest of the programmers in the world and let old technology die (surprisingly enough that's why the .net framework doesn't have any managed methods for ini files -.-")
02/25/2012 17:42 I don't have a username#12
Quote:
Originally Posted by Korvacs View Post
There's practically no difference in readability unless your a complete moron who doesn't understand simple syntax, if anything its clearer to read if your sensible about your syntax.

And as for performance its not even worth thinking about, at no point should you need to be worrying about how long it takes you to parse a .ini or .xml file, if your worrying about it then you need to rethink what your doing entirely.

Honestly do like the rest of the programmers in the world and let old technology die (surprisingly enough that's why the .net framework doesn't have any managed methods for ini files -.-")
I'm not saying I am doing it, was just saying why he could be doing it. I'm using xml at its best lmao.
02/25/2012 21:00 Mr_PoP#13
Quote:
Originally Posted by Korvacs View Post
Of course a better question is why the hell are you still using .ini files at all?
ahaha am not using .ini am using xml , I was just bored so I made it really :D
02/26/2012 18:47 |xabi|#14
Best Ya POP :)
02/26/2012 21:05 Korvacs#15
Quote:
Originally Posted by Mr_PoP View Post
ahaha am not using .ini am using xml , I was just bored so I made it really :D
lol what a waste of time :rolleyes: