diff --git a/Tests/ExporterTestsV4.cs b/Tests/ExporterTestsV4.cs index a1a45a7c..c359771a 100644 --- a/Tests/ExporterTestsV4.cs +++ b/Tests/ExporterTestsV4.cs @@ -14,6 +14,36 @@ public ExporterTestsV4() _eds = new EDSsharp(); } + [Theory] + [InlineData("\n")] + [InlineData("\r\n")] + [InlineData("\r")] + public void ExportUsesLfLineEndings(string newLine) + { + string folder = Path.Combine(Path.GetTempPath(), "eds-line-endings-" + Guid.NewGuid().ToString("N")); + Directory.CreateDirectory(folder); + try + { + _eds.fi.Description = "First line" + newLine + "Second line"; + _eds.ods.Add(0x2000, new ODentry("Test value", 0x2000, DataType.UNSIGNED8, + "0", EDSsharp.AccessType.rw, PDOMappingType.no)); + export(Path.Combine(folder, "OD.c"), _eds); + + foreach (string extension in new[] { ".c", ".h" }) + { + string text = File.ReadAllText(Path.Combine(folder, "OD" + extension)); + Assert.Contains("\n", text); + Assert.DoesNotContain("\r", text); + Assert.EndsWith("\n", text); + } + Assert.Contains("First line\nSecond line", File.ReadAllText(Path.Combine(folder, "OD.h"))); + } + finally + { + Directory.Delete(folder, true); + } + } + void GetExportResult(EDSsharp eds, out IEnumerable cfile, out IEnumerable hfile, out string odname) { var fullPath = Path.GetTempFileName(); diff --git a/libEDSsharp/CanOpenNodeExporter_V4.cs b/libEDSsharp/CanOpenNodeExporter_V4.cs index 8a523412..e8fbcfa7 100644 --- a/libEDSsharp/CanOpenNodeExporter_V4.cs +++ b/libEDSsharp/CanOpenNodeExporter_V4.cs @@ -33,6 +33,21 @@ namespace libEDSsharp /// public class CanOpenNodeExporter_V4 : IExporter, IFileExporter { + // NewLine only controls the terminator appended by WriteLine. Verbatim + // templates can also contain CRLF from a Windows source checkout. + private sealed class LfStreamWriter : StreamWriter + { + public LfStreamWriter(string path) : base(path) + { + NewLine = "\n"; + } + + public override void WriteLine(string value) + { + base.WriteLine(value?.Replace("\r\n", "\n").Replace("\r", "\n")); + } + } + private string odname; private List ODStorageGroups; @@ -416,8 +431,7 @@ private void Export_h(string folderpath, string filename, string gitVersion, EDS if (filename == "") filename = "OD"; - StreamWriter file = new StreamWriter(folderpath + Path.DirectorySeparatorChar + filename + ".h"); - file.NewLine = "\n"; + StreamWriter file = new LfStreamWriter(folderpath + Path.DirectorySeparatorChar + filename + ".h"); file.WriteLine(string.Format( @"/******************************************************************************* @@ -593,8 +607,7 @@ private void Export_c(string folderpath, string filename, string gitVersion, EDS if (filename == "") filename = "OD"; - StreamWriter file = new StreamWriter(folderpath + Path.DirectorySeparatorChar + filename + ".c"); - file.NewLine = "\n"; + StreamWriter file = new LfStreamWriter(folderpath + Path.DirectorySeparatorChar + filename + ".c"); file.WriteLine(string.Format( @"/*******************************************************************************