Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unified file exporter interface #131

Merged
merged 8 commits into from
Sep 25, 2024
Merged
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
2 changes: 1 addition & 1 deletion EDSEditorGUI/Form1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -491,13 +491,13 @@
}

// Use our own font.
Font _tabFont = new Font("Arial", (float)10.0, FontStyle.Bold, GraphicsUnit.Pixel);

Check warning on line 494 in EDSEditorGUI/Form1.cs

View workflow job for this annotation

GitHub Actions / build (net6, Debug)

This call site is reachable on all platforms. 'GraphicsUnit.Pixel' is only supported on: 'windows'. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1416)

Check warning on line 494 in EDSEditorGUI/Form1.cs

View workflow job for this annotation

GitHub Actions / build (net6, Debug)

This call site is reachable on all platforms. 'Font' is only supported on: 'windows'. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1416)

Check warning on line 494 in EDSEditorGUI/Form1.cs

View workflow job for this annotation

GitHub Actions / build (net6, Debug)

This call site is reachable on all platforms. 'FontStyle.Bold' is only supported on: 'windows'. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1416)

// Draw string. Center the text.
StringFormat _stringFlags = new StringFormat();
_stringFlags.Alignment = StringAlignment.Center;

Check warning on line 498 in EDSEditorGUI/Form1.cs

View workflow job for this annotation

GitHub Actions / build (net6, Debug)

This call site is reachable on all platforms. 'StringFormat.Alignment' is only supported on: 'windows'. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1416)
_stringFlags.LineAlignment = StringAlignment.Center;

Check warning on line 499 in EDSEditorGUI/Form1.cs

View workflow job for this annotation

GitHub Actions / build (net6, Debug)

This call site is reachable on all platforms. 'StringAlignment.Center' is only supported on: 'windows'. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1416)
g.DrawString(_tabPage.Text, _tabFont, _textBrush, _tabBounds, new StringFormat(_stringFlags));

Check warning on line 500 in EDSEditorGUI/Form1.cs

View workflow job for this annotation

GitHub Actions / build (net6, Debug)

This call site is reachable on all platforms. 'Graphics.DrawString(string?, Font, Brush, RectangleF, StringFormat?)' is only supported on: 'windows'. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1416)
}

private void closeFileToolStripMenuItem_Click(object sender, EventArgs e)
Expand Down Expand Up @@ -643,7 +643,7 @@
Warnings.warning_list.Clear();

CanOpenXDD_1_1 coxdd = new CanOpenXDD_1_1();
coxdd.WriteXML(FileName, dv.eds, this.gitVersion, Path.GetExtension(FileName) == ".xdc", stripped);
coxdd.WriteXML(FileName, dv.eds, Path.GetExtension(FileName) == ".xdc", stripped);
dv.eds.Dirty = false;

if (Warnings.warning_list.Count != 0)
Expand Down
119 changes: 95 additions & 24 deletions EDSSharp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,53 +43,73 @@ static void Main(string[] args)
}


if (argskvp.ContainsKey("--type") && argskvp.ContainsKey("--infile") && argskvp.ContainsKey("--outfile"))
if (argskvp.ContainsKey("--infile") && argskvp.ContainsKey("--outfile"))
{
string infile = argskvp["--infile"];
string outfile = argskvp["--outfile"];
string outtype = "";
if (argskvp.ContainsKey("--type"))
{
outtype = argskvp["--type"];
}

ExporterFactory.Exporter type = ExporterFactory.Exporter.CANOPENNODE_LEGACY; //sensible default

if (argskvp["--type"].IndexOf("4") > 0)
type = ExporterFactory.Exporter.CANOPENNODE_V4;

switch (Path.GetExtension(infile).ToLower())
{
case ".xdd":
openXDDfile(infile, outfile,type);
openXDDfile(infile);
break;

case ".eds":
openEDSfile(infile, outfile,InfoSection.Filetype.File_EDS,type);
openEDSfile(infile);
break;


default:
return;

}
if(eds != null)
{
Export(outfile, outtype);
}
}
else
{
Console.WriteLine("Usage EDSEditor --type [CanOpenNode|CanOpenNodeV4] --infile file.[xdd|eds] --outfile [CO_OD.c|OD]");
PrintHelpText();
}
}
catch(Exception e)
{
Console.WriteLine(e.ToString());
PrintHelpText();
}
}

private static void openEDSfile(string infile, string outfile, InfoSection.Filetype ft, ExporterFactory.Exporter exporttype)
private static void openEDSfile(string infile)
{

eds.Loadfile(infile);
}

