crashanalysercmd/PerfToolsSharedLibraries/Engine/SymBuildParsingLib/Stream/SymStream.cs
changeset 0 818e61de6cd1
equal deleted inserted replaced
-1:000000000000 0:818e61de6cd1
       
     1 /*
       
     2 * Copyright (c) 2004-2008 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 
       
    18 using System;
       
    19 using System.IO;
       
    20 using System.Text;
       
    21 using System.Collections;
       
    22 using System.Diagnostics;
       
    23 
       
    24 namespace SymBuildParsingLib
       
    25 {
       
    26 	#region Exceptions
       
    27 	public class SymParserStreamExceptionInvalidRewindAmount : Exception
       
    28 	{
       
    29 		public SymParserStreamExceptionInvalidRewindAmount( string aMessage )
       
    30 			: base( aMessage )
       
    31 		{
       
    32 		}
       
    33 	}
       
    34 	#endregion
       
    35 
       
    36 	public class SymStream
       
    37 	{
       
    38 		#region Constructors & destructor
       
    39 		public SymStream( string aFileName )
       
    40 		{
       
    41 			iFileName = aFileName;
       
    42 			iFileStream = new FileStream( aFileName, System.IO.FileMode.Open );
       
    43 			iStream = new StreamReader( iFileStream );
       
    44 			iFileData = new Stack( 1024 * 20 );
       
    45 		}
       
    46 		#endregion
       
    47 
       
    48 		#region API
       
    49 		public char PeekChar()
       
    50 		{
       
    51 			Debug.Assert( !EOF );
       
    52 			//
       
    53 			char ret = (char) iStream.Peek();
       
    54 			//
       
    55 			if	( iPushedBackCharacters.Count > 0 )
       
    56 			{
       
    57 				ret = (char) iPushedBackCharacters.Peek();
       
    58 			}
       
    59 			return ret;
       
    60 		}
       
    61 
       
    62 		public char ReadChar()
       
    63 		{
       
    64 			Debug.Assert( !EOF );
       
    65 			//
       
    66 			char ret;
       
    67 			//
       
    68 			if	( iPushedBackCharacters.Count > 0 )
       
    69 			{
       
    70 				ret = (char) iPushedBackCharacters.Pop();
       
    71 			}
       
    72 			else
       
    73 			{
       
    74 				ret = (char) iStream.Read();
       
    75 			}
       
    76 			//
       
    77 			iFileData.Push( ret );
       
    78 			++iPosition;
       
    79 			//
       
    80 			return ret;
       
    81 		}
       
    82 
       
    83 		public void RewindChar()
       
    84 		{
       
    85 			RewindChars( 1 );
       
    86 		}
       
    87 
       
    88 		public void RewindChars( int aRewindCount )
       
    89 		{
       
    90 			long newPos = Position - aRewindCount;
       
    91 			if	( aRewindCount < 0 )
       
    92 				throw new SymParserStreamExceptionInvalidRewindAmount( "Rewind specifier must be >= 0" );
       
    93 			else if ( newPos < 0 || newPos >= Size ) 
       
    94 				throw new SymParserStreamExceptionInvalidRewindAmount( "Rewind specifier takes stream position beyond scope of file" );
       
    95 
       
    96 			for( int i=0; i<aRewindCount; i++ )
       
    97 			{
       
    98 				char character = (char) iFileData.Pop();
       
    99 				iPushedBackCharacters.Push( character );
       
   100 			}
       
   101 
       
   102 			iPosition -= aRewindCount;
       
   103 		}
       
   104 		#endregion
       
   105 
       
   106 		#region Properties
       
   107 		public string FileName
       
   108 		{
       
   109 			get { return iFileName; }
       
   110 		}
       
   111 
       
   112 		public long Position
       
   113 		{
       
   114 			get { return iPosition; }
       
   115 		}
       
   116 
       
   117 		public long Size
       
   118 		{
       
   119 			get { return iStream.BaseStream.Length; }
       
   120 		}
       
   121 
       
   122 		public bool EOF
       
   123 		{
       
   124 			get
       
   125 			{
       
   126 				long fileSize = Size;
       
   127 				bool atEnd = ( Position >= fileSize );
       
   128 				return atEnd;
       
   129 			}
       
   130 		}
       
   131 		#endregion
       
   132 
       
   133 		#region Constants
       
   134 		private const int KStreamEOF = -1;
       
   135 		#endregion
       
   136 
       
   137 		#region Data members
       
   138 		private readonly string iFileName;
       
   139 		private StreamReader iStream;
       
   140 		private FileStream iFileStream;
       
   141 		private Stack iPushedBackCharacters = new Stack( 1024 );
       
   142 		private Stack iFileData;
       
   143 		private long iPosition;
       
   144 		#endregion
       
   145 	}
       
   146 }