コード例 #1
0
ファイル: mbsrtowcs_s_ex.c プロジェクト: dwinner/CppAppdev
int main()
{
    const char *mbptr = "Any multibyte string";
    wchar_t wcstr[10];            // A buffer for wide characters
    size_t len;                   // and its capacity.
    mbstate_t state = {0};

    if( mbsrtowcs_s( &len, wcstr, 10, &mbptr, 9, &state) != 0)
        printf("The array contains an invalid multibyte character.\n");
    else
    {
       printf("Length: %zu; text: %ls\n", len, wcstr);
       printf("The remaining characters: %s\n", mbptr);
    }
    return 0;
}
コード例 #2
0
void TestToWide( void )
{
    wchar_t             wcs[20];
    errno_t             rc;
    size_t              retval;
    size_t              retval2;
    mbstate_t           ps;
    const char         *pchar;
    const char         *pchar2;
    int                 violations = NumViolations;



    rc = mbstowcs_s( &retval, wcs, 20, "", 0 );
    VERIFY( rc == 0 );
    VERIFY( wcs[0] == L'\0' );
    VERIFY( violations == NumViolations );

    rc = mbstowcs_s( &retval, wcs, 20, "foo", 3 );
    VERIFY( rc == 0 );
    VERIFY( retval == 3 );
    VERIFY( violations == NumViolations );

    rc = mbstowcs_s( &retval, wcs, 20, "\x81\xFC""foo", 0 );
    VERIFY( rc == 0 );
    VERIFY( retval == 0 );
    VERIFY( violations == NumViolations );

    rc = mbstowcs_s( &retval, wcs, 20, "foo", 10 );
    VERIFY( rc == 0 );
    VERIFY( retval == 3 );
    VERIFY( violations == NumViolations );

    rc = mbstowcs_s( &retval, wcs, 6, "\x81\xFC""foo", 10 );
    VERIFY( rc == 0 );
    VERIFY( retval == 4 );
    VERIFY( wcs[1] == 'f' );
    VERIFY( wcs[2] == 'o' );
    VERIFY( wcs[3] == 'o' );
    VERIFY( wcs[4] == '\0' );
    VERIFY( violations == NumViolations );
    rc = mbstowcs_s( &retval2, NULL, 0, "\x81\xFC""foo", 10 );
    VERIFY( retval == retval2 );
    VERIFY( violations == NumViolations );

    pchar = "";
    rc = mbsrtowcs_s( &retval, wcs, 20, &pchar, 0, &ps );
    VERIFY( rc == 0 );
    VERIFY( wcs[0] == L'\0' );
    VERIFY( violations == NumViolations );

    pchar = "foo";
    rc = mbsrtowcs_s( &retval, wcs, 20, &pchar, 3, &ps );
    VERIFY( rc == 0 );
    VERIFY( retval == 3 );
    VERIFY( violations == NumViolations );

    pchar = "\x81\xFC""foo";
    rc = mbsrtowcs_s( &retval, wcs, 20, &pchar, 0, &ps );
    VERIFY( rc == 0 );
    VERIFY( retval == 0 );
    VERIFY( violations == NumViolations );

    pchar = "foo";
    rc = mbsrtowcs_s( &retval, wcs, 20, &pchar, 10, &ps );
    VERIFY( rc == 0 );
    VERIFY( retval == 3 );
    VERIFY( violations == NumViolations );

    pchar = "\x81\xFC""foo";
    rc = mbsrtowcs_s( &retval, wcs, 6, &pchar, 10, &ps );
    VERIFY( rc == 0 );
    VERIFY( retval == 4 );
    VERIFY( wcs[1] == 'f' );
    VERIFY( wcs[2] == 'o' );
    VERIFY( wcs[3] == 'o' );
    VERIFY( wcs[4] == '\0' );
    VERIFY( violations == NumViolations );
    pchar2 = "\x81\xFC""foo";
    rc = mbsrtowcs_s( &retval2, NULL, 0, &pchar2, 10, &ps );
    VERIFY( retval == retval2 );
    VERIFY( violations == NumViolations );

    /***********************************************************************/
    /*  now test runtime-constraints                                       */
    /***********************************************************************/
    rc = mbstowcs_s( NULL, wcs, 20, "\x81\xFC""foo", 10 );
    VERIFY( rc != 0 );
    VERIFY( wcs[0] == L'\0' );
    VERIFY( ++violations == NumViolations );

    rc = mbstowcs_s( &retval, wcs, 6, NULL, 10 );
    VERIFY( rc != 0 );
    VERIFY( retval == -1 );
    VERIFY( wcs[0] == L'\0' );
    VERIFY( ++violations == NumViolations );

#if RSIZE_MAX != SIZE_MAX
    wcs[0] = L'X';
    rc = mbstowcs_s( &retval, wcs, ~0, "\x81\xFC""foo", 10 );
    VERIFY( rc != 0 );
    VERIFY( retval == -1 );
    VERIFY( wcs[0] == L'X' );
    VERIFY( ++violations == NumViolations );

    rc = mbstowcs_s( &retval, wcs, 20, "\x81\xFC""foo", ~0 );
    VERIFY( rc != 0 );
    VERIFY( retval == -1 );
    VERIFY( wcs[0] == L'\0' );
    VERIFY( ++violations == NumViolations );
#endif

    wcs[0] = L'X';
    rc = mbstowcs_s( &retval, wcs, 0, "\x81\xFC""foo", 10 );
    VERIFY( rc != 0 );
    VERIFY( retval == -1 );
    VERIFY( wcs[0] == L'X' );
    VERIFY( ++violations == NumViolations );

    rc = mbstowcs_s( &retval, wcs, 2, "\x81\xFC""foo", 10 );
    VERIFY( rc != 0 );
    VERIFY( retval == -1 );
    VERIFY( wcs[0] == L'\0' );
    VERIFY( ++violations == NumViolations );

#if 0
    /* don't know how to force an encoding error */
    rc = mbstowcs_s( &retval, wcs, 20, "\x01\x11""foo", 10 );
    VERIFY( rc != 0 );
    VERIFY( retval == -1 );
    VERIFY( wcs[0] == L'\0' );
    VERIFY( ++violations == NumViolations );
#endif

    pchar = "\x81\xFC""foo";
    rc = mbsrtowcs_s( NULL, wcs, 20, &pchar, 10, &ps );
    VERIFY( rc != 0 );
    VERIFY( wcs[0] == L'\0' );
    VERIFY( ++violations == NumViolations );

    rc = mbsrtowcs_s( &retval, wcs, 6, NULL, 10, &ps );
    VERIFY( rc != 0 );
    VERIFY( retval == -1 );
    VERIFY( wcs[0] == L'\0' );
    VERIFY( ++violations == NumViolations );

    pchar = NULL;
    rc = mbsrtowcs_s( &retval, wcs, 6, &pchar, 10, &ps );
    VERIFY( rc != 0 );
    VERIFY( retval == -1 );
    VERIFY( wcs[0] == L'\0' );
    VERIFY( ++violations == NumViolations );

#if RSIZE_MAX != SIZE_MAX
    wcs[0] = L'X';
    pchar = "\x81\xFC""foo";
    rc = mbsrtowcs_s( &retval, wcs, ~0, &pchar, 10, &ps );
    VERIFY( rc != 0 );
    VERIFY( retval == -1 );
    VERIFY( wcs[0] == L'X' );
    VERIFY( ++violations == NumViolations );

    pchar = "\x81\xFC""foo";
    rc = mbsrtowcs_s( &retval, wcs, 20, &pchar, ~0, &ps );
    VERIFY( rc != 0 );
    VERIFY( retval == -1 );
    VERIFY( wcs[0] == L'\0' );
    VERIFY( ++violations == NumViolations );
#endif

    pchar = "\x81\xFC""foo";
    wcs[0] = L'X';
    rc = mbsrtowcs_s( &retval, wcs, 0, &pchar, 10, &ps );
    VERIFY( rc != 0 );
    VERIFY( retval == -1 );
    VERIFY( wcs[0] == L'X' );
    VERIFY( ++violations == NumViolations );

    pchar = "\x81\xFC""foo";
    rc = mbsrtowcs_s( &retval, wcs, 2, &pchar, 10, &ps );
    VERIFY( rc != 0 );
    VERIFY( retval == -1 );
    VERIFY( wcs[0] == L'\0' );
    VERIFY( ++violations == NumViolations );
#if 0
    /* don't know how to force an encoding error */
    pchar = "\x01\x11""foo";
    rc = mbsrtowcs_s( &retval, wcs, 20, &pchar, 10, &ps );
    VERIFY( rc != 0 );
    VERIFY( retval == -1 );
    VERIFY( wcs[0] == L'\0' );
    VERIFY( ++violations == NumViolations );
#endif
}
コード例 #3
0
void TestAddendum( void )
{
    wchar_t             wc;
    wchar_t             wcs[10];
    wchar_t *           wcsPtr;
    int                 status;
    mbstate_t           state;
    unsigned char       mbc[MB_LEN_MAX];
    unsigned char       mbcs[10*MB_LEN_MAX+1];
    unsigned char *     mbcsPtr;

    errno_t             rc;
    size_t              retval;
    int                 violations = NumViolations;

    status = wctob( L'!' );
    VERIFY( status == '!' );

    status = mbtowc( &wc, "\x90\x90", 2 );
    #ifndef DUMMY_DBCS
        VERIFY( status == 2 );
        status = wctob( wc );
        VERIFY( status == EOF );
    #else
        VERIFY( status == 1 );
        status = wctob( wc );
        VERIFY( status == 0x90 );
    #endif

    status = sisinit( NULL );
    VERIFY( status != 0 );

    state = 0;
    status = sisinit( &state );
    VERIFY( status == 0 );

    state = 123;
    status = sisinit( &state );
    VERIFY( status == 0 );

    status = mbrlen( "\0""!", 2, &state );
    VERIFY( status == 0 );

    status = mbrlen( "!", 2, &state );
    VERIFY( status == 1 );

    wc = L'\0';
    rc = wcrtomb_s( &retval, mbc, MB_LEN_MAX, wc, &state );
    VERIFY( rc == 0 );
    VERIFY( *mbc == '\0' );
    VERIFY( retval == 1 );
    VERIFY( violations == NumViolations );

    rc = wcrtomb_s( &retval, mbc, MB_LEN_MAX, L'X', &state );
    VERIFY( rc == 0 );
    VERIFY( *mbc == 'X' );
    VERIFY( retval == 1 );
    VERIFY( violations == NumViolations );

    rc = wcrtomb_s( &retval, NULL, 0, L'X', &state );
    VERIFY( rc == 0 );
    VERIFY( retval == 1 );
    VERIFY( violations == NumViolations );


    _mbscpy( mbcs, "Foo!\x90\x90" );
    mbcsPtr = mbcs;
    wcs[5] = wcs[6] = L'-';
    rc = mbsrtowcs_s( &retval, wcs, 10, (const char**)(&mbcsPtr), 6, &state );
    VERIFY( rc == 0 );
    VERIFY( retval == 5 );
    VERIFY( wcs[0] == L'F' );
    VERIFY( wcs[1] == L'o' );
    VERIFY( wcs[2] == L'o' );
    VERIFY( wcs[3] == L'!' );

    mbcsPtr = mbcs;
    rc = mbsrtowcs_s( &retval, NULL, 0, (const char**)(&mbcsPtr), 1, &state );
    VERIFY( rc == 0 );
    VERIFY( retval == 5 );

    wcsPtr = wcs;
    _mbsset( mbcs, _mbsnextc("#") );
    rc = wcsrtombs_s( &retval, mbcs, 10, (const wchar_t**)(&wcsPtr), 6, &state );
    VERIFY( rc == 0 );
    VERIFY( retval == 6 );
    mbcs[retval] = '\0';
    VERIFY( _mbscmp( mbcs, "Foo!\x90\x90" ) == 0 );

    wcsPtr = wcs;
    rc = wcsrtombs_s( &retval, NULL, 0, (const wchar_t**)(&wcsPtr), 20, &state );
    VERIFY( rc == 0 );
    VERIFY( retval == 6 );

    /***********************************************************************/
    /*  test runtime-constraints                                           */
    /***********************************************************************/

    *mbc = 'X';
    rc = wcrtomb_s( NULL, mbc, MB_LEN_MAX, wc, &state );
    VERIFY( rc != 0 );
    VERIFY( *mbc == '\0' );
    VERIFY( ++violations == NumViolations );

    *mbc = 'X';
    rc = wcrtomb_s( &retval, mbc, MB_LEN_MAX, L'X', NULL );
    VERIFY( rc != 0 );
    VERIFY( *mbc == '\0' );
    VERIFY( retval == -1 );
    VERIFY( ++violations == NumViolations );

    rc = wcrtomb_s( &retval, NULL, MB_LEN_MAX, L'X', &state );
    VERIFY( rc != 0 );
    VERIFY( retval == -1 );
    VERIFY( ++violations == NumViolations );

    *mbc = 'X';
    rc = wcrtomb_s( &retval, mbc, 0, L'X', &state );
    VERIFY( rc != 0 );
    VERIFY( *mbc == 'X' );
    VERIFY( retval == -1 );
    VERIFY( ++violations == NumViolations );

#if RSIZE_MAX != SIZE_MAX
    rc = wcrtomb_s( &retval, mbc, ~0, L'X', &state );
    VERIFY( rc != 0 );
    VERIFY( retval == -1 );
    VERIFY( *mbc == 'X' );
    VERIFY( ++violations == NumViolations );
#endif

    mbcsPtr = mbcs;
    rc = mbsrtowcs_s( NULL, NULL, 0, (const char**)(&mbcsPtr), 1, &state );
    VERIFY( rc != 0 );
    VERIFY( ++violations == NumViolations );

    rc = mbsrtowcs_s( &retval, NULL, 0, NULL, 1, &state );
    VERIFY( rc != 0 );
    VERIFY( retval == -1 );
    VERIFY( ++violations == NumViolations );

    mbcsPtr = NULL;
    rc = mbsrtowcs_s( &retval, NULL, 0, (const char**)(&mbcsPtr), 1, &state );
    VERIFY( rc != 0 );
    VERIFY( retval == -1 );
    VERIFY( ++violations == NumViolations );

    rc = mbsrtowcs_s( &retval, NULL, 0, (const char**)(&wcsPtr), 1, NULL );
    VERIFY( rc != 0 );
    VERIFY( retval == -1 );
    VERIFY( ++violations == NumViolations );

    rc = mbsrtowcs_s( &retval, NULL, 111, (const char**)(&wcsPtr), 1, &state );
    VERIFY( rc != 0 );
    VERIFY( retval == -1 );
    VERIFY( ++violations == NumViolations );

    wcsPtr = wcs;
    rc = wcsrtombs_s( &retval, mbcs, 5, (const wchar_t**)(&wcsPtr), 10, &state );
    VERIFY( rc != 0 );
    VERIFY( retval == -1 );
    VERIFY( ++violations == NumViolations );
}