2012-01-01 15:14:30 +00:00
# include <qse/cmn/mbwc.h>
2011-12-11 15:43:19 +00:00
# include <qse/cmn/mem.h>
# include <qse/cmn/str.h>
2016-04-29 03:55:42 +00:00
# include <qse/si/sio.h>
2011-12-11 15:43:19 +00:00
# include <locale.h>
# if defined(_WIN32)
# include <windows.h>
# endif
# define R(f) \
do { \
qse_printf ( QSE_T ( " == %s == \n " ) , QSE_T ( # f ) ) ; \
if ( f ( ) = = - 1 ) return - 1 ; \
} while ( 0 )
2012-01-10 15:05:40 +00:00
/* the texts here are all in utf-8. you will see many errors
* on a non - utf8 locale */
2011-12-11 15:43:19 +00:00
static int test1 ( void )
{
int i ;
const qse_mchar_t * x [ ] =
{
QSE_MT ( " " ) ,
2012-01-10 15:05:40 +00:00
QSE_MT ( " This is good. " ) ,
2011-12-11 15:43:19 +00:00
QSE_MT ( " 이거슨 " ) ,
QSE_MT ( " 뭐냐이거 " ) ,
QSE_MT ( " 過去一個月 " ) ,
QSE_MT ( " 是成功的建商 " ) ,
QSE_MT ( " 뛰어 올라봐. 멀리멀리 잘난척하기는 " ) ,
QSE_MT ( " Fly to the universe. eat my shorts. " )
} ;
qse_wchar_t buf [ 5 ] ;
qse_wchar_t buf2 [ 50 ] ;
for ( i = 0 ; i < QSE_COUNTOF ( x ) ; i + + )
{
int n ;
qse_size_t wlen = QSE_COUNTOF ( buf ) , wlen1 ;
qse_size_t mlen , mlen1 ;
n = qse_mbstowcs ( x [ i ] , & mlen , buf , & wlen ) ;
qse_printf ( QSE_T ( " [%hs]=> " ) , x [ i ] ) ;
qse_mbstowcs ( x [ i ] , & mlen1 , QSE_NULL , & wlen1 ) ;
/* the string should not be properly null terminated
* if buf is shorter than the necesary buffer size needed for
* full conversion */
qse_wcsxncpy ( buf2 , QSE_COUNTOF ( buf2 ) , buf , wlen ) ;
qse_printf ( QSE_T ( " %s %d chars %d bytes (IF FULL %d chars %d bytes) [%ls] \n " ) ,
( ( n = = - 3 ) ? QSE_T ( " INCOMPLETE-SEQ " ) : ( n = = - 2 ) ? QSE_T ( " BUFFER-SMALL " ) : ( n = = - 1 ) ? QSE_T ( " ILLEGAL-CHAR " ) : QSE_T ( " FULL " ) ) ,
( int ) wlen , ( int ) mlen , ( int ) wlen1 , ( int ) mlen1 , buf2 ) ;
qse_fflush ( QSE_STDOUT ) ;
}
qse_printf ( QSE_T ( " ----------------- \n " ) ) ;
for ( i = 0 ; i < QSE_COUNTOF ( x ) ; i + + )
{
int n ;
qse_size_t wlen = QSE_COUNTOF ( buf2 ) , wlen1 ;
qse_size_t mlen , mlen1 ;
n = qse_mbstowcs ( x [ i ] , & mlen , buf2 , & wlen ) ;
qse_mbstowcs ( x [ i ] , & mlen1 , QSE_NULL , & wlen1 ) ;
qse_printf ( QSE_T ( " %s %d chars %d bytes (IF FULL %d chars %d bytes) [%.*ls] \n " ) ,
( ( n = = - 3 ) ? QSE_T ( " INCOMPLETE-SEQ " ) : ( n = = - 2 ) ? QSE_T ( " BUFFER-SMALL " ) : ( n = = - 1 ) ? QSE_T ( " ILLEGAL-CHAR " ) : QSE_T ( " FULL " ) ) ,
( int ) wlen , ( int ) mlen , ( int ) wlen , ( int ) wlen1 , ( int ) mlen1 , buf2 ) ;
qse_fflush ( QSE_STDOUT ) ;
}
return 0 ;
}
static int test2 ( void )
{
int i ;
const qse_mchar_t * x [ ] =
{
QSE_MT ( " \0 \0 \0 " ) ,
2012-01-10 15:05:40 +00:00
QSE_MT ( " This is good. " ) ,
2011-12-11 15:43:19 +00:00
QSE_MT ( " 이거슨 " ) ,
QSE_MT ( " 뭐냐이거 " ) ,
QSE_MT ( " 過去一個月 " ) ,
QSE_MT ( " 是成功的建商 " ) ,
QSE_MT ( " 뛰어 올라봐. 멀리멀리 잘난척하기는 " ) ,
QSE_MT ( " Fly to the universe " )
} ;
for ( i = 0 ; i < QSE_COUNTOF ( x ) ; i + + )
{
qse_size_t k = qse_mbslen ( x [ i ] ) ;
qse_size_t j = 0 ;
qse_wchar_t buf [ 2 ] ;
qse_size_t wlen , mlen ;
int iter = 0 ;
if ( k = = 0 ) k = 3 ; /* for x[0] */
mlen = k ;
qse_mbsntowcsn ( & x [ i ] [ j ] , & mlen , QSE_NULL , & wlen ) ;
qse_printf ( QSE_T ( " expecting %d chars, %d bytes [ " ) , wlen , mlen ) ;
while ( j < k )
{
2012-01-10 15:05:40 +00:00
int y ;
2011-12-11 15:43:19 +00:00
wlen = QSE_COUNTOF ( buf ) ;
mlen = k - j ;
2012-01-10 15:05:40 +00:00
y = qse_mbsntowcsn ( & x [ i ] [ j ] , & mlen , buf , & wlen ) ;
2011-12-11 15:43:19 +00:00
if ( y < = - 1 & & y ! = - 2 )
{
qse_printf ( QSE_T ( " ***illegal or incomplete sequence*** " ) ) ;
break ;
}
if ( wlen > 0 & & buf [ 0 ] = = QSE_T ( ' \0 ' ) )
{
while ( wlen > 0 & & buf [ - - wlen ] = = QSE_T ( ' \0 ' ) )
{
qse_printf ( QSE_T ( " \\ 0 " ) ) ;
}
}
else
{
qse_printf ( QSE_T ( " %.*s " ) , ( int ) wlen , buf ) ;
}
j + = mlen ;
iter + + ;
}
qse_printf ( QSE_T ( " ] => %d bytes, %d iterations \n " ) , ( int ) k , ( int ) iter ) ;
}
return 0 ;
}
static int test3 ( void )
{
int i ;
const qse_mchar_t * x [ ] =
{
QSE_MT ( " " ) ,
2012-01-10 15:05:40 +00:00
QSE_MT ( " This is good. " ) ,
2011-12-11 15:43:19 +00:00
QSE_MT ( " 이거슨 " ) ,
QSE_MT ( " 뭐냐이거 " ) ,
QSE_MT ( " 過去一個月 " ) ,
QSE_MT ( " 是成功的建商 " ) ,
QSE_MT ( " 뛰어 올라봐. 멀리멀리 잘난척하기는 " ) ,
QSE_MT ( " Fly to the universe " )
} ;
for ( i = 0 ; i < QSE_COUNTOF ( x ) ; i + + )
{
qse_wchar_t * wcs ;
2012-10-18 06:52:03 +00:00
wcs = qse_mbstowcsdup ( x [ i ] , QSE_NULL , QSE_MMGR_GETDFL ( ) ) ;
2011-12-11 15:43:19 +00:00
if ( wcs = = QSE_NULL )
{
qse_printf ( QSE_T ( " [ERROR] " ) ) ;
}
else
{
qse_printf ( QSE_T ( " [%ls] \n " ) , wcs ) ;
QSE_MMGR_FREE ( QSE_MMGR_GETDFL ( ) , wcs ) ;
}
}
return 0 ;
}
static int test4 ( void )
{
const qse_mchar_t * x [ ] =
{
QSE_MT ( " \0 \0 \0 " ) ,
2012-01-10 15:05:40 +00:00
QSE_MT ( " This is good. " ) ,
2011-12-11 15:43:19 +00:00
QSE_MT ( " 이거슨 " ) ,
QSE_MT ( " 뭐냐이거 " ) ,
QSE_MT ( " 過去一個月 " ) ,
QSE_MT ( " 是成功的建商 " ) ,
QSE_MT ( " 뛰어 올라봐. 멀리멀리 잘난척하기는 " ) ,
QSE_MT ( " Fly to the universe " ) ,
QSE_NULL
} ;
qse_wchar_t * wcs ;
2012-10-18 06:52:03 +00:00
wcs = qse_mbsatowcsdup ( x , QSE_NULL , QSE_MMGR_GETDFL ( ) ) ;
2011-12-11 15:43:19 +00:00
if ( wcs = = QSE_NULL )
{
qse_printf ( QSE_T ( " [ERROR] \n " ) ) ;
}
else
{
qse_printf ( QSE_T ( " [%ls] \n " ) , wcs ) ;
QSE_MMGR_FREE ( QSE_MMGR_GETDFL ( ) , wcs ) ;
}
return 0 ;
}
int main ( )
{
# if defined(_WIN32)
2012-01-10 15:05:40 +00:00
char locale [ 100 ] ;
2015-03-11 06:12:48 +00:00
UINT codepage = GetConsoleOutputCP ( ) ;
2012-01-10 15:05:40 +00:00
if ( codepage = = CP_UTF8 )
{
/*SetConsoleOUtputCP (CP_UTF8);*/
2012-10-15 09:36:39 +00:00
qse_setdflcmgrbyid ( QSE_CMGR_UTF8 ) ;
2012-01-10 15:05:40 +00:00
}
else
{
sprintf ( locale , " .%u " , ( unsigned int ) codepage ) ;
2015-03-11 06:12:48 +00:00
setlocale ( LC_ALL , locale ) ;
2015-04-27 08:37:57 +00:00
/*qse_setdflcmgrbyid (QSE_CMGR_SLMB);*/
2012-01-10 15:05:40 +00:00
}
2011-12-11 15:43:19 +00:00
# else
setlocale ( LC_ALL , " " ) ;
2015-04-27 08:37:57 +00:00
/*qse_setdflcmgrbyid (QSE_CMGR_SLMB);*/
2011-12-11 15:43:19 +00:00
# endif
2017-09-16 08:54:25 +00:00
qse_open_stdsios ( ) ;
2015-03-11 06:12:48 +00:00
2011-12-11 15:43:19 +00:00
qse_printf ( QSE_T ( " -------------------------------------------------------------------------------- \n " ) ) ;
qse_printf ( QSE_T ( " Set the environment LANG to a Unicode locale such as UTF-8 if you see the illegal XXXXX errors. If you see such errors in Unicode locales, this program might be buggy. It is normal to see such messages in non-Unicode locales as it uses Unicode data \n " ) ) ;
qse_printf ( QSE_T ( " -------------------------------------------------------------------------------- \n " ) ) ;
R ( test1 ) ;
R ( test2 ) ;
R ( test3 ) ;
R ( test4 ) ;
2017-09-16 08:54:25 +00:00
qse_close_stdsios ( ) ;
2011-12-11 15:43:19 +00:00
return 0 ;
}