static void Main ( string [ ] args ) { const int EXTRA_LEADING_CHARACTERS = 19; const string INTERNAL_ERROR_OO2_PREFIX = @"INTERNAL PROCESSING ERROR 002:"; const string INTERNAL_ERROR_OO3_PREFIX = @"INTERNAL PROCESSING ERROR 003:"; const string INTERNAL_ERROR_OO4_PREFIX = @"INTERNAL PROCESSING ERROR 004:"; const string INTERNAL_ERROR_OO5_PREFIX = @"INTERNAL PROCESSING ERROR 005:"; s_theApp = ConsoleAppStateManager.GetTheSingleInstance ( ); s_theApp.DisplayBOJMessage ( ); s_theApp.BaseStateManager.AppExceptionLogger.OptionFlags = s_theApp.BaseStateManager.AppExceptionLogger.OptionFlags | ExceptionLogger.OutputOptions.Stack | ExceptionLogger.OutputOptions.EventLog | ExceptionLogger.OutputOptions.StandardError; s_theApp.BaseStateManager.LoadErrorMessageTable ( s_astrErrorMessages ); try { Console.WriteLine ( "Namespace of Entry Point Routine {0} = {1} " , System.Reflection.MethodBase.GetCurrentMethod ( ).Name , System.Reflection.Assembly.GetEntryAssembly ( ).EntryPoint.DeclaringType.Namespace ); Type typeCmdArgsBasic = typeof ( CmdLneArgsBasic ); System.Reflection.Assembly asmClassLibrary = System.Reflection.Assembly.GetAssembly ( typeCmdArgsBasic ); Console.WriteLine ( "Namespace of Entry Point Routine of the assembly that exports class {0} = {1} " , typeCmdArgsBasic.FullName , asmClassLibrary.EntryPoint == null ? Properties.Resources.RENDERED_STRING_IS_NULL : asmClassLibrary.EntryPoint.DeclaringType.Namespace ); Utl.ListInternalResourceNames ( System.Reflection.Assembly.GetExecutingAssembly ( ) ); AppSettingsForEntryAssembly appSettingsForEntryAssembly = AppSettingsForEntryAssembly.GetTheSingleInstance ( Properties.Settings.Default.Properties); string strAppSettingMane = @"OperatingParameter1"; { // Constrain the scope of objAppSettingValue. object objAppSettingValue = appSettingsForEntryAssembly.GetAppSettingByName ( strAppSettingMane ); Console.WriteLine ( "Application Setting {0} = {1}" , strAppSettingMane , objAppSettingValue == null ? Properties.Resources.RENDERED_STRING_IS_NULL : objAppSettingValue ); } // Let objAppSettingValue go out of scope. strAppSettingMane = @"OperatingParameter2"; { // Constrain the scope of objAppSettingValue. object objAppSettingValue = appSettingsForEntryAssembly.GetAppSettingByName ( strAppSettingMane ); Console.WriteLine ( "Application Setting {0} = {1}" , strAppSettingMane , objAppSettingValue == null ? Properties.Resources.RENDERED_STRING_IS_NULL : objAppSettingValue ); } // Let objAppSettingValue go out of scope. appSettingsForEntryAssembly.ListAllAppSettings ( ); OperatingParametersCollection<ParameterType , ParameterSource> operatingParametersColl = OperatingParametersCollection<ParameterType , ParameterSource>.GetTheSingleInstance ( Properties.Settings.Default.Properties , Properties.Resources.PARAMETER_DISPLAY_NAME_TEMPLATE , ParameterSource.ApplicationSettings ); CmdLneArgsBasic cmdArgs = new CmdLneArgsBasic ( operatingParametersColl.GetParameterNames ( ) , CmdLneArgsBasic.ArgMatching.CaseInsensitive ); operatingParametersColl.SetFromCommandLineArguments ( cmdArgs , ParameterSource.CommandLine ); int intTotalParameters = operatingParametersColl.GetCount ( ); Console.WriteLine ( Properties.Resources.MSG_PARAMETER_LIST_HEADER , // Format Control String: The {0} application has {1} parameters. s_theApp.BaseStateManager.AppRootAssemblyFileBaseName , // Format Item 0: The {0} application intTotalParameters , // Format Item 1: has {1} parameters. Environment.NewLine ); // Format Item 2: platform-dependent newline int intParameterNumber = ListInfo.LIST_IS_EMPTY; foreach ( string strParamName in operatingParametersColl.GetParameterNames ( ) ) { Console.WriteLine ( Properties.Resources.MESSAGE_PARAMETER_NAME , // Format Control String: " Parameter # {0}: {1}" (excluding the quotation marks, of course!) ++intParameterNumber , // Format Item 0: Parameter # {0}: Utl.ListPropertiesPerDefaultToString ( // Format Item 1: Property Details listed by its ToString method operatingParametersColl.GetParameterByName ( strParamName ) , EXTRA_LEADING_CHARACTERS ) ); } // foreach ( string strParamName in operatingParametersColl.GetParameterNames ( ) ) Console.WriteLine ( Properties.Resources.MSG_PARAMETER_LIST_FOOTER , // Format Control String: End of {0} parameter list. s_theApp.BaseStateManager.AppRootAssemblyFileBaseName , // Format Item 0: End of {0} parameter Environment.NewLine ); // Format Item 1: platform-dependent newline Console.WriteLine ( Properties.Resources.MSG_VALIDATION_BEGIN , // Format Control String Environment.NewLine ); // Format Item 0: platform-dependent newline foreach ( string strParamName in operatingParametersColl.GetParameterNames ( ) ) { OperatingParameter<ParameterType , ParameterSource> operatingParameter = operatingParametersColl.GetParameterByName ( strParamName ); bool fParamIsValid = operatingParameter.IsValueValid<ParameterType> ( ); Console.WriteLine ( Properties.Resources.MSG_VALIDATION_DETAIL , // Format Control String: Parameter {0} value of {1} is {2}. operatingParameter.DisplayName , // Format Item 0: Parameter {0} Utl.RenderStringValue ( operatingParameter.ParamValue ) , // Format Item 1: value of {1} fParamIsValid ); // Format Item 2: is {2} } // foreach ( string strParamName in operatingParametersColl.GetParameterNames ( ) ) Console.WriteLine ( Properties.Resources.MSG_VALIDATION_END , // Format Control String Environment.NewLine ); // Format Item 0: platform-dependent newline } // try block catch ( Exception exAllKinds ) { // Theere must be a lower-maintenance way to do this. if ( exAllKinds.Message.StartsWith ( INTERNAL_ERROR_OO2_PREFIX ) ) { s_theApp.BaseStateManager.AppReturnCode = ERROR_INTERNAL_PROCESSING_ERROR_002; } // TRUE block, if ( exAllKinds.Message.StartsWith ( INTERNAL_ERROR_OO2_PREFIX ) ) else if ( exAllKinds.Message.StartsWith ( INTERNAL_ERROR_OO3_PREFIX ) ) { s_theApp.BaseStateManager.AppReturnCode = ERROR_INTERNAL_PROCESSING_ERROR_003; } // TRUE block, else if ( exAllKinds.Message.StartsWith ( INTERNAL_ERROR_OO3_PREFIX ) ) else if ( exAllKinds.Message.StartsWith ( INTERNAL_ERROR_OO4_PREFIX ) ) { s_theApp.BaseStateManager.AppReturnCode = ERROR_INTERNAL_PROCESSING_ERROR_004; } // TRUE block, else if ( exAllKinds.Message.StartsWith ( INTERNAL_ERROR_OO4_PREFIX ) ) else if ( exAllKinds.Message.StartsWith ( INTERNAL_ERROR_OO5_PREFIX ) ) { s_theApp.BaseStateManager.AppReturnCode = ERROR_INTERNAL_PROCESSING_ERROR_005; } // TRUE block, else if ( exAllKinds.Message.StartsWith ( INTERNAL_ERROR_OO4_PREFIX ) ) else { // If all else fails, call it a run-of-the-mill runtime error. s_theApp.BaseStateManager.AppReturnCode = WizardWrx.MagicNumbers.ERROR_RUNTIME; } // FALSE block, if ( exAllKinds.Message.StartsWith ( INTERNAL_ERROR_OO2_PREFIX ) ) ... s_theApp.BaseStateManager.AppExceptionLogger.ReportException ( exAllKinds ); } // catch ( Exception exAllKinds ) block if ( s_theApp.BaseStateManager.AppReturnCode == WizardWrx.MagicNumbers.ERROR_SUCCESS ) { s_theApp.NormalExit ( ConsoleAppStateManager.NormalExitAction.Timed ); } // TRUE (anticipated outcome) block, if ( s_theApp.BaseStateManager.AppReturnCode == WizardWrx.MagicNumbers.ERROR_SUCCESS ) else { s_theApp.ErrorExit ( ( uint ) s_theApp.BaseStateManager.AppReturnCode ); } // FALSE (unanticipated outcome) block, if ( s_theApp.BaseStateManager.AppReturnCode == WizardWrx.MagicNumbers.ERROR_SUCCESS ) } // static void Main
}; // static char [ ] s_achrValidSwitches static void Main ( string [ ] args ) { s_theApp = ConsoleAppStateManager.GetTheSingleInstance ( ); CmdLneArgsBasic cmdArgs = new CmdLneArgsBasic ( s_achrValidSwitches , CmdLneArgsBasic.ArgMatching.CaseInsensitive ) { AllowEmptyStringAsDefault = CmdLneArgsBasic.BLANK_AS_DEFAULT_ALLOWED }; // ---------------------------------------------------------------- // The default value of the AppSubsystem property is GUI, which // disables output to the console. Since ReportException returns // the message that would have been written, you still have the // option of displaying or discarding it. If EventLoggingState is // set to Enabled, the message is written into the Application // Event log, where it is preserved until the event log record is // purged by the aging rules or some other method. // ---------------------------------------------------------------- s_theApp.BaseStateManager.AppExceptionLogger.OptionFlags = s_theApp.BaseStateManager.AppExceptionLogger.OptionFlags | ExceptionLogger.OutputOptions.EventLog | ExceptionLogger.OutputOptions.Stack | ExceptionLogger.OutputOptions.StandardError; s_theApp.BaseStateManager.LoadErrorMessageTable ( s_astrErrorMessages ); string strDeferredMessage = null; OutputFormat enmOutputFormat = SetOutputFormat ( cmdArgs , ref strDeferredMessage ); if ( enmOutputFormat != OutputFormat.None ) { // Unless output is suppressed, display the standard BOJ message. s_theApp.DisplayBOJMessage ( ); } // if ( enmOutputFormat != OutputFormat.None ) if ( !string.IsNullOrEmpty ( strDeferredMessage ) ) { // SetOutputFormat saves its error message, if any, in SetOutputFormat. Console.WriteLine ( strDeferredMessage ); } // if ( !string.IsNullOrEmpty ( s_strDeferredMessage ) ) if ( cmdArgs.PositionalArgsInCmdLine < CmdLneArgsBasic.FIRST_POSITIONAL_ARG ) { // Required input is missing. Report and bug out. s_theApp.ErrorExit ( ERR_NEED_INPUT_FILENAME ); } // if ( cmdArgs.PositionalArgsInCmdLine < CmdLneArgsBasic.FIRST_POSITIONAL_ARG ) string strTestFileName = cmdArgs.GetArgByPosition ( CmdLneArgsBasic.FIRST_POSITIONAL_ARG ); Console.WriteLine ( Properties.Resources.MSG_INPUT_FILENAME , s_theApp.BaseStateManager.InitialWorkingDirectoryName , strTestFileName , Environment.NewLine ); try { #if ANYCSV int intNCases = System.IO.File.ReadAllLines ( strTestFileName , Encoding.ASCII ).Length; int intCurrCase = MagicNumbers.ZERO; WizardWrx.AnyCSV.Parser ReusableParser = new WizardWrx.AnyCSV.Parser ( WizardWrx.AnyCSV.CSVParseEngine.DelimiterChar.Comma , WizardWrx.AnyCSV.CSVParseEngine.GuardChar.DoubleQuote , WizardWrx.AnyCSV.CSVParseEngine.GuardDisposition.Strip ); Console.WriteLine ( Properties.Resources.MSG_TOSTRING_CONSTRUCTED_FROM_ENUMS , // Format control string Properties.Resources.MSG_TOSTRING_JUST_CONSTRUCTED , // Format Item 0: {0}, the ToString method nameof ( ReusableParser ) , // Format Item 1: the ToString method on {1} reports ReusableParser.ToString ( ) ); // Format Item 2: reports as follows: {2}. using ( System.IO.StreamReader srTestData = new System.IO.StreamReader ( strTestFileName , Encoding.ASCII , IGNORE_BOM , MagicNumbers.CAPACITY_08KB ) ) { do { string strTestCase = srTestData.ReadLine ( ); string [ ] astrInstanceTestOutput = ReusableParser.Parse ( strTestCase ); if ( intCurrCase == MagicNumbers.ZERO ) { // On the first iteration, intCurrCase is zero. Console.WriteLine ( Properties.Resources.MSG_TOSTRING_CONSTRUCTED_FROM_ENUMS , // Format control string Properties.Resources.MSG_TOSTRING_POST_FIRST_USE , // Format Item 0: {0}, the ToString method nameof ( ReusableParser ) , // Format Item 1: the ToString method on {1} reports ReusableParser.ToString ( ) ); // Format Item 2: reports as follows: {2}. } // if ( intCurrCase == MagicNumbers.ZERO ) string [ ] astrStaticTestOutput1 = WizardWrx.AnyCSV.Parser.Parse ( strTestCase , // Input string WizardWrx.AnyCSV.Parser.COMMA , // Field delimiter character WizardWrx.AnyCSV.Parser.DOUBLE_QUOTE ); // Delimiter guard character // ---------------------------------------------------- // Identify the case number, and show the input string. // ---------------------------------------------------- intCurrCase++; int intNFields = astrStaticTestOutput1.Length; Console.WriteLine ( Properties.Resources.MSG_CASE_LABEL , new string [ ] { intCurrCase.ToString ( ) , intNCases.ToString ( ) , intNFields.ToString ( ) , strTestCase , Environment.NewLine } ); ReportScenarioOutcome ( Properties.Resources.INSTANCE_SCENARIO_1 , astrInstanceTestOutput , astrInstanceTestOutput.Length ); ReportScenarioOutcome ( Properties.Resources.STATIC_SCENARIO_1 , astrStaticTestOutput1 , intNFields ); string [ ] astrStaticTestOutput2 = WizardWrx.AnyCSV.Parser.Parse ( strTestCase , // Input string WizardWrx.AnyCSV.Parser.COMMA , // Field delimiter character WizardWrx.AnyCSV.Parser.DOUBLE_QUOTE , // Delimiter guard character WizardWrx.AnyCSV.Parser.GuardDisposition.Keep ); // Override to keep. intNFields = astrStaticTestOutput2.Length; ReportScenarioOutcome ( Properties.Resources.STATIC_SCENARIO_2 , astrStaticTestOutput2 , intNFields ); string [ ] astrStaticTestOutput3 = WizardWrx.AnyCSV.Parser.Parse ( strTestCase , // Input string WizardWrx.AnyCSV.Parser.COMMA , // Field delimiter character WizardWrx.AnyCSV.Parser.DOUBLE_QUOTE , // Delimiter guard character WizardWrx.AnyCSV.Parser.GuardDisposition.Strip , // Override to keep. WizardWrx.AnyCSV.Parser.TrimWhiteSpace.TrimLeading ); intNFields = astrStaticTestOutput3.Length; ReportScenarioOutcome ( Properties.Resources.STATIC_SCENARIO_3 , astrStaticTestOutput3 , intNFields ); } while ( !srTestData.EndOfStream ); } // using ( System.IO.StreamReader srTestData = new System.IO.StreamReader ( #else CsvParser engine = new CsvParser ( ); string [ ] [ ] astrParsedStrings = engine.Parse ( new System.IO.StreamReader ( strTestFileName , Encoding.ASCII , IGNORE_BOM , StandardConstants.FILE_BUFSIZE_STANDARD_08KB ) ); int intNCases = astrParsedStrings.Length; for ( int intCurrCase = StandardConstants.ARRAY_FIRST_ELEMENT ; intCurrCase < intNCases ; intCurrCase++ ) { int intNFields = astrParsedStrings [ intCurrCase ].Length; Console.WriteLine ( Properties.Resources.MSG_CASE_LABEL , new string [ ] { OrdinalFromSubscript ( intCurrCase ).ToString(), intNCases.ToString() , intNFields.ToString() , Environment.NewLine } ); for ( int intCurrField = StandardConstants.ARRAY_FIRST_ELEMENT ; intCurrField < astrParsedStrings [ intCurrCase ].Length ; intCurrField++ ) { Console.WriteLine ( Properties.Resources.MSG_CASE_DETAIL , OrdinalFromSubscript ( intCurrField ) , intNFields , astrParsedStrings [ intCurrCase ] [ intCurrField ] ); } // for ( int intCurrField = StandardConstants.ARRAY_FIRST_ELEMENT ; intCurrField < astrParsedStrings [ intCurrCase ].Length ; intCurrField++ ) } // for ( int intCurrCase = StandardConstants.ARRAY_FIRST_ELEMENT ; intCurrCase < intNCases ; intNCases++ ) #endif } catch ( Exception exAll ) { // The Message string is displayed, but the complete exception goes to the event log. s_theApp.BaseStateManager.AppExceptionLogger.ReportException ( exAll ); Console.WriteLine ( exAll.Message ); ExitWithError ( enmOutputFormat , ERR_RUNTIME ); } // Providing a catch block is enough to cause the program to fall through. #if DEBUG if ( enmOutputFormat == OutputFormat.None ) { // Suppress all output. s_theApp.NormalExit ( ConsoleAppStateManager.NormalExitAction.Silent ); } // TRUE block, if ( enmOutputFormat == OutputFormat.None ) else { // Display the standard exit banner. s_theApp.NormalExit ( ConsoleAppStateManager.NormalExitAction.WaitForOperator ); } // FALSE block, if ( enmOutputFormat == OutputFormat.None ) #else if ( enmOutputFormat == OutputFormat.None ) { // Suppress all output. s_theApp.NormalExit ( ConsoleAppStateManager.NormalExitAction.Silent ); } // TRUE block, if ( enmOutputFormat == OutputFormat.None ) else { // Display the standard exit banner. s_theApp.NormalExit(ConsoleAppStateManager.NormalExitAction.WaitForOperator); } // FALSE block, if ( enmOutputFormat == OutputFormat.None ) #endif } // static void Main