crashanalysercmd/PerfToolsSharedLibraries/Engine/SymbianUtils/TextUtilities/Readers/Types/Text/AsyncTextDataReader.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 AsyncTextDataReader : AsyncTextReader
       
    27 	{
       
    28 		#region Enumerations
       
    29 		public enum TReadDirection
       
    30 		{
       
    31 			EReadDirectionForwards = 0,
       
    32 			EReadDirectionBackwards
       
    33 		}
       
    34 		#endregion
       
    35 
       
    36 		#region Construct & destruct
       
    37 		protected AsyncTextDataReader( string[] aLines )
       
    38 			: this( aLines, new AsyncTextReaderPrefix() )
       
    39 		{
       
    40 		}
       
    41 
       
    42         protected AsyncTextDataReader( string[] aLines, ITracer aTracer )
       
    43             : this( aLines, new AsyncTextReaderPrefix(), aTracer )
       
    44         {
       
    45         }
       
    46 
       
    47         protected AsyncTextDataReader( string[] aLines, AsyncTextReaderPrefix aPrefixes )
       
    48 			: this( aLines, TReadDirection.EReadDirectionForwards, aPrefixes )
       
    49 		{
       
    50 		}
       
    51 
       
    52         protected AsyncTextDataReader( string[] aLines, AsyncTextReaderPrefix aPrefixes, ITracer aTracer )
       
    53             : this( aLines, TReadDirection.EReadDirectionForwards, aPrefixes, aTracer )
       
    54         {
       
    55         }
       
    56 
       
    57         protected AsyncTextDataReader( string[] aLines, TReadDirection aReadDirection )
       
    58 			: this( aLines, TReadDirection.EReadDirectionForwards, new AsyncTextReaderPrefix(), null )
       
    59 		{
       
    60 		}
       
    61 
       
    62         protected AsyncTextDataReader( string[] aLines, TReadDirection aReadDirection, ITracer aTracer )
       
    63             : this( aLines, TReadDirection.EReadDirectionForwards, new AsyncTextReaderPrefix(), aTracer )
       
    64 		{
       
    65 		}
       
    66 
       
    67         protected AsyncTextDataReader( string[] aLines, TReadDirection aReadDirection, AsyncTextReaderPrefix aPrefixes )
       
    68             : this( aLines, aReadDirection, aPrefixes, null )
       
    69         {
       
    70 		}
       
    71 
       
    72         protected AsyncTextDataReader( string[] aLines, TReadDirection aReadDirection, AsyncTextReaderPrefix aPrefixes, ITracer aTracer )
       
    73 			: base( aPrefixes, aTracer )
       
    74 		{
       
    75 			iLines = aLines;
       
    76 			iReadDirection = aReadDirection;
       
    77 			//
       
    78 			switch( iReadDirection )
       
    79 			{
       
    80 			default:
       
    81 			case TReadDirection.EReadDirectionForwards:
       
    82 				iLineIndex = 0;
       
    83 				break;
       
    84 			case TReadDirection.EReadDirectionBackwards:
       
    85 				iLineIndex = iLines.Length - 1;
       
    86 				break;
       
    87 			}
       
    88 		}
       
    89 		#endregion
       
    90 
       
    91         #region Properties
       
    92         public int LineNumber
       
    93 		{
       
    94 			get { return iLineIndex; }
       
    95 		}
       
    96 		#endregion
       
    97 
       
    98 		#region Abstract reading framework
       
    99 		protected override int CalculateProgress()
       
   100 		{
       
   101 			int prog = 0;
       
   102 			//
       
   103 			switch( iReadDirection )
       
   104 			{
       
   105 			default:
       
   106 			case TReadDirection.EReadDirectionForwards:
       
   107 				{
       
   108 				prog = base.CalculateProgress();
       
   109 				break;
       
   110 				}
       
   111 			case TReadDirection.EReadDirectionBackwards:
       
   112 				{
       
   113 				float positionAsFloat = (float)Position;
       
   114 				float sizeAsFloat = (float)Size;
       
   115 				prog = (int)((positionAsFloat / sizeAsFloat) * 100.0);
       
   116 				prog = System.Math.Max(1, System.Math.Min(100, prog));
       
   117 				break;
       
   118 				}
       
   119 			}
       
   120 			//
       
   121 			return prog;
       
   122 		}
       
   123 
       
   124 		protected override string ProvideReadLine()
       
   125 		{
       
   126 			string ret = null;
       
   127 			//
       
   128 			switch( iReadDirection )
       
   129 			{
       
   130 			default:
       
   131 			case TReadDirection.EReadDirectionForwards:
       
   132 				if	( iLineIndex < iLines.Length )
       
   133 				{
       
   134 					ret = iLines[ iLineIndex++ ];
       
   135 				}
       
   136 				break;
       
   137 			case TReadDirection.EReadDirectionBackwards:
       
   138 				if	( iLineIndex >= 0 )
       
   139 				{
       
   140 					ret = iLines[ iLineIndex-- ];
       
   141 				}
       
   142 				break;
       
   143 			}
       
   144 			//
       
   145 			return ret;
       
   146 		}
       
   147 
       
   148 		protected override long Size
       
   149 		{
       
   150 			get
       
   151 			{
       
   152 				long size = iLines.LongLength;
       
   153 				return size;
       
   154 			}
       
   155 		}
       
   156 
       
   157 		protected override long Position
       
   158 		{
       
   159 			get
       
   160 			{
       
   161 				long position = (long) iLineIndex;
       
   162 				return position;
       
   163 			}
       
   164 		}
       
   165 		#endregion
       
   166 
       
   167 		#region Data members
       
   168 		private readonly string[] iLines;
       
   169 		private readonly TReadDirection iReadDirection;
       
   170 		private int iLineIndex = 0;
       
   171 		#endregion
       
   172 	}
       
   173 }