applayerprotocols/httptransportfw/Test/T_HttpIntegration/TfrLex.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 // $Header$
       
    15 // This module implements TfrLex collection of static lexical functions.
       
    16 // rev:	mjdavey, symbian@mjdss.com, July 2002
       
    17 // for:	Typhoon (7.0s) & JetStream (8.0)
       
    18 // 
       
    19 //
       
    20 
       
    21 #include <e32std.h>                     //
       
    22 #include <e32cons.h>
       
    23 
       
    24 //-----------------------------------------------------------------------------
       
    25 //  Include Files  
       
    26 
       
    27 #include "TfrLex.h"                     // this module
       
    28 
       
    29 //-----------------------------------------------------------------------------
       
    30 //	Extract (skip over) a term.
       
    31 //-----------------------------------------------------------------------------
       
    32 
       
    33 TInt  TfrLex::Val( TLex& aLex, const TDesC& aTerm )
       
    34 {
       
    35 TLex term( aTerm );
       
    36 TLexMark mark;
       
    37 
       
    38 aLex.Mark( mark );
       
    39 while ( !term.Eos() )
       
    40 	{
       
    41 	if ( aLex.Eos() || term.Get() != aLex.Get() )
       
    42 		{
       
    43 		aLex.UnGetToMark( mark );
       
    44 		return KErrNotFound;
       
    45 		}
       
    46 	}
       
    47 return KErrNone;
       
    48 }
       
    49 
       
    50 //-----------------------------------------------------------------------------
       
    51 //	Extract (check if text begins with) a term.
       
    52 
       
    53 TBool TfrLex::Val( const TDesC& aText, const TDesC& aTerm )
       
    54 {
       
    55 TLex lex( aText );
       
    56 return ( Val( lex, aTerm ) == KErrNone );
       
    57 }
       
    58 
       
    59 //-----------------------------------------------------------------------------
       
    60 //	Extract (skip over) a term. Folded comparison.
       
    61 
       
    62 TInt  TfrLex::ValF( TLex& aLex, const TDesC& aTerm )
       
    63 {
       
    64 TLex term( aTerm );
       
    65 TLexMark mark;
       
    66 
       
    67 aLex.Mark( mark );
       
    68 while ( !term.Eos() )
       
    69 	{
       
    70 	if ( aLex.Eos() || User::Fold(term.Get()) != User::Fold(aLex.Get()) )
       
    71 		{
       
    72 		aLex.UnGetToMark( mark );
       
    73 		return KErrNotFound;
       
    74 		}
       
    75 	}
       
    76 return KErrNone;
       
    77 }
       
    78 
       
    79 //-----------------------------------------------------------------------------
       
    80 //	Extract (check if text begins with) a term.
       
    81 
       
    82 TBool TfrLex::ValF( const TDesC& aText, const TDesC& aTerm )
       
    83 {
       
    84 TLex lex( aText );
       
    85 return ( ValF( lex, aTerm ) == KErrNone );
       
    86 }
       
    87 
       
    88 //-----------------------------------------------------------------------------
       
    89 //	Extract (check if text begins with) a term.
       
    90 //
       
    91 //	Skip spaces and get token up to end-of-string, a space char or up to the
       
    92 //	very character you define. The GetF supports tokens like "*". If a token
       
    93 //	begins with apostrophe but does not end to one, the function L E A V E S
       
    94 //	with error KErrArgument.
       
    95 
       
    96 TPtrC TfrLex::GetL( TLex& aLex, const TChar  aChar )
       
    97 {
       
    98 // Skip spaces and mark the token's start point.
       
    99 aLex.SkipSpace();
       
   100 TLexMark mark;
       
   101 
       
   102 aLex.Mark(mark);    
       
   103 
       
   104 if (aLex.Peek() == '"' )
       
   105 	{
       
   106 	// Skip the " and do find next " followed by eos, space or aChar.
       
   107 	aLex.Inc();
       
   108 	TChar x;
       
   109 	while ( x = aLex.Get(), !x.Eos() )
       
   110 		{
       
   111 		if ( x == '"' )
       
   112 			{
       
   113 			// Found a " character - but is it the end of the token?
       
   114 			x = aLex.Peek(); // peek the next character
       
   115 			if ( x.Eos() || x.IsSpace() || x == aChar )
       
   116 				// End of token: return token.
       
   117 				return aLex.MarkedToken(mark);
       
   118 			}
       
   119 		}
       
   120 	// Unget and L E A V E because did not find the end " of token.
       
   121 	aLex.UnGetToMark(mark);
       
   122 	User::Leave(KErrArgument);
       
   123 	return aLex.MarkedToken(mark); // never reached (l e a v e).
       
   124 	}
       
   125 else
       
   126 	{
       
   127 	// Is not a "*" token: find eos or the next space or the aChar
       
   128 	// and return the token.
       
   129 	TChar x;
       
   130 	while ( x = aLex.Peek(), !x.Eos() && !x.IsSpace() && x != aChar )
       
   131 		aLex.Inc();
       
   132 
       
   133 	return aLex.MarkedToken(mark);
       
   134 	}
       
   135 }
       
   136 
       
   137 //-----------------------------------------------------------------------------
       
   138 //	Skip spaces and get token up to end-of-string, a space char or up to the
       
   139 //	very characters you define. The GetF supports tokens like "*".  If token
       
   140 //	begins with apostrophe but does not end to one, the function L E A V E S
       
   141 //	with error KErrArgument.
       
   142 
       
   143 TPtrC TfrLex::GetL( TLex& aLex, const TDesC& aList )
       
   144 {
       
   145 // Skip spaces and mark the token's start point.
       
   146 aLex.SkipSpace();
       
   147 TLexMark mark;
       
   148 aLex.Mark(mark);    
       
   149 
       
   150 if ( aLex.Peek() == '"' )
       
   151 	{
       
   152 	// Skip the " and do find next " followed by eos, space or any char
       
   153 	// in the aList.
       
   154 	aLex.Inc();
       
   155 	TChar x;
       
   156 	while ( x = aLex.Get(), !x.Eos() )
       
   157 		{
       
   158 		if ( x == '"' )
       
   159 			{
       
   160 			// Found a " character - but is it the end of the token?
       
   161 			x = aLex.Peek();
       
   162 			if ( x.Eos() || x.IsSpace() || aList.Locate(x) < 0 )
       
   163 				// End of token: return token.
       
   164 				return aLex.MarkedToken(mark);
       
   165 			}
       
   166 		}
       
   167 	// Unget and L E A V E because did not find the end " of token.
       
   168 	aLex.UnGetToMark(mark);
       
   169 	User::Leave(KErrArgument);
       
   170 	return aLex.MarkedToken(mark); // never reached (l e a v e).
       
   171 	}
       
   172 else
       
   173 	{
       
   174 	// Is not a "*" token: find eos or the next space or any char
       
   175 	// in the aList and return the token.
       
   176 	TChar x;
       
   177 	while ( x = aLex.Peek(), !x.Eos() && !x.IsSpace() && aList.Locate(x) < 0 )
       
   178 		aLex.Inc();
       
   179 
       
   180 	return aLex.MarkedToken(mark);
       
   181 	}
       
   182 }
       
   183 
       
   184 //-----------------------------------------------------------------------------
       
   185 //	Skip spaces and then eat the very character or the very term you define.
       
   186 //	Return ETrue (was found and swallowed) or EFalse.
       
   187 // Skip spaces and eat a particular character.
       
   188 //
       
   189 
       
   190 TBool TfrLex::Eat( TLex& aLex, const TChar aChar )
       
   191 {
       
   192 TLexMark unget;
       
   193 if ( aChar.IsSpace() )
       
   194 	{
       
   195 	// A space character requires its own logic = go over
       
   196 	// the other space characters - x gets the next char.
       
   197 	TChar x;
       
   198 	aLex.Mark(unget);
       
   199 	while ( x = aLex.Get(), x.IsSpace() && x != aChar )
       
   200 		{};
       
   201 	if ( x == aChar ) 
       
   202 		return ETrue;
       
   203 	}
       
   204 else
       
   205 	{
       
   206 	// For other, non-space, characters: skip spaces and
       
   207 	// get the next character x.
       
   208 	aLex.SkipSpace();
       
   209 	aLex.Mark(unget);
       
   210 	if ( aLex.Get() == aChar ) 
       
   211 	return ETrue;
       
   212 	}
       
   213 
       
   214 // The character wasn't there, unget to the start point.
       
   215 aLex.UnGetToMark(unget);
       
   216 return EFalse;
       
   217 }
       
   218 
       
   219 //-----------------------------------------------------------------------------
       
   220 // Skip spaces and eat a term, exact comparison.
       
   221 
       
   222 TBool TfrLex::Eat( TLex& aLex, const TDesC& aTerm )
       
   223 {
       
   224 aLex.SkipSpace();
       
   225 TLexMark unget;
       
   226 aLex.Mark(unget);
       
   227 if ( Val(aLex,aTerm) == KErrNone ) 
       
   228 	return ETrue;
       
   229 aLex.UnGetToMark(unget);
       
   230 return EFalse;
       
   231 }
       
   232 
       
   233 //-----------------------------------------------------------------------------
       
   234 // Skip spaces and eat a term, folded comparison.
       
   235 //
       
   236 
       
   237 TBool TfrLex::EatF( TLex& aLex, const TDesC& aTerm )
       
   238 {
       
   239 aLex.SkipSpace();
       
   240 TLexMark unget;
       
   241 aLex.Mark(unget);
       
   242 if ( ValF(aLex,aTerm) == KErrNone ) return ETrue;
       
   243 aLex.UnGetToMark(unget);
       
   244 return EFalse;
       
   245 } 
       
   246 
       
   247 //-----------------------------------------------------------------------------
       
   248 //	Get from first non white space onwards.
       
   249 
       
   250 TPtrC TfrLex::TrimLeft( const TDesC& aText )
       
   251 {
       
   252 TLex lex( aText );
       
   253 lex.SkipSpaceAndMark();
       
   254 return lex.Remainder();
       
   255 }
       
   256 
       
   257 //-----------------------------------------------------------------------------
       
   258 //	Get up to the last non white space.
       
   259 
       
   260 TPtrC TfrLex::TrimRight( const TDesC& aText )
       
   261 {
       
   262 TLex lex( aText );
       
   263 TPtrC remainder;
       
   264 do
       
   265 	{
       
   266 	lex.SkipCharacters();
       
   267 	remainder.Set( lex.MarkedToken() );
       
   268 	lex.SkipSpace();
       
   269 	} while ( !lex.Eos() );
       
   270 
       
   271 return remainder;
       
   272 }
       
   273 
       
   274 //-----------------------------------------------------------------------------
       
   275 //	Get from first non white space onwards and up to last non white space.
       
   276 //	Is TrimLeft and TrimRight.
       
   277 
       
   278 TPtrC TfrLex::Trim( const TDesC& aText )
       
   279 {
       
   280 return TrimRight( TrimLeft( aText ) );
       
   281 }
       
   282 
       
   283 //-----------------------------------------------------------------------------
       
   284 //	Peel = Get text like "*" without the two apostrophes. If not like "*",
       
   285 //	then returns the text as such as the whole.
       
   286 
       
   287 TPtrC TfrLex::Peel( const TDesC& aText )
       
   288 {
       
   289 TPtrC result;
       
   290 TInt  length = aText.Length();
       
   291 if ( length >= 2 && (aText[0] == '"' && aText[length-1] == '"') )
       
   292 	result.Set( aText.Mid(1,length-2) );
       
   293 else
       
   294 	result.Set( aText );
       
   295 
       
   296 return result;
       
   297 }
       
   298 
       
   299 //-----------------------------------------------------------------------------
       
   300 //	Trim and then Peel.
       
   301 
       
   302 TPtrC TfrLex::TrimAndPeel( const TDesC& aText )
       
   303 {
       
   304 return Peel(Trim(aText));
       
   305 }
       
   306 
       
   307 //-----------------------------------------------------------------------------
       
   308 //	Peel and them Trim.
       
   309 
       
   310 TPtrC TfrLex::PeelAndTrim( const TDesC& aText )
       
   311 {
       
   312 return Trim(Peel(aText));
       
   313 }
       
   314 
       
   315 //-----------------------------------------------------------------------------
       
   316 //  End of File  
       
   317 //-----------------------------------------------------------------------------