Exemplo n.º 1
0
        /// <summary>
        /// Load INI file
        /// </summary>
        /// <param name="INIStream">A Stream, which contains INI file</param>
        public void Load(Stream INIStream)
        {
            StreamReader sr = new StreamReader(INIStream);

            while (!sr.EndOfStream)
            {
                INIcontent += sr.ReadLine() + Environment.NewLine;
            }

            string SectionName = "INI#";
            Dictionary <string, string> SectionContent = new Dictionary <string, string>();

            foreach (string str in INIcontent.Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries))
            {
                //Console.WriteLine(str);

                if (str.StartsWith(";"))
                {
                    continue;
                }

                if (str.StartsWith("["))
                {
                    if (SectionName != "INI#" && SectionContent.Count > 0)
                    {
                        Sections.Add(SectionName, SectionContent);
                    }
                    SectionName = str.Substring(1, str.Length - 2);
                    if (Sections.ContainsKey(SectionName))
                    {
                        SectionName = SectionName + "dupl" + new Random().Next();
                    }
                    SectionContent = new Dictionary <string, string>();
                    continue;
                }

                string OptionName  = "";
                string OptionValue = "";
                bool   OnValue     = false;
                for (int pos = 0; pos < str.Length; pos++)
                {
                    if (str[pos] == '=')
                    {
                        OnValue = true; continue;
                    }
                    else
                    {
                        if (OnValue)
                        {
                            OptionValue += str[pos];
                            //add parsing for quotes and comments (напр.: name="value\"part in quotes\"";comment )
                        }
                        else
                        {
                            OptionName += str[pos];
                        }
                    }
                }
                if (OptionName == "")
                {
                    OptionName = "noname" + new Random().Next();
                }

                if (!SectionContent.ContainsKey(OptionName))
                {
                    SectionContent.Add(OptionName, OptionValue);
                }
                else
                {
                    SectionContent.Add(OptionName + "=" + new Random().Next(), OptionValue);
                }
            }
            Sections.Add(SectionName, SectionContent);
        }
Exemplo n.º 2
0
        void Parse()
        {
            if (Tegs.Count != 0)
            {
                if (Tegs[0].Position != 0)
                {
                    Text text = new Text();
                    text.Content = SomeNeedOverWrite.CopyStrToStr(Content, 0, Tegs[0].Position);
                    text.Content = FormattingText.DeleteSpace(text.Content);
                    if (text.Content.Length != 0)
                    {
                        SectionContent.Add(text);
                    }
                }
                for (int i = 0; i < Tegs.Count; i++)
                {
                    switch (Tegs[i].TegType)
                    {
                    case "/с/":
                    {
                        Section section = new Section();
                        int     j       = EndTeg(i, "с/");
                        section.Content = SomeNeedOverWrite.CopyStrToStr(Content, Tegs[i].Position + 3, Tegs[j].Position);
                        section.Tegs    = SomeNeedOverWrite.CopyListToList(Tegs, i + 1, j);
                        SectionContent.Add(section);
                        i = j;
                        break;
                    }

                    case "/к/":
                    {
                        Columns column = new Columns();
                        int     j      = EndTeg(i, "к/");
                        column.Content = SomeNeedOverWrite.CopyStrToStr(Content, Tegs[i].Position + 3, Tegs[j].Position);
                        column.Tegs    = SomeNeedOverWrite.CopyListToList(Tegs, i + 1, j);
                        SectionContent.Add(column);
                        i = j;
                        break;
                    }

                    case "/з/":
                    {
                        Title title = new Title();
                        int   j     = EndTeg(i, "з/");
                        title.TitleTx = SomeNeedOverWrite.CopyStrToStr(Content, Tegs[i].Position + 3, Tegs[j].Position);
                        SectionContent.Add(title);
                        i = j;
                        break;
                    }

                    case "/л/":
                    {
                        MarkerList mrList = new MarkerList();
                        int        j      = EndTeg(i, "л/");
                        mrList.Content = SomeNeedOverWrite.CopyStrToStr(Content, Tegs[i].Position + 3, Tegs[j].Position);
                        mrList.Tegs    = SomeNeedOverWrite.CopyListToList(Tegs, i + 1, j);
                        SectionContent.Add(mrList);
                        i = j;
                        break;
                    }
                    }
                    if (i < Tegs.Count - 2)
                    {
                        if (i < Tegs.Count - 1)
                        {
                            if ((Tegs[i + 1].Position - Tegs[i].Position + 4 > 3) && FormattingText.DeleteSpace(SomeNeedOverWrite.CopyStrToStr(Content, Tegs[i].Position + 4, Tegs[i + 1].Position)) != "")
                            {
                                Text text = new Text();
                                text.Content = SomeNeedOverWrite.CopyStrToStr(Content, Tegs[i].Position + 4, Tegs[i + 1].Position);
                                text.Content = FormattingText.DeleteSpace(text.Content);
                                SectionContent.Add(text);
                            }
                        }
                    }
                    if ((i == Tegs.Count - 1) && (Tegs[i].Position + 3 < Content.Length - 1))
                    {
                        Text text = new Text();
                        text.Content = SomeNeedOverWrite.CopyStrToStr(Content, Tegs[i].Position + 4, Content.Length);
                        text.Content = FormattingText.DeleteSpace(text.Content);
                        if (text.Content.Length != 0)
                        {
                            SectionContent.Add(text);
                        }
                    }
                }
            }
            else
            {
                Text text = new Text();
                text.Content = Content;
                SectionContent.Add(text);
            }
        }