Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions Tests/ExporterTestsV4.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> cfile, out IEnumerable<String> hfile, out string odname)
{
var fullPath = Path.GetTempFileName();
Expand Down
21 changes: 17 additions & 4 deletions libEDSsharp/CanOpenNodeExporter_V4.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,21 @@ namespace libEDSsharp
/// </summary>
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<string> ODStorageGroups;
Expand Down Expand Up @@ -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(
@"/*******************************************************************************
Expand Down Expand Up @@ -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(
@"/*******************************************************************************
Expand Down