예제 #1
0
 internal eFind Find(List<Section> mySections,string sSection, string Key, ref Section sect, ref Key xKey)
 {
     eFind eres = eFind.SECTION_NOT_FOUND;
     foreach (Section sec in mySections)
     {
         if (sec.Name.Equals(sSection))
         {
             sect = sec;
             eres = eFind.KEY_NOT_FOUND;
             foreach (Key xkey in sec.Keys)
             {
                 if (xkey.Name.Equals(Key))
                 {
                     xKey = xkey;
                     return eFind.OK;
                 }
             }
             return eres;
         }
     }
     return eres;
 }
예제 #2
0
        internal bool WritePrivateProfileString(string sSection, string Key, string sLiteralString, ref string Err)
        {
            Sections.Clear();
            eReadSections eres = ReadSections(ref Sections, ref Err);
            switch (eres)
            {
                case eReadSections.OK:
                    Key mykey = null;
                    Section sect = null;
                    eFind efres = Find(this.Sections,sSection,Key,ref  sect, ref mykey);
                    switch (efres)
                    {
                        case eFind.OK:
                            mykey.sValue = sLiteralString;
                            if (Write(ref Err))
                            {
                                return true;
                            }
                            break;
                        case eFind.SECTION_NOT_FOUND:
                            sect = new Section(sSection);
                            mykey = new Key(Key, sLiteralString);
                            sect.Keys.Add(mykey);
                            Sections.Add(sect);
                            if (Write(ref Err))
                            {
                                return true;
                            }
                            break;

                        case eFind.KEY_NOT_FOUND:
                            mykey = new Key(Key, sLiteralString);
                            sect.Keys.Add(mykey);
                            if (Write(ref Err))
                            {
                                return true;
                            }
                            break;
                    }
                    return false;
                case eReadSections.FILE_NOT_EXISTS:
                    sect = new Section(sSection);
                    mykey = new Key(Key, sLiteralString);
                    sect.Keys.Add(mykey);
                    Sections.Add(sect);
                    if (Write(ref Err))
                    {
                        return true;
                    }
                    return false;
                case eReadSections.ERROR:
                    return false;
            }
            return false;
        }
예제 #3
0
        public void Format(IniFormatOptions options = null)
        {
            options = options ?? IniFormatOptions.Default;

            for (int s = 0; s < this.Count; s++)
            {
                Section section = this[s];

                // Reset padding for each minor item in the section
                FormatMinorItems(section.Items);

                // Insert blank line between sections, if specified by options
                if (options.EnsureBlankLineBetweenSections)
                {
                    if (s > 0 && (!section.Items.Any() || !(section.Items[0] is BlankLine)))
                    {
                        section.Items.Insert(0, new BlankLine());
                    }
                }

                // Reset padding for the section itself
                section.Padding.Reset();

                for (int p = 0; p < section.Count; p++)
                {
                    Property property = section[p];

                    // Reset padding for each minor item in the property
                    FormatMinorItems(property.Items);

                    // Insert blank line between properties, if specified
                    if (options.EnsureBlankLineBetweenProperties)
                    {
                        if (p > 0 && (!property.Items.Any() || !(property.Items[0] is BlankLine)))
                        {
                            property.Items.Insert(0, new BlankLine());
                        }
                    }

                    // Reset padding for the property itself
                    property.Padding.Reset();
                }
            }

            // Remove any trailing blank lines
            for (int i = TrailingItems.Count - 1; i >= 0; i--)
            {
                // Non blank lines are fine, so when we encounter the first non blank line, exit
                // this loop.
                if (!(TrailingItems[i] is BlankLine))
                {
                    break;
                }
                if (TrailingItems[i] is BlankLine)
                {
                    TrailingItems.RemoveAt(i);
                }
            }

            // Format any remaining trailing items
            FormatMinorItems(TrailingItems);
        }
