UTF8_Encoding_Converter.cpp

Go to the documentation of this file.
00001 // UTF8_Encoding_Converter.cpp,v 4.1 2006/01/09 15:18:53 elliott_c Exp
00002 
00003 #include "ace/UTF8_Encoding_Converter.h"
00004 
00005 #if defined (ACE_USES_WCHAR)
00006 #include "ace/UTF16_Encoding_Converter.h"
00007 #include "ace/UTF32_Encoding_Converter.h"
00008 #include "ace/OS_NS_string.h"
00009 #include "ace/OS_Memory.h"
00010 
00011 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
00012 
00013 ACE_UTF8_Encoding_Converter::ACE_UTF8_Encoding_Converter (void)
00014  : native_ (0)
00015 {
00016   // Choose a converter for the ASCII or UTF-8 string to a wide character
00017   // string which we will use in from_utf8.  We have to make an
00018   // assumption here about the encoding based on the size of ACE_TCHAR.
00019   switch (sizeof (ACE_TCHAR))
00020     {
00021     case 4:
00022       ACE_NEW(this->native_, ACE_UTF32_Encoding_Converter);
00023       break;
00024     case 2:
00025       ACE_NEW(this->native_, ACE_UTF16_Encoding_Converter);
00026       break;
00027     }
00028 }
00029 
00030 ACE_UTF8_Encoding_Converter::~ACE_UTF8_Encoding_Converter (void)
00031 {
00032   delete native_;
00033 }
00034 
00035 ACE_UTF8_Encoding_Converter::Result
00036 ACE_UTF8_Encoding_Converter::to_utf8 (const void* source,
00037                                       size_t source_size,
00038                                       ACE_Byte* target,
00039                                       size_t target_size,
00040                                       bool /*strict*/)
00041 {
00042   if (target_size >= source_size)
00043     {
00044       ACE_OS::memcpy (target, source, source_size);
00045       return CONVERSION_OK;
00046     }
00047 
00048   return TARGET_EXHAUSTED;
00049 }
00050 
00051 ACE_UTF8_Encoding_Converter::Result
00052 ACE_UTF8_Encoding_Converter::from_utf8 (const ACE_Byte* source,
00053                                         size_t source_size,
00054                                         void* target,
00055                                         size_t target_size,
00056                                         bool strict)
00057 {
00058   if (this->native_ != 0)
00059     {
00060       return this->native_->from_utf8(source, source_size,
00061                                       target, target_size, strict);
00062     }
00063 
00064   ACE_TCHAR* targetStart = static_cast<ACE_TCHAR*> (target);
00065   ACE_OS::strncpy (targetStart,
00066                    ACE_TEXT_CHAR_TO_TCHAR (
00067                      reinterpret_cast<const char*> (source)),
00068                    source_size);
00069   targetStart[source_size] = 0;
00070   return CONVERSION_OK;
00071 }
00072 
00073 ACE_UTF8_Encoding_Converter*
00074 ACE_UTF8_Encoding_Converter::encoded (const ACE_Byte* source,
00075                                       size_t source_size)
00076 {
00077   for(size_t i = 0; i < source_size; i++)
00078     {
00079       if (source[i] < 0x01 || source[i] > 0x7f)
00080         return 0;
00081     }
00082 
00083   // All characters are "valid" ASCII
00084   ACE_UTF8_Encoding_Converter* converter = 0;
00085   ACE_NEW_RETURN (converter,
00086                   ACE_UTF8_Encoding_Converter,
00087                   0);
00088   return converter;
00089 }
00090 
00091 ACE_END_VERSIONED_NAMESPACE_DECL
00092 #endif /* ACE_USES_WCHAR */

Generated on Thu Nov 9 09:42:09 2006 for ACE by doxygen 1.3.6