utils_cl2_cxx.cpp

Go to the documentation of this file.
00001 #include "utils_cl2.h"
00002 
00003 #ifdef __EPOC32__
00004 #ifdef __cplusplus
00005 
00006 /*
00007  !concept {:name => "Double byte string / UTF-8 conversion utilities"}
00008 */
00009 
00010 #include <utf.h> // CnvUtfConverter
00011 
00012 // The caller must free the returned buffer. The buffer is guaranteed
00013 // to have a zero terminator.
00014 HBufC8* ConvToUtf8ZL(const TDesC& name16)
00015 {
00016   // Note that there is no non-leaving variant of this function, such
00017   // that the result has no maximum size.
00018   // 
00019   // Do not know what happens if any 0 values appear in "name16"; this
00020   // is not documented.
00021   HBufC8* name8 = CnvUtfConverter::ConvertFromUnicodeToUtf8L(name16);
00022 
00023   // Ensure there is a zero terminator. Quite a lot of work to do this
00024   // efficiently.
00025   TPtr8 ptr = name8->Des();
00026   if (ptr.Length() < ptr.MaxLength()) {
00027     ptr.PtrZ(); // modifies HBufC8 content
00028   } else {
00029     HBufC8* bigger8 = name8->ReAlloc(ptr.Length() + 1);
00030     if (bigger8) {
00031       name8 = bigger8; // name8 already deleted by ReAlloc
00032       name8->Des().PtrZ(); // modifies HBufC8 content
00033     } else {
00034       delete name8;
00035       User::Leave(KErrNoMemory);
00036     }
00037   }
00038 
00039   return name8;
00040 }
00041 
00042 // The caller must free the returned buffer. The buffer is guaranteed
00043 // to have a zero terminator. Returns NULL in an out-of-memory
00044 // situation.
00045 HBufC8* ConvToUtf8(const TDesC& name16)
00046 {
00047   HBufC8* res = NULL;
00048   TRAPD(errCode, res = ConvToUtf8ZL(name16));
00049   return res;
00050 }
00051 
00052 // We really just have this here for the convenience of not having to
00053 // include yet another header file when doing charset conversions. Yet
00054 // one case where we sacrifice performance for implementation
00055 // convenience. [language problem]
00056 HBufC* ConvFromUtf8L(const TDesC8& name8)
00057 {
00058   HBufC16* name16 = CnvUtfConverter::ConvertToUnicodeFromUtf8L(name8);
00059   return name16;
00060 }
00061 
00062 HBufC* ConvFromUtf8CStringL(const char* s)
00063 {
00064   TPtrC8 ptr((const TUint8*)s);
00065   return ConvFromUtf8L(ptr);
00066 }
00067 
00068 // The caller must free the return value with g_free.
00069 gchar* ConvToUtf8CStringL(const TDesC& name16)
00070 {
00071   HBufC8* des = ConvToUtf8ZL(name16);
00072   int strLen = des->Length() + 1;
00073   char* str = (char*)(g_try_malloc(strLen));
00074   if (!str) {
00075     delete des;
00076     User::LeaveNoMemory();
00077   }
00078   memcpy(str, des->Ptr(), strLen);
00079   delete des;
00080   return str;
00081 }
00082 
00083 // Returns NULL if fails to allocate.  
00084 gchar* ConvToUtf8CString(const TDesC& name16)
00085 {
00086   gchar* value = NULL;
00087   TRAPD(errCode, value = ConvToUtf8CStringL(name16));
00088   return value;
00089 }
00090 
00091 // Returns a Symbian error code, or number of unconverted characters
00092 // if the provided buffer is too small. The buffer will be set to the
00093 // empty string if the return value is negative.
00094 int ConvToUtf8CString(gchar* buf, int bufLen, const TDesC& name16)
00095 {
00096   TPtr8 text((TUint8*)buf, bufLen - 1);
00097   TInt errCode = CnvUtfConverter::ConvertFromUnicodeToUtf8(text, name16);
00098   if (errCode < 0) {
00099     *(buf) = '\0';
00100     return errCode;
00101   }
00102   *(buf + text.Length()) = '\0';
00103   return errCode;
00104 }
00105 
00106 #endif // __cplusplus
00107 #endif // __EPOC32__
00108 
00109 #if PRINTF_DOUBLE_BUGGY
00110 // This is only an approximation. The semantics are not exactly the
00111 // same, and hence we are using a different name.
00112 extern "C" gint g_snprintf_fix(gchar *string,
00113              gulong n,
00114              gchar const *format,
00115              ...)
00116 {
00117   VA_LIST argp;
00118   VA_START(argp, format);
00119 
00120   TPtr8 buf((TUint8*)string, n - 1); // length set to 0
00121   TPtrC8 fmt((TUint8*)format); // like strlen for length
00122   buf.FormatList(fmt, argp);
00123   buf.PtrZ();
00124   
00125   return buf.Length();
00126 }
00127 #endif // PRINTF_DOUBLE_BUGGY
00128 
00129 #if PRINTF_DOUBLE_BUGGY
00130 // This is only an approximation. The semantics are not exactly the
00131 // same, and hence we are using a different name.
00132 //
00133 // Note that this implementation is not general purpose, since the
00134 // size of the internal buffer is limited.
00135 extern "C" void g_string_append_printf_fix(GString *gs,
00136              const gchar *format,
00137              ...)
00138 {
00139   static const int bufSize = 100;
00140   TBuf8<bufSize> buf;
00141 
00142   VA_LIST argp;
00143   VA_START(argp, format);
00144 
00145   TPtrC8 fmt((TUint8*)format); // like strlen for length
00146   buf.FormatList(fmt, argp);
00147 
00148   // may throw a GLib OOM error on Symbian
00149   g_string_append(gs, (gchar*)buf.PtrZ());
00150 
00151 #if 0
00152   char buf[bufSize];
00153 
00154   va_list argp;
00155   va_start(argp, fmt);
00156   // vsnprintf would be safer but itself buggy.
00157   int ret = vsprintf(buf, fmt, argp);
00158   va_end(argp); // any cleanup
00159 
00160   if (ret >= 0 && ret < bufSize) {
00161     // No error, and everything did fit.
00162     g_string_append(gs, buf);
00163   } else {
00164     assert(ret < bufSize && "likely memory corruption");
00165   }
00166 #endif
00167 }
00168 #endif
00169 
00170 /**
00171 
00172 utils_cl2_cxx.cpp
00173 
00174 Copyright 2009 Helsinki Institute for Information Technology (HIIT)
00175 and the authors. All rights reserved.
00176 
00177 Authors: Tero Hasu <tero.hasu@hut.fi>
00178 
00179 Permission is hereby granted, free of charge, to any person
00180 obtaining a copy of this software and associated documentation files
00181 (the "Software"), to deal in the Software without restriction,
00182 including without limitation the rights to use, copy, modify, merge,
00183 publish, distribute, sublicense, and/or sell copies of the Software,
00184 and to permit persons to whom the Software is furnished to do so,
00185 subject to the following conditions:
00186 
00187 The above copyright notice and this permission notice shall be
00188 included in all copies or substantial portions of the Software.
00189 
00190 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00191 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00192 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00193 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
00194 BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
00195 ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
00196 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
00197 SOFTWARE.
00198 
00199  **/

ContextLogger2—ContextLogger2 Logger Daemon Internals—Generated on Mon May 2 13:49:57 2011 by Doxygen 1.6.1