zeroconf/dnsparser/src/cstringparser.cpp
changeset 14 da856f45b798
equal deleted inserted replaced
12:78fbd574edf4 14:da856f45b798
       
     1 /*
       
     2 * Copyright (c) 2009 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 
       
    19 #include <e32base.h>
       
    20 #include "cstringparser.h"
       
    21 
       
    22 
       
    23 CStringParser::CStringParser ( const TDesC8& aBuffer ): iBuffer ( aBuffer ),
       
    24     iCurrentPos ( 0 )
       
    25 	{
       
    26 	}
       
    27 
       
    28 CStringParser::~CStringParser ()
       
    29 	{
       
    30 	}
       
    31 
       
    32 
       
    33 CStringParser* CStringParser::NewL ( const TDesC8& aBuffer )
       
    34 	{
       
    35 	return (new( ELeave)CStringParser( aBuffer ));
       
    36 	}
       
    37 
       
    38 /**
       
    39 	Get the next character from the current position, and increments the 
       
    40 	current position by 1. 
       
    41 	
       
    42 	@param aChar [out] Next character to be read
       
    43 	
       
    44 	@return TBool ETrue if the current position is less than the string length 
       
    45 	else returns EFalse
       
    46  */
       
    47 TBool CStringParser::GetNextCharacter ( TChar& aChar )
       
    48 	{
       
    49 	IncrementCurrentPos ();
       
    50 	return ( GetCurrentCharacter ( aChar ) );	
       
    51 	}
       
    52 
       
    53 /**
       
    54 	Get the character from the current position. 	
       
    55 	
       
    56 	@param aChar [out] Current character to be read
       
    57 	
       
    58 	@return TBool ETrue if the current position is less than the string length 
       
    59 	else returns EFalse
       
    60  */
       
    61 TBool CStringParser::GetCurrentCharacter ( TChar& aChar )
       
    62 	{
       
    63 	if ( iCurrentPos >= iBuffer.Length () )
       
    64 		{
       
    65 		return EFalse;		
       
    66 		}
       
    67 	
       
    68 	aChar = iBuffer[ iCurrentPos ];
       
    69 	return ETrue;
       
    70 	}
       
    71 
       
    72 /**
       
    73 	Parses the string till it finds the aEndChar.
       
    74 	
       
    75 	@param TChar [in] Character to search for.
       
    76 	@see GetNextWorrd
       
    77  */	
       
    78 TBool CStringParser::ParseTill ( TPtrC8& aWord, TChar aEndChar )
       
    79 	{	
       
    80 	TPtrC8 cur( iBuffer.Mid( iCurrentPos ) );
       
    81 	TBool found = ETrue;
       
    82 	TInt pos = cur.Locate( aEndChar );
       
    83 	if ( pos < 0 )
       
    84 		{
       
    85 		// Unable to locate the aEndChar. Sets the end position to
       
    86 		// the length of the string.
       
    87 		pos = iBuffer.Length (); 
       
    88 		found = EFalse;
       
    89 		}
       
    90 
       
    91 	aWord.Set ( cur.Left (pos) ); 
       
    92 	SkipLength ( pos );
       
    93 	return found;	
       
    94 	}	
       
    95 
       
    96 /**
       
    97 	Increments the current parsing position by aLen.
       
    98 	
       
    99 	@param TInt [in] Number of character to skip.
       
   100 	
       
   101 	@return TBool ETrue if the current parsing position is less than the buffer length
       
   102     else returns EFalse.
       
   103  */
       
   104 TBool CStringParser::SkipLength ( TInt aLen )
       
   105 	{
       
   106 	iCurrentPos += aLen;
       
   107 
       
   108 	if ( iCurrentPos < iBuffer.Length () )
       
   109 		{
       
   110 		return ETrue;		
       
   111 		}
       
   112 	return EFalse;
       
   113 	}
       
   114 
       
   115 /**
       
   116 	Finds the remainder string from the current parsing position to the buffer length.
       
   117 		
       
   118 	@param TPtrC8& [out] Points to the current starting position till end
       
   119 	
       
   120 	@return TBool ETrue if the current parsing position is less than the buffer length
       
   121     else returns EFalse.
       
   122  */
       
   123 void CStringParser::GetRemainder ( TPtrC8& aBuffer )
       
   124 	{
       
   125 	aBuffer.Set ( iBuffer.Mid ( iCurrentPos ) ); 
       
   126 	SkipLength ( aBuffer.Length () );
       
   127 	}
       
   128 
       
   129 /**
       
   130  	Increments the current parsing position by 1.
       
   131   */
       
   132 void CStringParser::IncrementCurrentPos ( )
       
   133 	{
       
   134 	++iCurrentPos;
       
   135 	}
       
   136 
       
   137 /**
       
   138 	Parses till the first occurrence of any one of a set of characters in aCharSet,
       
   139 	from the current parsing position.		
       
   140 	
       
   141 	@param TPtrC8& [out] Points to the current starting position.
       
   142 	@param const TDesC8& [in] Descriptor containing characters for matching.
       
   143  */
       
   144 void CStringParser::ParseTill ( TPtrC8& aWord, const TDesC8& aCharSet )
       
   145 	{
       
   146 	TPtrC8 cur( iBuffer.Mid( iCurrentPos ) );
       
   147 	
       
   148 	TInt lowpos = cur.Length ();
       
   149 	TInt pos = KErrNotFound;
       
   150 	
       
   151 	for ( TInt i = 0; i < aCharSet.Length(); ++i )
       
   152 		{
       
   153 		pos = cur.Locate ( aCharSet [ i ] );
       
   154 		
       
   155 		if ( (pos != KErrNotFound) && (pos < lowpos ) )
       
   156 			{
       
   157 			lowpos = pos;				
       
   158 			}
       
   159 		}
       
   160 		
       
   161 	aWord.Set ( cur.Left ( lowpos ) );
       
   162 	SkipLength ( lowpos );	
       
   163 	}
       
   164 
       
   165 
       
   166 
       
   167 
       
   168