applayerprotocols/httptransportfw/Test/T_HttpIntegration/CConsoleReader.cpp
changeset 0 b16258d2340f
equal deleted inserted replaced
-1:000000000000 0:b16258d2340f
       
     1 // Copyright (c) 2002-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 // rev:	mjdavey, symbian@mjdss.com, July 2002
       
    15 // for:	Typhoon (7.0s) & JetStream (8.0)
       
    16 // 
       
    17 //
       
    18 
       
    19 #include "CConsoleReader.h"
       
    20 
       
    21 //-----------------------------------------------------------------------------
       
    22 
       
    23 CConsoleReader* CConsoleReader::NewL(CConsoleBase& aConsole)
       
    24 {
       
    25 CConsoleReader* self = new (ELeave) CConsoleReader(aConsole);
       
    26 return self;
       
    27 }
       
    28 
       
    29 //-----------------------------------------------------------------------------
       
    30 
       
    31 CConsoleReader::~CConsoleReader()
       
    32 {
       
    33 CActive::Cancel();
       
    34 }
       
    35 
       
    36 //-----------------------------------------------------------------------------
       
    37 
       
    38 CConsoleReader::CConsoleReader(CConsoleBase& aConsole)
       
    39 	: 
       
    40 	CActive(EPriorityUserInput), 
       
    41 	iConsole(aConsole)
       
    42 {
       
    43 CActiveScheduler::Add(this);
       
    44 }
       
    45 
       
    46 //-----------------------------------------------------------------------------
       
    47 
       
    48 void CConsoleReader::ReadLine(TDes& aData, TRequestStatus& aCallbackStatus)
       
    49 {
       
    50 // Set data buffer and call back request status
       
    51 iData = &aData;
       
    52 iCallbackStatus = &aCallbackStatus;
       
    53 *iCallbackStatus = KRequestPending;
       
    54 
       
    55 // Set mode
       
    56 iMode = ELineMode;
       
    57 
       
    58 // Issue character request
       
    59 IssueRequest();
       
    60 }
       
    61 
       
    62 //-----------------------------------------------------------------------------
       
    63 
       
    64 void CConsoleReader::ReadChar(TDes& aData, TRequestStatus& aCallbackStatus)
       
    65 {
       
    66 // Set data buffer and call back request status
       
    67 iData = &aData;
       
    68 iCallbackStatus = &aCallbackStatus;
       
    69 *iCallbackStatus = KRequestPending;
       
    70 
       
    71 // Set mode
       
    72 iMode = ECharMode;
       
    73 
       
    74 // Issue character request
       
    75 IssueRequest();
       
    76 }
       
    77 
       
    78 //-----------------------------------------------------------------------------
       
    79 
       
    80 void CConsoleReader::ReadCancel()
       
    81 {
       
    82 Cancel();
       
    83 }
       
    84 
       
    85 //-----------------------------------------------------------------------------
       
    86 
       
    87 void CConsoleReader::IssueRequest()
       
    88 {
       
    89 __ASSERT_DEBUG( !IsActive(), User::Panic(_L("Reader"), KErrCorrupt) );
       
    90 
       
    91 // Issue read character request
       
    92 iConsole.Read(iStatus);
       
    93 // Wait for request to be handled;
       
    94 SetActive();
       
    95 }
       
    96 
       
    97 //-----------------------------------------------------------------------------
       
    98 
       
    99 void CConsoleReader::RunL()
       
   100 {
       
   101 ProcessKeyPress(TChar(iConsole.KeyCode()));
       
   102 }
       
   103 
       
   104 //-----------------------------------------------------------------------------
       
   105 
       
   106 void CConsoleReader::DoCancel()
       
   107 {
       
   108 iConsole.ReadCancel();
       
   109 User::RequestComplete(iCallbackStatus, KErrCancel);
       
   110 }
       
   111 
       
   112 //-----------------------------------------------------------------------------
       
   113 
       
   114 void CConsoleReader::ProcessKeyPress(TChar aKey)
       
   115 {
       
   116 TBuf<1> charBuf;
       
   117 charBuf.Format(_L("%d"), (TInt)aKey);
       
   118 
       
   119 switch( aKey )
       
   120 	{
       
   121 	case EKeyBackspace:
       
   122 	case EKeyDelete:
       
   123 		// Reduce the data length and request another key press
       
   124 		if( iData->Length() > 0 )
       
   125 			{
       
   126 			iData->SetLength(iData->Length() - 1);
       
   127 			iConsole.Write(charBuf);
       
   128 			}
       
   129 
       
   130 		if( iMode == ECharMode )
       
   131 			{
       
   132 			// Complete the request
       
   133 			User::RequestComplete(iCallbackStatus, KErrNone);
       
   134 			iCallbackStatus = NULL;
       
   135 			}
       
   136 		else
       
   137 			{
       
   138 			// Get the next key press
       
   139 			IssueRequest();
       
   140 			}
       
   141 		break;
       
   142 
       
   143 	case EKeyEnter:
       
   144 
       
   145 		// Write the new line to the console and complete the callback request status
       
   146 		iConsole.Write(KTxtNewLine);
       
   147 		User::RequestComplete(iCallbackStatus, KErrNone);
       
   148 		iCallbackStatus = NULL;
       
   149 		break;
       
   150 
       
   151 	default:
       
   152 		// Append to input line, display and request another key press
       
   153 		iData->Append(aKey);
       
   154 		iConsole.Write(charBuf);
       
   155 		if( iMode == ECharMode )
       
   156 			{
       
   157 			// Add a carriage return
       
   158 			iConsole.Write(KTxtNewLine);
       
   159 			// Complete the request
       
   160 			User::RequestComplete(iCallbackStatus, KErrNone);
       
   161 			iCallbackStatus = NULL;
       
   162 			}
       
   163 		else
       
   164 			// Get the next key press
       
   165 			IssueRequest();
       
   166 		break;
       
   167 	}
       
   168 }
       
   169 
       
   170 //-----------------------------------------------------------------------------
       
   171 //	End of File
       
   172 //-----------------------------------------------------------------------------
       
   173 
       
   174