Beispiel #1
0
        void init(byte[] data, string filename)
        {
            updated = false;

            lock (typeof(ReadIni))
            {
                string[] lines;
                string   srcstr;
                DateTime lastUpdate = new DateTime(0);

                if (filename != null)
                {
                    lastUpdate = IO.GetLastWriteTimeUtc(filename);

                    datas = IniCache.GetCache(filename, lastUpdate);
                }

                if (datas == null)
                {
                    if (data == null)
                    {
                        try
                        {
                            data = Buf.ReadFromFile(filename).ByteData;
                        }
                        catch
                        {
                            data  = new byte[0];
                            datas = IniCache.GetCache(filename, new DateTime());
                        }
                    }

                    if (datas == null)
                    {
                        datas = new Dictionary <string, string>();
                        Encoding currentEncoding = Str.Utf8Encoding;
                        srcstr = currentEncoding.GetString(data);

                        lines = Str.GetLines(srcstr);

                        foreach (string s in lines)
                        {
                            string line = s.Trim();

                            if (Str.IsEmptyStr(line) == false)
                            {
                                if (line.StartsWith("#") == false &&
                                    line.StartsWith("//") == false &&
                                    line.StartsWith(";") == false)
                                {
                                    string key, value;

                                    if (Str.GetKeyAndValue(line, out key, out value))
                                    {
                                        key = key.ToUpper();

                                        if (datas.ContainsKey(key) == false)
                                        {
                                            datas.Add(key, value);
                                        }
                                        else
                                        {
                                            int i;
                                            for (i = 1; ; i++)
                                            {
                                                string key2 = string.Format("{0}({1})", key, i).ToUpper();

                                                if (datas.ContainsKey(key2) == false)
                                                {
                                                    datas.Add(key2, value);
                                                    break;
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }

                        if (filename != null)
                        {
                            IniCache.AddCache(filename, lastUpdate, datas);
                        }

                        updated = true;
                    }
                }
            }
        }