Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for airregphcmd.cs

(view source code of airregphcmd.cs as plain text)

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Reflection;
  7. using System.Text;
  8. using ExcelDataReader;
  9.  
  10.  
  11. namespace RobvanderWoude
  12. {
  13. 	class AirRegPHCmd
  14. 	{
  15. 		static readonly string progver = "1.00";
  16.  
  17. 		static readonly string progdir = Directory.GetParent( new Uri( Assembly.GetExecutingAssembly( ).CodeBase ).LocalPath ).FullName;
  18. 		static string sheet = string.Empty;
  19.  
  20. 		static int Main( string[] args )
  21. 		{
  22. 			int rc = 0;
  23. 			if ( args.Length == 0 )
  24. 			{
  25. 				return ShowHelp( );
  26. 			}
  27.  
  28. 			string regdir = Path.Combine( progdir, "PH" );
  29.  
  30. 			if ( !Directory.Exists( regdir ) )
  31. 			{
  32. 				return ShowHelp( "Directory \"{0}\" not found", regdir );
  33. 			}
  34.  
  35. 			foreach ( string file in Directory.GetFiles( regdir, "*.xlsx" ) )
  36. 			{
  37. 				sheet = Path.GetFullPath( file );
  38. 			}
  39.  
  40. 			if ( string.IsNullOrEmpty( sheet ) )
  41. 			{
  42. 				return ShowHelp( "No Excel sheet found in directory \"{0}\"", regdir );
  43. 			}
  44.  
  45. 			ICSharpCode.SharpZipLib.Zip.ZipConstants.DefaultCodePage = Encoding.UTF8.CodePage;
  46.  
  47. 			foreach ( string arg in args )
  48. 			{
  49. 				rc += AircraftData( arg );
  50. 			}
  51.  
  52. 			return rc;
  53. 		}
  54.  
  55.  
  56. 		public static int AircraftData( string reg )
  57. 		{
  58. 			int rc = 0;
  59.  
  60. 			using ( var stream = File.Open( sheet, FileMode.Open, FileAccess.Read ) )
  61. 			{
  62. 				using ( var reader = ExcelReaderFactory.CreateReader( stream, new ExcelReaderConfiguration( )
  63. 				{
  64. 					FallbackEncoding = Encoding.UTF8
  65. 				} ) )
  66. 				{
  67. 					DataSet dataset = reader.AsDataSet( new ExcelDataSetConfiguration( )
  68. 					{
  69. 						UseColumnDataType = false,
  70. 						ConfigureDataTable = ( tableReader ) => new ExcelDataTableConfiguration( )
  71. 						{
  72. 							EmptyColumnNamePrefix = "Column",
  73. 							UseHeaderRow = true
  74. 						}
  75. 					} );
  76.  
  77. 					var query =
  78. 						from aircraft in dataset.Tables[0].AsEnumerable( )
  79. 						where aircraft.Field<string>( "Registration" ).Replace( "-", "" ).ToUpper( ) == reg.Replace( "-", "" ).ToUpper( )
  80. 						select new
  81. 						{
  82. 							registration = aircraft.Field<string>( "Registration" ),
  83. 							manufacturer = aircraft.Field<string>( "Manufacturer" ),
  84. 							model = aircraft.Field<string>( "Model" )
  85. 						};
  86.  
  87. 					foreach ( var airplane in query )
  88. 					{
  89. 						rc += 1;
  90. 						Console.WriteLine( "{0}\t{1}\t{2}", airplane.registration, airplane.manufacturer, airplane.model );
  91. 					}
  92. 				}
  93.  
  94. 				return rc;
  95. 			}
  96. 		}
  97.  
  98.  
  99. 		#region Error Handling
  100.  
  101. 		public static int ShowHelp( params string[] errmsg )
  102. 		{
  103. 			#region Error Message
  104.  
  105. 			if ( errmsg.Length > 0 )
  106. 			{
  107. 				List<string> errargs = new List<string>( errmsg );
  108. 				errargs.RemoveAt( 0 );
  109. 				Console.Error.WriteLine( );
  110. 				Console.ForegroundColor = ConsoleColor.Red;
  111. 				Console.Error.Write( "ERROR:\t" );
  112. 				Console.ForegroundColor = ConsoleColor.White;
  113. 				Console.Error.WriteLine( errmsg[0], errargs.ToArray( ) );
  114. 				Console.ResetColor( );
  115. 			}
  116.  
  117. 			#endregion Error Message
  118.  
  119. 			#region Help Text
  120.  
  121. 			/*
  122. 			AirRegPHCmd.exe,  Version 1.00
  123. 			Search downloaded Dutch aircraft database (PH-***) for specific aircraft(s)
  124.  
  125. 			Usage:   AirRegPHCmd.exe   PH-***  [ PH-***  [ PH-***  [ ... ] ] ]
  126.  
  127. 			Where:   PH-***            is a Dutch aircraft registration "number"
  128.  
  129. 			Notes:   This program requires a downloaded aircraft registration file in
  130. 			         Excel format, located in the subdirectory "PH"; the Excel file
  131. 			         can be downloaded at:
  132. 			         https://www.ilent.nl/onderwerpen/luchtvaartuigregister/documenten
  133. 			         /publicaties/2019/05/27/luchtvaartuigregister-aircraft-registration
  134. 			         This program requires ExcelDataReader.dll, ExcelDataReader.DataSet.dll
  135. 			         and ICSharpCode.SharpZipLib.dll in the program's parent directory
  136. 			         to read the Excel file; NuGet packages for these DLLs available at
  137. 			         https://www.nuget.org/packages/ExcelDataReader/ and
  138. 			         https://www.nuget.org/packages/ExcelDataReader.DataSet/
  139. 			         Make sure to add the packages under References to recompile this code.
  140. 			         Command line arguments are case insensitive, dashes are not required.
  141. 			         Output is tab-delimited Registration, Manufacturer and Model for each
  142. 			         command line argument.
  143. 			         Return code ("errorlevel") equals the number of registrations found
  144. 			         or -1 in case of errors.
  145.  
  146. 			Written by Rob van der Woude
  147. 			https://www.robvanderwoude.com
  148. 			*/
  149.  
  150. 			Console.Error.WriteLine( );
  151.  
  152. 			Console.Error.WriteLine( "AirRegPHCmd.exe,  Version {0}", progver );
  153.  
  154. 			Console.Error.WriteLine( "Search downloaded Dutch aircraft database (PH-***) for specific aircraft(s)" );
  155.  
  156. 			Console.Error.WriteLine( );
  157.  
  158. 			Console.Error.Write( "Usage:   " );
  159.  
  160. 			Console.ForegroundColor = ConsoleColor.White;
  161. 			Console.Error.WriteLine( "AirRegPHCmd.exe   PH-***  [ PH-***  [ PH-***  [ ... ] ] ]" );
  162. 			Console.ResetColor( );
  163.  
  164. 			Console.Error.WriteLine( );
  165.  
  166. 			Console.Error.Write( "Where:   " );
  167. 			Console.ForegroundColor = ConsoleColor.White;
  168. 			Console.Error.Write( "PH-***" );
  169. 			Console.ResetColor( );
  170. 			Console.Error.WriteLine( "            is a Dutch aircraft registration \"number\"" );
  171.  
  172. 			Console.Error.WriteLine( );
  173.  
  174. 			Console.Error.WriteLine( "Notes:   This program requires a downloaded aircraft registration file in" );
  175.  
  176. 			Console.Error.WriteLine( "         Excel format, located in the subdirectory \"PH\"; the Excel file" );
  177.  
  178. 			Console.Error.WriteLine( "         can be downloaded at:" );
  179.  
  180. 			Console.ForegroundColor = ConsoleColor.DarkGray;
  181. 			Console.Error.WriteLine( "         https://www.ilent.nl/onderwerpen/luchtvaartuigregister/documenten" );
  182.  
  183. 			Console.Error.WriteLine( "         /publicaties/2019/05/27/luchtvaartuigregister-aircraft-registration" );
  184. 			Console.ResetColor( );
  185.  
  186. 			Console.Error.WriteLine( "         This program requires ExcelDataReader.dll, ExcelDataReader.DataSet.dll" );
  187.  
  188. 			Console.Error.WriteLine( "         and ICSharpCode.SharpZipLib.dll in the program's parent directory" );
  189.  
  190. 			Console.Error.WriteLine( "         to read the Excel file; NuGet packages for these DLLs available at" );
  191.  
  192. 			Console.ForegroundColor = ConsoleColor.DarkGray;
  193. 			Console.Error.Write( "         https://www.nuget.org/packages/ExcelDataReader/" );
  194. 			Console.ResetColor( );
  195. 			Console.Error.WriteLine( " and" );
  196.  
  197. 			Console.ForegroundColor = ConsoleColor.DarkGray;
  198. 			Console.Error.WriteLine( "         https://www.nuget.org/packages/ExcelDataReader.DataSet/" );
  199. 			Console.ResetColor( );
  200.  
  201. 			Console.Error.WriteLine( "         Make sure to add the packages under References to recompile this code." );
  202.  
  203. 			Console.Error.WriteLine( "         Command line arguments are case insensitive, dashes are not required." );
  204.  
  205. 			Console.Error.WriteLine( "         Output is tab-delimited Registration, Manufacturer and Model for each" );
  206.  
  207. 			Console.Error.WriteLine( "         command line argument." );
  208.  
  209. 			Console.Error.WriteLine( "         Return code (\"errorlevel\") equals the number of registrations found" );
  210.  
  211. 			Console.Error.WriteLine( "         or -1 in case of errors." );
  212.  
  213. 			Console.Error.WriteLine( );
  214.  
  215. 			Console.Error.WriteLine( "Written by Rob van der Woude" );
  216.  
  217. 			Console.Error.WriteLine( "https://www.robvanderwoude.com" );
  218.  
  219. 			#endregion Help Text
  220.  
  221. 			return -1;
  222. 		}
  223.  
  224. 		#endregion Error Handling
  225. 	}
  226. }

page last modified: 2024-04-16; loaded in 0.0080 seconds