exportCOOD(outfile,exporttype);
private static void openXDDfile(string path)
{
CanOpenXDD_1_1 coxml_1_1 = new CanOpenXDD_1_1();
eds = coxml_1_1.ReadXML(path);

if (eds == null)
{
CanOpenXDD coxml = new CanOpenXDD();
eds = coxml.readXML(path);

if (eds == null)
return;
}

eds.projectFilename = path;
}

private static void exportCOOD(string outpath,ExporterFactory.Exporter type)
private static void Export(string outpath, string outType)
{
outpath = Path.GetFullPath(outpath);

Expand All @@ -99,10 +119,15 @@ private static void exportCOOD(string outpath,ExporterFactory.Exporter type)

Warnings.warning_list.Clear();

IExporter exporter = ExporterFactory.getExporter(type);
var filepath = Path.Combine(savePath, Path.GetFileNameWithoutExtension(outpath));
var exporterDef = FindMatchingExporter(outpath, outType);

exporter.export(filepath, eds);
if(exporterDef == null)
{
throw new Exception("Unable to find matching exporter)");
}

var edss = new List<EDSsharp> { eds };
exporterDef.Func(outpath, edss);

foreach(string warning in Warnings.warning_list)
{
Expand All @@ -111,22 +136,68 @@ private static void exportCOOD(string outpath,ExporterFactory.Exporter type)

}

private static void openXDDfile(string path, string outpath,ExporterFactory.Exporter exportertype)
static ExporterDescriptor FindMatchingExporter(string outpath, string outType)
{
CanOpenXDD_1_1 coxml_1_1 = new CanOpenXDD_1_1();
eds = coxml_1_1.ReadXML(path);
//Find exporter(s) matching the file extension
var exporters = Filetypes.GetExporters();

if (eds == null)
var outFiletype = Path.GetExtension(outpath);
var exporterMatchingFiletype = new List<ExporterDescriptor>();
foreach (var exporter in exporters)
{
CanOpenXDD coxml = new CanOpenXDD();
eds = coxml.readXML(path);
foreach (var type in exporter.Filetypes)
{
if (type == outFiletype)
{
exporterMatchingFiletype.Add(exporter);
break;
}
}
}

if (eds == null)
return;
if (exporterMatchingFiletype.Count == 1)
{
//If only one match we use that one.
return exporterMatchingFiletype[0];
}

eds.projectFilename = path;
exportCOOD(outpath,exportertype);
//If multiple or zero matches use type
foreach (var exporter in exporters)
{
if (exporter.Description.Replace(" ", null) == outType)
{
return exporter;
}
}
return null;
}

static void PrintHelpText()
{
Console.WriteLine("Usage: EDSEditor --infile file.[xdd|eds] --outfile [valid output file] [OPTIONAL] --type [exporter type]");
Console.WriteLine("The output file format depends on --outfile extension and --type");
Console.WriteLine("If --outfile extension matcher one exporter then --type IS NOT needed");
Console.WriteLine("If --outfile extension matcher multiple exporter then --type IS needed");
Console.WriteLine("If --outfile has no extension --type IS needed");
Console.WriteLine("Exporter types:");

var exporters = Filetypes.GetExporters();
foreach (var exporter in exporters)
{
string filetypes = "";
for (int i = 0; i < exporter.Filetypes.Length; i++)
{
filetypes += exporter.Filetypes[i];
//add seperator char if multiple filetypes
if(i +1 != exporter.Filetypes.Length)
{
filetypes += ',';
}
}

string description = $" {exporter.Description.Replace(" ",null)} [{filetypes}]";
Console.WriteLine(description);
}
}
}
}
32 changes: 31 additions & 1 deletion Tests/CLITest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Diagnostics;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using Xunit;

