imgtools/imglib/host/utf16string.cpp
author jjkang
Fri, 25 Jun 2010 18:11:34 +0800
changeset 600 6d08f4a05d93
child 606 30b30f9da0b7
permissions -rw-r--r--
add deprecated files
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
600
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
     1
/*
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
     2
* Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
     3
* All rights reserved.
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
     4
* This component and the accompanying materials are made available
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
     5
* under the terms of the License "Eclipse Public License v1.0"
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
     6
* which accompanies this distribution, and is available
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
     7
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
     8
*
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
     9
* Initial Contributors:
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    10
* Nokia Corporation - initial contribution.
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    11
*
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    12
* Contributors:
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    13
*
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    14
* Description: 
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    15
* @internalComponent * @released
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    16
*
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    17
*/
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    18
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    19
#include "utf16string.h"
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    20
#ifdef __LINUX__
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    21
#include <iconv.h>
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    22
#elif defined(WIN32)
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    23
#ifdef _STLP_INTERNAL_WINDOWS_H
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    24
#define __INTERLOCKED_DECLARED
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    25
#endif
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    26
#include <windows.h>
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    27
#endif
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    28
UTF16String::UTF16String() :iData(0), iLength(0){
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    29
}
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    30
UTF16String::UTF16String(const UTF16String& aRight){
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    31
	iLength = aRight.iLength ;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    32
	iData = new TUint16[iLength + 1];
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    33
	memcpy(iData,aRight.iData, iLength << 1);
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    34
	iData[iLength] = 0;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    35
}
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    36
UTF16String::UTF16String(const string& aUtf8Str){
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    37
	iData = 0 ;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    38
	iLength = 0 ;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    39
	Assign(aUtf8Str.c_str(),aUtf8Str.length());
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    40
}
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    41
UTF16String::UTF16String(const TUint16* aUtf16Str,TInt aLength /* = -1*/){
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    42
	
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    43
	if(aLength < 0){
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    44
		const TUint16* p = aUtf16Str ;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    45
		while(*p){
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    46
			p++ ;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    47
			aLength ++ ;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    48
		}
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    49
	}
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    50
	if(aLength > 0){		
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    51
		iLength = aLength ;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    52
		aLength <<= 1 ;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    53
		iData = new TUint16[iLength + 1] ;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    54
		memcpy(iData,aUtf16Str,aLength);
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    55
		iData[iLength] = 0;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    56
	}else{
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    57
		iData = 0 ;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    58
		iLength = 0 ;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    59
	}
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    60
	
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    61
}
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    62
UTF16String::UTF16String(const char* aUtf8Str,TInt aLength /*= -1 */){
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    63
	iData = 0 ;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    64
	iLength = 0 ;	
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    65
	Assign(aUtf8Str,aLength);
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    66
}	
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    67
UTF16String::~UTF16String(){
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    68
	if(iData)
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    69
		delete []iData ;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    70
}	
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    71
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    72
UTF16String& UTF16String::operator = (const UTF16String& aRight){
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    73
	if(&aRight != this){
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    74
		if(iData) 
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    75
			delete []iData ; 
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    76
		iLength = aRight.iLength ;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    77
		iData = new TUint16[iLength + 1];
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    78
		memcpy(iData,aRight.iData, iLength << 1);
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    79
		iData[iLength] = 0;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    80
	}
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    81
	return *this;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    82
}
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    83
bool UTF16String::FromFile(const char* aFileName){
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    84
	if(!aFileName || !(*aFileName))
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    85
		return false ;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    86
	ifstream ifs(aFileName,ios_base::in + ios_base::binary); 	
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    87
	if(!ifs.is_open())
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    88
		return false ;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    89
	
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    90
	ifs.seekg(0,ios_base::end);
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    91
	size_t length = ifs.tellg();
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    92
	if((length % 2) == 1 ){
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    93
		ifs.close() ;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    94
		return false ;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    95
	}
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    96
	ifs.seekg(0,ios_base::beg);
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    97
	TUint16 hdr ;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    98
	size_t readLen = length - sizeof(hdr) ;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
    99
	length >>= 1 ;	
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   100
	TUint16 *newData = new TUint16[length + 1];
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   101
	ifs.read(reinterpret_cast<char*>(&hdr),sizeof(hdr));		
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   102
	if(hdr == 0xFEFF){ 
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   103
		ifs.read(reinterpret_cast<char*>(newData),readLen);
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   104
		length -- ;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   105
	}
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   106
	else{		 
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   107
		*newData = hdr ;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   108
		ifs.read(reinterpret_cast<char*>(&newData[1]),readLen);
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   109
	} 
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   110
	ifs.close();
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   111
	iLength = length ;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   112
	if(iData)
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   113
		delete []iData ;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   114
	iData = newData ;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   115
	iData[iLength] = 0;	
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   116
	return true ;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   117
}
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   118
/**
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   119
* aResult will not changed on error
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   120
*/
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   121
bool UTF16String::ToUTF8(string& aResult) const {
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   122
	if(IsEmpty()){
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   123
		aResult = "";
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   124
		return true;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   125
	}
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   126
	size_t bufLen = (iLength + 1) << 2 ;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   127
	char* buffer = new char[bufLen] ;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   128
#ifdef WIN32
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   129
	int r = WideCharToMultiByte(CP_UTF8,0,reinterpret_cast<WCHAR*>(iData),iLength,buffer,bufLen,NULL,NULL);
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   130
	if(r < 0){
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   131
		delete []buffer ;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   132
		return false ;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   133
	}
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   134
	buffer[r] = 0;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   135
	aResult.assign(buffer,r);
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   136
#else
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   137
	iconv_t it = iconv_open("UTF-8","UTF-16");
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   138
	if((iconv_t)(-1) == it){ 
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   139
		return  false;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   140
	}
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   141
	char* bufferEnd = buffer ;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   142
	char* in_ptr = reinterpret_cast<char*>(iData);
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   143
	size_t in_len = iLength << 1 ;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   144
	iconv(it,&in_ptr,&in_len ,&bufferEnd,&bufLen);
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   145
	iconv_close(it);
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   146
	*bufferEnd = 0 ;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   147
	size_t length = bufferEnd - buffer ;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   148
	aResult.assign(buffer,length);
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   149
#endif
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   150
	delete []buffer ;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   151
	return true ;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   152
}
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   153
bool UTF16String::Assign(const char* aUtf8Str,TInt aLength /* = -1*/) {
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   154
	if(0 == aUtf8Str) return false ;	
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   155
	if(aLength < 0) aLength = strlen(aUtf8Str); 
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   156
		
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   157
#ifdef WIN32
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   158
	size_t newLength = aLength + 1;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   159
	TUint16* newData = new TUint16[newLength];
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   160
	int r = MultiByteToWideChar(CP_UTF8,0,aUtf8Str,aLength,reinterpret_cast<WCHAR*>(newData),newLength);
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   161
	if(r < 0){
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   162
		delete []newData ;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   163
		return false ;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   164
	}
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   165
	iLength = r ;	
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   166
#else
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   167
	char* forFree = 0 ;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   168
	if(aUtf8Str[aLength - 1] != 0){
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   169
		forFree = new char[aLength + 1];
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   170
		memcpy(forFree,aUtf8Str,aLength );
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   171
		forFree[aLength] = 0;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   172
		aUtf8Str = forFree ;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   173
	}
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   174
	iconv_t it = iconv_open("UTF-16","UTF-8");
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   175
	if((iconv_t)(-1) == it){ 
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   176
		 
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   177
		return  false;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   178
	}
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   179
	size_t newLength = aLength + 2;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   180
	TUint16* newData = new TUint16[newLength];
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   181
	newLength <<= 1;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   182
	char* out_ptr = reinterpret_cast<char*>(newData);
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   183
	size_t in_len = aLength ;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   184
	char* in_ptr = const_cast<char*>(aUtf8Str);
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   185
	iconv(it,&in_ptr,&in_len ,&out_ptr ,&newLength);
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   186
	newLength = out_ptr - reinterpret_cast<char*>(newData);
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   187
	iconv_close(it);
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   188
	if(newLength % 2 == 1){ //should not be possible
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   189
		delete []newData;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   190
		return false ;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   191
	}
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   192
	iLength = (newLength >> 1) ;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   193
	if(forFree)
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   194
		delete []forFree;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   195
#endif
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   196
	newData[iLength] = 0 ;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   197
	if(iData){
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   198
		delete []iData ;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   199
	}
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   200
	iData = newData ;	
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   201
	if(*iData == 0xFEFF)
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   202
		iLength -- ;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   203
	
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   204
	return true ;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   205
}
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   206
static const TUint16 NullUInt16Str[1] = {0};
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   207
const TUint16* UTF16String::c_str() const {
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   208
	if(0 == iData)
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   209
		return NullUInt16Str;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   210
	else if(0xFEFF != *iData)
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   211
		return iData ;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   212
	else
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   213
		return iData + 1;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   214
}
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   215
///
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   216
/// aUtf16Str must end with '\0'
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   217
/// 
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   218
int UTF16String::Compare(const TUint16* aUtf16Str) const {
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   219
	if(!aUtf16Str || !(*aUtf16Str))
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   220
		return (iLength > 0) ? 1 : 0; 
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   221
	size_t i ;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   222
	for(i = 0 ; aUtf16Str[i] != 0 ; i++) { 
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   223
		if( iData[i] > aUtf16Str[i])
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   224
			return 1;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   225
		else if(iData[i] < aUtf16Str[i])
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   226
			return -1 ;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   227
	}
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   228
	return (i < iLength) ? 1 : 0 ; 
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   229
}
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   230
///
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   231
/// aUtf16Str must end with '\0'
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   232
/// 
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   233
int UTF16String::CompareNoCase(const TUint16* aUtf16Str) const {
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   234
	if(!aUtf16Str || !(*aUtf16Str))
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   235
		return (iLength > 0) ? 1 : 0; 
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   236
	size_t i ;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   237
	TUint16 a, b;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   238
	for(i = 0 ; aUtf16Str[i] != 0 ; i++) { 
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   239
		a = iData[i];
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   240
		b = aUtf16Str[i] ;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   241
		if( a >= 'A' && a <= 'Z') a |= 0x20 ;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   242
		if( b >= 'A' && b <= 'Z') b |= 0x20 ;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   243
			
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   244
		if( a > b )
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   245
			return 1;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   246
		else if( a < b )
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   247
			return -1 ;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   248
	}
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   249
	return (i < iLength) ? 1 : 0 ; 
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   250
}
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   251
TUint16* UTF16String::Alloc(size_t aNewLen) {
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   252
	TUint16* newData = new TUint16[aNewLen + 1] ;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   253
	if(!newData) return 0;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   254
	if(iData) delete []iData ;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   255
	
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   256
	iLength = aNewLen ;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   257
	iData = newData ;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   258
	*iData = 0 ;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   259
	iData[aNewLen] = 0;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   260
	return iData;
6d08f4a05d93 add deprecated files
jjkang
parents:
diff changeset
   261
}