javaextensions/iapinfo/src.s60/stringbuffer.cpp
branchRCL_3
changeset 14 04becd199f91
equal deleted inserted replaced
13:f5050f1da672 14:04becd199f91
       
     1 /*
       
     2 * Copyright (c) 2008 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:  stringbuffer class implementation
       
    15  *
       
    16 */
       
    17 
       
    18 
       
    19 #include "stringbuffer.h"
       
    20 
       
    21 //////////////////////////////////////////////////////////////////////////////
       
    22 //
       
    23 // NewL Method to create a new StringBuffer object
       
    24 // @return StringBuffer*
       
    25 //
       
    26 //////////////////////////////////////////////////////////////////////////////
       
    27 
       
    28 StringBuffer* StringBuffer::NewL()
       
    29 {
       
    30     StringBuffer* self = new(ELeave) StringBuffer;
       
    31     CleanupStack::PushL(self);
       
    32     self->ConstructL();
       
    33     CleanupStack::Pop(self);
       
    34     return self;
       
    35 }
       
    36 
       
    37 //////////////////////////////////////////////////////////////////////////////
       
    38 //
       
    39 // Constructor Method
       
    40 //
       
    41 //////////////////////////////////////////////////////////////////////////////
       
    42 StringBuffer::StringBuffer()
       
    43 {
       
    44 
       
    45 }
       
    46 
       
    47 //////////////////////////////////////////////////////////////////////////////
       
    48 //
       
    49 // ConstructL to create the String object which stores the data
       
    50 //
       
    51 //////////////////////////////////////////////////////////////////////////////
       
    52 void StringBuffer::ConstructL()
       
    53 {
       
    54     String = HBufC::NewL(KInitialSize);
       
    55 }
       
    56 
       
    57 //////////////////////////////////////////////////////////////////////////////
       
    58 //
       
    59 // AppendText Method to append the content of an HBufC object to the
       
    60 // StringBuffer's content
       
    61 // @param HBufC*
       
    62 //
       
    63 //////////////////////////////////////////////////////////////////////////////
       
    64 
       
    65 void StringBuffer::Append(HBufC* what)
       
    66 {
       
    67     TInt _size = what->Length();
       
    68     if ((String->Des().MaxLength() - String->Size()) < _size)
       
    69     {
       
    70         String = String->ReAlloc(String->Des().MaxLength() + 2 * _size);
       
    71     }
       
    72     String->Des().Append(what->Des());
       
    73 }
       
    74 
       
    75 //////////////////////////////////////////////////////////////////////////////
       
    76 //
       
    77 // GetString Method to get the content of the StringBuffer
       
    78 // @return HBufC *
       
    79 //
       
    80 //////////////////////////////////////////////////////////////////////////////
       
    81 
       
    82 HBufC* StringBuffer::GetString()
       
    83 {
       
    84     return String;
       
    85 }
       
    86 //////////////////////////////////////////////////////////////////////////////
       
    87 //
       
    88 // Append Method to append the content of a TDesC object to the
       
    89 //  StringBuffer's content
       
    90 // @param TDesC
       
    91 //
       
    92 //////////////////////////////////////////////////////////////////////////////
       
    93 
       
    94 void StringBuffer::Append(const TDesC &what)
       
    95 {
       
    96     TInt _size = what.Length();
       
    97     if ((String->Des().MaxLength() - String->Size()) < _size)
       
    98     {
       
    99         String = String->ReAlloc(String->Des().MaxLength() + 2 * _size);
       
   100     }
       
   101     String->Des().Append(what);
       
   102 }
       
   103 
       
   104 //////////////////////////////////////////////////////////////////////////////
       
   105 //
       
   106 // Append Method to append a TInt object to the StringBuffer's content
       
   107 // @param TInt
       
   108 //
       
   109 //////////////////////////////////////////////////////////////////////////////
       
   110 void StringBuffer::Append(TInt aWhat)
       
   111 {
       
   112     TBufC<KBUFMAX> Temp;
       
   113     Temp.Des().AppendNum(aWhat);
       
   114     TInt _size = Temp.Des().Length();
       
   115     if ((String->Des().MaxLength() - String->Size()) < _size)
       
   116     {
       
   117         String = String->ReAlloc(String->Des().MaxLength() + 2 * _size);
       
   118     }
       
   119     String->Des().Append(Temp);
       
   120 }
       
   121 //////////////////////////////////////////////////////////////////////////////
       
   122 //
       
   123 // CreateJavaString Method to create java string from an HBufC
       
   124 // @param aJNI JNIEnv*
       
   125 // @param aString HBufC
       
   126 // @return jstring
       
   127 //
       
   128 //////////////////////////////////////////////////////////////////////////////
       
   129 
       
   130 jstring StringBuffer::CreateJavaString(JNIEnv* aJNI, const HBufC* aString)
       
   131 {
       
   132     TPtrC16 buf;
       
   133     jstring str = aJNI->NewString(aString->Ptr(), aString->Length());
       
   134     return str;
       
   135 }
       
   136 //////////////////////////////////////////////////////////////////////////////
       
   137 //
       
   138 // CreateHBufCFromJavaStringLC Method to cretae HBufC from a java string
       
   139 // @param JNIEnv*
       
   140 // @param jstring aString
       
   141 // @return HBufC*
       
   142 //
       
   143 //////////////////////////////////////////////////////////////////////////////
       
   144 
       
   145 HBufC* StringBuffer::CreateHBufCFromJavaStringLC(JNIEnv* aJNI, jstring aString)
       
   146 {
       
   147     HBufC16* Str = HBufC16::NewL(aJNI->GetStringLength(aString));
       
   148     CleanupStack::PushL(Str);
       
   149     const TUint16* temp = aJNI->GetStringChars(aString, 0);
       
   150     Str->Des() = temp;
       
   151     aJNI->ReleaseStringChars(aString, temp);
       
   152     return Str;
       
   153 }
       
   154 
       
   155 //////////////////////////////////////////////////////////////////////////////
       
   156 //
       
   157 // ToJavaString Method to create java string from the content of the
       
   158 // StringBuffer.
       
   159 // @param JNIEnv
       
   160 // @return jstring
       
   161 //
       
   162 //////////////////////////////////////////////////////////////////////////////
       
   163 
       
   164 jstring StringBuffer::ToJavaString(JNIEnv* aJNI)
       
   165 {
       
   166     jstring str = aJNI->NewString(String->Ptr(), String->Length());
       
   167     return str;
       
   168 }
       
   169 
       
   170 //////////////////////////////////////////////////////////////////////////////
       
   171 //
       
   172 // Destructor
       
   173 //
       
   174 //////////////////////////////////////////////////////////////////////////////
       
   175 
       
   176 StringBuffer::~StringBuffer()
       
   177 {
       
   178     delete String;
       
   179     String = NULL;
       
   180 }