applayerprotocols/httpexamples/httpexampleclient/httpexampleutils.cpp
changeset 0 b16258d2340f
child 19 c0c2f28ace9c
equal deleted inserted replaced
-1:000000000000 0:b16258d2340f
       
     1 // Copyright (c) 2001-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 //
       
    15 
       
    16 
       
    17 // for StartC32()
       
    18 #include <c32comm.h>
       
    19 
       
    20 #include "httpexampleutils.h"
       
    21 
       
    22 
       
    23 // PDD names for the physical device drivers that are loaded in wins or arm
       
    24 #if defined (__WINS__)
       
    25 #define PDD_NAME		_L("ECDRV")
       
    26 #else
       
    27 #define PDD_NAME		_L("EUART1")
       
    28 #define PDD2_NAME		_L("EUART2")
       
    29 #define PDD3_NAME		_L("EUART3")
       
    30 #define PDD4_NAME		_L("EUART4")
       
    31 #endif
       
    32 
       
    33 #define LDD_NAME		_L("ECOMM")
       
    34 
       
    35 
       
    36 void CHttpExampleUtils::InitCommsL()
       
    37 	{
       
    38 	TInt ret = User::LoadPhysicalDevice(PDD_NAME);
       
    39 	User::LeaveIfError(ret == KErrAlreadyExists?KErrNone:ret);
       
    40 
       
    41 #ifndef __WINS__
       
    42 	ret = User::LoadPhysicalDevice(PDD2_NAME);
       
    43 	ret = User::LoadPhysicalDevice(PDD3_NAME);
       
    44 	ret = User::LoadPhysicalDevice(PDD4_NAME);
       
    45 #endif
       
    46 
       
    47 	ret = User::LoadLogicalDevice(LDD_NAME);
       
    48 	User::LeaveIfError(ret == KErrAlreadyExists?KErrNone:ret);
       
    49 	ret = StartC32();
       
    50 	User::LeaveIfError(ret == KErrAlreadyExists?KErrNone:ret);
       
    51 	}
       
    52 
       
    53 
       
    54 CHttpExampleUtils* CHttpExampleUtils::NewL(const TDesC& aTestName)
       
    55 	{
       
    56 	CHttpExampleUtils* me = new (ELeave) CHttpExampleUtils(aTestName);
       
    57 	return me;
       
    58 	}
       
    59 
       
    60 
       
    61 CHttpExampleUtils::CHttpExampleUtils(const TDesC& aTestName) : iTest(aTestName)
       
    62 	{
       
    63 	iTest.Start(KNullDesC);
       
    64 	}
       
    65 
       
    66 CHttpExampleUtils::~CHttpExampleUtils()
       
    67 	{
       
    68 	iTest.End();
       
    69 	iTest.Close();
       
    70 	}
       
    71 
       
    72 RTest& CHttpExampleUtils::Test()
       
    73 	{
       
    74 	return iTest;
       
    75 	}
       
    76 
       
    77 void CHttpExampleUtils::PressAnyKey()
       
    78 	{
       
    79 	iTest.Printf(TRefByValue<const TDesC>_L("\nPress a key"));	
       
    80 	iTest.Getch();
       
    81 	}
       
    82 
       
    83 
       
    84 TInt CHttpExampleUtils::GetSelection(const TDesC& aPrompt, const TDesC& aValidChoices)
       
    85 	//
       
    86 	//	Present the user with a list of options, and get their selection
       
    87 	{
       
    88 	TKeyCode key = EKeyNull;
       
    89 	iTest.Console()->SetPos (0, iTest.Console()->WhereY ());
       
    90 	iTest.Console()->Printf(_L("%S "), &aPrompt);
       
    91 	iTest.Console()->Printf(_L("[%S] :"), &aValidChoices);
       
    92 	TInt retVal = KErrNotFound;
       
    93 	while (retVal == KErrNotFound)
       
    94 		{
       
    95 		key = iTest.Getch();
       
    96 		// Check that key is in the list of valid choices
       
    97 		retVal = aValidChoices.Locate((TChar)key);
       
    98 		}
       
    99 	iTest.Console()->Printf(_L("%c\n\n"), key);
       
   100 	return retVal;
       
   101 	}
       
   102 
       
   103 
       
   104 void CHttpExampleUtils::LogIt(TRefByValue<const TDesC> aFmt, ...)
       
   105 	{
       
   106 	VA_LIST list;
       
   107 	VA_START(list,aFmt);
       
   108 	TBuf<KMaxFileName + 4> buf; // 4 for the log prompt
       
   109 	buf.Zero();
       
   110 	buf.Append(_L(">  "));
       
   111 	buf.AppendFormatList(aFmt,list);
       
   112 	VA_END(list);
       
   113 	iTest.Printf(_L("%S\n"), &buf);	
       
   114 	}
       
   115 
       
   116 
       
   117 void CHttpExampleUtils::GetAnEntry(const TDesC& ourPrompt, TDes& currentstring)
       
   118 	{
       
   119 	TBuf16<KMaxUserEntrySize> ourLine;
       
   120 	TBuf<KMaxUserEntrySize> tempstring;	//tempstring is a unicode descriptor
       
   121 										//create a temporary buffer where the
       
   122 										//unicode strings are stored in order to 
       
   123 										//be displayed
       
   124 	ourLine.Zero ();
       
   125 	tempstring.Copy(currentstring);		//Copy current string to Unicode buffer
       
   126 	TKeyCode key = EKeyNull;						//current string buffer is 8 bits wide.
       
   127 										//Unicode string bufffer (tempstring) is 16 bits wide.
       
   128 	FOREVER
       
   129 		{
       
   130 		if (ourLine.Length () == 0)
       
   131 			{
       
   132 			iTest.Console()->SetPos (0, iTest.Console()->WhereY ());
       
   133 			iTest.Console()->Printf (_L ("%S"), &ourPrompt);
       
   134 			if (tempstring.Length () != 0)						//get tempstring's number of items
       
   135 				iTest.Console()->Printf (_L (" = %S"), &tempstring);	//if not zero print them to iTest.Console()
       
   136 			iTest.Console()->Printf (_L (" : "));
       
   137 			iTest.Console()->ClearToEndOfLine ();
       
   138 			}
       
   139 		key = iTest.Getch();
       
   140 		
       
   141 		  if (key == EKeyBackspace)
       
   142 				{
       
   143 					if (ourLine.Length() !=0)
       
   144 					{
       
   145 						ourLine.SetLength(ourLine.Length()-1);
       
   146 						iTest.Console()->Printf (_L ("%c"), key);
       
   147 						iTest.Console()->SetPos(iTest.Console()->WhereX(),iTest.Console()->WhereY());
       
   148 						iTest.Console()->ClearToEndOfLine();
       
   149 					}	// end if (ourLine.Length() !=0)
       
   150 				}	// end if (key == KeyBackSpace)
       
   151 		  
       
   152 		  		  
       
   153 		  if (key == EKeyDelete) 			
       
   154 				{
       
   155 					ourLine.Zero();
       
   156 					iTest.Console()->SetPos (0, iTest.Console()->WhereY ());
       
   157 					iTest.Console()->ClearToEndOfLine ();
       
   158 					tempstring.Copy(ourLine);
       
   159 					break;
       
   160 				}
       
   161 		  
       
   162 		  if (key == EKeyEnter)
       
   163 			break;
       
   164 		
       
   165 		  if (key < ' ') // ascii code thats not a printable character
       
   166 			{
       
   167 			continue;
       
   168 			}
       
   169 		
       
   170 		ourLine.Append (key);
       
   171 		iTest.Console()->Printf (_L ("%c"), key);
       
   172 		iTest.Console()->SetPos(iTest.Console()->WhereX(),iTest.Console()->WhereY());
       
   173 		iTest.Console()->ClearToEndOfLine();
       
   174 		if (ourLine.Length () == ourLine.MaxLength ())
       
   175 			break;
       
   176 		}	// end of for statement
       
   177 
       
   178 	if ((key == EKeyEnter) && (ourLine.Length () == 0))
       
   179 		tempstring.Copy (currentstring);				//copy contents of 8 bit "ourLine" descriptor
       
   180 	
       
   181 	iTest.Console()->SetPos (0, iTest.Console()->WhereY ());		
       
   182 	iTest.Console()->ClearToEndOfLine ();
       
   183 	
       
   184 	if ((key == EKeyEnter) && (ourLine.Length() !=0))
       
   185 		tempstring.Copy(ourLine);
       
   186 	if (tempstring.Length () != 0)						//if temstring length is not zero
       
   187 		{
       
   188 		iTest.Console()->Printf (_L (" Entered = %S\n"), &tempstring);	//print the contents to iTest.Console()
       
   189 		LogIt(_L ("%S = %S\n"), &ourPrompt, &tempstring);
       
   190 		}
       
   191 
       
   192 	iTest.Console()->Printf (_L ("\n"));
       
   193 	currentstring.Copy(tempstring);						//copy 16 bit tempstring descriptor back 
       
   194 	}