Пример #1
0
    uint16_t langID;
    uint32_t localeIndex;
#ifdef USE_WINDOWS_LOCALE_API
    int32_t ret = 0;

    uprv_memset(gPosixFromLCID, 0, sizeof(gPosixFromLCID));

    ret = GetLocaleInfoA(hostid, LOCALE_SNAME, (LPSTR)gPosixFromLCID, sizeof(gPosixFromLCID));
    if (ret > 1) {
        FIX_LOCALE_ID_TAG_SEPARATOR(gPosixFromLCID, ret, localeIndex)
        FIX_LANGUAGE_ID_TAG(gPosixFromLCID, ret)

        return gPosixFromLCID;
    }
#endif
    langID = LANGUAGE_LCID(hostid);

    for (localeIndex = 0; localeIndex < gLocaleCount; localeIndex++)
    {
        if (langID == gPosixIDmap[localeIndex].regionMaps->hostID)
        {
            return getPosixID(&gPosixIDmap[localeIndex], hostid);
        }
    }

    /* no match found */
    *status = U_ILLEGAL_ARGUMENT_ERROR;
    return NULL;
}

/*
Пример #2
0
U_CAPI int32_t
uprv_convertToPosix(uint32_t hostid, char *posixID, int32_t posixIDCapacity, UErrorCode* status)
{
    uint16_t langID;
    uint32_t localeIndex;
    UBool bLookup = TRUE;
    const char *pPosixID = NULL;

#ifdef USE_WINDOWS_LOCALE_API
    int32_t tmpLen = 0;
    char locName[157];  /* ULOC_FULLNAME_CAPACITY */

    tmpLen = GetLocaleInfoA(hostid, LOCALE_SNAME, (LPSTR)locName, sizeof(locName)/sizeof(locName[0]));
    if (tmpLen > 1) {
        /* Windows locale name may contain sorting variant, such as "es-ES_tradnl".
           In such case, we need special mapping data found in the hardcoded table
           in this source file. */
        char *p = uprv_strchr(locName, '_');
        if (p) {
            /* Keep the base locale, without variant */
            *p = 0;
            tmpLen = uprv_strlen(locName);
        } else {
            /* No hardcoded table lookup necessary */
            bLookup = FALSE;
        }
        /* Change the tag separator from '-' to '_' */
        p = locName;
        while (*p) {
            if (*p == '-') {
                *p = '_';
            }
            p++;
        }
        FIX_LANGUAGE_ID_TAG(locName, tmpLen);
        pPosixID = locName;
    }
#endif
    if (bLookup) {
        const char *pCandidate = NULL;
        langID = LANGUAGE_LCID(hostid);

        for (localeIndex = 0; localeIndex < gLocaleCount; localeIndex++) {
            if (langID == gPosixIDmap[localeIndex].regionMaps->hostID) {
                pCandidate = getPosixID(&gPosixIDmap[localeIndex], hostid);
                break;
            }
        }

        /* On Windows, when locale name has a variant, we still look up the hardcoded table.
           If a match in the hardcoded table is longer than the Windows locale name without
           variant, we use the one as the result */
        if (pCandidate && (pPosixID == NULL || uprv_strlen(pCandidate) > uprv_strlen(pPosixID))) {
            pPosixID = pCandidate;
        }
    }

    if (pPosixID) {
        int32_t resLen = uprv_strlen(pPosixID);
        int32_t copyLen = resLen <= posixIDCapacity ? resLen : posixIDCapacity;
        uprv_memcpy(posixID, pPosixID, copyLen);
        if (resLen < posixIDCapacity) {
            posixID[resLen] = 0;
            if (*status == U_STRING_NOT_TERMINATED_WARNING) {
                *status = U_ZERO_ERROR;
            }
        } else if (resLen == posixIDCapacity) {
            *status = U_STRING_NOT_TERMINATED_WARNING;
        } else {
            *status = U_BUFFER_OVERFLOW_ERROR;
        }
        return resLen;
    }

    /* no match found */
    *status = U_ILLEGAL_ARGUMENT_ERROR;
    return -1;
}