using System;
using System.IO;
using System.Windows.Forms;
using System.Collections.Generic;
using jnsoft.ASAP2;
using jnsoft.ASAP2.Formulas;
using jnsoft.Helpers;
/// <summary>
/// Sample console application.
///
/// Demonstrating a simple csv output of
/// some ASAP2 nodes from the specified A2L file content.
///
/// Usage: ASAP2ParserExample A2LFile.a2l > test.csv
/// Does an csv output to stdout, watch result by
/// opening the test.csv with excel or something.
/// </summary>
class Program
{
static void Main(string[] args)
{
if (args.Length == 0)
{ // no args -> present usage
Console.WriteLine($"{Application.ProductName}({Application.ProductVersion})");
Console.WriteLine("\\t Parse the specified A2L file");
Console.WriteLine("\\t and do some example output of A2L contents");
Console.WriteLine($"Usage: {Application.ProductName} A2LFile.a2l");
return;
}
if (!File.Exists(args[0]))
{ // file does not exist
Console.WriteLine($"File \'{args[0]}\' does not exist!");
return;
}
var prevColor = Console.ForegroundColor;
try
{ // parse specified A2L file
// create parser and register parser events
using (var a2lParser = new A2LParser())
{
a2lParser.ParserMessage += onParserMessage;
a2lParser.ProgressChanged += onProgressChanged;
if (!a2lParser.parse(args[0]))
{ // parsing failed
Console.WriteLine($"File \'{args[0]}\' seems not to be an A2L file");
return;
}
// Output the system constants
Console.ForegroundColor = ConsoleColor.Cyan;
var modpar = a2lParser.Project.getNode<A2LMODULE>(false).getNode<A2LMODPAR>(false);
if (modpar != null && modpar.SystemConstants.Count > 0)
{ // system contstans available
Console.WriteLine("\\n\\nSYSTEM_CONSTANTs...");
Console.WriteLine("SYSTEM_CONSTANT;Formula;Value");
Dictionary<string, string>.Enumerator en = modpar.SystemConstants.GetEnumerator();
while (en.MoveNext())
{ // iterate through defined constants
A2LFormula formula;
a2lParser.Project.FormulaDict.TryGetValue(en.Current.Key, out formula);
Console.WriteLine("{0};{1};{2}"
, en.Current.Key, en.Current.Value
, formula != null ? formula.toPhysical(0).ToString() : "not defined"
);
}
}
// Output the measurements
Console.WriteLine("\\n\\nMEASUREMENTs...");
Console.WriteLine("Measurment;DataType;Address;Min;Max;Description");
foreach (var measurement in a2lParser.Project.enumChildNodes<A2LMEASUREMENT>())
Console.WriteLine("{0};{1};{2};{3};{4};{5}"
, measurement.Name, measurement.DataType
, $"0x{measurement.Address.ToString("X8")}"
, measurement.LowerLimit, measurement.UpperLimit, measurement.Description
);
// Output Characteristics from project
Console.WriteLine("\\n\\nCHARACTERISTICs...");
Console.WriteLine("Characteristic;Type;Address;Min;Max;Description");
foreach (var characteristic in a2lParser.Project.enumChildNodes<A2LCHARACTERISTIC>())
Console.WriteLine("{0};{1};{2};{3};{4};{5}"
, characteristic.Name, characteristic.CharType
, $"0x{characteristic.Address.ToString("X8")}"
, characteristic.LowerLimit, characteristic.UpperLimit, characteristic.Description
);
// Output CompuMethods from dictionary
// use the pre calculated dictionary
Console.WriteLine("\\n\\nCOMPUMETHODs...");
Console.WriteLine("CompuMethod;Type;Description");
foreach (var compuMethod in a2lParser.Project.CompDict.Values)
Console.WriteLine("{0};{1};{2}", compuMethod.Name, compuMethod.ConversionType, compuMethod.Description);
}
}
catch (Exception ex)
{ // should not occur...
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("File \'{0}\' seems not to be an A2L file, caught exception:\\n{1}\\nStacktrace\\n{2}"
, args[0], ex.Message, ex.StackTrace
);
}
finally
{ // reset starting color
Console.ForegroundColor = prevColor;
}
}
/// <summary>
/// Handles parser progress events.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
static void onProgressChanged(object sender, A2LParserProgressEventArgs e)
{
Console.ForegroundColor = ConsoleColor.Blue;
Console.WriteLine($"{e.LinesProcessed * 100 / e.LinesMax}% processed.");
}
/// <summary>
/// Handles parser message events.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
static void onParserMessage(object sender, ParserEventArgs e)
{
Console.ForegroundColor = e.Type == MessageType.Error
? ConsoleColor.Red
: e.Type == MessageType.Warning
? ConsoleColor.Yellow
: ConsoleColor.White;
Console.WriteLine($"{e.Type}:\\t{e.Message}({(e.Source != null ? ((A2LNODE)e.Source).Name : string.Empty)})");
}
}