Пример #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, List <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)
                                        {
                                            List <string> list = new List <string>();
                                            list.Add(value);

                                            datas.Add(key, list);
                                        }
                                        else
                                        {
                                            List <string> list = datas[key];

                                            list.Add(value);
                                        }
                                    }
                                }
                            }
                        }

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

                        updated = true;
                    }
                }
            }

            // calc hash
            StringWriter w = new StringWriter();
            SortedList <string, List <string> > tmp = new SortedList <string, List <string> >();

            foreach (string key in datas.Keys)
            {
                tmp.Add(key, datas[key]);
            }
            foreach (string key in tmp.Keys)
            {
                w.WriteLine("{0}={1}", key, Str.CombineStringArray2(",", tmp[key].ToArray()));
            }

            this.hash_value = Str.HashStrToLong(w.ToString());
        }
Пример #2
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;
                    }
                }
            }
        }