Advertisement
alesi2000

ASAP2ParserExample

Apr 13th, 2012 (edited)
4,047
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.15 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Windows.Forms;
  4. using System.Collections.Generic;
  5. using jnsoft.ASAP2;
  6. using jnsoft.ASAP2.Formulas;
  7. using jnsoft.Helpers;
  8.  
  9. /// <summary>
  10. /// Sample console application.
  11. ///
  12. /// Demonstrating a simple csv output of
  13. /// some ASAP2 nodes from the specified A2L file content.
  14. ///
  15. /// Usage: ASAP2ParserExample A2LFile.a2l > test.csv
  16. /// Does an csv output to stdout, watch result by
  17. /// opening the test.csv with excel or something.
  18. /// </summary>
  19. class Program
  20. {
  21.   static void Main(string[] args)
  22.   {
  23.     if (args.Length == 0)
  24.     { // no args -> present usage
  25.       Console.WriteLine($"{Application.ProductName}({Application.ProductVersion})");
  26.       Console.WriteLine("\t Parse the specified A2L file");
  27.       Console.WriteLine("\t and do some example output of A2L contents");
  28.       Console.WriteLine($"Usage: {Application.ProductName} A2LFile.a2l");
  29.       return;
  30.     }
  31.  
  32.     if (!File.Exists(args[0]))
  33.     { // file does not exist
  34.       Console.WriteLine($"File '{args[0]}' does not exist!");
  35.       return;
  36.     }
  37.  
  38.     var prevColor = Console.ForegroundColor;
  39.     try
  40.     { // parse specified A2L file
  41.       // create parser and register parser events
  42.       using (var a2lParser = new A2LParser())
  43.       {
  44.         a2lParser.ParserMessage += onParserMessage;
  45.         a2lParser.ProgressChanged += onProgressChanged;
  46.         if (!a2lParser.parse(args[0]))
  47.         { // parsing failed
  48.           Console.WriteLine($"File '{args[0]}' seems not to be an A2L file");
  49.           return;
  50.         }
  51.  
  52.         // Output the system constants
  53.         Console.ForegroundColor = ConsoleColor.Cyan;
  54.         var modpar = a2lParser.Project.getNode<A2LMODULE>(false).getNode<A2LMODPAR>(false);
  55.         if (modpar != null && modpar.SystemConstants.Count > 0)
  56.         { // system contstans available
  57.           Console.WriteLine("\n\nSYSTEM_CONSTANTs...");
  58.           Console.WriteLine("SYSTEM_CONSTANT;Formula;Value");
  59.           Dictionary<string, string>.Enumerator en = modpar.SystemConstants.GetEnumerator();
  60.           while (en.MoveNext())
  61.           { // iterate through defined constants
  62.             A2LFormula formula;
  63.             a2lParser.Project.FormulaDict.TryGetValue(en.Current.Key, out formula);
  64.             Console.WriteLine("{0};{1};{2}"
  65.               , en.Current.Key, en.Current.Value
  66.               , formula != null ? formula.toPhysical(0).ToString() : "not defined"
  67.               );
  68.           }
  69.         }
  70.  
  71.         // Output the measurements
  72.         Console.WriteLine("\n\nMEASUREMENTs...");
  73.         Console.WriteLine("Measurment;DataType;Address;Min;Max;Description");
  74.         foreach (var measurement in a2lParser.Project.enumChildNodes<A2LMEASUREMENT>())
  75.           Console.WriteLine("{0};{1};{2};{3};{4};{5}"
  76.             , measurement.Name, measurement.DataType
  77.             , $"0x{measurement.Address.ToString("X8")}"
  78.             , measurement.LowerLimit, measurement.UpperLimit, measurement.Description
  79.             );
  80.  
  81.         // Output Characteristics from project
  82.         Console.WriteLine("\n\nCHARACTERISTICs...");
  83.         Console.WriteLine("Characteristic;Type;Address;Min;Max;Description");
  84.         foreach (var characteristic in a2lParser.Project.enumChildNodes<A2LCHARACTERISTIC>())
  85.           Console.WriteLine("{0};{1};{2};{3};{4};{5}"
  86.             , characteristic.Name, characteristic.CharType
  87.             , $"0x{characteristic.Address.ToString("X8")}"
  88.             , characteristic.LowerLimit, characteristic.UpperLimit, characteristic.Description
  89.             );
  90.  
  91.         // Output CompuMethods from dictionary
  92.         // use the pre calculated dictionary
  93.         Console.WriteLine("\n\nCOMPUMETHODs...");
  94.         Console.WriteLine("CompuMethod;Type;Description");
  95.         foreach (var compuMethod in a2lParser.Project.CompDict.Values)
  96.           Console.WriteLine("{0};{1};{2}", compuMethod.Name, compuMethod.ConversionType, compuMethod.Description);
  97.       }
  98.     }
  99.     catch (Exception ex)
  100.     { // should not occur...
  101.       Console.ForegroundColor = ConsoleColor.Red;
  102.       Console.WriteLine("File '{0}' seems not to be an A2L file, caught exception:\n{1}\nStacktrace\n{2}"
  103.         , args[0], ex.Message, ex.StackTrace
  104.         );
  105.     }
  106.     finally
  107.     { // reset starting color
  108.       Console.ForegroundColor = prevColor;
  109.     }
  110.   }
  111.   /// <summary>
  112.   /// Handles parser progress events.
  113.   /// </summary>
  114.   /// <param name="sender"></param>
  115.   /// <param name="e"></param>
  116.   static void onProgressChanged(object sender, A2LParserProgressEventArgs e)
  117.   {
  118.     Console.ForegroundColor = ConsoleColor.Blue;
  119.     Console.WriteLine($"{e.LinesProcessed * 100 / e.LinesMax}% processed.");
  120.   }
  121.   /// <summary>
  122.   /// Handles parser message events.
  123.   /// </summary>
  124.   /// <param name="sender"></param>
  125.   /// <param name="e"></param>
  126.   static void onParserMessage(object sender, ParserEventArgs e)
  127.   {
  128.     Console.ForegroundColor = e.Type == MessageType.Error
  129.       ? ConsoleColor.Red
  130.       : e.Type == MessageType.Warning
  131.         ? ConsoleColor.Yellow
  132.         : ConsoleColor.White;
  133.     Console.WriteLine($"{e.Type}:\t{e.Message}({(e.Source != null ? ((A2LNODE)e.Source).Name : string.Empty)})");
  134.   }
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement