Ignore:
Timestamp:
Nov 16, 2005, 8:36:46 PM (20 years ago)
Author:
dmik
Message:

Transferred Qt for OS/2 version 3.3.1-rc5 sources from the CVS

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/tools/qstring.cpp

    r7 r8  
    6262#if defined(Q_WS_WIN)
    6363#include "qt_windows.h"
     64#endif
     65#if defined(Q_OS_OS2)
     66#include <unidef.h>
     67#include <uconv.h>
    6468#endif
    6569#if !defined( QT_NO_COMPONENT ) && !defined( QT_LITE_COMPONENT )
     
    57015705/*!
    57025706    Returns the string encoded in a locale-specific format. On X11,
    5703     this is the QTextCodec::codecForLocale(). On Windows, it is a
     5707    this is the QTextCodec::codecForLocale(). On Windows and OS/2, it is a
    57045708    system-defined encoding. On Mac OS X, this always uses UTF-8 as
    57055709    the encoding.
     
    57315735    return isNull() ? QCString("") : qt_winQString2MB( *this );
    57325736#endif
     5737#if defined(Q_OS_OS2)
     5738    return isNull() ? QCString("") : qt_os2QString2MB( *this );
     5739#endif
    57335740#ifdef Q_WS_QWS
    57345741    return utf8(); // ### if there is any 8 bit format supported?
     
    57805787    return qt_winMB2QString( local8Bit );
    57815788#endif
     5789#if defined(Q_OS_OS2)
     5790    return qt_os2MB2QString( local8Bit, len );
     5791#endif
    57825792#ifdef Q_WS_QWS
    57835793    return fromUtf8(local8Bit,len);
     
    61936203        return 0;
    61946204    }
     6205#elif defined(Q_OS_OS2)
     6206    LocaleObject lo = qt_os2DefLocaleObj();
     6207    if (lo)
     6208        return UniStrcoll( lo, (UniChar*) ucs2(), (UniChar *) s.ucs2() );
     6209    else
     6210        return ucstrcmp( *this, s );
    61956211#elif defined(Q_WS_MACX)
    61966212    int delta = 0;
     
    69286944
    69296945#endif // Q_OS_WIN32
     6946
     6947#if defined (Q_OS_OS2)
     6948
     6949static LocaleObject qt_os2DefLocaleObj_var = NULL;
     6950static UconvObject qt_os2DefUconvObj_var = NULL;
     6951
     6952const LocaleObject qt_os2DefLocaleObj()
     6953{
     6954    if ( !qt_os2DefLocaleObj_var )
     6955        UniCreateLocaleObject( UNI_UCS_STRING_POINTER, (UniChar *)L"",
     6956                &qt_os2DefLocaleObj_var );
     6957    return qt_os2DefLocaleObj_var;
     6958}
     6959
     6960const UconvObject qt_os2DefUconvObj()
     6961{
     6962    if ( !qt_os2DefUconvObj_var )
     6963        UniCreateUconvObject( (UniChar *)L"", &qt_os2DefUconvObj_var );
     6964    return qt_os2DefUconvObj_var;
     6965}
     6966
     6967QCString qt_os2QString2MB( const QString& s, int uclen )
     6968{
     6969    if ( uclen < 0 || (uint) uclen > s.length() )
     6970        uclen = s.length();
     6971    if ( s.isNull() )
     6972        return QCString();
     6973    if ( uclen == 0 )
     6974        return QCString("");
     6975
     6976    UniChar *ucPtr = (UniChar *) s.unicode();
     6977    size_t ucLeft = uclen;
     6978   
     6979    size_t mblen = uclen;
     6980    QCString mbBuf( mblen + 1 );
     6981    char *mbPtr = mbBuf.data();
     6982    size_t mbLeft = mblen;
     6983   
     6984    size_t nonIdent = 0;
     6985    UconvObject uo = qt_os2DefUconvObj();
     6986    int rc;
     6987   
     6988    while ( ucLeft ) {
     6989        rc = UniUconvFromUcs ( uo, &ucPtr, &ucLeft, (void**)&mbPtr, &mbLeft, &nonIdent );
     6990        if ( rc == ULS_BUFFERFULL ) {
     6991            size_t mbDone = mblen - mbLeft;
     6992            size_t ucDone = uclen - ucLeft;
     6993            // assume that ucLeft/mbLeft is an approximation of ucDone/mbDone
     6994            mblen = mbDone + (ucLeft * mbDone) / ucDone;
     6995            mbBuf.resize( mblen + 1 );
     6996            mbPtr = mbBuf.data() + mbDone;
     6997        } else if ( rc != ULS_SUCCESS ) {
     6998#ifndef QT_NO_DEBUG
     6999            // Fail.
     7000            qWarning("UniUconvFromUcs cannot convert multibyte text (error %d): %s (UTF8)",
     7001                rc, s.utf8().data());
     7002#endif
     7003            break;
     7004        }
     7005    }
     7006    mbBuf[ mblen - mbLeft ] = '\0';
     7007    return mbBuf;
     7008}
     7009
     7010// WATCH OUT: mblen must NOT include the NUL (in contrast with qt_winMB2QString)
     7011QString qt_os2MB2QString( const char* mb, int mblen )
     7012{
     7013    if ( !mb )
     7014        return QString::null;
     7015    if ( mblen < 0 ) {
     7016        mblen = strlen( mb );
     7017    } else {
     7018        char *end = (char *) memchr (mb, '\0', mblen);
     7019        if ( end )
     7020            mblen = end - mb;
     7021    }
     7022    if ( !mblen )
     7023        return QString::null;
     7024   
     7025    const char *mbPtr = mb;
     7026    size_t mbLeft = mblen;
     7027
     7028    size_t uclen = mblen;
     7029    QMemArray<UniChar> ucBuf( uclen );
     7030    UniChar *ucPtr = ucBuf.data();
     7031    size_t ucLeft = uclen;
     7032
     7033    size_t nonIdent = 0;
     7034    UconvObject uo = qt_os2DefUconvObj();
     7035    int rc;
     7036   
     7037    while ( mbLeft ) {
     7038        rc = UniUconvToUcs ( uo, (void**)&mbPtr, &mbLeft, &ucPtr, &ucLeft, &nonIdent );
     7039        if ( rc == ULS_BUFFERFULL ) {
     7040            size_t ucDone = uclen - ucLeft;
     7041            size_t mbDone = mblen - mbLeft;
     7042            // assume that mbLeft/ucLeft is an approximation of mbDone/ucDone
     7043            uclen = ucDone + (mbLeft * ucDone) / mbDone;
     7044            ucBuf.resize( uclen );
     7045            ucPtr = ucBuf.data() + ucDone;
     7046        } else if ( rc != ULS_SUCCESS ) {
     7047#ifndef QT_NO_DEBUG
     7048            // Fail.
     7049            qWarning("UniUconvFromUcs cannot convert multibyte text (error %d)", rc);
     7050#endif
     7051            break;
     7052        }
     7053    }
     7054    uclen -= ucLeft;
     7055    if ( uclen == 0 )
     7056        return QString::null;
     7057    QString s( (QChar*)ucBuf.data(), uclen );
     7058    return s;
     7059}
     7060
     7061#endif // Q_OS_OS2
Note: See TracChangeset for help on using the changeset viewer.