diff -r 000000000000 -r 818e61de6cd1 crashanalysercmd/PerfToolsSharedLibraries/Engine/SymbianUtils/TextUtilities/Writers/AsyncTextWriters.cs --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/crashanalysercmd/PerfToolsSharedLibraries/Engine/SymbianUtils/TextUtilities/Writers/AsyncTextWriters.cs Thu Feb 11 15:50:58 2010 +0200 @@ -0,0 +1,881 @@ +/* +* Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). +* All rights reserved. +* This component and the accompanying materials are made available +* under the terms of "Eclipse Public License v1.0" +* which accompanies this distribution, and is available +* at the URL "http://www.eclipse.org/legal/epl-v10.html". +* +* Initial Contributors: +* Nokia Corporation - initial contribution. +* +* Contributors: +* +* Description: +* +*/ +using System; +using System.IO; +using System.Text; +using System.Threading; +using System.Collections; +using System.Drawing; + +namespace SymbianUtils +{ + #region Enumerations + public enum TNotUsingThread + { + ENotUsingThread + } + #endregion + + public abstract class AsyncTextWriterBase : DisposableObject + { + #region Events + public enum TEvent + { + EWritingStarted = 0, + EWritingProgress, + EWritingComplete + } + + public delegate void Observer( TEvent aEvent, AsyncTextWriterBase aObject ); + public event Observer iObserver; + #endregion + + #region Construct & destruct + public AsyncTextWriterBase() + : this( System.Threading.ThreadPriority.BelowNormal ) + { + } + + public AsyncTextWriterBase( System.Threading.ThreadPriority aPriority ) + { + iWorkerThread = new Thread( new System.Threading.ThreadStart( WorkerThreadFunction ) ); + iWorkerThread.Name = "WorkerThreadFunction_" + this.ToString(); + iWorkerThread.Priority = aPriority; + iWorkerThread.IsBackground = true; + } + + public AsyncTextWriterBase( TNotUsingThread aNotUsingThread ) + { + } + #endregion + + #region API + public void AsyncWrite() + { + lock( this ) + { + if ( iWorkerThread != null ) + { + iWorkerThread.Start(); + } + } + } + #endregion + + #region From DisposableObject - Cleanup Framework + protected override void CleanupManagedResources() + { + try + { + } + finally + { + base.CleanupManagedResources(); + } + } + + protected override void CleanupUnmanagedResources() + { + try + { + } + finally + { + base.CleanupUnmanagedResources(); + } + } + #endregion + + #region Properties + public bool IsReady + { + get + { + lock(this) + { + return iReady; + } + } + set + { + lock( this ) + { + iReady = value; + } + } + } + + public int Progress + { + get + { + lock(this) + { + if (Size == 0) + return 0; + else + { + float positionAsFloat = (float)Position; + float sizeAsFloat = (float)Size; + int progress = (int)((positionAsFloat / sizeAsFloat) * 100.0); + // + return System.Math.Max(1, System.Math.Min(100, progress)); + } + } + } + } + #endregion + + #region Write handlers + protected virtual bool ContinueProcessing() + { + return false; + } + + protected virtual void HandleWriteStarted() + { + } + + protected virtual void HandleWriteCompleted() + { + } + + protected virtual void HandleWriteException( Exception aException ) + { + } + #endregion + + #region Abstract writing framework + public abstract void ExportData(); + public abstract long Size { get; } + public abstract long Position { get; } + #endregion + + #region Internal methods + private void NotifyEvent( TEvent aEvent ) + { + if ( iObserver != null ) + { + iObserver( aEvent, this ); + } + } + + private void AsyncWriteLines() + { + bool forcedContinue = false; + lock( this ) + { + forcedContinue = ContinueProcessing(); + } + + while( Progress != KAllWorkCompletedPercentage || forcedContinue ) + { + ExportData(); + + lock( this ) + { + NotifyEvent( TEvent.EWritingProgress ); + forcedContinue = ContinueProcessing(); + } + } + } + + private void WorkerThreadFunction() + { + try + { + lock( this ) + { + iReady = false; + HandleWriteStarted(); + NotifyEvent( TEvent.EWritingStarted ); + } + + AsyncWriteLines(); + } + catch( Exception exception ) + { + lock( this ) + { + HandleWriteException( exception ); + } + } + finally + { + lock( this ) + { + HandleWriteCompleted(); + iReady = true; + NotifyEvent( TEvent.EWritingComplete ); + } + } + } + #endregion + + #region Internal constants + protected const int KAllWorkCompletedPercentage = 100; + #endregion + + #region Data members + protected bool iReady = true; + private readonly Thread iWorkerThread; + #endregion + } + + public abstract class AsyncTextFileWriter : AsyncTextWriterBase + { + #region Construct & destruct + public AsyncTextFileWriter( string aFileName ) + { + iSourceFileName = aFileName; + } + + public AsyncTextFileWriter( string aFileName, TNotUsingThread aNotUsingThread ) + : this( aNotUsingThread ) + { + iSourceFileName = aFileName; + } + + public AsyncTextFileWriter( TNotUsingThread aNotUsingThread ) + : base( aNotUsingThread ) + { + } + #endregion + + #region API + public void ConstructWriter() + { + iWriter = new StreamWriter( FileName ); + } + + public void WriteLine( string aLine ) + { + iWriter.WriteLine( aLine ); + } + #endregion + + #region Properties + public StreamWriter Writer + { + get { return iWriter; } + } + + public string FileName + { + get { return iSourceFileName; } + } + #endregion + + #region From DisposableObject - Cleanup Framework + protected override void CleanupManagedResources() + { + try + { + Cleanup(); + } + finally + { + base.CleanupManagedResources(); + } + } + #endregion + + #region From AsyncTextWriterBase + protected override void HandleWriteStarted() + { + if ( iWriter == null ) + { + ConstructWriter(); + } + + base.HandleWriteStarted(); + } + + protected override void HandleWriteCompleted() + { + try + { + Cleanup(); + } + finally + { + base.HandleWriteCompleted(); + } + } + #endregion + + #region Internal methods + private void Cleanup() + { + lock( this ) + { + if ( iWriter != null ) + { + iWriter.Close(); + iWriter = null; + } + } + } + #endregion + + #region Data members + private StreamWriter iWriter; + private readonly string iSourceFileName; + #endregion + } + + public abstract class AsyncHTMLFileWriter : AsyncTextFileWriter + { + #region Construct & destruct + public AsyncHTMLFileWriter( string aFileName ) + : base( aFileName ) + { + } + + public AsyncHTMLFileWriter( TNotUsingThread aNotUsingThread ) + : base( aNotUsingThread ) + { + } + + public AsyncHTMLFileWriter( string aFileName, TNotUsingThread aNotUsingThread ) + : base( aFileName ) + { + } + #endregion + + #region Enumerations + public enum TAlignment + { + EAlignNone = -1, + EAlignLeft = 0, + EAlignRight, + EAlignCenter, + EAlignJustify + } + #endregion + + #region Helper methods - document + public void WriteDocumentBegin() + { + WriteLine( "" ); + WriteLine( "" ); + } + + public void WriteDocumentEnd() + { + WriteLine( "" ); + } + #endregion + + #region Helper methods - header + public void WriteHeadBegin() + { + WriteLine( "
" ); + } + + public void WriteHeadEnd() + { + Writer.WriteLine( "" ); + } + + public void WriteTitle( string aTitle ) + { + Writer.WriteLine( "" ); + } + + public void WriteParagraphBegin( string aClass ) + { + WriteLine( "
" ); + } + + public void WriteParagraphEnd() + { + WriteLine( "
" ); + } + + public void WriteSpanBegin() + { + WriteSpanBegin( string.Empty ); + } + + public void WriteSpanBegin( string aClass ) + { + WriteSpanBegin( aClass, string.Empty ); + } + + public void WriteSpanBegin( string aClass, string aId ) + { + Writer.Write( "" ); + } + + public void WriteSpanEnd() + { + WriteLine( "" ); + } + + public void WriteNewLine() + { + WriteLine( "