Home > Products > SophiaFramework UNIVERSE > Tutorial > RSS Reader > - 7 / 9 -

BREW RSS Reader - 7 / 9 -

Handle Japanese Characters

When the character encoding of XML documents is UTF-8, it is necessary to convert UTF-8 characters to Shift_JIS by using KDDI extention functions for displaying characters on mobile phones used in Japan.

In order to use C++ wrappers for KDDI extention functions, SophiaFramework.hpp should be included after TARGET_EXTENSION_KDDI macro is defined.

#ifndef TARGET_EXTENSION_KDDI
#define TARGET_EXTENSION_KDDI
#endif
#include <SophiaFramework.hpp>

Unicode16ToShiftJIS(): Convert Unicode16 to Shift_JIS

SFCError Unicode16ToShiftJIS(SFXWideStringConstRef in, SFXAnsiStringPtr out)
{
    SFCError error;
    SFBKDDIUnicodeSJISSmp jis;

    jis = SFBKDDIUnicodeSJIS::NewInstance();
    if (jis != null) {
        // reserve buffer for string to convert 
        error = out->SetLength(in.GetLengthCString() * 2);
        if (error == SFERR_NO_ERROR) {
         // convert Unicode16 to Shift_JIS
         if (jis->Unicode16toSjis(const_cast<WCharPtr>(in.GetBuffer()),
                    out->GetBuffer(), out->GetLength())) {
         	// Following is for handling the bug 
            // that Unicode16toSjis() has

             // Extra '\0' should be deleted before ASCII characters
             // added when converting Unicode16 to Shift_JIS

              // If ASCII characters are included
              if (in.GetLengthCString()*2 != out->GetLengthCString()) 
              {
                    ACharPtr p1 = out->GetBuffer();
                    ACharPtr p2 = p1;
                    SIntN i;
                    for (i = 0; i < in.GetLengthCString() * 2; ++i) {
                        if (*p1 != '\0') {
                            *p2 = *p1;
                            ++p2;
                        }
                        ++p1;
                    }
                    *p2 = '\0';
                }
                out->Set(out->Truncate());
            }
            else {
                error = SFERR_FAILED; // failure
            }
        }
    }
    else {
        error = SFERR_FAILED; // failure
    }
    return error;
}

Utf8toShiftJIS(): Convert UFT-8 to Shift_JIS through UTF-16

SFCError Utf8toShiftJIS(SFXAnsiStringConstRef in, SFXAnsiStringPtr out)
{
    SFXAnsiString sjis;
    SFXWideString temp, line;
    
    SFCError error;
    SInt32 i, j, start, end, length;

    error = temp.SetLength(in.GetLengthCString());
    if (error == SFERR_NO_ERROR) {
        temp.Fill('\0');
        // convert UTF-8 to UTF-16
        if(SFXHelper::utf8towstr(in.GetCString(),in.GetLengthCString(),
                temp.GetBuffer(), temp.GetLength() * 2)) {
            temp = temp.Truncate();

            error = out->SetLength(temp.GetLengthCString() * 2);
            if (error == SFERR_NO_ERROR) {
                i = 0;
                j = 0;
                start = 0;
                length = temp.GetLengthCString();
                // convert line by line
                while (error == SFERR_NO_ERROR && i < length) {
                    while (i < length && temp[i] 
                           != '\r' && temp[i] != '\n'
                           && temp[i] != '\t' && temp[i] != 0x301C) {
                        ++i;
                    }
                    end = i;
                    if (start < end) {
                        line = temp.Substring(start, end);
                        error = Unicode16ToShiftJIS(line, &sjis);
                        if (error != SFERR_NO_ERROR) {
                            break;
                        }
                        out->Copy(j, sjis);
                        j += sjis.GetLength();
                    }
                    if (i < length) {
                        out->SetChar(j, static_cast<AChar>(temp[i]));
                        ++j;
                        ++i;
                        start = i;
                    }
                }
                out->SetLength(j);
            }
        }
        else {
            error = SFERR_FAILED; // failure
        }
    }
    if (error != SFERR_NO_ERROR) {
        out->Clear();
    }
    return error;
}

Go back  1   2   3   4   5   6   7   Apdx1   Apdx2  Next page