crashanalysercmd/PerfToolsSharedLibraries/Engine/SymbianUtils/TextUtilities/Readers/Types/Text/AsyncTextReaderBase.cs
changeset 0 818e61de6cd1
equal deleted inserted replaced
-1:000000000000 0:818e61de6cd1
       
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). 
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 * 
       
    14 * Description:
       
    15 *
       
    16 */
       
    17 using System;
       
    18 using System.IO;
       
    19 using System.Text;
       
    20 using System.Threading;
       
    21 using System.Collections;
       
    22 using SymbianUtils.Tracer;
       
    23 
       
    24 namespace SymbianUtils
       
    25 {
       
    26 	public abstract class AsyncTextReaderBase : AsyncReaderBase
       
    27 	{
       
    28 		#region Constructors
       
    29 		protected AsyncTextReaderBase()
       
    30             : this( null )
       
    31 		{
       
    32 		}
       
    33         
       
    34         protected AsyncTextReaderBase( ITracer aTracer )
       
    35             : base( aTracer )
       
    36         {
       
    37         }
       
    38         #endregion
       
    39 
       
    40         #region Properties
       
    41         protected bool TrimLine
       
    42         {
       
    43             get { return iTrimLine; }
       
    44             set { iTrimLine = value; }
       
    45         }
       
    46         #endregion
       
    47 
       
    48         #region Read handlers
       
    49         protected virtual bool ContinueProcessing()
       
    50 		{
       
    51 			return false;
       
    52 		}
       
    53 
       
    54 		protected virtual bool ImmediateAbort()
       
    55 		{
       
    56 			return false;
       
    57 		}
       
    58 
       
    59 		protected virtual void HandleReadLine( string aLine )
       
    60 		{
       
    61 		}
       
    62 		#endregion
       
    63 
       
    64 		#region Framework API
       
    65 		protected abstract string ProvideReadLine();
       
    66 
       
    67         protected virtual void OnProgressChanged( int aProgress )
       
    68         {
       
    69             NotifyEvent( TEvent.EReadingProgress );
       
    70         }
       
    71 		#endregion
       
    72 
       
    73         #region Internal constants
       
    74         private const int KProgressCheckGranularity = 500;
       
    75         #endregion
       
    76 
       
    77         #region Internal methods
       
    78         protected override void PerformOperation()
       
    79 		{
       
    80 			bool forcedContinue = false;
       
    81 			bool immediateAbort = false;
       
    82 			string line;
       
    83 			//
       
    84 			lock( this )
       
    85 			{
       
    86 				line = ProvideReadLine();
       
    87 				forcedContinue = ContinueProcessing();
       
    88 				immediateAbort = ImmediateAbort();
       
    89 			}
       
    90 
       
    91             System.DateTime tenPercentTime = DateTime.Now;
       
    92 
       
    93             int newProgress = 0;
       
    94             int oldProgress = 0;
       
    95             int progressTracker = 0;
       
    96 
       
    97 			while ( ( line != null || forcedContinue ) && !immediateAbort )
       
    98 			{
       
    99                 if ( line != null && TrimLine )
       
   100 				{
       
   101 					line = line.Trim();
       
   102 				}
       
   103 				//
       
   104 				lock( this )
       
   105 				{
       
   106 					HandleReadLine( line );
       
   107 				}
       
   108 				//
       
   109 				lock( this )
       
   110 				{
       
   111                     if ( progressTracker == KProgressCheckGranularity )
       
   112                     {
       
   113                         newProgress = Progress;
       
   114                         progressTracker = 0;
       
   115                     }
       
   116 
       
   117                     ++progressTracker;
       
   118 				}
       
   119                 if ( newProgress != oldProgress )
       
   120                 {
       
   121 #if DEBUG
       
   122                     if ( newProgress > 0 && ( newProgress % 10 ) == 0 )
       
   123                     {
       
   124                         System.DateTime now = DateTime.Now;
       
   125                         long intermediateTickDuration = ( ( now.Ticks - tenPercentTime.Ticks ) / 100 );
       
   126                         System.Diagnostics.Debug.WriteLine( newProgress.ToString( "d2" ) + " % COMPLETE - " + intermediateTickDuration.ToString( "d12" ) );
       
   127                         tenPercentTime = now;
       
   128                     }
       
   129 #endif
       
   130                     OnProgressChanged( newProgress );
       
   131                     oldProgress = newProgress;
       
   132                 }
       
   133                 //
       
   134 				lock( this )
       
   135 				{
       
   136 					line = ProvideReadLine();
       
   137                     forcedContinue = ContinueProcessing();
       
   138                     immediateAbort = ImmediateAbort();
       
   139                 }
       
   140 			}
       
   141 
       
   142             System.DateTime endTime = DateTime.Now;
       
   143             long tickDuration = ( ( endTime.Ticks - iOperationStartTime.Ticks ) / 100 );
       
   144             System.Diagnostics.Debug.WriteLine( "TEXT READ COMPLETE - " + tickDuration.ToString( "d12" ) );
       
   145         }
       
   146 		#endregion
       
   147 
       
   148         #region Data members
       
   149         private bool iTrimLine = true;
       
   150         #endregion
       
   151     }
       
   152 }