private void InjectCsv(string filePath, string fileName, IniParser.Model.SectionData sectionData) { //var fileWriter = File.OpenWrite(filePath + ".new"); var writer = new StreamWriter(filePath + ".new", false, Encoding.Default); var csvWriter = new CsvHelper.CsvWriter(writer); StreamReader reader = new StreamReader(filePath, Encoding.Default); var csvReader = new CsvHelper.CsvReader(reader); while (csvReader.Read()) { if (csvReader.Row == 2) { var headers = csvReader.FieldHeaders; foreach (var item in headers) { csvWriter.WriteField(item); } csvWriter.NextRecord(); } var ligne = csvReader.CurrentRecord; if (ligne != null) { foreach (var sectionKey in sectionData.Keys) { int valueIndex = ExtractCsvValueIndexFromString(sectionKey.KeyName); string key = ExtractKeyFromString(sectionKey.KeyName); if (ligne.Length > valueIndex) { if (ligne.Any(k => k == key)) { ligne[valueIndex] = sectionKey.Value.RemoveSurroundedQuotes().SanitizeQuotes().Trim(); if (this.RemoveSpecialsChars) { ligne[valueIndex] = ligne[valueIndex].DeleteAccentAndSpecialsChar().RemoveDiacritics(); } } } } foreach (var item in ligne) { csvWriter.WriteField(item); } csvWriter.NextRecord(); } } reader.Close(); reader.Dispose(); csvReader.Dispose(); writer.Close(); writer.Dispose(); // replace old file File.Delete(filePath); File.Move(filePath + ".new", filePath); }
private void InjectIni(string sourceFilePath, string sectionName, IniParser.Model.SectionData translatedSectionData) { var stringIniParser = new IniParser.FileIniDataParser(); stringIniParser.Parser.Configuration.AllowDuplicateKeys = true; stringIniParser.Parser.Configuration.SkipInvalidLines = true; stringIniParser.Parser.Configuration.CommentRegex = new System.Text.RegularExpressions.Regex("^;.*"); var stringInidata = stringIniParser.ReadFile(sourceFilePath, Encoding.Default); foreach (var sectionKey in translatedSectionData.Keys) { var section = sectionKey.KeyName.Split('@').First(); var key = sectionKey.KeyName.Split('@').Last(); var value = sectionKey.Value.SanitizeQuotes().Trim(); if (this.RemoveSpecialsChars) { value = value.DeleteAccentAndSpecialsChar().RemoveDiacritics(); } if (stringInidata.Sections.Any(s => s.SectionName == section)) { var iniSectionData = stringInidata.Sections.GetSectionData(section); if (iniSectionData.Keys.Any(k => k.KeyName == key)) { iniSectionData.Keys.GetKeyData(key).Value = value; } } } stringIniParser.WriteFile(sourceFilePath, stringInidata, Encoding.Default); }
private IDevice LoadGhettoRemoteServo(SectionData data) { return deviceFactory.RemoteServo( data.Keys["geturl"], data.Keys["seturl"], float.Parse(data.Keys["default_angle"])); }
public void change_section_name_with_invalid_name() { var sd = new SectionData("section_test"); sd.SectionName = ""; Assert.That(sd.SectionName, Is.EqualTo("section_test")); }
/// <summary> /// Initializes a new instance of the <see cref="SectionData"/> class /// from a previous instance of <see cref="SectionData"/>. /// </summary> /// <remarks> /// Data is deeply copied /// </remarks> /// <param name="ori"> /// The instance of the <see cref="SectionData"/> class /// used to create the new instance. /// </param> /// <param name="searchComparer"> /// Search comparer. /// </param> public SectionData(SectionData ori, IEqualityComparer<string> searchComparer = null) { SectionName = ori.SectionName; _searchComparer = searchComparer; _leadingComments = new List<string>(ori._leadingComments); _keyDataCollection = new KeyDataCollection(ori._keyDataCollection, searchComparer ?? ori._searchComparer); }
public void check_default_values() { var sd = new SectionData("section_test"); Assert.That(sd, Is.Not.Null); Assert.That(sd.SectionName, Is.EqualTo("section_test")); Assert.That(sd.LeadingComments, Is.Empty); Assert.That(sd.Keys, Is.Empty); }
public void change_section_name() { var sd = new SectionData("section_test"); sd.SectionName = "section_test_2"; Assert.That(sd, Is.Not.Null); Assert.That(sd.SectionName, Is.EqualTo("section_test_2")); Assert.That(sd.LeadingComments, Is.Empty); Assert.That(sd.Keys, Is.Empty); }
public void check_deep_clone() { var section = new SectionData("ori_section"); section.Keys.AddKey("key1", "value1"); section.Keys.AddKey("key2", "value2"); var copy = (SectionData)section.Clone(); copy.Keys["key1"] = "value3"; copy.Keys["key2"] = "value4"; Assert.That(section.Keys["key1"], Is.EqualTo("value1")); Assert.That(section.Keys["key2"], Is.EqualTo("value2")); }
public void try_adding_duplicated_keys_to_section() { string strKeyTest = "Mykey"; var sd = new SectionData("section_test"); //Add key sd.Keys.AddKey(strKeyTest); Assert.That(sd.Keys.Count, Is.EqualTo(1)); Assert.That(sd.Keys.ContainsKey(strKeyTest), Is.True); sd.Keys.AddKey(strKeyTest); Assert.That(sd.Keys.Count, Is.EqualTo(1)); }
public void remove_key_from_section() { string strKeyTest = "Mykey"; var sd = new SectionData("section_test"); //Add key sd.Keys.AddKey(strKeyTest); Assert.That(sd.Keys.Count, Is.EqualTo(1)); Assert.That(sd.Keys.ContainsKey(strKeyTest), Is.True); sd.Keys.RemoveKey(strKeyTest); Assert.That(sd.Keys.Count, Is.EqualTo(0)); Assert.That(sd.Keys.ContainsKey(strKeyTest), Is.False); }
public IDevice LoadGpioMotor(SectionData data) { var flipped = data.Keys["flip"].ContainsAny(new[] { "1", "true" }, StringComparison.OrdinalIgnoreCase); var tweenFactor = data.Keys.ContainsKey("tweenFactor") ? float.Parse(data.Keys["tweenFactor"]) : 0; var speedMultiplier = data.Keys.ContainsKey("speedMultiplier") ? float.Parse(data.Keys["speedMultiplier"]) : 1; var motor = deviceFactory.PwmMotor(int.Parse(data.Keys["pin"]), tweenFactor, speedMultiplier, flipped); // HACK if (data.Keys.ContainsKey("angle")) { var device = (DeviceBase)motor; var angle = double.Parse(data.Keys["angle"]); var vect = Vector2D.YAxis.Rotate(Angle.FromDegrees(angle)); device.AddComponent(DeviceComponentType.DriveWheelForceVector, new VectorComponent((float)vect.X, (float)vect.Y, 0)); } return motor; }
public void add_keys_to_section() { string strKeyTest = "Mykey"; string strValueTest = "My value"; var sd = new SectionData("section_test"); //Add key sd.Keys.AddKey(strKeyTest); Assert.That(sd.Keys.Count, Is.EqualTo(1)); Assert.That(sd.Keys.ContainsKey(strKeyTest), Is.True); //Assign value sd.Keys.GetKeyData(strKeyTest).Value = strValueTest; Assert.That(sd.Keys.GetKeyData(strKeyTest).Value, Is.EqualTo(strValueTest)); }
public void check_adding_sections_to_collection() { var col = new SectionDataCollection(); var exampleSection = new SectionData("section1"); exampleSection.Keys.AddKey("examplekey"); exampleSection.Keys["examplekey"] = "examplevalue"; col.Add(exampleSection); Assert.That(col["section1"], Is.Not.Null); // Add sections directly to the collection Assert.That(col.AddSection("section2"), Is.True); Assert.That(col.AddSection("section2"), Is.False); Assert.That(col["section2"], Is.Not.Null); }
public void check_you_can_merge_sections() { var destinySection = new SectionData("destiny_section"); var newSection= new SectionData("new_section"); //Add key destinySection.Keys.AddKey("key1","value1"); destinySection.Keys.AddKey("key2","value2"); newSection.Keys.AddKey("key2","newvalue2"); newSection.Keys.AddKey("key3","value3"); destinySection.Merge(newSection); Assert.That(destinySection.Keys["key1"], Is.EqualTo("value1")); Assert.That(destinySection.Keys["key2"], Is.EqualTo("newvalue2")); Assert.That(destinySection.Keys.ContainsKey("key3")); Assert.That(destinySection.Keys["key3"], Is.EqualTo("value3")); }
/// <summary> /// Merges otherSection into this, adding new keys if they don't exists /// or overwriting values if the key already exists. /// Comments get appended. /// </summary> /// <remarks> /// Comments are also merged but they are always added, not overwritten. /// </remarks> /// <param name="toMergeSection"></param> public void Merge(SectionData toMergeSection) { #pragma warning disable CS0618 // Typ oder Element ist veraltet foreach (var comment in toMergeSection.LeadingComments) #pragma warning restore CS0618 // Typ oder Element ist veraltet #pragma warning disable CS0618 // Typ oder Element ist veraltet { LeadingComments.Add(comment); } #pragma warning restore CS0618 // Typ oder Element ist veraltet Keys.Merge(toMergeSection.Keys); #pragma warning disable CS0618 // Typ oder Element ist veraltet foreach (var comment in toMergeSection.TrailingComments) #pragma warning restore CS0618 // Typ oder Element ist veraltet #pragma warning disable CS0618 // Typ oder Element ist veraltet { TrailingComments.Add(comment); } #pragma warning restore CS0618 // Typ oder Element ist veraltet }
/// <summary> /// Merges otherSection into this, adding new keys if they don't exists /// or overwriting values if the key already exists. /// Comments get appended. /// </summary> /// <remarks> /// Comments are also merged but they are always added, not overwritten. /// </remarks> /// <param name="toMergeSection"></param> public void Merge(SectionData toMergeSection) { #pragma warning disable CS0618 // Type or member is obsolete foreach (var comment in toMergeSection.LeadingComments) #pragma warning restore CS0618 // Type or member is obsolete { #pragma warning disable CS0618 // Type or member is obsolete LeadingComments.Add(comment); #pragma warning restore CS0618 // Type or member is obsolete } Keys.Merge(toMergeSection.Keys); #pragma warning disable CS0618 // Type or member is obsolete foreach (var comment in toMergeSection.TrailingComments) #pragma warning restore CS0618 // Type or member is obsolete #pragma warning disable CS0618 // Type or member is obsolete { TrailingComments.Add(comment); } #pragma warning restore CS0618 // Type or member is obsolete }
/// <summary> /// Merge the sections into this by overwriting this sections. /// </summary> private void MergeSection(SectionData otherSection) { // no overlap -> create no section if (!Sections.ContainsSection(otherSection.SectionName)) { Sections.AddSection(otherSection.SectionName); } // merge section into the new one Sections.GetSectionData(otherSection.SectionName).Merge(otherSection); }
public void Merge(SectionData toMergeSection) { }
/// <summary> /// Initializes a new instance of the <see cref="SectionData"/> class /// from a previous instance of <see cref="SectionData"/>. /// </summary> /// <remarks> /// Data is deeply copied /// </remarks> /// <param name="ori"> /// The instance of the <see cref="SectionData"/> class /// used to create the new instance.</param> public SectionData(SectionData ori) { _comments = new List <string>(ori._comments); _keyDataCollection = new KeyDataCollection(ori._keyDataCollection); }
public void try_removing_non_existing_key_from_section() { string strKeyTest = "Mykey"; string strValueTest = "My value"; var sd = new SectionData("section_test"); //Add key sd.Keys.AddKey(strKeyTest); sd.Keys.RemoveKey("asdf"); Assert.That(sd.Keys.Count, Is.EqualTo(1)); Assert.That(sd.Keys.ContainsKey(strKeyTest), Is.True); Assert.That(sd.Keys.ContainsKey("asdf"), Is.False); }
private void WriteSection(SectionData section, StringBuilder sb) { //Write section name WriteComments(section.Comments, sb); sb.AppendLine(string.Format("{0}{1}{2}", Configuration.SectionStartChar, section.SectionName, Configuration.SectionEndChar)); WriteKeyValueData(section.Keys, sb); }
/// <summary> /// Initializes a new instance of the <see cref="SectionData"/> class /// from a previous instance of <see cref="SectionData"/>. /// </summary> /// <remarks> /// Data is deeply copied /// </remarks> /// <param name="ori"> /// The instance of the <see cref="SectionData"/> class /// used to create the new instance.</param> public SectionData(SectionData ori) { _leadingComments = new List<string>(ori._leadingComments); _keyDataCollection = new KeyDataCollection(ori._keyDataCollection); }
public void try_accessing_non_existing_key() { var sd = new SectionData("section_test"); //Access invalid keydata Assert.That(sd.Keys["asdf"], Is.Null); }
/// <summary> /// Merges otherSection into this, adding new keys if they don't exists /// or overwriting values if the key already exists. /// Comments get appended. /// </summary> /// <remarks> /// Comments are also merged but they are always added, not overwritten. /// </remarks> /// <param name="toMergeSection"></param> public void Merge(SectionData toMergeSection) { foreach (var comment in toMergeSection.LeadingComments) LeadingComments.Add(comment); Keys.Merge(toMergeSection.Keys); foreach(var comment in toMergeSection.TrailingComments) TrailingComments.Add(comment); }
private void WriteSection(SectionData section, StringBuilder sb) { // Write blank line before section, but not if it is the first line if (sb.Length > 0) sb.AppendLine(); // Write section name WriteComments(section.Comments, sb); sb.AppendLine(string.Format("{0}{1}{2}", Configuration.SectionStartChar, section.SectionName, Configuration.SectionEndChar)); WriteKeyValueData(section.Keys, sb); }