예제 #4
0
 internal eReadSections ReadSections(ref List<Section> mySections, ref string Err)
 {
     Sections.Clear();
     string mySectionName = null;
     Section CurrentSection = null;
     if (File.Exists(inifile))
     {
         try
         {
             string[] sLines = File.ReadAllLines(inifile);
             foreach (string sline in sLines)
             {
                 if (IsNewSection(ref mySectionName,sline))
                 {
                     if (CurrentSection != null)
                     {
                         CurrentSection = new Section(mySectionName);
                         mySections.Add(CurrentSection);
                     }
                     else
                     {
                         CurrentSection = new Section(mySectionName);
                         mySections.Add(CurrentSection);
                     }
                 }
                 else
                 {
                     string Key = null;
                     string sValue = null;
                     if (GetKeyAndValue(sline, ref Key, ref sValue))
                     {
                         Key key = new Key(Key, sValue);
                         CurrentSection.Keys.Add(key);
                     }
                 }
             }
             return eReadSections.OK;
         }
         catch (Exception ex)
         {
             Err = "ERROR:IniFile Exception = " + ex.Message;
             return eReadSections.ERROR;
         }
     }
     else
     {
         return eReadSections.FILE_NOT_EXISTS;
     }
 }
예제 #5
0
        public void RecursiveSaveAndLoad(int repeats)
        {
            if (repeats < 2)
            {
                repeats = 2;
            }
            if (repeats > 30)
            {
                repeats = 30;
            }

            var config = new Ini.Config();

            var section1Name     = "Number Tests";
            var section2Name     = "String Tests";
            var section1Var1Name = "IntArray";
            var section1Var2Name = "DoubleValue";
            var section2Var1Name = "StringArray";
            var section2Var2Name = "String";

            var s1var1value          = new int[] { 1, 2, 3 };
            var s1var1valueEscaped   = "{ 1, 2, 3 }";
            var s1var2value          = 0.5d;
            var s1var2valueFormatted = "0.5";
            var s2var1value          = new string[] { "abc", "xyz" };
            var s2var1valueEscaped   = "{ abc, xyz }";
            var s2var2value          = "Bad\"#";
            var s2var2valueEscaped   = "\"Bad\\\"#\"";

            var section1 = new Ini.Section(section1Name);

            section1.Add(new Ini.Setting()
            {
                Name = section1Var1Name, IntArray = s1var1value
            });
            section1.Add(new Ini.Setting()
            {
                Name = section1Var2Name, Double = s1var2value
            });
            config.Add(section1);

            var section2 = new Ini.Section(section2Name);

            section2.Add(new Ini.Setting()
            {
                Name = section2Var1Name, Array = s2var1value
            });
            section2.Add(new Ini.Setting()
            {
                Name = section2Var2Name, Value = s2var2value
            });
            config.Add(section2);


            const string incrName = "Incrementor";
            const string rollName = "Roller";

            var section3 = new Section(incrName);

            section3.Add(new Setting()
            {
                Name = rollName, Int = 0
            });
            config.Add(section3);

            var data = config.GetLines();

            for (int i = 0; i < repeats; i++)
            {
                config = Ini.Config.FromData(data);                 // read written config

                Assert.AreEqual(3, config.ItemCount);
                var s1 = config.Get(section1Name);
                var s2 = config.Get(section2Name);

                Assert.IsNotNull(s1);
                Assert.IsNotNull(s2);

                var s1v1 = s1.Get(section1Var1Name);
                var s1v2 = s1.Get(section1Var2Name);
                var s2v1 = s2.Get(section2Var1Name);
                var s2v2 = s2.Get(section2Var2Name);

                Assert.IsNotNull(s1v1);
                Assert.IsNotNull(s1v2);
                Assert.IsNotNull(s2v1);
                Assert.IsNotNull(s2v2);

                Assert.AreEqual(s1var1valueEscaped, new ExposedSetting(s1v1).ExposedEscapedValue);
                Assert.AreEqual(s1var2valueFormatted, s1v2.Value);
                Assert.AreEqual(s2var1valueEscaped, new ExposedSetting(s2v1).ExposedEscapedValue);
                Assert.AreEqual(s2var2valueEscaped, new ExposedSetting(s2v2).ExposedEscapedValue);

                int num = config[incrName][rollName].Int;
                Assert.AreEqual(i, num);
                config[incrName][rollName].Int = num + 1;

                data = config.GetLines();                 // re-write config
            }
        }