Expand All @@ -8,6 +9,13 @@ public class CliTest : libEDSsharp.EDSsharp
{
string RunEDSSharp(string arguments)
{
File.Delete("Legacy.c");
File.Delete("Legacy.h");
File.Delete("V4.c");
File.Delete("V4.h");
File.Delete("file.eds");
File.Delete("file.xpd");

Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
Expand All @@ -33,5 +41,27 @@ public void XddToCanOpenNodeV4()
string[] files = Directory.GetFiles(".", "V4.*");
Assert.Equal(2, files.Length);
}
[Fact]
public void OnlySingleExporterByExtensionPossible()
{
RunEDSSharp("--infile minimal_project.xdd --outfile file.eds");
string[] files = Directory.GetFiles(".", "file.eds");
Assert.Single(files);
}
[Fact]
public void MultipleExporterByExtensionPossibleWithoutType()
{
//this should fail
RunEDSSharp("--infile minimal_project.xdd --outfile file.xdd");
string[] files = Directory.GetFiles(".", "file.xdd");
Assert.Empty(files);
}
[Fact]
public void MultipleExporterByExtensionPossibleWithType()
{
RunEDSSharp("--type CanOpenXDDv1.1 --infile minimal_project.xdd --outfile file.nxdd");
string[] files = Directory.GetFiles(".", "file.nxdd");
Assert.Single(files);
}
}
}
18 changes: 17 additions & 1 deletion libEDSsharp/CanOpenNodeExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ namespace libEDSsharp
/// <summary>
/// Export .c and .h files for CanOpenNode v1-3
/// </summary>
public class CanOpenNodeExporter : IExporter
public class CanOpenNodeExporter : IExporter, IFileExporter
{

private string folderpath;
Expand All @@ -59,6 +59,22 @@ public class CanOpenNodeExporter : IExporter
ODentry maxRXmappingsOD=null;
ODentry maxTXmappingsOD=null;

/// <summary>
/// Fetches all the different fileexporter types the class supports
/// </summary>
/// <returns>List of the different exporters the class supports</returns>
public ExporterDescriptor[] GetExporters()
{
return new ExporterDescriptor[]
{
new ExporterDescriptor("CanOpenNode", new string[] { ".h", ".c" }, ExporterDescriptor.ExporterFlags.CanOpenNode, delegate (string filepath, List<EDSsharp> edss)
{
var e = new CanOpenNodeExporter();
e.export(filepath, edss[0]);
})
};
}

/// <summary>
/// Register names of index and subindex that need to have standard names to be able to work with CanOpenNode
/// </summary>
Expand Down
18 changes: 17 additions & 1 deletion libEDSsharp/CanOpenNodeExporter_V4.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ namespace libEDSsharp
/// <summary>
/// Exporter for CanOpenNode_V4
/// </summary>
public class CanOpenNodeExporter_V4 : IExporter
public class CanOpenNodeExporter_V4 : IExporter, IFileExporter
{
private string odname;

Expand All @@ -47,6 +47,22 @@ public class CanOpenNodeExporter_V4 : IExporter
private Dictionary<string, UInt16> ODCnt;
private Dictionary<string, int> ODArrSize;

/// <summary>
/// Fetches all the different fileexporter types the class supports
/// </summary>
/// <returns>List of the different exporters the class supports</returns>
public ExporterDescriptor[] GetExporters()
{
return new ExporterDescriptor[]
{
new ExporterDescriptor("CanOpenNodeV4", new string[] { ".h", ".c" }, ExporterDescriptor.ExporterFlags.CanOpenNode, delegate (string filepath, List<EDSsharp> edss)
{
var e = new CanOpenNodeExporter_V4();
e.export(filepath, edss[0]);
})
};
}

/// <summary>
/// export the current data set in the CanOpen Node format V4
/// </summary>
Expand Down
23 changes: 22 additions & 1 deletion libEDSsharp/CanOpenXDD.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,30 @@

namespace libEDSsharp
{
public class CanOpenXDD
public class CanOpenXDD : IFileExporter
{
public ISO15745ProfileContainer dev;

/// <summary>
/// Fetches all the different fileexporter types the class supports
/// </summary>
/// <returns>List of the different exporters the class supports</returns>
public ExporterDescriptor[] GetExporters()
{
return new ExporterDescriptor[] {
new ExporterDescriptor("CanOpen XDD v1.0", new string[] { ".xdd" }, 0, delegate (string filepath, List<EDSsharp> edss)
{
var e = new CanOpenXDD();
e.writeXML(filepath,edss[0]);
}),
new ExporterDescriptor("CanOpen Network v1.0", new string[] { ".nxdd" }, ExporterDescriptor.ExporterFlags.MultipleNodeSupport, delegate (string filepath, List<EDSsharp> edss)
{
var e = new CanOpenXDD();
e.writeMultiXML(filepath,edss);
})
};
}

public EDSsharp readXML(string file)
{

Expand Down Expand Up @@ -765,7 +786,7 @@
productID = ApplicationLayers.identity.productID.Value;
}

if (ApplicationLayers.identity.buildDate != null)

Check warning on line 789 in libEDSsharp/CanOpenXDD.cs

View workflow job for this annotation

GitHub Actions / build (net6, Debug)

The result of the expression is always 'true' since a value of type 'DateTime' is never equal to 'null' of type 'DateTime?'

Check warning on line 789 in libEDSsharp/CanOpenXDD.cs

View workflow job for this annotation

GitHub Actions / build (net6, Release)

The result of the expression is always 'true' since a value of type 'DateTime' is never equal to 'null' of type 'DateTime?'
{
buildDate = ApplicationLayers.identity.buildDate;
}
Expand Down
Loading
Loading