Example #1
0
        public DBMemory(string file, bool bCacheAble, ReadSectionFunction func)
        {
            if (bCacheAble)
            {
                string bin = file + ".cache";
                if (File.Exists(bin) && File.GetLastWriteTime(bin) > File.GetLastWriteTime(file))
                {
                    try
                    {
                        using (FileStream fs = new FileStream(bin, FileMode.Open, FileAccess.Read))
                        {
                            data = (new DBSerial(fs)).Deserialize() as Hashtable;
                            return;
                        }
                    }
                    catch (Exception e)
                    {
                        Const.Log("DB", "load {0} ERROR: {1}", bin, e.Message);
                    }
                }
            }

            ReadFile(file, data, func);

            if (bCacheAble)
            {
                SaveCache(file);
            }
        }
Example #2
0
        public static bool ReadFile(string file, Hashtable data, ReadSectionFunction readOneSection)
        {
            StreamReader r = new StreamReader(file, System.Text.Encoding.Default);
            string       line;
            string       sec = null;

            while ((line = r.ReadLine()) != null)
            {
                line = FixMacro(line.Replace('\t', ' '));
                if (line == "")
                {
                    continue;
                }

                if (line[0] == '[')
                {
                    if (readOneSection != null)
                    {
                        readOneSection(sec, data);
                    }
                    sec = line.Substring(1, line.Length - 2);
                    data.Remove(sec);
                    continue;
                }
                int pos = line.IndexOf("=");
                if (pos < 0)
                {
                    continue;
                }

                Hashtable h;
                if (sec == null)
                {
                    h = data;
                }
                else
                {
                    h = data[sec] as Hashtable;
                    if (h == null)
                    {
                        h         = new Hashtable();
                        data[sec] = h;
                    }
                }
                string key = line.Substring(0, pos).Trim();
                if (h.Contains(key))
                {
                    object v = h[key];
                    if (v is StringBuilder)
                    {
                        StringBuilder sv = (StringBuilder)v;
                        sv.Append("\t");
                        sv.Append(line.Substring(pos + 1).Trim());
                    }
                    else
                    {
                        h[key] = new StringBuilder((v as string) + "\t" + line.Substring(pos + 1).Trim());
                    }
                }
                else
                {
                    h[key] = line.Substring(pos + 1).Trim();
                }
            }
            r.Close();
            if (readOneSection != null)
            {
                readOneSection(sec, data);
            }
            return(true);
        }