/// <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 ( )
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
}; // 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