00001 /* 00002 * ============================================================================== 00003 * Name : example.cpp 00004 * Part of : OpenCStringUtilitiesExeEx 00005 * Interface : 00006 * Description : 00007 * Version : 00008 * 00009 * Copyright (c) 2005-2007 Nokia Corporation. 00010 * This material, including documentation and any related 00011 * computer programs, is protected by copyright controlled by 00012 * Nokia Corporation. 00013 * ============================================================================== 00014 */ 00015 #ifdef __GCCE__ 00016 //#include <staticlibinit_gcce.h> 00017 #endif 00018 00019 #include "example.h" 00020 00021 // Constant strings 00022 _LIT( KSampleWideString, "Sample Wide String" ); 00023 _LIT8( KSampleNarrowString, "Sample Narrow String" ); 00024 00025 void testTbuf16() 00026 { 00027 // Tbuf16 Conversions 00028 TBuf16<SIZE> buf(KSampleWideString); 00029 00030 wchar_t* ws = tbuf16towchar(buf); 00031 00032 printf("After tbuf16towchar.........\n"); 00033 wprintf(L"Result string: %ls\n", ws); 00034 GETCHAR(); 00035 00036 //Should allocate space for the destination 00037 char nar_str[SIZE] = "\0"; 00038 int retVal = tbuf16tochar(buf ,nar_str); 00039 00040 if ( retVal == -1 ) 00041 { 00042 printf(" tbuf16tochar failed..........\n"); 00043 GETCHAR(); 00044 return; 00045 } 00046 00047 printf(" After tbuf16tochar..........\n"); 00048 printf("\nResult string: %s\n", nar_str ); 00049 GETCHAR(); 00050 } 00051 00052 void testTbuf8() 00053 { 00054 // TBuf8 Conversions 00055 TBuf8<SIZE> buf2((TText8*)"Hello World"); 00056 char *ns = (char*)tbuf8tochar(buf2); 00057 00058 printf("After tbuf8tochar.........\n"); 00059 printf("Result string: %s\n", ns); 00060 GETCHAR(); 00061 00062 //Should allocate space for the destination 00063 wchar_t wstr[SIZE]; 00064 TInt retVal = tbuf8towchar(buf2, wstr); 00065 if ( retVal == -1 ) 00066 { 00067 printf("tbuf8towchar failed..........\n"); 00068 GETCHAR(); 00069 return; 00070 } 00071 00072 printf("After tbuf8tochar.........\n"); 00073 wprintf(L"Result string: %ls\n", wstr); 00074 GETCHAR(); 00075 } 00076 00077 void testTbufC8() 00078 { 00079 // TBufC8 Conversions 00080 wchar_t wstr[SIZE]; 00081 TBufC8<SIZE> bufc8((TText8*)"Sample String"); 00082 int retVal = tbufC8towchar(bufc8 ,wstr); 00083 if ( retVal == -1 ) 00084 { 00085 printf("tbufC8towchar failed..........\n"); 00086 GETCHAR(); 00087 return; 00088 } 00089 printf(" After tbufC8towchar.........\n"); 00090 wprintf(L"Result string: %ls\n", wstr); 00091 GETCHAR(); 00092 00093 char str[SIZE]; 00094 tbufC8tochar(bufc8, str); 00095 00096 printf(" After tbufc8tochar.........\n"); 00097 printf("Result string: %s\n", str); 00098 GETCHAR(); 00099 } 00100 00101 void testTbufC16() 00102 { 00103 // TBufC16 Conversions 00104 wchar_t wstr[SIZE]; 00105 TBufC<SIZE> bufc(KSampleWideString); 00106 00107 tbufC16towchar(bufc ,(wchar_t*)wstr); 00108 00109 printf(" After tbufC16towchar.........\n"); 00110 wprintf(L"Result string: %ls\n", wstr); 00111 GETCHAR(); 00112 00113 char str[SIZE]; 00114 int retVal = tbufC16tochar(bufc ,str); 00115 if ( retVal == -1 ) 00116 { 00117 printf("tbufC16tochar failed..........\n"); 00118 GETCHAR(); 00119 return; 00120 } 00121 00122 printf(" After tbufC16tochar.........\n"); 00123 printf("Result string: %s\n", str); 00124 GETCHAR(); 00125 00126 } 00127 00128 void testHbufC16() 00129 { 00130 // HBufC16 conversions 00131 HBufC16 *p = HBufC16::NewLC(SIZE); 00132 TBuf16<SIZE> buf(KSampleWideString); 00133 *p = buf; 00134 00135 wchar_t wptr[SIZE]; 00136 00137 hbufC16towchar(*p, wptr); 00138 00139 printf("After hbufC16towchar.........\n"); 00140 wprintf(L"Result string: %ls\n", wptr); 00141 GETCHAR(); 00142 00143 char char_ptr[SIZE]; 00144 int retVal = hbufC16tochar(*p, char_ptr); 00145 00146 if ( retVal == -1 ) 00147 { 00148 printf("hbufC16tochar failed..........\n"); 00149 GETCHAR(); 00150 return; 00151 } 00152 00153 printf("After hbufC16tochar.........\n"); 00154 printf("Result string: %s\n", char_ptr); 00155 GETCHAR(); 00156 00157 CleanupStack::PopAndDestroy(1); 00158 } 00159 00160 00161 void testHbufC8() 00162 { 00163 //HBufC8 conversions 00164 HBufC8 *p8 = HBufC8::NewLC( 30 ); 00165 TBuf8<SIZE> buf8(KSampleNarrowString); 00166 *p8 = buf8; 00167 00168 wchar_t ptr[SIZE] = L"\0"; 00169 int retVal = hbufC8towchar(*p8, ptr); 00170 00171 if ( retVal == -1 ) 00172 { 00173 printf("hbufC8towchar failed..........\n"); 00174 GETCHAR(); 00175 return; 00176 } 00177 00178 printf("After hbufC8towchar.........\n"); 00179 wprintf(L"Result string: %ls\n", ptr); 00180 GETCHAR(); 00181 00182 char char_ptr[SIZE]= "\0"; 00183 hbufC8tochar(*p8, char_ptr); 00184 00185 printf("After hbufC8tochar.........\n"); 00186 printf("Result string: %s\n", char_ptr); 00187 GETCHAR(); 00188 00189 CleanupStack::PopAndDestroy(1); 00190 } 00191 00192 void testWchar() 00193 { 00194 // Wchar Conversions 00195 const wchar_t *wstr = L"Hi good morning"; 00196 TBuf16<SIZE> buf; 00197 00198 wchartotbuf16(wstr, buf); 00199 00200 00201 // Allocate mem for the Destination 00202 HBufC16 *p = HBufC16::NewLC(SIZE); 00203 TBuf16<SIZE> tbuf (KSampleWideString); 00204 *p = tbuf; 00205 wchartohbufc16(wstr ,*p); 00206 00207 00208 TBuf8<SIZE> buf8(KSampleNarrowString); 00209 int retVal = wchartotbuf8(wstr,buf8); 00210 00211 if ( retVal == -1 ) 00212 { 00213 printf("wchartotbuf8 failed..........\n"); 00214 GETCHAR(); 00215 return; 00216 } 00217 00218 00219 HBufC8 *p_8 = HBufC8::NewLC(30); 00220 TBuf8<SIZE> buf_8(KSampleNarrowString); 00221 *p_8 = buf_8; 00222 int ret = wchartohbufc8(wstr ,*p_8); 00223 if ( retVal == -1 ) 00224 { 00225 printf("wchartohbufc8 failed..........\n"); 00226 GETCHAR(); 00227 return; 00228 } 00229 CleanupStack::PopAndDestroy(2); 00230 00231 } 00232 00233 00234 void testChar() 00235 { 00236 // Char Conversions 00237 const char *str = "Hi_good morning!!"; 00238 TBuf16<SIZE> buf(KSampleWideString); 00239 00240 TInt retVal = chartotbuf16(str, buf); 00241 if ( retVal == -1 ) 00242 { 00243 printf("chartotbuf16 failed..........\n"); 00244 GETCHAR(); 00245 return; 00246 } 00247 00248 00249 // Allocate mem for the Destination 00250 HBufC16 *p = HBufC16::NewLC(SIZE); 00251 TBuf16<SIZE> tbuf(KSampleWideString); 00252 *p = tbuf; 00253 int ret = chartohbufc16(str, *p); 00254 if ( ret == -1 ) 00255 { 00256 printf("chartohbufc16 failed..........\n"); 00257 GETCHAR(); 00258 return; 00259 } 00260 00261 TBuf8<SIZE> buf8(KSampleNarrowString); 00262 chartotbuf8(str,buf8 ); 00263 00264 HBufC8 *p_8 = HBufC8::NewLC(SIZE); 00265 TBuf8<SIZE> buf_8(KSampleNarrowString); 00266 *p_8 = buf_8; 00267 00268 chartohbufc8( str,*p_8); 00269 00270 CleanupStack::PopAndDestroy(2); 00271 } 00272 00273 00274 int main() 00275 { 00276 // Tbuf16 Conversions 00277 testTbuf16(); 00278 // Tbuf8 Conversions 00279 testTbuf8(); 00280 00281 // TbufC8 Conversions 00282 testTbufC8(); 00283 00284 // TBufC16 Conversions 00285 testTbufC16(); 00286 00287 // HBufC16 Conversions 00288 testHbufC16(); 00289 00290 // HBufC8 Conversions 00291 testHbufC8(); 00292 00293 // Wchar conversions 00294 testWchar(); 00295 00296 // Char Conversions 00297 testChar(); 00298 00299 return 0; 00300 }
Copyright ©2010 Nokia Corporation and/or its subsidiary(-ies).
All rights
reserved. Unless otherwise stated, these materials are provided under the terms of the Eclipse Public License
v1.0.