summaryrefslogtreecommitdiff
path: root/Tizen.Runtime/Tizen.Runtime/Ini.cs
blob: d5c2544511714d31bb3692372a71c20436a4c83e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;

namespace Tizen.Runtime
{
    internal class Ini
    {
        public string GlobalName = "___GLOBAL";
        public Ini(Uri path)
        {
            if (path.Scheme != "file")
            {
                throw new NotSupportedException("Only File Uri is supported now");
            }
            string absPath = path.AbsolutePath;
            Initialize();
            Contents = File.ReadAllText(absPath);
        }

        public Ini(string contents)
        {
            Initialize();
            Contents = contents;
        }

        public string Contents
        {
            get { return contents; }
            
            set
            {
                contents = value;
                ReadContents(contents);
            }
        }

        public Dictionary<string, string> this[string name]
        {
            get
            {
                if (groups.ContainsKey(name))
                    return groups[name];
                return null;
            }
        }

        public override string ToString()
        {
            string str = "";

            if (groups.ContainsKey(GlobalName))
            {
                foreach (var kv in groups[GlobalName])
                {
                    str += $"{kv.Key} = {kv.Value}" + Environment.NewLine;
                }
            }

            foreach (var gkv in groups)
            {
                if (gkv.Key == GlobalName) continue;
                str += $"[{gkv.Key}]" + Environment.NewLine;
                foreach (var kv in gkv.Value)
                {
                    str += $"{kv.Key} = {kv.Value}" + Environment.NewLine;
                }
            }

            return str;
        }

        public Dictionary<string, string> Global
        {
            get
            {
                return groups[GlobalName];
            }
        }

        private void Initialize()
        {
            groups = new Dictionary<string, Dictionary<string, string> >();
            groups[GlobalName] = new Dictionary<string, string>();
        }

        private string Uncommont(string str)
        {
            int comment_start = str.IndexOf('#');
            return str.Substring(0, comment_start);
        }

        private bool GetHeader(string str, ref string groupName)
        {
            if (str.Length < 2 || str[0] != '[' || str[str.Length-1] != ']') return false;
            string expected = str.Substring(1, str.Length-2);
            groupName = expected.Trim();
            return true;
        }

        private void GetKeyValueFromString(string str, out string key, out string value)
        {
            int eq_pos = str.IndexOf('=');
            if (eq_pos == -1)
            {
                key = str.Trim();
                value = "";
            }
            else
            {
                key = str.Substring(0, eq_pos).Trim();
                value = str.Substring(eq_pos+1).Trim();
            }
        }

        private void ReadContents(string contents)
        {
            string key, value;
            string groupName = GlobalName;
            string[] lines = contents.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);

            foreach (string line in lines)
            {
                if (string.IsNullOrWhiteSpace(line)) continue;
                if (GetHeader(line, ref groupName))
                {
                    groups[groupName] = new Dictionary<string, string>();
                    continue;
                }
                GetKeyValueFromString(line, out key, out value);
                groups[groupName][key] = value;
            }
        }

        private Dictionary<string, Dictionary<string, string> > groups;
        private string contents;
    }
}