osncore/osncore/src/alfstring.cpp
changeset 17 3eca7e70b1b8
parent 3 4526337fb576
equal deleted inserted replaced
3:4526337fb576 17:3eca7e70b1b8
     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: Osn String Implementation.
       
    15 *
       
    16 */
       
    17 
       
    18 #if defined(__GNUC__)
       
    19 #include <string.h>
       
    20 #include <assert.h>
       
    21 #else
       
    22 #include <e32base.h>
       
    23 #include <libc/string.h>
       
    24 #include <libc/assert.h>
       
    25 #endif
       
    26 
       
    27 #include <stdio.h>
       
    28 #include <stdarg.h>
       
    29 #include "osn/alfstring.h"
       
    30 #include "stringmacros.h"
       
    31 //#include "char.h"
       
    32 #include "alfstringdata.h"
       
    33 //#include "CHARCONV.H"
       
    34 #include <osn/osnnew.h>
       
    35 #include <osn/osnbadalloc.h>
       
    36 #define CHECK_FOR_HANDLE_LEAKS 0
       
    37 
       
    38 using namespace std;
       
    39 namespace osncore
       
    40     {
       
    41 
       
    42 
       
    43 
       
    44 
       
    45 #define IS_ASCII_QCHAR(c) ((c).unicode() > 0 && (c).unicode() <= 0xff)
       
    46 
       
    47 
       
    48 
       
    49 
       
    50 EXPORT_C CAlfString::CAlfString()
       
    51     :internalData(new(S60)SAlfStringData())
       
    52     {
       
    53     }
       
    54 
       
    55 
       
    56 
       
    57 EXPORT_C CAlfString::~CAlfString()
       
    58     {
       
    59 
       
    60     if(internalData.get())
       
    61         {
       
    62         internalData->deref();
       
    63         if(internalData->refCount != 0)
       
    64             {
       
    65             internalData.release(); // don't kill it, we have ref
       
    66             }
       
    67         }
       
    68     }
       
    69 
       
    70 
       
    71 
       
    72 
       
    73 EXPORT_C CAlfString::CAlfString(const char *chs)
       
    74     :internalData(new(S60)SAlfStringData())
       
    75     {
       
    76     internalData->initialize(chs, strlen(chs));
       
    77 
       
    78     }
       
    79 
       
    80 
       
    81 
       
    82 EXPORT_C CAlfString::CAlfString(const CAlfString &qs)
       
    83     :internalData(qs.internalData.get())
       
    84     {
       
    85     internalData->ref();  
       
    86     }
       
    87 
       
    88 EXPORT_C CAlfString &CAlfString::operator=(const CAlfString &qs)
       
    89     {
       
    90     if (this == &qs)
       
    91         return *this;
       
    92     qs.internalData->ref(); // increase source ref count
       
    93     internalData->deref();
       
    94     if(internalData->refCount != 0)
       
    95         {
       
    96         internalData.release();
       
    97         }
       
    98         
       
    99     internalData.reset(qs.internalData.get()); 
       
   100     return *this;
       
   101     }
       
   102 
       
   103 EXPORT_C  const char* CAlfString::latin1() const
       
   104 {
       
   105 	return internalData->ascii();
       
   106 }
       
   107 
       
   108 
       
   109 
       
   110 EXPORT_C CAlfString::CAlfString(const char *chs, int len)
       
   111     : internalData(new(S60)SAlfStringData())
       
   112     {
       
   113 
       
   114     internalData->initialize(chs, len);
       
   115  
       
   116     }
       
   117 
       
   118 EXPORT_C int CAlfString::compare(const CAlfString& s) const
       
   119 {
       
   120     //if (internalData->_isAsciiValid && s.internalData->_isAsciiValid)
       
   121         return strcmp(latin1(), s.latin1());
       
   122     //return ucstrcmp(*this,s);
       
   123     // return -1;
       
   124 }
       
   125 
       
   126 
       
   127 EXPORT_C CAlfString& CAlfString::append(const CAlfString &s)
       
   128     {
       
   129     return insert(internalData->_length, s);
       
   130     }
       
   131 
       
   132 
       
   133 EXPORT_C CAlfString &CAlfString::insert(uint index, const char *insertChars, uint insertLength)
       
   134 {
       
   135     if (insertLength == 0)
       
   136         return *this;
       
   137 
       
   138     //detach();
       
   139 
       
   140     // if (internalData->_isAsciiValid){
       
   141         uint originalLength = internalData->_length;
       
   142         char *targetChars;
       
   143 
       
   144         // Ensure that we have enough space.
       
   145         if( !setLength (originalLength + insertLength) ) 
       
   146             {
       
   147             throw std::bad_alloc();
       
   148             //return *this;    
       
   149             }
       
   150         
       
   151         targetChars = (char *)internalData->ascii();
       
   152 
       
   153         // Move tail to make space for inserted characters.
       
   154         memmove (targetChars+index+insertLength, targetChars+index, originalLength-index);
       
   155 
       
   156         // Insert characters.
       
   157         memcpy (targetChars+index, insertChars, insertLength);
       
   158 
       
   159         internalData->_isUnicodeValid = 0;
       
   160     //}
       
   161     /*    
       
   162     else if (dataHandle[0]->_isUnicodeValid){
       
   163         uint originalLength = dataHandle[0]->_length;
       
   164         QChar *targetChars;
       
   165 
       
   166         // Ensure that we have enough space.
       
   167         if( !setLength (originalLength + insertLength) ) return *this;
       
   168         targetChars = (QChar *)unicode();
       
   169 
       
   170         // Move tail to make space for inserted characters.
       
   171         memmove (targetChars+(index+insertLength), targetChars+index, (originalLength-index)*sizeof(QChar));
       
   172 
       
   173         // Insert characters.
       
   174         uint i = insertLength;
       
   175         QChar *target = targetChars+index;
       
   176 
       
   177         while (i--)
       
   178             *target++ = *insertChars++;
       
   179         }
       
   180     else
       
   181         FATAL("invalid character cache",0);
       
   182         */
       
   183 
       
   184     return *this;
       
   185 }
       
   186 
       
   187 
       
   188 
       
   189 EXPORT_C CAlfString& CAlfString::insert(uint index, const CAlfString &s)
       
   190 {
       
   191     if (s.internalData->_length == 0)
       
   192         return *this;
       
   193 
       
   194 #ifdef QSTRING_DEBUG_UNICODE
       
   195     //forceUnicode();
       
   196 #endif
       
   197 
       
   198     // if (internalData->_isAsciiValid) //&& s.isAllLatin1()
       
   199         // {
       
   200         insert(index, s.latin1(), s.internalData->_length);
       
   201         // }
       
   202     /*
       
   203     else 
       
   204         {
       
   205         uint insertLength = qs.dataHandle[0]->_length;
       
   206         uint originalLength = dataHandle[0]->_length;
       
   207         AlfChar *targetChars;
       
   208 
       
   209         // Ensure that we have enough space.
       
   210         if( !setLength (originalLength + insertLength) ) return *this;
       
   211         targetChars = forceUnicode();
       
   212 
       
   213         // Move tail to make space for inserted characters.
       
   214         memmove (targetChars+(index+insertLength), targetChars+index, (originalLength-index)*sizeof(QChar));
       
   215 
       
   216         // Insert characters.
       
   217         if (qs.dataHandle[0]->_isAsciiValid){
       
   218             uint i = insertLength;
       
   219             AlfChar *target = targetChars+index;
       
   220             char *a = (char *)qs.ascii();
       
   221 
       
   222             while (i--)
       
   223                 *target++ = *a++;
       
   224         } 
       
   225         else {
       
   226             AlfChar *insertChars = (AlfChar *)qs.unicode();
       
   227             memcpy (targetChars+index, insertChars, insertLength*sizeof(QChar));
       
   228         }
       
   229 
       
   230         internalData->_isAsciiValid = 0;
       
   231         }
       
   232     */
       
   233 
       
   234     return *this;
       
   235 }
       
   236 
       
   237 
       
   238 // Increase buffer size if necessary.  Newly allocated
       
   239 // bytes will contain garbage.
       
   240 bool CAlfString::setLength(uint newLen)
       
   241 {
       
   242 
       
   243     if (newLen == 0) {
       
   244         //setUnicode(0, 0);
       
   245         return true;
       
   246     }
       
   247 
       
   248     // Missing optimization: Could avoid copying characters we are going to remove
       
   249     // by making a special version of detach().
       
   250 //    detach();
       
   251 
       
   252 //    ASSERT(dataHandle != shared_null_handle);
       
   253 
       
   254 /*
       
   255 #ifdef QSTRING_DEBUG_UNICODE
       
   256     forceUnicode();
       
   257 #endif
       
   258 */
       
   259 
       
   260     // if (internalData->_isAsciiValid){
       
   261         if (newLen+1 > internalData->_maxAscii) {  
       
   262             if( internalData->increaseAsciiSize(newLen+1) == false ) return false;
       
   263         }
       
   264         // Ensure null termination, although newly allocated
       
   265         // bytes contain garbage.
       
   266         internalData->_ascii[newLen] = 0;
       
   267     // }
       
   268     /*else if (internalData->_isUnicodeValid){
       
   269         if (newLen > internalData->_maxUnicode) {
       
   270             if( internalData->increaseUnicodeSize(newLen) == false ) return false;
       
   271         }
       
   272     }*/
       
   273 /*    
       
   274     else
       
   275         FATAL("invalid character cache",0);
       
   276 */        
       
   277 
       
   278     internalData->_length = newLen;
       
   279     return true;
       
   280 }
       
   281 
       
   282 
       
   283 
       
   284 EXPORT_C bool CAlfString::operator==(const CAlfString &s1)const
       
   285     {
       
   286     // if (internalData->_isAsciiValid && s1.internalData->_isAsciiValid) 
       
   287     //     {
       
   288         return strcmp(latin1(), s1.latin1()) == 0;
       
   289     //    }
       
   290     // return false;    
       
   291     /*    
       
   292     return s1.dataHandle[0]->_length == s2.dataHandle[0]->_length
       
   293         && memcmp(s1.unicode(), s2.unicode(), s1.dataHandle[0]->_length * sizeof(QChar)) == 0;
       
   294         */
       
   295     }
       
   296 
       
   297 
       
   298 
       
   299 }