コード例 #1
0
        /// <summary>
        /// Read the .toml file into _config
        /// </summary>
        /// <param name="path"></param>
        /// <param name="ret"></param>
        public void ReadFile(string path, out Ret ret)
        {
            _config = new Dictionary <string, Dictionary <string, string> >();
            // Is toml file ?
            string[] paths = FF.StringSplit(path, '/');
            string[] name  = FF.StringSplit(FF.LastOfArray(paths), '.');
            if (FF.LastOfArray(name) != "toml")
            {
                ret = new Ret(LogLevel.Error, RET_NOT_TOML, "File:" + path + " isn't .toml");
                return;
            }
            // Does file exist ?
            if (!File.Exists(path))
            {
                ret = new Ret(LogLevel.Error, RET_NO_FILE, "File:" + path + " doesn't exist");
                return;
            }
            // Push toml to config
            string curSec = "";

            string[] lines = File.ReadAllLines(path);
            foreach (string line in lines)
            {
                string lineTrim = line.Trim();
                if (FF.RegexMatch(lineTrim, @"^#.*"))
                {
                    continue;
                }
                string[] vals = FF.RegexGetValue(lineTrim, @"^\[.+\]$", "[", "]");
                if (vals != null)
                {
                    if (vals.Length == 1)
                    {
                        string section = vals[0];
                        if (!_config.ContainsKey(section))
                        {
                            _config.Add(section, new Dictionary <string, string>());
                        }
                        curSec = section;
                        continue;
                    }
                }
                if (curSec == "")
                {
                    continue;
                }
                string[] kv = lineTrim.Split('=');
                if (kv.Length != 2)
                {
                    continue;
                }
                _config[curSec].Add(kv[0].Trim(), kv[1].Trim());
            }
            ret = Ret.ok;
        }