upnp/upnpstack/upnputils/src/upnpstring.cpp
changeset 0 f5a58ecadc66
equal deleted inserted replaced
-1:000000000000 0:f5a58ecadc66
       
     1 /** @file
       
     2 * Copyright (c) 2005-2006 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:  String handling
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDES
       
    20 #include <e32base.h>
       
    21 #include <charconv.h>
       
    22 #include <f32file.h>
       
    23 #include <s32mem.h>
       
    24 #include <escapeutils.h>
       
    25 #include "upnpcons.h"
       
    26 #include "upnpstring.h"
       
    27 #include "upnpcommonupnplits.h"
       
    28 
       
    29 // ================= MEMBER FUNCTIONS =======================
       
    30 
       
    31 
       
    32 namespace UpnpString
       
    33     {
       
    34     
       
    35     // PR: following for optimized encode/decodexml functions    
       
    36     _LIT8( KGtEntity,       "gt;");
       
    37     _LIT8( KLtEntity,       "lt;");
       
    38     _LIT8( KQuotEntity,     "quot;");
       
    39     _LIT8( KAposEntity,     "apos;");
       
    40     _LIT8( KAmpEntity,      "amp;");
       
    41     
       
    42     // for formatting a date string (in UT)
       
    43     _LIT8( KDay1,"Mon");
       
    44     _LIT8( KDay2,"Tue");
       
    45     _LIT8( KDay3,"Wed");
       
    46     _LIT8( KDay4,"Thu");
       
    47     _LIT8( KDay5,"Fri");
       
    48     _LIT8( KDay6,"Sat");
       
    49     _LIT8( KDay7,"Sun");
       
    50     
       
    51     // for formatting a date string (in UT)
       
    52     _LIT8( KMonth1,"Jan");
       
    53     _LIT8( KMonth2,"Feb");
       
    54     _LIT8( KMonth3,"Mar");
       
    55     _LIT8( KMonth4,"Apr");
       
    56     _LIT8( KMonth5,"May");
       
    57     _LIT8( KMonth6,"Jun");
       
    58     _LIT8( KMonth7,"Jul");
       
    59     _LIT8( KMonth8,"Aug");
       
    60     _LIT8( KMonth9,"Sep");
       
    61     _LIT8( KMonth10,"Oct");
       
    62     _LIT8( KMonth11,"Nov");
       
    63     _LIT8( KMonth12,"Dec");
       
    64     
       
    65     //for formatting UT
       
    66     _LIT8( KTimeGMT,"GMT");
       
    67     
       
    68     //for formatting UT
       
    69     const static TInt KWidthFour = 4;
       
    70     const static TInt KWidthTwo = 2;
       
    71     
       
    72     const static TInt KMaxEntityLength = 6; // e.g. &quot;
       
    73     const static TInt KEntitiesCount = 5;
       
    74     const static TReal KReallocRatio = 1.3;
       
    75 
       
    76 
       
    77 // -----------------------------------------------------------------------------
       
    78 // UpnpString::DeleteArray
       
    79 // 
       
    80 // -----------------------------------------------------------------------------
       
    81 //
       
    82 void DeleteArray(TAny* param);
       
    83     
       
    84 // -----------------------------------------------------------------------------
       
    85 // UpnpString::IsColon
       
    86 // Is char colon.
       
    87 // -----------------------------------------------------------------------------
       
    88 //
       
    89 EXPORT_C TBool IsColon(const TUint8& aChar)
       
    90     {
       
    91     return aChar == ':';
       
    92     }
       
    93 // -----------------------------------------------------------------------------
       
    94 // UpnpString::FromUnicodeL
       
    95 // Convert from unicode.
       
    96 // -----------------------------------------------------------------------------
       
    97 //    
       
    98 EXPORT_C HBufC8* FromUnicodeL(const TDesC& aBuffer)
       
    99     {
       
   100     return EscapeUtils::ConvertFromUnicodeToUtf8L(aBuffer);
       
   101     }
       
   102 
       
   103 // -----------------------------------------------------------------------------
       
   104 // UpnpString::ToUnicodeL
       
   105 // Convert to unicode.
       
   106 // -----------------------------------------------------------------------------
       
   107 //    
       
   108 EXPORT_C HBufC16* ToUnicodeL(const TDesC8& aBuffer)
       
   109     {
       
   110     return EscapeUtils::ConvertToUnicodeFromUtf8L(aBuffer);
       
   111     }
       
   112 
       
   113 // -----------------------------------------------------------------------------
       
   114 // UpnpString::CurrentDateLC
       
   115 // Return current date.
       
   116 // -----------------------------------------------------------------------------
       
   117 //    
       
   118 EXPORT_C HBufC8* CurrentDateLC()
       
   119     {
       
   120     HBufC8* timeBuf = NULL;
       
   121         
       
   122     TTime time;    
       
   123     time.UniversalTime();    
       
   124     
       
   125     timeBuf = GetDateLC(time);
       
   126     
       
   127     return timeBuf;
       
   128     }
       
   129 
       
   130 // -----------------------------------------------------------------------------
       
   131 // UpnpString::GetDateLC
       
   132 // Return current date.
       
   133 // -----------------------------------------------------------------------------
       
   134 //    
       
   135 HBufC8* GetDateLC(const TTime& aTime)
       
   136     {
       
   137     TDateTime datetime = aTime.DateTime();    
       
   138     
       
   139     HBufC8* timeBuf = HBufC8::NewLC(KDateTimeStringLen);
       
   140     //weekday
       
   141     timeBuf->Des().Copy(GetWeekday(aTime));
       
   142     timeBuf->Des().Append(KComma);
       
   143     timeBuf->Des().Append(KSpace);
       
   144     
       
   145     //date
       
   146     //days numerated from 0 to 30, so +1
       
   147     timeBuf->Des().AppendNumFixedWidth((datetime.Day()+1),EDecimal, KWidthTwo);
       
   148     timeBuf->Des().Append(KSpace);
       
   149     timeBuf->Des().Append(GetMonth(datetime));
       
   150     timeBuf->Des().Append(KSpace);    
       
   151     timeBuf->Des().AppendNumFixedWidth(datetime.Year(),EDecimal, KWidthFour);
       
   152     timeBuf->Des().Append(KSpace);
       
   153     
       
   154     //time
       
   155     timeBuf->Des().AppendNumFixedWidth((datetime.Hour()),EDecimal, KWidthTwo);
       
   156     timeBuf->Des().Append(KColon);
       
   157     timeBuf->Des().AppendNumFixedWidth((datetime.Minute()),EDecimal, KWidthTwo);
       
   158     timeBuf->Des().Append(KColon);
       
   159     timeBuf->Des().AppendNumFixedWidth((datetime.Second()),EDecimal, KWidthTwo);
       
   160     timeBuf->Des().Append(KSpace);
       
   161     
       
   162     //GMT
       
   163     timeBuf->Des().Append(KTimeGMT);    
       
   164     
       
   165     return timeBuf;
       
   166     }
       
   167 
       
   168 // -----------------------------------------------------------------------------
       
   169 // UpnpString::GetWeekday
       
   170 // 
       
   171 // -----------------------------------------------------------------------------
       
   172 //    
       
   173 const TDesC8& GetWeekday(const TTime& aDate)
       
   174     {
       
   175     switch (aDate.DayNoInWeek())
       
   176         {
       
   177         case EMonday:
       
   178             return KDay1();            
       
   179         case ETuesday:
       
   180             return KDay2();    
       
   181         case EWednesday:
       
   182             return KDay3();
       
   183         case EThursday:
       
   184             return KDay4();
       
   185         case EFriday:
       
   186             return KDay5();   
       
   187         case ESaturday:
       
   188             return KDay6();
       
   189         case ESunday:
       
   190             return KDay7();
       
   191         default:
       
   192             return KNullDesC8;                                                    
       
   193             
       
   194         }
       
   195     }
       
   196 
       
   197 // -----------------------------------------------------------------------------
       
   198 // UpnpString::GetMonth
       
   199 // 
       
   200 // -----------------------------------------------------------------------------
       
   201 //    
       
   202 const TDesC8& GetMonth(const TDateTime& aDate)
       
   203     {
       
   204     switch (aDate.Month())
       
   205         {
       
   206         case EJanuary:
       
   207             return KMonth1();            
       
   208         case EFebruary:
       
   209             return KMonth2();    
       
   210         case EMarch:
       
   211             return KMonth3();
       
   212         case EApril:
       
   213             return KMonth4();
       
   214         case EMay:
       
   215             return KMonth5();   
       
   216         case EJune:
       
   217             return KMonth6();
       
   218         case EJuly:
       
   219             return KMonth7();
       
   220         case EAugust:
       
   221             return KMonth8();
       
   222         case ESeptember:
       
   223             return KMonth9();
       
   224         case EOctober:
       
   225             return KMonth10();
       
   226         case ENovember:
       
   227             return KMonth11();
       
   228         case EDecember:
       
   229             return KMonth12();                    
       
   230         default:
       
   231             return KNullDesC8;        
       
   232         }
       
   233     }    
       
   234 
       
   235 // -----------------------------------------------------------------------------
       
   236 // UpnpString::CutToPiecesL
       
   237 // 
       
   238 // -----------------------------------------------------------------------------
       
   239 //    
       
   240 EXPORT_C void CutToPiecesL( TPtrC8 aPtr, 
       
   241                             TChar aDelim, 
       
   242                             RPointerArray<TPtrC8>& aArray )
       
   243     {
       
   244     TInt i = 0;
       
   245     CleanupStack::PushL(TCleanupItem(&DeleteArray,&aArray));
       
   246 
       
   247     while(KErrNotFound != aPtr.Locate(aDelim))
       
   248         {    
       
   249         TPtrC8* tmp = new (ELeave)TPtrC8();
       
   250         CleanupStack::PushL(tmp);
       
   251         User::LeaveIfError( aArray.Append( tmp ) );
       
   252         CleanupStack::Pop(tmp);
       
   253     
       
   254         aArray[i]->Set(aPtr.Left((aPtr.Locate(aDelim))));
       
   255         aPtr.Set(aPtr.Right(aPtr.Length() - (aPtr.Locate(aDelim) + 1)) ); 
       
   256         aArray[i]->Set(Trim(*aArray[i], EFalse));
       
   257         i++;
       
   258         }
       
   259     TPtrC8* tmp = new (ELeave)TPtrC8();
       
   260     CleanupStack::PushL(tmp);
       
   261     User::LeaveIfError( aArray.Append( tmp ) );
       
   262     CleanupStack::Pop(tmp);        
       
   263        
       
   264     aArray[i]->Set(aPtr);
       
   265     aArray[i]->Set(Trim(*aArray[i], EFalse));
       
   266         
       
   267     CleanupStack::Pop(&aArray);    
       
   268     }
       
   269 
       
   270 // -----------------------------------------------------------------------------
       
   271 // UpnpString::Trim
       
   272 // 
       
   273 // -----------------------------------------------------------------------------
       
   274 //    
       
   275 EXPORT_C TPtrC8 Trim(TPtrC8 aPtr, TBool aQuotations)
       
   276     {
       
   277     if( aPtr.Length() == 0 )
       
   278         {
       
   279         return aPtr;
       
   280         }
       
   281     while( ( ('"' == aPtr[0]) && aQuotations ) ||
       
   282         (' ' == aPtr[0]) )
       
   283         {
       
   284             aPtr.Set(aPtr.Right(aPtr.Length() - 1));
       
   285         if( aPtr.Length() == 0) 
       
   286             {
       
   287             return aPtr;
       
   288             }
       
   289         }
       
   290 
       
   291     while( ( ('"' == aPtr[aPtr.Length() - 1] ) && aQuotations ) ||
       
   292         (' ' == aPtr[aPtr.Length() - 1] ) )
       
   293         {
       
   294         aPtr.Set(aPtr.Left(aPtr.Length() - 1 ));
       
   295         if( aPtr.Length() == 0 ) 
       
   296             {
       
   297             return aPtr;
       
   298             }
       
   299         }
       
   300     return aPtr;
       
   301     }
       
   302 
       
   303 // -----------------------------------------------------------------------------
       
   304 // CutToPiecesL
       
   305 // 
       
   306 // -----------------------------------------------------------------------------
       
   307 //
       
   308 void CutToPiecesL( TPtrC8 aPtr,TChar aDelim, RArray<TPtrC8>& aArray )
       
   309     {
       
   310     TInt i = 0;
       
   311     TInt loc = aPtr.Locate(aDelim);
       
   312     while(KErrNotFound != loc)
       
   313         {    
       
   314         TPtrC8 r(aPtr.Left(loc));
       
   315         User::LeaveIfError(aArray.Append(r));
       
   316         aPtr.Set(aPtr.Right(aPtr.Length() - (loc + 1)) ); 
       
   317         i++;
       
   318         loc = aPtr.Locate(aDelim);
       
   319         }
       
   320     TInt parenthesis;
       
   321     parenthesis =  aPtr.Locate(')');
       
   322     if( KErrNotFound != parenthesis )
       
   323         {
       
   324         User::LeaveIfError(aArray.Append(aPtr.Left( parenthesis )));
       
   325         }
       
   326     else
       
   327         {
       
   328         User::LeaveIfError(aArray.Append(aPtr));
       
   329           }
       
   330     }
       
   331 
       
   332 // -----------------------------------------------------------------------------
       
   333 // UpnpString::ValueFromCsvL
       
   334 // 
       
   335 // -----------------------------------------------------------------------------
       
   336 //
       
   337 EXPORT_C HBufC8* ValueFromCsvL(const TDesC8& aCSV, TInt aPlace)
       
   338     {
       
   339     RArray<TPtrC8> array;
       
   340     CleanupClosePushL(array);
       
   341     CutToPiecesL(aCSV, ',', array);
       
   342 
       
   343     HBufC8* value = HBufC8::NewL(array[aPlace].Length());
       
   344     value->Des().Copy(array[aPlace]);
       
   345     CleanupStack::PopAndDestroy();
       
   346     return value;
       
   347     }
       
   348 
       
   349 // -----------------------------------------------------------------------------
       
   350 // UpnpString::AddValueToCsvL
       
   351 // 
       
   352 // -----------------------------------------------------------------------------
       
   353 //
       
   354 EXPORT_C HBufC8* AddValueToCsvL(const TDesC8& aCSV, const TDesC8& aValue)
       
   355     {
       
   356     HBufC8* buf=NULL;
       
   357     if( 0 == KEmptyString().Compare(aCSV) )
       
   358         {
       
   359         buf = aValue.AllocL();
       
   360         }
       
   361     else
       
   362         {
       
   363         buf = HBufC8::NewL(aCSV.Length() + KComma().Length() + aValue.Length());
       
   364         buf->Des().Append(aCSV);
       
   365         buf->Des().Append(KComma());
       
   366         buf->Des().Append(aValue);
       
   367         }
       
   368     return buf;
       
   369     }
       
   370 
       
   371 // -----------------------------------------------------------------------------
       
   372 // UpnpString::RemoveFromCsvLC
       
   373 // 
       
   374 // -----------------------------------------------------------------------------
       
   375 //
       
   376 EXPORT_C HBufC8* RemoveFromCsvLC(const TDesC8& aCSV, const TDesC8& aValue)
       
   377     {
       
   378     RPointerArray<TPtrC8> array;
       
   379     CsvToRPointerArrayL(aCSV, array);
       
   380     TPtrC8 val(Trim(aValue, EFalse));
       
   381     for( TInt i = 0; i < array.Count(); i++ )
       
   382         {
       
   383         if( 0 == array[i]->CompareF(val) )
       
   384             {
       
   385             delete array[i];
       
   386             array.Remove(i);
       
   387             }
       
   388         }
       
   389     CleanupStack::PushL(TCleanupItem(&DeleteArray, &array));    
       
   390     HBufC8* buf = RPointerArrayToCsvLC( array );
       
   391     CleanupStack::Pop( buf ); 
       
   392     CleanupStack::PopAndDestroy( &array ); 
       
   393     CleanupStack::PushL( buf ); 
       
   394     array.Close();
       
   395     return buf;
       
   396     }
       
   397 
       
   398 // -----------------------------------------------------------------------------
       
   399 // UpnpString::CsvToRPointerArrayL
       
   400 // 
       
   401 // -----------------------------------------------------------------------------
       
   402 //
       
   403 EXPORT_C void CsvToRPointerArrayL( const TDesC8& aCSV, 
       
   404                                    RPointerArray<TPtrC8>& aArray )
       
   405     {
       
   406     aArray.ResetAndDestroy();
       
   407     CutToPiecesL(aCSV, ',', aArray);
       
   408     }
       
   409 
       
   410 // -----------------------------------------------------------------------------
       
   411 // UpnpString::RPointerArrayToCsvLC
       
   412 // 
       
   413 // -----------------------------------------------------------------------------
       
   414 //
       
   415 EXPORT_C HBufC8* RPointerArrayToCsvLC( RPointerArray<TPtrC8>& aArray )
       
   416     {
       
   417     TInt length (0);
       
   418     TInt i(0);
       
   419     for( ; i< aArray.Count(); ++i)
       
   420         {
       
   421         length += aArray[i]->Length() + KComma().Length();
       
   422         }
       
   423 
       
   424     HBufC8* buf = HBufC8::NewLC(length);
       
   425     TPtr8 des = buf->Des();
       
   426 
       
   427     for( i = 0; i < aArray.Count(); i++ )
       
   428         {
       
   429         des.Append(*aArray[i]);
       
   430         if( i != aArray.Count() - 1 )
       
   431             {
       
   432             des.Append(KComma());
       
   433             }
       
   434         }
       
   435     return buf;
       
   436     }
       
   437 
       
   438 // -----------------------------------------------------------------------------
       
   439 // UpnpString::AppendStringL
       
   440 // Append to string.
       
   441 // -----------------------------------------------------------------------------
       
   442 //
       
   443 EXPORT_C void AppendStringL( CBufFlat& aBuffer, const TDesC8& aString )
       
   444     {
       
   445     RBufWriteStream bufWs(aBuffer, aBuffer.Size());
       
   446     CleanupClosePushL(bufWs);
       
   447     bufWs.WriteL(aString);
       
   448     bufWs.CommitL();
       
   449     CleanupStack::PopAndDestroy(); 
       
   450     }
       
   451 
       
   452 // -----------------------------------------------------------------------------
       
   453 // UpnpString::AppendStringL
       
   454 // Append to string.
       
   455 // -----------------------------------------------------------------------------
       
   456 //
       
   457 EXPORT_C void AppendStringL( HBufC8*& aString, const TDesC8& aString2 )
       
   458     {
       
   459     if(aString==NULL)
       
   460         { 
       
   461         if(aString2.Length()==0)
       
   462             {
       
   463             return;
       
   464             }
       
   465         aString = HBufC8::NewLC(aString2.Length());
       
   466         aString->Des().Append(aString2);
       
   467         CleanupStack::Pop();
       
   468         }
       
   469     else if(aString2.Length()==0)
       
   470         {
       
   471         return;
       
   472         }
       
   473     else
       
   474         {
       
   475         HBufC8* nString = HBufC8::NewLC(aString->Length() + aString2.Length());
       
   476         nString->Des().Append(aString->Des());
       
   477         nString->Des().Append(aString2);
       
   478 
       
   479         delete aString;
       
   480         aString = nString;
       
   481         CleanupStack::Pop();
       
   482         }
       
   483     }
       
   484 
       
   485 // -----------------------------------------------------------------------------
       
   486 // UpnpString::StringToInt
       
   487 // String to integer.
       
   488 // -----------------------------------------------------------------------------
       
   489 //
       
   490 EXPORT_C TInt StringToInt(const TDesC8& aStr, TInt* aInt)
       
   491     {
       
   492     if( 0 == aStr.Length() || aInt == NULL)
       
   493         {
       
   494         return KErrArgument;
       
   495         }
       
   496     TLex8 lex( aStr );
       
   497     return lex.Val( *aInt );
       
   498     }
       
   499 
       
   500 // -----------------------------------------------------------------------------
       
   501 // UpnpString::StringReplaceL
       
   502 // 
       
   503 // -----------------------------------------------------------------------------
       
   504 //    
       
   505 EXPORT_C void StringReplaceL( const TDesC8& aOrginal, 
       
   506                               HBufC8*& aResult, 
       
   507                               const TDesC8& aTrg, 
       
   508                               const TDesC8& aReplacement )
       
   509     {
       
   510     TInt place = aOrginal.Find( aTrg );
       
   511     if( KErrNotFound != place )
       
   512         {
       
   513         aResult = StringReplaceL(aOrginal, aTrg, aReplacement);
       
   514         }
       
   515     else
       
   516         {    
       
   517         delete aResult;
       
   518         aResult = NULL;
       
   519         aResult = aOrginal.AllocL();
       
   520         }
       
   521     }
       
   522 
       
   523 // -----------------------------------------------------------------------------
       
   524 // UpnpString::StringReplaceL
       
   525 // 
       
   526 // -----------------------------------------------------------------------------
       
   527 //    
       
   528 EXPORT_C HBufC8* StringReplaceL( const TDesC8& aStr, 
       
   529                                  const TDesC8& aTrg, 
       
   530                                  const TDesC8& aReplacement )
       
   531     {
       
   532     __ASSERT_DEBUG(aTrg.Length() && aStr.Length(), User::Panic(_L("String"), 0));
       
   533     TInt length = aStr.Length();
       
   534     if(aReplacement.Length() > aTrg.Length())
       
   535         {
       
   536         TInt position(0);
       
   537         TPtrC8 loc;
       
   538         TInt numOccurrences(0);
       
   539         loc.Set(aStr);
       
   540         for(;;)
       
   541             {
       
   542             position = loc.Find(aTrg);
       
   543             if(position == KErrNotFound)
       
   544                 {
       
   545                 break;
       
   546                 }
       
   547             numOccurrences++;
       
   548             position +=aTrg.Length();
       
   549             if(position >= loc.Length())
       
   550                 {
       
   551                 break;
       
   552                 }
       
   553             loc.Set(loc.Mid(position)); 
       
   554             }
       
   555         length += (aReplacement.Length() - aTrg.Length())*numOccurrences;
       
   556         }
       
   557     HBufC8* result = HBufC8::NewLC(length);
       
   558     TPtr8 ptr = result->Des();
       
   559     ptr.Copy(aStr);
       
   560     StringReplaceInPlaceL(ptr, aTrg, aReplacement);
       
   561     if(ptr.MaxLength() > ptr.Length())
       
   562         {
       
   563         result = result->ReAllocL(result->Length());
       
   564         }
       
   565     CleanupStack::Pop(); 
       
   566     return result;
       
   567     }
       
   568 
       
   569 // -----------------------------------------------------------------------------
       
   570 // UpnpString::StringReplaceInPlaceL
       
   571 // expected: aTrg.Length() >= aReplacement.Length()
       
   572 // -----------------------------------------------------------------------------
       
   573 //    
       
   574 EXPORT_C void StringReplaceInPlaceL(
       
   575     TDes8& aString, 
       
   576     const TDesC8& aTrg,
       
   577     const TDesC8& aReplacement )
       
   578     {
       
   579     __ASSERT_DEBUG(aTrg != aReplacement, User::Panic(_L("String"), 1));
       
   580 
       
   581     TPtrC8 ptr(aString);
       
   582     TInt position(0);
       
   583     FOREVER
       
   584         {
       
   585         TInt found = ptr.Find( aTrg );
       
   586         if(found < 0)
       
   587             {
       
   588             return;
       
   589             }
       
   590         aString.Replace( position + found, aTrg.Length(), aReplacement );
       
   591         if(position + found + aReplacement.Length() < aString.Length())
       
   592             {
       
   593             position += found + aReplacement.Length();
       
   594             ptr.Set(aString.Mid(position));
       
   595             }
       
   596         else
       
   597             return;
       
   598         }
       
   599     }
       
   600 
       
   601 // -----------------------------------------------------------------------------
       
   602 // String::EncodeXmlStringL
       
   603 // Encode XML string.
       
   604 // -----------------------------------------------------------------------------
       
   605 // 
       
   606 
       
   607 EXPORT_C HBufC8* EncodeXmlStringL( HBufC8*& aBuf )
       
   608     {
       
   609     return EncodeXmlStringL( *aBuf );
       
   610     }
       
   611 
       
   612 // -----------------------------------------------------------------------------
       
   613 // String::DecodeXmlStringL
       
   614 // Decode XML string.
       
   615 // -----------------------------------------------------------------------------
       
   616 //
       
   617 EXPORT_C HBufC8* DecodeXmlStringL( HBufC8*& aBuf )
       
   618     {
       
   619     return DecodeXmlStringL( *aBuf );
       
   620     }
       
   621 
       
   622 EXPORT_C HBufC8* EncodeXmlStringL( const TDesC8& aXmlString )
       
   623     {
       
   624     const TPtrC8 KEntities[5] = 
       
   625         {
       
   626         KGtEntity(), KLtEntity(), KQuotEntity(), KAposEntity(), KAmpEntity()
       
   627         };
       
   628     const TChar KChars[5] = 
       
   629         {
       
   630         '>', '<', '"', '\'', '&' 
       
   631         };
       
   632     
       
   633     HBufC8* result = HBufC8::NewLC( aXmlString.Length() * KReallocRatio );
       
   634     TPtr8 ptr = result->Des();
       
   635     
       
   636     TLex8 lexer( aXmlString );
       
   637     TChar ch;
       
   638     while ( ch = lexer.Get() )
       
   639         {
       
   640         if ( result->Length() >= ( ptr.MaxLength() - KMaxEntityLength ) )
       
   641             {
       
   642             TInt newLength( ptr.MaxLength() * KReallocRatio ) ;
       
   643             
       
   644             result = result->ReAllocL( newLength );
       
   645             CleanupStack::Pop(); // "old" result descriptor
       
   646             CleanupStack::PushL( result );
       
   647             ptr.Set( result->Des() );
       
   648             
       
   649             }
       
   650         TBool found = EFalse;
       
   651         for ( TInt i = 0; i < KEntitiesCount; i++ )
       
   652             {
       
   653             if ( ch == KChars[i] )
       
   654                 {
       
   655                 ptr.Append( '&' );
       
   656                 ptr.Append( KEntities[i] );
       
   657                 found = ETrue;
       
   658                 break;
       
   659                 }
       
   660             }
       
   661         if ( !found )
       
   662             {
       
   663             ptr.Append( ch );
       
   664             }
       
   665         }
       
   666     CleanupStack::Pop( result );
       
   667     return result;
       
   668     }
       
   669 
       
   670 EXPORT_C HBufC8* DecodeXmlStringL( const TDesC8& aXmlString )
       
   671     {
       
   672     const TPtrC8 KEntities[5] = 
       
   673         {
       
   674         KGtEntity(), KLtEntity(), KQuotEntity(), KAposEntity(), KAmpEntity()
       
   675         };
       
   676     const TChar KChars[5] = 
       
   677         {
       
   678         '>', '<', '"', '\'', '&' 
       
   679         };
       
   680     
       
   681     HBufC8* result = HBufC8::NewLC( aXmlString.Length() );
       
   682     TPtr8 ptr = result->Des();
       
   683     
       
   684     TLex8 lexer( aXmlString );
       
   685     TChar ch;
       
   686     TBool insideEntity = EFalse;
       
   687     
       
   688     while ( ch = lexer.Get() )
       
   689         {
       
   690         if ( insideEntity )
       
   691             {
       
   692             if ( ch == ';' ) // entity ends
       
   693                 {
       
   694                 insideEntity = EFalse;
       
   695                 TPtrC8 entity = lexer.MarkedToken();
       
   696                 TBool found = EFalse;
       
   697                 
       
   698                 for ( TInt i = 0; i < KEntitiesCount; i++ )
       
   699                     {
       
   700                     if ( !entity.Compare( KEntities[i] )) // match
       
   701                         {
       
   702                         ptr.Append( KChars[i] );
       
   703                         found = ETrue;
       
   704                         break;           
       
   705                         }
       
   706                     }
       
   707                 if ( !found )
       
   708                     {
       
   709                     // no match, appending as it is
       
   710                     ptr.Append( '&' );
       
   711                     ptr.Append( entity );    
       
   712                     }
       
   713                 }
       
   714             }
       
   715         else 
       
   716             {
       
   717             if ( ch == '&' ) // entity starts
       
   718                 {
       
   719                 insideEntity = ETrue;
       
   720                 lexer.Mark();
       
   721                 }
       
   722             else 
       
   723                 {
       
   724                 ptr.Append( ch );
       
   725                 }
       
   726             }
       
   727         }
       
   728     CleanupStack::Pop( result );
       
   729     return result;
       
   730     }
       
   731 
       
   732 
       
   733 // -----------------------------------------------------------------------------
       
   734 // UpnpString::InetToStringL
       
   735 // Intet address to string.
       
   736 // -----------------------------------------------------------------------------
       
   737 //
       
   738 EXPORT_C HBufC8* InetToStringL(const TInetAddr& aAddress)
       
   739     {
       
   740     TBuf<20> address;
       
   741     aAddress.Output(address);
       
   742     HBufC8* host8 = UpnpString::FromUnicodeL(address);
       
   743     CleanupStack::PushL(host8);
       
   744     TBuf8<10> port;
       
   745     port.Num(aAddress.Port());
       
   746     
       
   747     HBufC8* host = HBufC8::NewL(host8->Length() + port.Length() + 1);
       
   748     host->Des().Zero();
       
   749     host->Des().Append(*host8);
       
   750     host->Des().Append(UpnpString::KColon());
       
   751     host->Des().Append(port);
       
   752     
       
   753     CleanupStack::PopAndDestroy(host8);
       
   754 
       
   755     return host;
       
   756     }
       
   757     
       
   758 // -----------------------------------------------------------------------------
       
   759 // UpnpString::TrimLC
       
   760 // (other items were commented in a header).
       
   761 // -----------------------------------------------------------------------------
       
   762 //   
       
   763 EXPORT_C HBufC8* TrimLC(const TDesC8& aBuf, TBool aQuotations)
       
   764     {
       
   765     if( aBuf.Length() == 0 )
       
   766         {
       
   767         return aBuf.AllocLC();
       
   768         }
       
   769     TPtrC8 ptr(aBuf);    
       
   770 
       
   771     while( ( ('"' == ptr[0]) && aQuotations ) ||
       
   772         (' ' == ptr[0]) )
       
   773         {
       
   774         if(ptr.Length() == 1)
       
   775             {
       
   776             return HBufC8::NewLC(0);
       
   777             }
       
   778         ptr.Set(ptr.Mid(1));
       
   779         }
       
   780     while( ( ('"' == ptr[ptr.Length() - 1] ) && aQuotations ) ||
       
   781         (' ' == ptr[ptr.Length() - 1] ) )
       
   782         {
       
   783         ptr.Set(ptr.Left(ptr.Length() -1));            
       
   784         }
       
   785 
       
   786     return ptr.AllocLC();
       
   787     }
       
   788 
       
   789 // -----------------------------------------------------------------------------
       
   790 // UpnpString::DeleteArray
       
   791 // Empties the array and deletes the referenced objects.
       
   792 // -----------------------------------------------------------------------------
       
   793 //
       
   794 void DeleteArray(TAny* param)
       
   795     {
       
   796     RPointerArray<TPtrC8>* array;
       
   797     
       
   798     array = reinterpret_cast<RPointerArray<TPtrC8>*>(param);
       
   799     array->ResetAndDestroy();
       
   800     }
       
   801 
       
   802     // -----------------------------------------------------------------------------
       
   803     // UpnpString::ReplaceHttpCharacters
       
   804     // -----------------------------------------------------------------------------
       
   805     //
       
   806 EXPORT_C void ReplaceHttpCharacters(TDes8& aString)
       
   807     {
       
   808     TInt i=-1;
       
   809     while(++i < aString.Length())
       
   810         {    //check if it is "%" and that the length from this position is at least 
       
   811         //3 = length of "%" HEX HEX
       
   812             if( (aString[i] == '%') && (aString.Length() >= i+3) )
       
   813             {
       
   814             TLex8 lexer(aString.Mid(i+1, KUrlCharNoLen-1)); // two characters next to '%'
       
   815             TUint8 charNo;
       
   816             if(lexer.Val(charNo, EHex) == KErrNone )
       
   817                 {
       
   818                 TText8 c(charNo);
       
   819                 TPtrC8 charDes(&c, KOneCharLen);
       
   820                 aString.Replace(i, KUrlCharNoLen, charDes);
       
   821                 }
       
   822             }
       
   823         } 
       
   824     }
       
   825     
       
   826 // -----------------------------------------------------------------------------
       
   827 // UpnpString::ReplaceHttpCharactersL
       
   828 // -----------------------------------------------------------------------------
       
   829 //
       
   830 EXPORT_C void ReplaceHttpCharactersL(TDes& aString)
       
   831     {
       
   832     HBufC8* temp8 = FromUnicodeL(aString);
       
   833     CleanupStack::PushL(temp8);
       
   834     TPtr8 tempPtr(temp8->Des());
       
   835     ReplaceHttpCharacters(tempPtr);
       
   836     
       
   837     HBufC* temp16 = ToUnicodeL(tempPtr);
       
   838     aString.Copy(*temp16);
       
   839     delete temp16;
       
   840     CleanupStack::PopAndDestroy(temp8);
       
   841     }
       
   842 
       
   843 }
       
   844 //  End of file