examples/PIPS/OpenCStringUtilitiesEx/library/src/stringutils.cpp

00001 /*
00002 Copyright (c) 2005-2010 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
00003 
00004 Redistribution and use in source and binary forms, with or without
00005 modification, are permitted provided that the following conditions are met:
00006 
00007 * Redistributions of source code must retain the above copyright notice, this
00008   list of conditions and the following disclaimer.
00009 * Redistributions in binary form must reproduce the above copyright notice,
00010   this list of conditions and the following disclaimer in the documentation
00011   and/or other materials provided with the distribution.
00012 * Neither the name of Nokia Corporation nor the names of its contributors
00013   may be used to endorse or promote products derived from this software
00014   without specific prior written permission.
00015 
00016 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00017 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00018 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00019 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00020 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00021 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00022 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00023 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00024 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00025 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00026 
00027 Description: 
00028 */
00029 
00030 // GCCE header
00031 //#ifdef __GCCE__
00032 //#include <staticlibinit_gcce.h>
00033 //#endif //__GCCE__
00034 
00035 //System headers
00036 #include <string.h>
00037 #include <stdlib.h>
00038 
00039 //User-definied headers
00040 #include "stringutils.h"
00041 
00042 
00043 EXPORT_C wchar_t* tbuf16towchar(TDes& aArg)
00044 {
00045         return (wchar_t*)aArg.PtrZ();
00046 }
00047         
00048 EXPORT_C char* tbuf8tochar(TDes8& aArg)
00049 {
00050         return (char*)aArg.PtrZ();
00051 }
00052 
00053 EXPORT_C int tbuf16tochar(TDes& aSrc, char* aDes)
00054 {
00055         const TUint16 *wideString = aSrc.PtrZ();
00056         
00057         TInt ret = wcstombs(aDes, (const wchar_t*)wideString, KMaxFileName );
00058         
00059         return ret;
00060 }
00061 
00062 EXPORT_C int tbuf8towchar(TDes8& aSrc  ,wchar_t* aDes)
00063 {
00064         const char *charString = (const char*)aSrc.PtrZ();
00065         
00066         TInt ret = mbstowcs(aDes, charString, KMaxFileName );
00067         
00068         return ret;
00069 }
00070         
00071         
00072 EXPORT_C void tbufC16towchar(TDesC& aSrc ,wchar_t *aDes)
00073 {
00074         wchar_t *temp = (wchar_t*)aSrc.Ptr();
00075         const TInt size = aSrc.Size();
00076         *(temp + size/2 ) = L'\0';
00077         wcscpy(aDes, temp);
00078 }
00079 
00080 EXPORT_C void tbufC8tochar(TDesC8& aSrc, char* aDes)
00081 {
00082         char *temp = (char*)aSrc.Ptr();
00083         const TInt size = aSrc.Length();
00084         *(temp + size) = '\0';
00085         strcpy(aDes, temp);
00086 }
00087         
00088 EXPORT_C int tbufC16tochar(TDesC& aSrc, char* aDes)
00089 {
00090         TUint16* wideString = (TUint16*)aSrc.Ptr();
00091         const TInt size = aSrc.Length();
00092         *(wideString + size) = L'\0';
00093         
00094         
00095         TInt ret = wcstombs(aDes, (const wchar_t*)wideString, size*2 );
00096         return ret;     
00097 }
00098 
00099 
00100 EXPORT_C int tbufC8towchar(TDesC8& aSrc, wchar_t* aDes)
00101 {
00102         TUint8* charString = (TUint8*)aSrc.Ptr();
00103         const TInt size = aSrc.Length();
00104         *(charString + size) = '\0';
00105         
00106         TInt ret = mbstowcs(aDes, (const char*)charString, KMaxFileName );
00107         return ret;
00108 }
00109 
00110 EXPORT_C void wchartotbuf16(const wchar_t *aSrc, TDes16 &aDes)
00111 {
00112         aDes = (const TUint16*)aSrc;
00113 }
00114 
00115 EXPORT_C  int chartotbuf16(const char *aSrc, TDes16 &aDes)
00116 {
00117         int len = strlen(aSrc);
00118         wchar_t *buf = new wchar_t[len];
00119         
00120         TInt ret = mbstowcs(buf, (const char*)aSrc, len + 1 );
00121         
00122         if( ret != -1)
00123                 aDes = (const TUint16*)buf;
00124         
00125         delete buf;
00126         return ret;
00127 }
00128 
00129 EXPORT_C int wchartotbuf8(const wchar_t *aSrc, TDes8 &aDes)
00130 {
00131         int len = wcslen(aSrc);
00132         char *buf = new char[len];
00133         
00134         TInt ret = wcstombs(buf, (const wchar_t*)aSrc, len + 1);
00135         
00136         if( ret != -1)
00137                 aDes = (const TUint8*)buf;
00138         
00139         delete buf;
00140         return ret;
00141 }
00142         
00143 EXPORT_C  void chartotbuf8 (const char *aSrc, TDes8 &aDes)
00144 {
00145         aDes = (const TUint8*)aSrc;
00146 }
00147 
00148 EXPORT_C  void wchartohbufc16 (const wchar_t* aSrc ,HBufC16& aDes )
00149 {
00150         aDes = (const TUint16*)aSrc;            
00151 }
00152         
00153 EXPORT_C int chartohbufc16(const char* aSrc, HBufC16& aDes)
00154 {       
00155         int len = strlen(aSrc);
00156         wchar_t *buf = new wchar_t[len];
00157         
00158         TInt ret = mbstowcs(buf, (const char*)aSrc, len + 1);
00159         
00160         if( ret != -1)
00161                 aDes = (const TUint16*)buf;
00162         
00163         delete buf;
00164         return ret;
00165 }
00166         
00167 EXPORT_C void chartohbufc8(const char* aSrc, HBufC8& aDes)
00168 {
00169         aDes = (const TUint8*)aSrc;
00170 }
00171         
00172 EXPORT_C int wchartohbufc8(const wchar_t* aSrc, HBufC8& aDes)
00173 {
00174         int len = wcslen(aSrc);
00175         char *buf = new char[len];
00176         
00177         TInt ret = wcstombs(buf, aSrc, len + 1 );
00178         
00179         if( ret != -1)
00180                 aDes = (const TUint8*)buf;
00181         
00182         delete buf;
00183         return ret;
00184 }
00185 
00186 // End of file

Generated by  doxygen 1.6.2