} // private static long LineCountToDisplay private string [ ] LoadInputList ( string pstrLabel , string pstrInputFileName ) { if ( File.Exists ( pstrInputFileName ) ) { FileInfo fiMasterFile = new FileInfo ( pstrInputFileName ); string [ ] rastrMasterFile = File.ReadAllLines ( pstrInputFileName ); string [ ] astrReportDetails = new string [ ] { pstrLabel , FileNameToDisplay ( fiMasterFile ) , SysDateFormatters.ReformatSysDate ( fiMasterFile.LastWriteTime , SysDateFormatters.RFD_YYYY_MM_DD_HH_MM_SS ) , fiMasterFile.Length.ToString ( NumericFormats.NUMBER_PER_REG_SETTINGS_0D ) , LineCountToDisplay ( rastrMasterFile ).ToString ( NumericFormats.NUMBER_PER_REG_SETTINGS_0D ) }; _lstReportDetails.Add ( string.Format ( _strReportDetailTemplate , astrReportDetails ) ); Console.WriteLine ( sr_strReportDetail , astrReportDetails ); return rastrMasterFile; } // TRUE (normal) block, if ( File.Exists ( pstrInputFileName ) ) else { Console.WriteLine ( sr_strReportDetail , new string [ ] { pstrLabel , FileNameToDisplay ( pstrInputFileName ) , Properties.Resources.IDS_MSG_FILE_NOT_FOUND , string.Empty , string.Empty } ); return null; } // FALSE (exception) block, if ( File.Exists ( pstrInputFileName ) ) } //private string [ ] LoadInputList
/// <summary> /// Override the base class ToString method, so that it renders the most /// critical properties of the underlying file. /// </summary> /// <returns> /// The returned string lists the name of the exporting class, as usual, /// but followed immediately by the key properties that a debugger will /// most likely need. /// </returns> public override string ToString( ) { return(string.Format( Properties.Resources.AGED_FILES_INFO_TOSTRING_TEMPLATE, new string [] { this.GetType( ).FullName, // Format Item 0 = Fully qualified class name _fileInfo.Name, // Format Item 1 = Base Name = {1}, SysDateFormatters.FormatDateTimeForShow( // Format Item 2 = LastWriteTimeUTC = {2} _fileInfo.LastAccessTimeUtc), // LastAccessTimeUtc is a System.DateTime structure, which FormatDateTimeForShow formats for display to carbon units. NumberFormatters.Integer( // Format Item 3 = ({3 Ticks}),LastWriteTime _fileInfo.LastAccessTimeUtc.Ticks), // LastAccessTimeUtc.Ticks is a long (64 bit) integer, which counts upwards from 01/01/0001 at Midnight UTC. SysDateFormatters.FormatDateTimeForShow( // Format Item 4 = LastWriteTime = {4} _fileInfo.LastAccessTime), // LastAccessTime, which reports the LastWriteTime, expressed as Local time is a System.DateTime structure, which FormatDateTimeForShow formats for display to carbon units. NumberFormatters.Integer( // Format Item 5 = ({5 Ticks}) _fileInfo.LastAccessTime.Ticks) // LastAccessTime.Ticks is a long (64 bit) integer, which counts upwards from 01/01/0001 at Midnight local time, })); } // public override string ToString ( )
} // private static void ConsumeResponse /// <summary> /// Format date/time and integral types for printing, passing all other /// types through as is, by way of their ToString methods. /// </summary> /// <param name="pstrStringFromJSON"> /// The string from the deserialized JSON object is converted to a safer /// type, then fed through its ToString method. /// </param> /// <returns> /// The return value is a formatted string, suitable for display on a /// report. /// </returns> internal static string Beautify ( string pstrStringFromJSON ) { object objOfType = ConvertToAppropriateType ( pstrStringFromJSON ); if ( objOfType is DateTime ) { DateTime dtmObjAsDate = ( DateTime ) objOfType; return SysDateFormatters.ReformatSysDate ( dtmObjAsDate , SysDateFormatters.RFD_YYYY_MM_DD ); } // TRUE (The input is a DateTime.) block, if ( objOfType is DateTime ) else if ( objOfType is long ) { long lngObjAsLongInteger = ( long ) objOfType; return lngObjAsLongInteger.ToString ( NumericFormats.NUMBER_PER_REG_SETTINGS_0D ); } // TRUE (The input is a Long Integer.) block, else if ( objOfType is long ) else { return objOfType.ToString ( ); } // FALSE block covering if ( objOfType is DateTime ) AND else if ( objOfType is long ) } // internal static string Beautify
public void Go ( ) { Console.WriteLine ( sr_strReportLabels ); string [ ] astrMasterFile = LoadInputList ( Properties.Resources.IDS_DESCR_MASTER_FILE , _strMasterFileName ); string [ ] astrNewItems = LoadInputList ( Properties.Resources.IDS_DESCR_NEWITEMS_FILE , _strNewItemsFileName ); if ( astrMasterFile.LongLength > MagicNumbers.ZERO && astrNewItems.LongLength > MagicNumbers.ZERO ) { TestItemSortByString [ ] atiMasterList = LoadList ( astrMasterFile ); TestItemSortByString [ ] atiNewItems = LoadList ( astrNewItems ); TestItemSortByString [ ] atiMergedList = WizardWrx.ListHelpers.MergeNewItemsIntoArray ( atiMasterList , atiNewItems ); string [ ] astrMergedList = new string [ atiMergedList.LongLength + ArrayInfo.ORDINAL_FROM_INDEX ]; long lngCurrentSlot = ArrayInfo.ARRAY_FIRST_ELEMENT; astrMergedList [ lngCurrentSlot ] = s_Labels; foreach ( TestItemSortByString tiCurrentItem in atiMergedList ) { astrMergedList [ ++lngCurrentSlot ] = string.Join ( SpecialCharacters.TAB_CHAR.ToString ( ) , new string [ ] { tiCurrentItem.Key.ToString ( NumericFormats.GENERAL_UC ) , tiCurrentItem.Data } ); } // foreach ( TestItemSortByString tiCurrentItem in atiMergedList ) File.WriteAllLines ( _strOutputFileName , astrMergedList ); FileInfo fiOutputFile = new FileInfo ( _strOutputFileName ); // ------------------------------------------------------------ // Stash this for the other test. // ------------------------------------------------------------ string [ ] astrDetails = new string [ ] { Properties.Resources.IDS_DESCR_OUTPUT_FILE , FileNameToDisplay ( fiOutputFile ) , SysDateFormatters.ReformatSysDate ( fiOutputFile.LastWriteTime , SysDateFormatters.RFD_YYYY_MM_DD_HH_MM_SS ) , fiOutputFile.Length.ToString ( NumericFormats.NUMBER_PER_REG_SETTINGS_0D ) , atiMergedList.LongLength.ToString ( NumericFormats.NUMBER_PER_REG_SETTINGS_0D ) }; Console.WriteLine ( sr_strReportDetail , astrDetails ); _lstReportDetails.Add ( string.Format ( _strReportDetailTemplate , astrDetails ) ); } // TRUE (normal) block, if ( astrMasterFile.LongLength > MagicNumbers.ZERO && astrNewItems.LongLength > MagicNumbers.ZERO ) else { Console.WriteLine ( @"Error!" ); } // FALSE (exception) block, if ( astrMasterFile.LongLength > MagicNumbers.ZERO && astrNewItems.LongLength > MagicNumbers.ZERO ) } // public void Go
internal static void Exercise_Base64_ToAndFrom ( ) { string strInputFileName = Properties.Settings.Default.Base64EncodingSource; string strOutputFileName = Properties.Settings.Default.Base64EncodingRoundTrip; Console.WriteLine ( @"{0}Exercise_Base64_ToAndFrom Begin:{0}" , Environment.NewLine ); // -------------------------------------------------------------------------- // List input and output file names, both of which are absolute. // -------------------------------------------------------------------------- Console.WriteLine ( @" Absolute Input FileName = {0}" , strInputFileName ); Console.WriteLine ( @" Absolute Output FileName = {0}" , strOutputFileName ); try { FileInfo fiInputFile = new FileInfo ( strInputFileName ); if ( fiInputFile.Exists ) { // -------------------------------------------------------- // List the siza and last modified date of the input file. // -------------------------------------------------------- Console.WriteLine ( @"{1} Input File Size (bytes) = {0}" , NumberFormatters.Integer ( fiInputFile.Length ) , Environment.NewLine ); Console.WriteLine ( @" Input File Last Modified = {0} ({1} UTC){2}" , SysDateFormatters.FormatDateTimeForShow ( fiInputFile.LastWriteTime ) , SysDateFormatters.FormatDateTimeForShow ( fiInputFile.CreationTimeUtc ) , Environment.NewLine ); // -------------------------------------------------------- // Read the input file into a byte array from which the // Base64 character array can be constructed, then report // its size, which should be equal to the length of the // input file reported above. // -------------------------------------------------------- byte [ ] abytInputBytes = File.ReadAllBytes ( strInputFileName ); Console.WriteLine ( @" Input Byte Array Size = {0}" , NumberFormatters.Integer ( abytInputBytes.Length ) ); if ( fiInputFile.Length == abytInputBytes.Length ) { Console.WriteLine ( $"{Environment.NewLine} The input file and byte array lengths are equal.{Environment.NewLine}" ); } // TRUE (expected outcome) block, if ( fiInputFile.Length == abytInputBytes.Length ) else { Console.WriteLine ( $"{Environment.NewLine} The input file and byte array lengths DIFFER.{Environment.NewLine}" ); } // FALSE (unexpected outcome) block, if ( fiInputFile.Length == abytInputBytes.Length ) // -------------------------------------------------------- // Construct a character array 4/3 as large as the input // array to hold the Base64 encoded characters created from // it, then call the Base64 encoder to create it. // -------------------------------------------------------- char [ ] achrOutputBytes = new char [ MoreMath2.MakeEvenlyDivisibleByFour ( MoreMath2.FractionalMultiply2Integer ( abytInputBytes.Length , ( double ) 4 / 3 ) ) ]; int intOutCount = Convert.ToBase64CharArray ( abytInputBytes , ArrayInfo.ARRAY_FIRST_ELEMENT , abytInputBytes.Length , achrOutputBytes , ArrayInfo.ARRAY_FIRST_ELEMENT ); // -------------------------------------------------------- // Creeate a Base64 encoded string from the same input, and // convert the character array into another string. // -------------------------------------------------------- string strBase64String = Convert.ToBase64String ( abytInputBytes ); string strBase64Array2String = new string ( achrOutputBytes ); // -------------------------------------------------------- // Show the lengths of both output strings, then report the // result of comparing them, which should report that they // are identical. // -------------------------------------------------------- Console.WriteLine ( @" Output Byte Array Size = {0}" , NumberFormatters.Integer ( achrOutputBytes.Length ) ); Console.WriteLine ( @" Convert.ToBase64CharArray Return Value = {0}" , NumberFormatters.Integer ( intOutCount ) ); Console.WriteLine ( @" Convert.ToBase64String String Length = {0}" , NumberFormatters.Integer ( strBase64String.Length ) ); Console.WriteLine ( @" strBase64Array2String String Length = {0}" , NumberFormatters.Integer ( strBase64Array2String.Length ) ); if ( strBase64String.Equals ( strBase64Array2String ) ) { Console.WriteLine ( $"{Environment.NewLine} Strings strBase64String and strBase64Array2String match.{Environment.NewLine}" ); } // TRUE (expected outcome) block, if ( strBase64String.Equals ( strBase64Array2String ) ) else { Console.WriteLine ( $"{Environment.NewLine} Strings strBase64String and strBase64Array2String DIFFER.{Environment.NewLine}" ); } // FALSE (unexpected outcome) block, if ( strBase64String.Equals ( strBase64Array2String ) ) // -------------------------------------------------------- // Decodee the Base64 character array, which creates a new // array dynamically, and compare it byte for byte against // the input array. Both should match exactly. // -------------------------------------------------------- byte [ ] abytRoundTripBytes = Convert.FromBase64CharArray ( achrOutputBytes , ArrayInfo.ARRAY_FIRST_ELEMENT , achrOutputBytes.Length ); Console.WriteLine ( @" Convert.FromBase64CharArray Array Size = {0}" , NumberFormatters.Integer ( abytRoundTripBytes.Length ) ); Compare2ArraysByte4Byte ( abytInputBytes , abytRoundTripBytes ); File.WriteAllBytes ( strOutputFileName , abytRoundTripBytes ); FileInfo fiRoundTripFile = new FileInfo ( strOutputFileName ); Console.WriteLine ( @" Round Trip Output File Size = {0}" , NumberFormatters.Integer ( fiRoundTripFile.Length ) ); if ( fiInputFile.Length == fiRoundTripFile.Length ) { Console.WriteLine ( $"{Environment.NewLine} Input and output file sizes match." ); } // TRUE (expected outcome) block, if ( fiInputFile.Length == fiRoundTripFile.Length ) else { Console.WriteLine ( $"{Environment.NewLine} Input and output file sizes DIFFER." ); } // FALSE (unexpected outcome) block, if ( fiInputFile.Length == fiRoundTripFile.Length ) // -------------------------------------------------------- // Exercise new Base64 round trip converters. // -------------------------------------------------------- byte [ ] abytBase64Characters = WizardWrx.Core.ByteArrayBase64Converters.Base64EncodeBinaryFile ( strInputFileName ); byte [ ] abytDecodedBase64Characters = WizardWrx.Core.ByteArrayBase64Converters.Base64DecodeByteArray ( abytBase64Characters ); Compare2ArraysByte4Byte ( abytInputBytes , abytDecodedBase64Characters ); } // TRUE (anticipated outcome) block, if ( fiInputFile.Exists ) else { Console.WriteLine ( @"FATAL ERROR: The specified input file cannot be found." ); } // FALSE (unanticipated outcome) block, if ( fiInputFile.Exists ) } catch ( Exception exAllKinds ) { Program.s_smTheApp.AppExceptionLogger.ReportException ( exAllKinds ); } Console.WriteLine ( @"{0}Exercise_Base64_ToAndFrom Done{0}" , Environment.NewLine ); } // internal static void Exercise_Base64_ToAndFrom
} // private static string [ ] MakePaddedLabels private ReportDetails UseInputFromFile ( ) { ReportDetails rptDetails = new ReportDetails ( s_astrPaddedLbels.Length ); int intItemIndex = ArrayInfo.ARRAY_INVALID_INDEX; string strInputFQFN = Path.Combine ( Program.rs_strDataDirectory , _strTestDataFileName ); FileInfo fiInputFile = new FileInfo ( strInputFQFN ); string [ ] astrLinesFromFile = File.ReadAllLines ( strInputFQFN ); int intLogestLine = WizardWrx.ReportHelpers.MaxStringLength ( new List<string> ( astrLinesFromFile ) ); foreach ( string strLabel in s_astrPaddedLbels ) { switch ( s_enmDetailItem [ ++intItemIndex ] ) { case DetailItem.DataSource: rptDetails.Add ( new ReportDetail ( strLabel , _enmDataSource.ToString ( ) ) ); break; // case DetailItem.DataSource case DetailItem.SourceFQFN: if ( fiInputFile.DirectoryName == Program.rs_strDataDirectory ) rptDetails.Add ( new ReportDetail ( strLabel , fiInputFile.Name ) ); else rptDetails.Add ( new ReportDetail ( strLabel , fiInputFile.FullName ) ); break; // case DetailItem.SourceFQFN case DetailItem.ModDate: rptDetails.Add ( new ReportDetail ( strLabel , SysDateFormatters.ReformatSysDate ( fiInputFile.LastWriteTime , SysDateFormatters.RFD_YYYY_MM_DD_HH_MM_SS ) ) ); break; // case DetailItem.ModDate case DetailItem.FileSize: rptDetails.Add ( new ReportDetail ( strLabel , fiInputFile.Length.ToString ( NumericFormats.NUMBER_PER_REG_SETTINGS_0D ) ) ); break; // case DetailItem.FileSize case DetailItem.LineCount: rptDetails.Add ( new ReportDetail ( strLabel , astrLinesFromFile.LongLength.ToString ( NumericFormats.NUMBER_PER_REG_SETTINGS_0D ) ) ); break; // case DetailItem.LineCount case DetailItem.LongestLine: rptDetails.Add ( new ReportDetail ( strLabel , intLogestLine.ToString ( NumericFormats.NUMBER_PER_REG_SETTINGS_0D ) ) ); break; // case DetailItem.LongestLine } // switch ( s_enmDetailItem [ ++intItemIndex ] ) } // foreach ( string strLabel in s_astrPaddedLbels ) return rptDetails; } // private void UseInputFromFile
}; // static readonly bool [ ] _afBlankAsDefault internal static void Go( ) { int intTestNumber = 0; // ------------------------------------------------------------ // Test a CmdLneArgsBasic object that supports ONLY positional // arguments. This is the simplest kind, and the least useful. // ------------------------------------------------------------ { // Set scope boundaries around CmdLneArgsBasic SimpleParser. CmdLneArgsBasic SimpleParser = new CmdLneArgsBasic( ); DisplayParsedArgs( "Supports positional arguments only, and does case sensitive parsing", SimpleParser, ref intTestNumber); } // CmdLneArgsBasic object SimpleParser goes out of scope. // ------------------------------------------------------------ // Test a CmdLneArgsBasic object that supports positional // arguments and switches. This is probably the most useful // implementation. // ------------------------------------------------------------ { // Set scope boundaries around CmdLneArgsBasic SwitchParser. CmdLneArgsBasic SwitchParser = new CmdLneArgsBasic(s_achrSwitchSetAllUC); DisplayParsedArgs( "Supports positional arguments and switches, and does case sensitive parsing", SwitchParser, ref intTestNumber); } // CmdLneArgsBasic SwitchParser goes out of scope. // ------------------------------------------------------------ // Test a CmdLneArgsBasic object that supports positional // arguments and switches, including an invalid switch. // ------------------------------------------------------------ { // Set scope boundaries around InvalidSwitchParser SwitchParser. try { CmdLneArgsBasic InvalidSwitchParser = new CmdLneArgsBasic(s_achrSwitchesWithDupicates); DisplayParsedArgs( "Supports positional arguments and switches, and does case sensitive parsing", InvalidSwitchParser, ref intTestNumber); } catch (Exception exAll) { System.Diagnostics.Trace.WriteLine( string.Format( "{0}: {1}", SysDateFormatters.FormatDateTimeForShow(DateTime.Now), ExceptionLogger.GetTheSingleInstance().ReportException(exAll))); // Log the error with the event viewer and the trace listener(s), if any. } } // CmdLneArgsBasic InvalidSwitchParser goes out of scope. // ------------------------------------------------------------ // Test a CmdLneArgsBasic object that supports positional // arguments and switches, including an invalid switch. // ------------------------------------------------------------ { // Set scope boundaries around IntegerSwitchParser SwitchParser. try { CmdLneArgsBasic IntegerSwitchParser = new CmdLneArgsBasic(s_achrSwitchSetAllUC); DisplayParsedArgs( "Supports positional arguments and switches, and does case sensitive parsing", IntegerSwitchParser, ref intTestNumber); Console.WriteLine( " Querying switch Q with a default value of -1 returns {0}", IntegerSwitchParser.GetSwitchByNameAsInt( 'Q', -1)); Console.WriteLine( " Querying switch R with a default value of -1 returns {0}", IntegerSwitchParser.GetSwitchByNameAsInt( 'S', -1)); } catch (Exception exAll) { Console.WriteLine( "Exception caught while constructing InvalidSwitchParser{2} Message = {0}{2} Method = {1}{2}", exAll.Message, exAll.TargetSite.Name, Environment.NewLine); } } // CmdLneArgsBasic IntegerSwitchParser goes out of scope. // ------------------------------------------------------------ // Test a CmdLneArgsBasic object that supports positional // arguments and switches. This is probably the most useful // implementation for die-hard Windows programmers, because its // parsing rules are case inssensitive. // ------------------------------------------------------------ { // Set scope boundaries around CmdLneArgsBasic CaseInsensitiveSwitchParser. CmdLneArgsBasic CaseInsensitiveSwitchParser = new CmdLneArgsBasic( s_achrSwitchSetAllUC, CmdLneArgsBasic.ArgMatching.CaseInsensitive); DisplayParsedArgs( "Supports positional arguments and switches, and does case INsensitive parsing", CaseInsensitiveSwitchParser, ref intTestNumber); } // CmdLneArgsBasic SwitchParser goes out of scope. // ------------------------------------------------------------ // Test a CmdLneArgsBasic object that supports all three kinds // of arguments. This is the most versatile parser, period. // ------------------------------------------------------------ { // Set scope boundaries around CmdLneArgsBasic FullyLoadedParser. CmdLneArgsBasic FullyLoadedParser1 = new CmdLneArgsBasic( s_achrSwitchSetAllUC, s_astrValidArgumentNames); DisplayParsedArgs( "Supports named and positional arguments and switches, and does case sensitive parsing", FullyLoadedParser1, ref intTestNumber); Console.WriteLine( " GetArgByMultipleAliases, NAMED_ARG_UC_2 first = {0}", FullyLoadedParser1.GetArgByMultipleAliases( new string [] { NAMED_ARG_UC_2, NAMED_ARG_UC_3 })); // --------------------------------------------------------- // Swap preferences, and repeat. // --------------------------------------------------------- Console.WriteLine( " GetArgByMultipleAliases, NAMED_ARG_UC_3 first = {0}", FullyLoadedParser1.GetArgByMultipleAliases( new string [] { NAMED_ARG_UC_3, NAMED_ARG_UC_2 })); } // CmdLneArgsBasic FullyLoadedParser goes out of scope. // ------------------------------------------------------------ // Test a CmdLneArgsBasic object that supports all three kinds // of arguments, and implements case insensitive parsing. This // is the most versatile parser for die-hard Window // programmers. // ------------------------------------------------------------ { // Set scope boundaries around CmdLneArgsBasic FullyLoadedWinCmdParser. CmdLneArgsBasic FullyLoadedWinCmdParser = new CmdLneArgsBasic( s_achrSwitchSetAllUC, s_astrValidArgumentNames, CmdLneArgsBasic.ArgMatching.CaseInsensitive); DisplayParsedArgs( "Supports named and positional arguments and switches, and does case INsensitive parsing", FullyLoadedWinCmdParser, ref intTestNumber); } // CmdLneArgsBasic FullyLoadedWinCmdParser goes out of scope. // ------------------------------------------------------------ // Finally, test a CmdLineArgs object that supports all three // kinds of arguments, and takes its lists of supported named // arguments and switches from dictionaries of names and // defaults. By making this object do case sensitive parsing // we get the default for the third named argument, as was the // case in the next to last test above. // ------------------------------------------------------------ { // Set scope boundaries around CmdLneArgsBasic FullyLoadedParser2. Dictionary <string, string> dctNamedArgsWithDefaults = CreateNamedArgListWithDefaults( ); Dictionary <char, string> dctSwitchesWithDefaults = CreateSwitchListWithDefaults( ); CmdLneArgsBasic FullyLoadedParser2 = new CmdLneArgsBasic( dctSwitchesWithDefaults, dctNamedArgsWithDefaults); DisplayParsedArgs( "Supports named and positional arguments and switches WITH DEFAULTS, and does case sensitive parsing", FullyLoadedParser2, ref intTestNumber); } // CmdLneArgsBasic FullyLoadedParser2 goes out of scope. } // internal static void Go