예제 #1
0
        /// <summary>
        ///   Loads settings from the file at the given path.
        /// </summary>
        /// <param name="path">
        ///   The path of the file to load.
        /// </param>
        /// <returns>
        ///   True if settings were loaded successfully, otherwise false.
        /// </returns>
        public bool LoadFromFile(string path)
        {
            List <string> lines = new List <string>();

            {
                try
                {
                    string[] ls = File.ReadAllLines(path);

                    foreach (string l in ls)
                    {
                        string line = StringTools.RemoveUnquotedWhitespace(l);

                        if (line.Length == 0 || line[0] == ';' || line[0] == '#')
                        {
                            continue;
                        }

                        lines.Add(line);
                    }
                }
                catch
                {
                    return(false);
                }
            }

            if (lines[0].ToLower() != "[user]")
            {
                Console.WriteLine("Settings file does not start with [User] header.");
                return(false);
            }

            for (int i = 1; i < lines.Count; i++)
            {
                if (lines[i].Length >= 7 && lines[i].Substring(0, 7).ToLower() == "author=")
                {
                    if (lines[i].Length == 7)
                    {
                        Author = string.Empty;
                    }
                    else
                    {
                        Author = lines[i].Substring(7);

                        if (Author.Length > 2 && Author[0] == '"' && Author[Author.Length - 1] == '"')
                        {
                            Author = Author.Substring(1, Author.Length - 2);
                        }
                    }
                }
                else if (lines[i].Length >= 6 && lines[i].Substring(0, 6).ToLower() == "email=")
                {
                    if (lines[i].Length == 6)
                    {
                        Email = string.Empty;
                    }
                    else
                    {
                        Email = lines[i].Substring(6);

                        if (Email.Length > 2 && Email[0] == '"' && Email[Email.Length - 1] == '"')
                        {
                            Email = Email.Substring(1, Email.Length - 2);
                        }
                    }
                }
                else if (lines[i].Length >= 8 && lines[i].Substring(0, 8).ToLower() == "lineend=")
                {
                    if (lines[i].Length == 8)
                    {
                        LineEnd = string.Empty;
                    }
                    else
                    {
                        LineEnd = lines[i].Substring(8);

                        if (LineEnd.Length > 2 && LineEnd[0] == '"' && LineEnd[LineEnd.Length - 1] == '"')
                        {
                            LineEnd = LineEnd.Substring(1, LineEnd.Length - 2);
                        }
                    }
                }
            }

            return(true);
        }