genericservices/httputils/Test/t_uriparser/TDelimitedDataCommon.cpp
changeset 0 e4d67989cc36
equal deleted inserted replaced
-1:000000000000 0:e4d67989cc36
       
     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 #include <e32base.h>
       
    17 
       
    18 //
       
    19 //
       
    20 //	Implementation of LOCAL functions - helper functions
       
    21 //
       
    22 //
       
    23 
       
    24 template<class HBufCType, class TPtrCType, class TDelimitedParserType>
       
    25 TInt DoDelimitedDataParsingL(const TDelimitedParserType& aParser, VA_LIST& aSegments)
       
    26 	{
       
    27 	// Loop through the list
       
    28 	TPtrCType parseSegment;
       
    29 	HBufCType* pExpected = GetNextArgLC<HBufCType>(aSegments);
       
    30 	while( pExpected )
       
    31 		{
       
    32 		// Get the parsed segment - test GetNext()
       
    33 		if( aParser.GetNext(parseSegment) == KErrNotFound )
       
    34 			{
       
    35 			// Should have found something - error!
       
    36 			CleanupStack::PopAndDestroy(pExpected);	// pExpected
       
    37 			return KErrNotFound;
       
    38 			}
       
    39 		// Got the segment - compare against expected
       
    40 		if( parseSegment.Compare(*pExpected) != 0 )
       
    41 			{
       
    42 			CleanupStack::PopAndDestroy(pExpected);	// pExpected
       
    43 			return KErrNotFound;
       
    44 			}
       
    45 		// Ok, test Dec()...
       
    46 		if( aParser.Dec() != KErrNone )
       
    47 			{
       
    48 			CleanupStack::PopAndDestroy(pExpected);	// pExpected
       
    49 			return KErrNotFound;
       
    50 			}
       
    51 		// Test Peek()
       
    52 		if( aParser.Peek(parseSegment) == KErrNotFound )
       
    53 			{
       
    54 			// Should have found something - error!
       
    55 			CleanupStack::PopAndDestroy(pExpected);	// pExpected
       
    56 			return KErrNotFound;
       
    57 			}
       
    58 		// Got the segment - compare against expected
       
    59 		if( parseSegment.Compare(*pExpected) != 0 )
       
    60 			{
       
    61 			CleanupStack::PopAndDestroy(pExpected);	// pExpected
       
    62 			return KErrNotFound;
       
    63 			}
       
    64 		// Ok, test Inc()...
       
    65 		if( aParser.Inc() != KErrNone )
       
    66 			{
       
    67 			CleanupStack::PopAndDestroy(pExpected);	// pExpected
       
    68 			return KErrNotFound;
       
    69 			}
       
    70 		// Ok get next expected segment
       
    71 		CleanupStack::PopAndDestroy(pExpected);	// pExpected
       
    72 		pExpected = GetNextArgLC<HBufCType>(aSegments);
       
    73 		}
       
    74 	// Got to the end - should parse no more segments
       
    75 	if( aParser.GetNext(parseSegment) != KErrNotFound )
       
    76 		{
       
    77 		// Should not have found something - error!
       
    78 		return KErrNotFound;
       
    79 		}
       
    80 	// Ok, test Dec()...
       
    81 	if( aParser.Dec() != KErrNone )
       
    82 		{
       
    83 		return KErrNotFound;
       
    84 		}
       
    85 	// Test Peek()
       
    86 	if( aParser.Peek(parseSegment) == KErrNotFound )
       
    87 		{
       
    88 		// Should have found something - error!
       
    89 		return KErrNotFound;
       
    90 		}
       
    91 	// Ok, test Inc()...
       
    92 	if( aParser.Inc() != KErrNone )
       
    93 		{
       
    94 		return KErrNotFound;
       
    95 		}
       
    96 	// Ok, test Eos()
       
    97 	if( !aParser.Eos() != KErrNone )
       
    98 		{
       
    99 		return KErrNotFound;
       
   100 		}
       
   101 	return KErrNone;
       
   102 	}
       
   103 
       
   104 template<class HBufCType>
       
   105 HBufCType* GetNextArgLC(VA_LIST& aList)
       
   106 	{
       
   107 	const TDesC* arg = VA_ARG(aList, const TDesC*);
       
   108 	if( arg == NULL )
       
   109 		{
       
   110 		return NULL;
       
   111 		}
       
   112 	HBufCType* buf = HBufCType::NewLC(arg->Length());
       
   113 	buf->Des().Copy(*arg);
       
   114 	return buf;
       
   115 	}
       
   116 
       
   117 template<class TDelimitedParserType>
       
   118 TInt DoDelimiterPresenceTest(const TDelimitedParserType& aParser, TBool aFrontDelim, TBool aBackDelim)
       
   119 	{
       
   120 	TBool frontDelim = aParser.FrontDelimiter();
       
   121 	if( (!frontDelim && aFrontDelim) | (frontDelim && !aFrontDelim) )
       
   122 		{
       
   123 		return KErrNotFound;
       
   124 		}
       
   125 	TBool backDelim = aParser.BackDelimiter();
       
   126 	if( (!backDelim && aBackDelim) | (backDelim && !aBackDelim) )
       
   127 		{
       
   128 		return KErrNotFound;
       
   129 		}
       
   130 	return KErrNone;
       
   131 	}
       
   132 
       
   133 template<class HBufCType, class TPtrCType, class TDelimitedParserType>
       
   134 TInt DoDelimitedDataRemainderTestL(const TDelimitedParserType& aParser, VA_LIST& aRemainders)
       
   135 	{
       
   136 	// Loop through the list
       
   137 	TPtrCType parseRemainder;
       
   138 	HBufCType* pExpected = GetNextArgLC<HBufCType>(aRemainders);
       
   139 	while( pExpected )
       
   140 		{
       
   141 		// Get the remaining data
       
   142 		if( aParser.Remainder(parseRemainder) == KErrNotFound )
       
   143 			{
       
   144 			// Should have found something - error!
       
   145 			CleanupStack::PopAndDestroy(pExpected);	// pExpected
       
   146 			return KErrNotFound;
       
   147 			}
       
   148 		// Got the remainder - compare against expected
       
   149 		if( parseRemainder.Compare(*pExpected) != 0 )
       
   150 			{
       
   151 			CleanupStack::PopAndDestroy(pExpected);	// pExpected
       
   152 			return KErrNotFound;
       
   153 			}
       
   154 		// Ok, parse the to the next segment...
       
   155 		if( aParser.Inc() != KErrNone )
       
   156 			{
       
   157 			CleanupStack::PopAndDestroy(pExpected);	// pExpected
       
   158 			return KErrNotFound;
       
   159 			}
       
   160 		// Ok get next expected remainder
       
   161 		CleanupStack::PopAndDestroy(pExpected);	// pExpected
       
   162 		pExpected = GetNextArgLC<HBufCType>(aRemainders);
       
   163 		}
       
   164 	// Got to the end - should get no remainder
       
   165 	if( aParser.Remainder(parseRemainder) != KErrNotFound )
       
   166 		{
       
   167 		// Should not have found something - error!
       
   168 		return KErrNotFound;
       
   169 		}
       
   170 	return KErrNone;
       
   171 	}
       
   172 
       
   173 template<class TDelimitedParserType, class TDesCType>
       
   174 TInt DoDelimitedDataDes(const TDelimitedParserType& aParser, const TDesCType& aDelimitedData)
       
   175 	{
       
   176 	if( aParser.Des().Compare(aDelimitedData) != 0)
       
   177 		{
       
   178 		return KErrNotFound;
       
   179 		}
       
   180 	return KErrNone;
       
   181 	}
       
   182 
       
   183 template<class HBufCType, class CDelimitedDataType>
       
   184 TInt DoDelimitedDataPushFrontL(CDelimitedDataType* aParser, VA_LIST& aData)
       
   185 	{
       
   186 	// Loop through the list
       
   187 	TInt error = KErrNone;
       
   188 	HBufCType* pData = GetNextArgLC<HBufCType>(aData);
       
   189 	HBufCType* pExpected = GetNextArgLC<HBufCType>(aData);
       
   190 	while( pExpected && pData )
       
   191 		{
       
   192 		// Push front
       
   193 		TRAP(error, aParser->PushFrontL(*pData));
       
   194 		if( error != KErrNone )
       
   195 			{
       
   196 			CleanupStack::PopAndDestroy(2, pData);	// pData, pExpected
       
   197 			return error;
       
   198 			}
       
   199 		// Check against expected
       
   200 		if( aParser->Parser().Des().Compare(*pExpected) != 0 )
       
   201 			{
       
   202 			CleanupStack::PopAndDestroy(2, pData);	// pData, pExpected
       
   203 			return KErrNotFound;
       
   204 			}
       
   205 		// Ok get next set of data
       
   206 		CleanupStack::PopAndDestroy(2, pData);	// pData, pExpected
       
   207 		pData = GetNextArgLC<HBufCType>(aData);
       
   208 		pExpected = GetNextArgLC<HBufCType>(aData);
       
   209 		}
       
   210 	return KErrNone;
       
   211 	}
       
   212 
       
   213 template<class HBufCType, class CDelimitedDataType>
       
   214 TInt DoDelimitedDataPushBackL(CDelimitedDataType* aParser, VA_LIST& aData)
       
   215 	{
       
   216 	// Loop through the list
       
   217 	TInt error = KErrNone;
       
   218 	HBufCType* pData = GetNextArgLC<HBufCType>(aData);
       
   219 	HBufCType* pExpected = GetNextArgLC<HBufCType>(aData);
       
   220 	while( pExpected && pData )
       
   221 		{
       
   222 		// Push front
       
   223 		TRAP(error, aParser->PushBackL(*pData));
       
   224 		if( error != KErrNone )
       
   225 			{
       
   226 			CleanupStack::PopAndDestroy(2, pData);	// pData, pExpected
       
   227 			return error;
       
   228 			}
       
   229 		// Check against expected
       
   230 		if( aParser->Parser().Des().Compare(*pExpected) != 0 )
       
   231 			{
       
   232 			CleanupStack::PopAndDestroy(2, pData);	// pData, pExpected
       
   233 			return KErrNotFound;
       
   234 			}
       
   235 		// Ok get next set of data
       
   236 		CleanupStack::PopAndDestroy(2, pData);	// pData, pExpected
       
   237 		pData = GetNextArgLC<HBufCType>(aData);
       
   238 		pExpected = GetNextArgLC<HBufCType>(aData);
       
   239 		}
       
   240 	return KErrNone;
       
   241 	}
       
   242 
       
   243 template<class HBufCType, class CDelimitedDataType>
       
   244 TInt DoDelimitedDataPopFrontL(CDelimitedDataType* aParser, VA_LIST& aData)
       
   245 	{
       
   246 	// Loop through the list
       
   247 	TInt error = KErrNone;
       
   248 	HBufCType* pExpected = GetNextArgLC<HBufCType>(aData);
       
   249 	while( pExpected )
       
   250 		{
       
   251 		// Push front
       
   252 		TRAP(error, aParser->PopFrontL());
       
   253 		if( error != KErrNone )
       
   254 			{
       
   255 			CleanupStack::PopAndDestroy(pExpected);	// pExpected
       
   256 			return error;
       
   257 			}
       
   258 		// Check against expected
       
   259 		if( aParser->Parser().Des().Compare(*pExpected) != 0 )
       
   260 			{
       
   261 			CleanupStack::PopAndDestroy(pExpected);	// pExpected
       
   262 			return KErrNotFound;
       
   263 			}
       
   264 		// Ok get next set of data
       
   265 		CleanupStack::PopAndDestroy(pExpected);	// pExpected
       
   266 		pExpected = GetNextArgLC<HBufCType>(aData);
       
   267 		}
       
   268 	return KErrNone;
       
   269 	}
       
   270 
       
   271 template<class HBufCType, class CDelimitedDataType>
       
   272 TInt DoDelimitedDataPopBackL(CDelimitedDataType* aParser, VA_LIST& aData)
       
   273 	{
       
   274 	// Loop through the list
       
   275 	TInt error = KErrNone;
       
   276 	HBufCType* pExpected = GetNextArgLC<HBufCType>(aData);
       
   277 	while( pExpected )
       
   278 		{
       
   279 		// Push front
       
   280 		TRAP(error, aParser->PopBackL());
       
   281 		if( error != KErrNone )
       
   282 			{
       
   283 			CleanupStack::PopAndDestroy(pExpected);	// pExpected
       
   284 			return error;
       
   285 			}
       
   286 		// Check against expected
       
   287 		if( aParser->Parser().Des().Compare(*pExpected) != 0 )
       
   288 			{
       
   289 			CleanupStack::PopAndDestroy(pExpected);	// pExpected
       
   290 			return KErrNotFound;
       
   291 			}
       
   292 		// Ok get next set of data
       
   293 		CleanupStack::PopAndDestroy(pExpected);	// pExpected
       
   294 		pExpected = GetNextArgLC<HBufCType>(aData);
       
   295 		}
       
   296 	return KErrNone;
       
   297 	}
       
   298 
       
   299 template<class CDelimitedDataType>
       
   300 TInt DoDelimitedDataAddAndTrimFrontAndBackDelimiterL(CDelimitedDataType* aParser)
       
   301 	{
       
   302 	TInt error = KErrNone;
       
   303 	// Add front delimiter
       
   304 	TRAP(error, aParser->AddFrontDelimiterL());
       
   305 	if( error != KErrNone )
       
   306 		{
       
   307 		return KErrNotFound;
       
   308 		}
       
   309 	if( !aParser->Parser().FrontDelimiter() )
       
   310 		{
       
   311 		return KErrNotFound;
       
   312 		}
       
   313 	// Trim front delimiter
       
   314 	TRAP(error, aParser->TrimFrontDelimiterL());
       
   315 	if( error != KErrNone )
       
   316 		{
       
   317 		return KErrNotFound;
       
   318 		}
       
   319 	if( aParser->Parser().FrontDelimiter() )
       
   320 		{
       
   321 		return KErrNotFound;
       
   322 		}
       
   323 	// Add back delimiter
       
   324 	TRAP(error, aParser->AddBackDelimiterL());
       
   325 	if( error != KErrNone )
       
   326 		{
       
   327 		return KErrNotFound;
       
   328 		}
       
   329 	if( !aParser->Parser().BackDelimiter() )
       
   330 		{
       
   331 		return KErrNotFound;
       
   332 		}
       
   333 	// Trim back delimiter
       
   334 	TRAP(error, aParser->TrimBackDelimiterL());
       
   335 	if( error != KErrNone )
       
   336 		{
       
   337 		return KErrNotFound;
       
   338 		}
       
   339 	if( aParser->Parser().BackDelimiter() )
       
   340 		{
       
   341 		return KErrNotFound;
       
   342 		}
       
   343 	return KErrNone;
       
   344 	}
       
   345 
       
   346 template<class CDelimitedDataType>
       
   347 TInt DoDelimitedDataParseToPosition(CDelimitedDataType* aParser, TInt aStartPos)
       
   348 	{
       
   349 	// Parse to correct place - initial parse must have been done
       
   350 	TInt i=0;
       
   351 	while( i < aStartPos && !aParser->Parser().Eos() )
       
   352 		{
       
   353 		aParser->Parser().Inc();
       
   354 		++i;
       
   355 		}
       
   356 	if( i != aStartPos )
       
   357 		{
       
   358 		return KErrNotFound;
       
   359 		}
       
   360 	return KErrNone;
       
   361 	}
       
   362 
       
   363 template<class HBufCType, class TPtrCType, class CDelimitedDataType>
       
   364 TInt DoDelimitedDataInsertL(CDelimitedDataType* aParser, VA_LIST& aData)
       
   365 	{
       
   366 	// Loop through the list and insert!
       
   367 	TInt error = KErrNone;
       
   368 	HBufCType* pData = GetNextArgLC<HBufCType>(aData);
       
   369 	HBufCType* pExpected = GetNextArgLC<HBufCType>(aData);
       
   370 	while( pExpected && pData )
       
   371 		{
       
   372 		// Get copy of current segment
       
   373 		TPtrCType seg;
       
   374 		aParser->Parser().Peek(seg);
       
   375 		HBufCType* segCopy = seg.AllocLC();
       
   376 		// Insert current
       
   377 		TRAP(error, aParser->InsertCurrentL(*pData));
       
   378 		if( error != KErrNone )
       
   379 			{
       
   380 			CleanupStack::PopAndDestroy(3, pData);	// pData, pExpected, segCopy
       
   381 			return error;
       
   382 			}
       
   383 		// Check against expected
       
   384 		if( aParser->Parser().Des().Compare(*pExpected) != 0 )
       
   385 			{
       
   386 			CleanupStack::PopAndDestroy(3, pData);	// pData, pExpected, segCopy
       
   387 			return KErrNotFound;
       
   388 			}
       
   389 		// Check that the current segment hasn't changed
       
   390 		aParser->Parser().Peek(seg);
       
   391 		if( segCopy->Compare(seg) != 0 )
       
   392 			{
       
   393 			CleanupStack::PopAndDestroy(3, pData);	// pData, pExpected, segCopy
       
   394 			return KErrNotFound;
       
   395 			}
       
   396 		// Ok get next set of data
       
   397 		CleanupStack::PopAndDestroy(3, pData);	// pData, pExpected, segCopy
       
   398 		pData = GetNextArgLC<HBufCType>(aData);
       
   399 		pExpected = GetNextArgLC<HBufCType>(aData);
       
   400 		}
       
   401 	return KErrNone;
       
   402 	}
       
   403 
       
   404 template<class HBufCType, class TPtrCType, class CDelimitedDataType>
       
   405 TInt DoDelimitedDataRemoveL(CDelimitedDataType* aParser, VA_LIST& aData)
       
   406 	{
       
   407 	// Loop through the list
       
   408 	TInt error = KErrNone;
       
   409 	HBufCType* pExpected = GetNextArgLC<HBufCType>(aData);
       
   410 	while( pExpected )
       
   411 		{
       
   412 		// Remove current
       
   413 		TRAP(error, aParser->RemoveCurrentL());
       
   414 		if( error != KErrNone )
       
   415 			{
       
   416 			CleanupStack::PopAndDestroy(pExpected);	// pExpected
       
   417 			return error;
       
   418 			}
       
   419 		// Check against expected
       
   420 		if( aParser->Parser().Des().Compare(*pExpected) != 0 )
       
   421 			{
       
   422 			CleanupStack::PopAndDestroy(pExpected);	// pExpected
       
   423 			return KErrNotFound;
       
   424 			}
       
   425 		// Ok get next set of data
       
   426 		CleanupStack::PopAndDestroy(pExpected);	// pExpected
       
   427 		pExpected = GetNextArgLC<HBufCType>(aData);
       
   428 		}
       
   429 	return KErrNone;
       
   430 	}
       
   431 
       
   432 template<class HBufCType, class TPtrCType, class CDelimitedDataType>
       
   433 TInt DoDelimitedDataInsertAndEscapeL(CDelimitedDataType* aParser, VA_LIST& aData)
       
   434 	{
       
   435 	// Loop through the list and insert!
       
   436 	TInt error = KErrNone;
       
   437 	HBufCType* pData = GetNextArgLC<HBufCType>(aData);
       
   438 	HBufCType* pExpected = GetNextArgLC<HBufCType>(aData);
       
   439 	while( pExpected && pData )
       
   440 		{
       
   441 		// Get copy of current segment
       
   442 		TPtrCType seg;
       
   443 		aParser->Parser().Peek(seg);
       
   444 		HBufCType* segCopy = seg.AllocLC();
       
   445 		// Insert current
       
   446 		TRAP(error, aParser->InsertAndEscapeCurrentL(*pData));
       
   447 		if( error != KErrNone )
       
   448 			{
       
   449 			CleanupStack::PopAndDestroy(3, pData);	// pData, pExpected, segCopy
       
   450 			return error;
       
   451 			}
       
   452 		// Check against expected
       
   453 		if( aParser->Parser().Des().Compare(*pExpected) != 0 )
       
   454 			{
       
   455 			CleanupStack::PopAndDestroy(3, pData);	// pData, pExpected, segCopy
       
   456 			return KErrNotFound;
       
   457 			}
       
   458 		// Check that the current segment hasn't changed
       
   459 		aParser->Parser().Peek(seg);
       
   460 		if( segCopy->Compare(seg) != 0 )
       
   461 			{
       
   462 			CleanupStack::PopAndDestroy(3, pData);	// pData, pExpected, segCopy
       
   463 			return KErrNotFound;
       
   464 			}
       
   465 		// Ok get next set of data
       
   466 		CleanupStack::PopAndDestroy(3, pData);	// pData, pExpected, segCopy
       
   467 		pData = GetNextArgLC<HBufCType>(aData);
       
   468 		pExpected = GetNextArgLC<HBufCType>(aData);
       
   469 		}
       
   470 	return KErrNone;
       
   471 	}
       
   472 
       
   473 template<class HBufCType, class CDelimitedDataType>
       
   474 TInt DoDelimitedDataPushAndEscapeFrontL(CDelimitedDataType* aParser, VA_LIST& aData)
       
   475 	{
       
   476 	// Loop through the list
       
   477 	TInt error = KErrNone;
       
   478 	HBufCType* pData = GetNextArgLC<HBufCType>(aData);
       
   479 	HBufCType* pExpected = GetNextArgLC<HBufCType>(aData);
       
   480 	while( pExpected && pData )
       
   481 		{
       
   482 		// Push front
       
   483 		TRAP(error, aParser->PushAndEscapeFrontL(*pData));
       
   484 		if( error != KErrNone )
       
   485 			{
       
   486 			CleanupStack::PopAndDestroy(2, pData);	// pData, pExpected
       
   487 			return error;
       
   488 			}
       
   489 		// Check against expected
       
   490 		if( aParser->Parser().Des().Compare(*pExpected) != 0 )
       
   491 			{
       
   492 			CleanupStack::PopAndDestroy(2, pData);	// pData, pExpected
       
   493 			return KErrNotFound;
       
   494 			}
       
   495 		// Ok get next set of data
       
   496 		CleanupStack::PopAndDestroy(2, pData);	// pData, pExpected
       
   497 		pData = GetNextArgLC<HBufCType>(aData);
       
   498 		pExpected = GetNextArgLC<HBufCType>(aData);
       
   499 		}
       
   500 	return KErrNone;
       
   501 	}
       
   502 
       
   503 template<class HBufCType, class CDelimitedDataType>
       
   504 TInt DoDelimitedDataPushAndEscapeBackL(CDelimitedDataType* aParser, VA_LIST& aData)
       
   505 	{
       
   506 	// Loop through the list
       
   507 	TInt error = KErrNone;
       
   508 	HBufCType* pData = GetNextArgLC<HBufCType>(aData);
       
   509 	HBufCType* pExpected = GetNextArgLC<HBufCType>(aData);
       
   510 	while( pExpected && pData )
       
   511 		{
       
   512 		// Push front
       
   513 		TRAP(error, aParser->PushAndEscapeBackL(*pData));
       
   514 		if( error != KErrNone )
       
   515 			{
       
   516 			CleanupStack::PopAndDestroy(2, pData);	// pData, pExpected
       
   517 			return error;
       
   518 			}
       
   519 		// Check against expected
       
   520 		if( aParser->Parser().Des().Compare(*pExpected) != 0 )
       
   521 			{
       
   522 			CleanupStack::PopAndDestroy(2, pData);	// pData, pExpected
       
   523 			return KErrNotFound;
       
   524 			}
       
   525 		// Ok get next set of data
       
   526 		CleanupStack::PopAndDestroy(2, pData);	// pData, pExpected
       
   527 		pData = GetNextArgLC<HBufCType>(aData);
       
   528 		pExpected = GetNextArgLC<HBufCType>(aData);
       
   529 		}
       
   530 	return KErrNone;
       
   531 	}