Exemplo n.º 1
0
        /// <summary>
        ///     Returns this <see cref="IniFile" /> Contents in Ini Format
        /// </summary>
        public override string ToString()
        {
            StringBuilder sb = new StringBuilder();

            for (int i = 0; i < Sections.Count; i++)
            {
                IniSection s = Sections[i];

                if (s.Comments.Comments.Any())
                {
                    sb.AppendLine(s.Comments.ToString());
                }
                sb.AppendLine(s.ToString());

                foreach (IniKey k in s.Keys)
                {
                    if (k.Comments.Comments.Any())
                    {
                        sb.AppendLine(k.Comments.ToString());
                    }
                    sb.AppendLine(k.ToString());
                }

                if (i < Sections.Count - 1)
                {
                    sb.AppendLine();
                }
            }

            if (Comments.Comments.Any())
            {
                sb.AppendLine();
                sb.AppendLine(Comments.ToString());
            }

            return(sb.ToString());
        }
Exemplo n.º 2
0
        public static IniFile Parse(string iniString)
        {
            IniFile ini = new IniFile();

            var lines = (from line in iniString.Split('\n')
                         //
                         let trimmed = line.Trim()
                         //
                                       select trimmed.TrimEnd('\r')).ToArray();
            var        comments    = new List <string>();
            IniSection section     = null;
            bool       hasComments = false;

            for (int i = 0; i < lines.Length; i++)
            {
                string line = lines[i];

                if (String.IsNullOrEmpty(line))
                {
                    continue;
                }

                if (IsComment(line))
                {
                    string comment = GetComment(line);
                    comments.Add(comment);
                    if (IsVariable(comment))
                    {
                        var    cVar  = GetVariable(comment);
                        string name  = cVar[0];
                        string value = cVar[1];
                        Environment.SetEnvironmentVariable(name, value);
                    }
                    hasComments = true;
                    continue;
                }

                if (IsSection(line))
                {
                    section = ini[GetSection(line)];

                    if (hasComments)
                    {
                        section.Comments.Append(comments.ToArray());
                        comments.Clear();
                        hasComments = false;
                    }
                    continue;
                }

                if (IsKey(line))
                {
                    if (section == null)
                    {
                        throw new Exception(string.Format("{0}: Sectionless Key Value Pair", i));
                    }

                    var    keyArr = GetKey(line);
                    string k      = keyArr[0];
                    string v      = keyArr[1];

                    if (hasComments)
                    {
                        section[k].Comments.Append(comments.ToArray());
                        comments.Clear();
                        hasComments = false;
                    }

                    section[k].Value = v;
                }
            }
            if (hasComments)
            {
                ini.Comments.Append(comments.ToArray());
            }
            return(ini);
        }
Exemplo n.º 3
0
        /// <summary>
        ///     Creates or Returns an existing <see cref="IniSection" />
        /// </summary>
        /// <param name="section">Section Name</param>
        public IniSection CreateSection(string section)
        {
            IniSection get = GetSection(section);
            if (get != null)
                return get;

            IniSection gen = new IniSection(section);
            _sections.Add(gen);
            return gen;
        }