revised qse_mbstowcs()/qse_mbsntowcsn()/qse_wcstombs()/qse_wcsntombsn().
changed the parts affected by the revision works including deleting unnecessary functions
This commit is contained in:
parent
2faee1f23f
commit
566e13d874
@ -2262,183 +2262,135 @@ int qse_wcsxnfnmat (
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The qse_mbstowcslen() function scans a null-terminated multibyte string
|
* The qse_mbstowcs() function converts a null-terminated multibyte string to
|
||||||
* to calculate the number of wide characters it can be converted to.
|
* a wide character string.
|
||||||
* The number of wide characters is returned via @a wcslen if it is not
|
*
|
||||||
* #QSE_NULL. The function may be aborted if it has encountered invalid
|
* It never returns -2 if @a wcs is #QSE_NULL.
|
||||||
* or incomplete multibyte sequences. The return value, in this case,
|
|
||||||
* is less than qse_strlen(mcs).
|
|
||||||
* @return number of bytes scanned
|
|
||||||
*/
|
|
||||||
qse_size_t qse_mbstowcslen (
|
|
||||||
const qse_mchar_t* mcs,
|
|
||||||
qse_size_t* wcslen
|
|
||||||
);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The qse_mbsntowcsnlen() function scans a multibyte string of @a mcslen bytes
|
|
||||||
* to get the number of wide characters it can be converted to.
|
|
||||||
* The number of wide characters is returned via @a wcslen if it is not
|
|
||||||
* #QSE_NULL. The function may be aborted if it has encountered invalid
|
|
||||||
* or incomplete multibyte sequences. The return value, in this case,
|
|
||||||
* is less than @a mcslen.
|
|
||||||
* @return number of bytes scanned
|
|
||||||
*/
|
|
||||||
qse_size_t qse_mbsntowcsnlen (
|
|
||||||
const qse_mchar_t* mcs,
|
|
||||||
qse_size_t mcslen,
|
|
||||||
qse_size_t* wcslen
|
|
||||||
);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The qse_mbstowcs() function converts a multibyte string to a wide
|
|
||||||
* character string.
|
|
||||||
*
|
*
|
||||||
* @code
|
* @code
|
||||||
* const qse_mchar_t* mbs = "a multibyte string";
|
* const qse_mchar_t* mbs = QSE_MT("a multibyte string");
|
||||||
* qse_wchar_t buf[100];
|
* qse_wchar_t wcs[100];
|
||||||
* qse_size_t bufsz = QSE_COUNTOF(buf), n;
|
* qse_size_t wcslen = QSE_COUNTOF(buf), n;
|
||||||
* n = qse_mbstowcs (mbs, buf, bufsz);
|
* qse_size_t mbslen;
|
||||||
* if (bufsz >= QSE_COUNTOF(buf)) { buffer too small }
|
* int n;
|
||||||
* if (mbs[n] != '\0') { incomplete processing }
|
* n = qse_mbstowcs (mbs, &mbslen, wcs, &wcslen);
|
||||||
* //if (n != strlen(mbs)) { incomplete processing }
|
* if (n <= -1) { invalid/incomplenete sequence or buffer to small }
|
||||||
* @endcode
|
* @endcode
|
||||||
*
|
*
|
||||||
* @return number of multibyte characters processed.
|
* @return 0 on success.
|
||||||
|
* -1 if @a mbs contains an illegal character.
|
||||||
|
* -2 if the wide-character string buffer is too small.
|
||||||
|
* -3 if @a mbs is not a complete sequence.
|
||||||
*/
|
*/
|
||||||
qse_size_t qse_mbstowcs (
|
int qse_mbstowcs (
|
||||||
const qse_mchar_t* mbs,
|
const qse_mchar_t* mbs, /**< [in] multibyte string to convert */
|
||||||
qse_wchar_t* wcs,
|
qse_size_t* mbslen, /**< [out] number of multibyte characters
|
||||||
qse_size_t* wcslen
|
handled */
|
||||||
|
qse_wchar_t* wcs, /**< [out] wide-character string buffer */
|
||||||
|
qse_size_t* wcslen /**< [in,out] buffer size for in,
|
||||||
|
number of characters in the buffer for out */
|
||||||
);
|
);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The qse_mbsntowcsn() function converts a multibyte string to a
|
* The qse_mbsntowcsn() function converts a multibyte string to a
|
||||||
* wide character string.
|
* wide character string.
|
||||||
* @return number of multibyte characters processed.
|
*
|
||||||
|
* It never returns -2 if @a wcs is #QSE_NULL.
|
||||||
|
*
|
||||||
|
* @return 0 on success.
|
||||||
|
* -1 if @a mbs contains an illegal character.
|
||||||
|
* -2 if the wide-character string buffer is too small.
|
||||||
|
* -3 if @a mbs is not a complete sequence.
|
||||||
*/
|
*/
|
||||||
qse_size_t qse_mbsntowcsn (
|
int qse_mbsntowcsn (
|
||||||
const qse_mchar_t* mbs,
|
const qse_mchar_t* mbs,
|
||||||
qse_size_t mbslen,
|
qse_size_t* mbslen,
|
||||||
qse_wchar_t* wcs,
|
qse_wchar_t* wcs,
|
||||||
qse_size_t* wcslen
|
qse_size_t* wcslen
|
||||||
);
|
);
|
||||||
|
|
||||||
/**
|
|
||||||
* The qse_wcstombslen() function scans a null-terminated wide character
|
|
||||||
* string @a wcs to get the total number of multibyte characters that it
|
|
||||||
* can be converted to. The resulting number of characters is stored into
|
|
||||||
* memory pointed to by @a mbslen.
|
|
||||||
* Complete scanning is indicated by the following condition:
|
|
||||||
* @code
|
|
||||||
* qse_wcstombslen(wcs,&xx) == qse_strlen(wcs)
|
|
||||||
* @endcode
|
|
||||||
* @return number of wide characters handled
|
|
||||||
*/
|
|
||||||
qse_size_t qse_wcstombslen (
|
|
||||||
const qse_wchar_t* wcs,
|
|
||||||
qse_size_t* mbslen
|
|
||||||
);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The qse_wcsntombsnlen() function scans a wide character wcs as long as
|
|
||||||
* @a wcslen characters to get the total number of multibyte characters
|
|
||||||
* that it can be converted to. The resulting number of characters is stored
|
|
||||||
* into memory pointed to by @a mbslen.
|
|
||||||
* Complete scanning is indicated by the following condition:
|
|
||||||
* @code
|
|
||||||
* qse_wcstombslen(wcs,&xx) == wcslen
|
|
||||||
* @endcode
|
|
||||||
* @return number of wide characters handled
|
|
||||||
*/
|
|
||||||
qse_size_t qse_wcsntombsnlen (
|
|
||||||
const qse_wchar_t* wcs,
|
|
||||||
qse_size_t wcslen,
|
|
||||||
qse_size_t* mbslen
|
|
||||||
);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The qse_wcstombs() function converts a null-terminated wide character
|
|
||||||
* string to a multibyte string and stores it into the buffer pointed to
|
|
||||||
* by @a mbs. The pointer to a variable holding the buffer length should be
|
|
||||||
* passed to the function as the third parameter. After conversion, it holds
|
|
||||||
* the length of the multibyte string excluding the terminating-null character.
|
|
||||||
* It may not null-terminate the resulting multibyte string if the buffer
|
|
||||||
* is not large enough.
|
|
||||||
* @code
|
|
||||||
* const qse_wchar_t* QSE_T("hello");
|
|
||||||
* qse_mchar_t mbs[10];
|
|
||||||
* qse_size_t mbslen = QSE_COUNTOF(mbs);
|
|
||||||
* n = qse_wcstombs (wcs, mbs, &mbslen);
|
|
||||||
* if (wcs[n] == QSE_WT('\0') && mbslen < QSE_COUNTOF(mbs))
|
|
||||||
* {
|
|
||||||
* // wcs fully scanned and mbs null-terminated
|
|
||||||
* }
|
|
||||||
* @endcode
|
|
||||||
* @return number of wide characters processed
|
|
||||||
*/
|
|
||||||
qse_size_t qse_wcstombs (
|
|
||||||
const qse_wchar_t* wcs, /**< wide-character string to convert */
|
|
||||||
qse_mchar_t* mbs, /**< multibyte string buffer */
|
|
||||||
qse_size_t* mbslen /**< [IN] buffer size, [OUT] string length */
|
|
||||||
);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The qse_wcsntombsn() function converts a wide character string to a
|
|
||||||
* multibyte string.
|
|
||||||
* @return the number of wide characters
|
|
||||||
*/
|
|
||||||
qse_size_t qse_wcsntombsn (
|
|
||||||
const qse_wchar_t* wcs, /**< wide string */
|
|
||||||
qse_size_t wcslen,/**< wide string length */
|
|
||||||
qse_mchar_t* mbs, /**< multibyte string buffer */
|
|
||||||
qse_size_t* mbslen /**< [IN] buffer size, [OUT] string length */
|
|
||||||
);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The qse_mbstowcsrigid() function performs the same as the qse_mbstowcs()
|
|
||||||
* function except that it returns an error if it can't fully convert the
|
|
||||||
* input string and/or the buffer is not large enough.
|
|
||||||
*
|
|
||||||
* @return 0 on success,
|
|
||||||
* -1 on failure for truncation,
|
|
||||||
* -2 on failure for invalid/incomplete input seqence.
|
|
||||||
*/
|
|
||||||
int qse_mbstowcsrigid (
|
|
||||||
const qse_mchar_t* mbs,
|
|
||||||
qse_wchar_t* wcs,
|
|
||||||
qse_size_t wcslen
|
|
||||||
);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The qse_wcstombsrigid() function performs the same as the qse_wcstombs()
|
|
||||||
* function except that it returns an error if it can't fully convert the
|
|
||||||
* input string and/or the buffer is not large enough.
|
|
||||||
*
|
|
||||||
* @return 0 on success,
|
|
||||||
* -1 on failure for truncation,
|
|
||||||
* -2 on failure for erroneous input seqence.
|
|
||||||
*/
|
|
||||||
int qse_wcstombsrigid (
|
|
||||||
const qse_wchar_t* wcs,
|
|
||||||
qse_mchar_t* mbs,
|
|
||||||
qse_size_t mbslen
|
|
||||||
);
|
|
||||||
|
|
||||||
|
|
||||||
qse_wchar_t* qse_mbstowcsdup (
|
qse_wchar_t* qse_mbstowcsdup (
|
||||||
const qse_mchar_t* mbs,
|
const qse_mchar_t* mbs,
|
||||||
qse_mmgr_t* mmgr
|
qse_mmgr_t* mmgr
|
||||||
);
|
);
|
||||||
|
|
||||||
qse_mchar_t* qse_wcstombsdup (
|
qse_wchar_t* qse_mbsatowcsdup (
|
||||||
const qse_wchar_t* wcs,
|
const qse_mchar_t* mbs[],
|
||||||
qse_mmgr_t* mmgr
|
qse_mmgr_t* mmgr
|
||||||
);
|
);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The qse_wcstombs() function converts a null-terminated wide character
|
||||||
|
* string @a wcs to a multibyte string and writes it into the buffer pointed to
|
||||||
|
* by @a mbs, but not more than @a mbslen bytes including the terminating null.
|
||||||
|
*
|
||||||
|
* Upon return, @a mbslen is modifed to the actual bytes written to @a mbs
|
||||||
|
* excluding the terminating null; @a wcslen is modifed to the number of
|
||||||
|
* wide characters converted.
|
||||||
|
*
|
||||||
|
* You may pass #QSE_NULL for @a mbs to dry-run conversion or to get the
|
||||||
|
* required buffer size for conversion. -2 is never returned in this case.
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
* - 0 on full conversion,
|
||||||
|
* - -1 on no or partial conversion for an illegal character encountered,
|
||||||
|
* - -2 on no or partial conversion for a small buffer.
|
||||||
|
*
|
||||||
|
* @code
|
||||||
|
* const qse_wchar_t* wcs = QSE_T("hello");
|
||||||
|
* qse_mchar_t mbs[10];
|
||||||
|
* qse_size_t wcslen;
|
||||||
|
* qse_size_t mbslen = QSE_COUNTOF(mbs);
|
||||||
|
* n = qse_wcstombs (wcs, &wcslen, mbs, &mbslen);
|
||||||
|
* if (n <= -1)
|
||||||
|
* {
|
||||||
|
* // wcs fully scanned and mbs null-terminated
|
||||||
|
* }
|
||||||
|
* @endcode
|
||||||
|
*/
|
||||||
|
int qse_wcstombs (
|
||||||
|
const qse_wchar_t* wcs, /**< [in] wide-character string to convert*/
|
||||||
|
qse_size_t* wcslen, /**< [out] number of wide-characters handled */
|
||||||
|
qse_mchar_t* mbs, /**< [out] #QSE_NULL or buffer pointer */
|
||||||
|
qse_size_t* mbslen /**< [in,out] buffer size for in,
|
||||||
|
actual length for out*/
|
||||||
|
);
|
||||||
|
|
||||||
qse_wchar_t* qse_mbsatombsdup (
|
/**
|
||||||
const qse_mchar_t* mbs[],
|
* The qse_wcsntombsn() function converts the first @a wcslen characters from
|
||||||
|
* a wide character string @a wcs to a multibyte string and writes it to a
|
||||||
|
* buffer @a mbs not more than @a mbslen bytes.
|
||||||
|
*
|
||||||
|
* Upon return, it modifies @a mbslen to the actual bytes written to @a mbs
|
||||||
|
* and @a wcslen to the number of wide characters converted.
|
||||||
|
*
|
||||||
|
* You may pass #QSE_NULL for @a mbs to dry-run conversion or to get the
|
||||||
|
* required buffer size for conversion.
|
||||||
|
*
|
||||||
|
* 0 is returned on full conversion. The number of wide characters handled
|
||||||
|
* is stored into @a wcslen and the number of produced multibyte characters
|
||||||
|
* is stored into @a mbslen. -1 is returned if an illegal character is
|
||||||
|
* encounterd during conversion and -2 is returned if the buffer is not
|
||||||
|
* large enough to perform full conversion. however, the number of wide
|
||||||
|
* characters handled so far stored into @a wcslen and the number of produced
|
||||||
|
* multibyte characters so far stored into @a mbslen are still valid.
|
||||||
|
* If @a mbs is #QSE_NULL, -2 is never returned.
|
||||||
|
*
|
||||||
|
* @return 0 on success,
|
||||||
|
* -1 if @a wcs contains an illegal character,
|
||||||
|
* -2 if the multibyte string buffer is too small.
|
||||||
|
*/
|
||||||
|
int qse_wcsntombsn (
|
||||||
|
const qse_wchar_t* wcs, /**< [in] wide string */
|
||||||
|
qse_size_t* wcslen,/**< [in,out] wide string length for in,
|
||||||
|
number of wide characters handled for out */
|
||||||
|
qse_mchar_t* mbs, /**< [out] #QSE_NULL or buffer pointer */
|
||||||
|
qse_size_t* mbslen /**< [in,out] buffer size for in,
|
||||||
|
actual size for out */
|
||||||
|
);
|
||||||
|
|
||||||
|
qse_mchar_t* qse_wcstombsdup (
|
||||||
|
const qse_wchar_t* wcs,
|
||||||
qse_mmgr_t* mmgr
|
qse_mmgr_t* mmgr
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@ -114,27 +114,19 @@ int StdAwk::system (Run& run, Value& ret, const Value* args, size_t nargs,
|
|||||||
#elif defined(QSE_CHAR_IS_MCHAR)
|
#elif defined(QSE_CHAR_IS_MCHAR)
|
||||||
return ret.setInt ((long_t)::system(ptr));
|
return ret.setInt ((long_t)::system(ptr));
|
||||||
#else
|
#else
|
||||||
char* mbs = (char*) qse_awk_allocmem ((awk_t*)(Awk*)run, l*5+1);
|
|
||||||
if (mbs == QSE_NULL) return -1;
|
|
||||||
|
|
||||||
/* at this point, the string is guaranteed to be
|
qse_mchar_t* mbs;
|
||||||
* null-terminating. so qse_wcstombs() can be used to convert
|
mbs = qse_wcstombsdup (ptr, ((awk_t*)(Awk*)run)->mmgr);
|
||||||
* the string, not qse_wcsntombsn(). */
|
if (mbs == QSE_NULL)
|
||||||
|
|
||||||
qse_size_t mbl = l * 5;
|
|
||||||
if (qse_wcstombs (ptr, mbs, &mbl) != l && mbl >= l * 5)
|
|
||||||
{
|
{
|
||||||
/* not the entire string is converted.
|
|
||||||
* mbs is not null-terminated properly. */
|
|
||||||
qse_awk_freemem ((awk_t*)(Awk*)run, mbs);
|
qse_awk_freemem ((awk_t*)(Awk*)run, mbs);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
mbs[mbl] = '\0';
|
|
||||||
int n = ret.setInt ((long_t)::system(mbs));
|
int n = ret.setInt ((long_t)::system(mbs));
|
||||||
|
|
||||||
qse_awk_freemem ((awk_t*)(Awk*)run, mbs);
|
qse_awk_freemem ((awk_t*)(Awk*)run, mbs);
|
||||||
return n;
|
return n;
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1446,7 +1446,7 @@ static int fnc_srand (qse_awk_rtx_t* run, const qse_cstr_t* fnm)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int fnc_system (qse_awk_rtx_t* run, const qse_cstr_t* fnm)
|
static int fnc_system (qse_awk_rtx_t* rtx, const qse_cstr_t* fnm)
|
||||||
{
|
{
|
||||||
qse_size_t nargs;
|
qse_size_t nargs;
|
||||||
qse_awk_val_t* v;
|
qse_awk_val_t* v;
|
||||||
@ -1454,10 +1454,10 @@ static int fnc_system (qse_awk_rtx_t* run, const qse_cstr_t* fnm)
|
|||||||
qse_size_t len;
|
qse_size_t len;
|
||||||
int n = 0;
|
int n = 0;
|
||||||
|
|
||||||
nargs = qse_awk_rtx_getnargs (run);
|
nargs = qse_awk_rtx_getnargs (rtx);
|
||||||
QSE_ASSERT (nargs == 1);
|
QSE_ASSERT (nargs == 1);
|
||||||
|
|
||||||
v = qse_awk_rtx_getarg (run, 0);
|
v = qse_awk_rtx_getarg (rtx, 0);
|
||||||
if (v->type == QSE_AWK_VAL_STR)
|
if (v->type == QSE_AWK_VAL_STR)
|
||||||
{
|
{
|
||||||
str = ((qse_awk_val_str_t*)v)->val.ptr;
|
str = ((qse_awk_val_str_t*)v)->val.ptr;
|
||||||
@ -1465,7 +1465,7 @@ static int fnc_system (qse_awk_rtx_t* run, const qse_cstr_t* fnm)
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
str = qse_awk_rtx_valtocpldup (run, v, &len);
|
str = qse_awk_rtx_valtocpldup (rtx, v, &len);
|
||||||
if (str == QSE_NULL) return -1;
|
if (str == QSE_NULL) return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1488,45 +1488,28 @@ static int fnc_system (qse_awk_rtx_t* run, const qse_cstr_t* fnm)
|
|||||||
#elif defined(QSE_CHAR_IS_MCHAR)
|
#elif defined(QSE_CHAR_IS_MCHAR)
|
||||||
n = system (str);
|
n = system (str);
|
||||||
#else
|
#else
|
||||||
{
|
|
||||||
char* mbs;
|
|
||||||
qse_size_t mbl;
|
|
||||||
|
|
||||||
mbs = (char*) qse_awk_allocmem (run->awk, len*5+1);
|
{
|
||||||
|
qse_mchar_t* mbs;
|
||||||
|
mbs = qse_wcstombsdup (str, rtx->awk->mmgr);
|
||||||
if (mbs == QSE_NULL)
|
if (mbs == QSE_NULL)
|
||||||
{
|
{
|
||||||
n = -1;
|
n = -1;
|
||||||
goto skip_system;
|
goto skip_system;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* at this point, the string is guaranteed to be
|
|
||||||
* null-terminating. so qse_wcstombs() can be used to convert
|
|
||||||
* the string, not qse_wcsntombsn(). */
|
|
||||||
|
|
||||||
mbl = len * 5;
|
|
||||||
if (qse_wcstombs (str, mbs, &mbl) != len && mbl >= len * 5)
|
|
||||||
{
|
|
||||||
/* not the entire string is converted.
|
|
||||||
* mbs is not null-terminated properly. */
|
|
||||||
n = -1;
|
|
||||||
goto skip_system_mbs;
|
|
||||||
}
|
|
||||||
|
|
||||||
mbs[mbl] = '\0';
|
|
||||||
n = system (mbs);
|
n = system (mbs);
|
||||||
|
QSE_AWK_FREE (rtx->awk, mbs);
|
||||||
skip_system_mbs:
|
|
||||||
qse_awk_freemem (run->awk, mbs);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
skip_system:
|
skip_system:
|
||||||
if (v->type != QSE_AWK_VAL_STR) QSE_AWK_FREE (run->awk, str);
|
if (v->type != QSE_AWK_VAL_STR) QSE_AWK_FREE (rtx->awk, str);
|
||||||
|
|
||||||
v = qse_awk_rtx_makeintval (run, (qse_long_t)n);
|
v = qse_awk_rtx_makeintval (rtx, (qse_long_t)n);
|
||||||
if (v == QSE_NULL) return -1;
|
if (v == QSE_NULL) return -1;
|
||||||
|
|
||||||
qse_awk_rtx_setretval (run, v);
|
qse_awk_rtx_setretval (rtx, v);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -89,11 +89,6 @@ void qse_assert_failed (
|
|||||||
void *btarray[128];
|
void *btarray[128];
|
||||||
qse_size_t btsize, i;
|
qse_size_t btsize, i;
|
||||||
char **btsyms;
|
char **btsyms;
|
||||||
|
|
||||||
#ifdef QSE_CHAR_IS_WCHAR
|
|
||||||
qse_wchar_t wcs[256];
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
qse_sio_puts (QSE_SIO_ERR, QSE_T("=[ASSERTION FAILURE]============================================================\n"));
|
qse_sio_puts (QSE_SIO_ERR, QSE_T("=[ASSERTION FAILURE]============================================================\n"));
|
||||||
@ -138,11 +133,15 @@ void qse_assert_failed (
|
|||||||
|
|
||||||
for (i = 0; i < btsize; i++)
|
for (i = 0; i < btsize; i++)
|
||||||
{
|
{
|
||||||
|
/* TODO: call qse_sio_putms() instead of using ifdef */
|
||||||
#ifdef QSE_CHAR_IS_MCHAR
|
#ifdef QSE_CHAR_IS_MCHAR
|
||||||
qse_sio_puts (QSE_SIO_ERR, btsyms[i]);
|
qse_sio_puts (QSE_SIO_ERR, btsyms[i]);
|
||||||
#else
|
#else
|
||||||
|
qse_wchar_t wcs[256];
|
||||||
qse_size_t wcslen = QSE_COUNTOF(wcs);
|
qse_size_t wcslen = QSE_COUNTOF(wcs);
|
||||||
qse_mbstowcs (btsyms[i], wcs, &wcslen);
|
qse_size_t mbslen;
|
||||||
|
qse_mbstowcs (btsyms[i], &mbslen, wcs, &wcslen);
|
||||||
|
wcs[QSE_COUNTOF(wcs) - 1] = QSE_T('\0');
|
||||||
qse_sio_puts (QSE_SIO_ERR, wcs);
|
qse_sio_puts (QSE_SIO_ERR, wcs);
|
||||||
#endif
|
#endif
|
||||||
qse_sio_puts (QSE_SIO_ERR, QSE_T("\n"));
|
qse_sio_puts (QSE_SIO_ERR, QSE_T("\n"));
|
||||||
|
@ -252,8 +252,9 @@ int qse_fio_init (
|
|||||||
const qse_mchar_t* path_mb = path;
|
const qse_mchar_t* path_mb = path;
|
||||||
#else
|
#else
|
||||||
qse_mchar_t path_mb[CCHMAXPATH];
|
qse_mchar_t path_mb[CCHMAXPATH];
|
||||||
if (qse_wcstombsrigid (path,
|
qse_size_t wl, ml = QSE_COUNTOF(path_mb);
|
||||||
path_mb, QSE_COUNTOF(path_mb)) <= -1) return -1;
|
/* TODO: use wcstombsdup??? */
|
||||||
|
if (qse_wcstombs (path, &wl, path_mb, &ml) <= -1) return -1;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
zero.ulLo = 0;
|
zero.ulLo = 0;
|
||||||
@ -342,8 +343,8 @@ int qse_fio_init (
|
|||||||
const qse_mchar_t* path_mb = path;
|
const qse_mchar_t* path_mb = path;
|
||||||
#else
|
#else
|
||||||
qse_mchar_t path_mb[_MAX_PATH];
|
qse_mchar_t path_mb[_MAX_PATH];
|
||||||
if (qse_wcstombsrigid (path,
|
qse_size_t wl, ml = QSE_COUNTOF(path_mb);
|
||||||
path_mb, QSE_COUNTOF(path_mb)) <= -1) return -1;
|
if (qse_wcstombs (path, &wl, path_mb, &ml) <= -1) return -1;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
if (flags & QSE_FIO_APPEND)
|
if (flags & QSE_FIO_APPEND)
|
||||||
@ -395,8 +396,9 @@ int qse_fio_init (
|
|||||||
const qse_mchar_t* path_mb = path;
|
const qse_mchar_t* path_mb = path;
|
||||||
#else
|
#else
|
||||||
qse_mchar_t path_mb[PATH_MAX + 1];
|
qse_mchar_t path_mb[PATH_MAX + 1];
|
||||||
if (qse_wcstombsrigid (path,
|
/* TODO: use qse_wcstombsdup(). path name may exceede PATH_MAX if it contains .. or . */
|
||||||
path_mb, QSE_COUNTOF(path_mb)) <= -1) return -1;
|
qse_size_t wl, ml = QSE_COUNTOF(path_mb);
|
||||||
|
if (qse_wcstombs (path, &wl, path_mb, &ml) <= -1) return -1;
|
||||||
#endif
|
#endif
|
||||||
/*
|
/*
|
||||||
* rwa -> RDWR | APPEND
|
* rwa -> RDWR | APPEND
|
||||||
@ -655,7 +657,8 @@ static qse_ssize_t fio_read (qse_fio_t* fio, void* buf, qse_size_t size)
|
|||||||
#if defined(_WIN32)
|
#if defined(_WIN32)
|
||||||
|
|
||||||
DWORD count;
|
DWORD count;
|
||||||
if (size > QSE_TYPE_MAX(DWORD)) size = QSE_TYPE_MAX(DWORD);
|
if (size > (QSE_TYPE_MAX(qse_ssize_t) & QSE_TYPE_MAX(DWORD)))
|
||||||
|
size = QSE_TYPE_MAX(qse_ssize_t) & QSE_TYPE_MAX(DWORD);
|
||||||
if (ReadFile (fio->handle,
|
if (ReadFile (fio->handle,
|
||||||
buf, (DWORD)size, &count, QSE_NULL) == FALSE) return -1;
|
buf, (DWORD)size, &count, QSE_NULL) == FALSE) return -1;
|
||||||
return (qse_ssize_t)count;
|
return (qse_ssize_t)count;
|
||||||
@ -663,18 +666,22 @@ static qse_ssize_t fio_read (qse_fio_t* fio, void* buf, qse_size_t size)
|
|||||||
#elif defined(__OS2__)
|
#elif defined(__OS2__)
|
||||||
|
|
||||||
ULONG count;
|
ULONG count;
|
||||||
if (size > QSE_TYPE_MAX(ULONG)) size = QSE_TYPE_MAX(ULONG);
|
if (size > (QSE_TYPE_MAX(qse_ssize_t) & QSE_TYPE_MAX(ULONG)))
|
||||||
|
size = QSE_TYPE_MAX(qse_ssize_t) & QSE_TYPE_MAX(ULONG);
|
||||||
if (DosRead (fio->handle,
|
if (DosRead (fio->handle,
|
||||||
buf, (ULONG)size, &count) != NO_ERROR) return -1;
|
buf, (ULONG)size, &count) != NO_ERROR) return -1;
|
||||||
return (qse_ssize_t)count;
|
return (qse_ssize_t)count;
|
||||||
|
|
||||||
#elif defined(__DOS__)
|
#elif defined(__DOS__)
|
||||||
|
|
||||||
if (size > QSE_TYPE_MAX(size_t)) size = QSE_TYPE_MAX(size_t);
|
if (size > (QSE_TYPE_MAX(qse_ssize_t) & QSE_TYPE_MAX(size_t)))
|
||||||
|
size = QSE_TYPE_MAX(qse_ssize_t) & QSE_TYPE_MAX(size_t);
|
||||||
return read (fio->handle, buf, size);
|
return read (fio->handle, buf, size);
|
||||||
|
|
||||||
#else
|
#else
|
||||||
if (size > QSE_TYPE_MAX(size_t)) size = QSE_TYPE_MAX(size_t);
|
|
||||||
|
if (size > (QSE_TYPE_MAX(qse_ssize_t) & QSE_TYPE_MAX(size_t)))
|
||||||
|
size = QSE_TYPE_MAX(qse_ssize_t) & QSE_TYPE_MAX(size_t);
|
||||||
return QSE_READ (fio->handle, buf, size);
|
return QSE_READ (fio->handle, buf, size);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
@ -690,8 +697,10 @@ qse_ssize_t qse_fio_read (qse_fio_t* fio, void* buf, qse_size_t size)
|
|||||||
static qse_ssize_t fio_write (qse_fio_t* fio, const void* data, qse_size_t size)
|
static qse_ssize_t fio_write (qse_fio_t* fio, const void* data, qse_size_t size)
|
||||||
{
|
{
|
||||||
#if defined(_WIN32)
|
#if defined(_WIN32)
|
||||||
|
|
||||||
DWORD count;
|
DWORD count;
|
||||||
if (size > QSE_TYPE_MAX(DWORD)) size = QSE_TYPE_MAX(DWORD);
|
if (size > (QSE_TYPE_MAX(qse_ssize_t) & QSE_TYPE_MAX(DWORD)))
|
||||||
|
size = QSE_TYPE_MAX(qse_ssize_t) & QSE_TYPE_MAX(DWORD);
|
||||||
if (WriteFile (fio->handle,
|
if (WriteFile (fio->handle,
|
||||||
data, (DWORD)size, &count, QSE_NULL) == FALSE) return -1;
|
data, (DWORD)size, &count, QSE_NULL) == FALSE) return -1;
|
||||||
return (qse_ssize_t)count;
|
return (qse_ssize_t)count;
|
||||||
@ -699,19 +708,23 @@ static qse_ssize_t fio_write (qse_fio_t* fio, const void* data, qse_size_t size)
|
|||||||
#elif defined(__OS2__)
|
#elif defined(__OS2__)
|
||||||
|
|
||||||
ULONG count;
|
ULONG count;
|
||||||
if (size > QSE_TYPE_MAX(ULONG)) size = QSE_TYPE_MAX(ULONG);
|
|
||||||
|
if (size > (QSE_TYPE_MAX(qse_ssize_t) & QSE_TYPE_MAX(ULONG)))
|
||||||
|
size = QSE_TYPE_MAX(qse_ssize_t) & QSE_TYPE_MAX(ULONG);
|
||||||
if (DosWrite(fio->handle,
|
if (DosWrite(fio->handle,
|
||||||
(PVOID)data, (ULONG)size, &count) != NO_ERROR) return -1;
|
(PVOID)data, (ULONG)size, &count) != NO_ERROR) return -1;
|
||||||
return (qse_ssize_t)count;
|
return (qse_ssize_t)count;
|
||||||
|
|
||||||
#elif defined(__DOS__)
|
#elif defined(__DOS__)
|
||||||
|
|
||||||
if (size > QSE_TYPE_MAX(size_t)) size = QSE_TYPE_MAX(size_t);
|
if (size > (QSE_TYPE_MAX(qse_ssize_t) & QSE_TYPE_MAX(size_t)))
|
||||||
|
size = QSE_TYPE_MAX(qse_ssize_t) & QSE_TYPE_MAX(size_t);
|
||||||
return write (fio->handle, data, size);
|
return write (fio->handle, data, size);
|
||||||
|
|
||||||
#else
|
#else
|
||||||
|
|
||||||
if (size > QSE_TYPE_MAX(size_t)) size = QSE_TYPE_MAX(size_t);
|
if (size > (QSE_TYPE_MAX(qse_ssize_t) & QSE_TYPE_MAX(size_t)))
|
||||||
|
size = QSE_TYPE_MAX(qse_ssize_t) & QSE_TYPE_MAX(size_t);
|
||||||
return QSE_WRITE (fio->handle, data, size);
|
return QSE_WRITE (fio->handle, data, size);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
@ -932,7 +945,6 @@ static qse_ssize_t fio_input (qse_tio_cmd_t cmd, void* arg, void* buf, qse_size_
|
|||||||
QSE_ASSERT (fio != QSE_NULL);
|
QSE_ASSERT (fio != QSE_NULL);
|
||||||
if (cmd == QSE_TIO_IO_DATA) return fio_read (fio, buf, size);
|
if (cmd == QSE_TIO_IO_DATA) return fio_read (fio, buf, size);
|
||||||
|
|
||||||
|
|
||||||
/* take no actions for OPEN and CLOSE as they are handled
|
/* take no actions for OPEN and CLOSE as they are handled
|
||||||
* by fio */
|
* by fio */
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -321,23 +321,24 @@ static int set_entry_name (qse_fs_t* fs, const qse_mchar_t* name)
|
|||||||
info_t* info;
|
info_t* info;
|
||||||
qse_size_t len;
|
qse_size_t len;
|
||||||
|
|
||||||
|
#if defined(QSE_CHAR_IS_MCHAR) || defined(_WIN32)
|
||||||
|
/* nothing more to declare */
|
||||||
|
#else
|
||||||
|
qse_size_t mlen;
|
||||||
|
#endif
|
||||||
|
|
||||||
info = fs->info;
|
info = fs->info;
|
||||||
QSE_ASSERT (info != QSE_NULL);
|
QSE_ASSERT (info != QSE_NULL);
|
||||||
|
|
||||||
#if defined(QSE_CHAR_IS_MCHAR) || defined(_WIN32)
|
#if defined(QSE_CHAR_IS_MCHAR) || defined(_WIN32)
|
||||||
len = qse_strlen (name);
|
len = qse_strlen (name);
|
||||||
#else
|
#else
|
||||||
{
|
|
||||||
qse_size_t mlen;
|
|
||||||
|
|
||||||
/* TODO: ignore MBWCERR */
|
/* TODO: ignore MBWCERR */
|
||||||
mlen = qse_mbstowcslen (name, &len);
|
if (qse_mbstowcs (name, &mlen, QSE_NULL, &len) <= -1)
|
||||||
if (name[mlen] != QSE_MT('\0'))
|
|
||||||
{
|
{
|
||||||
/* invalid name ??? */
|
/* invalid name ??? */
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
if (len > info->name.len)
|
if (len > info->name.len)
|
||||||
@ -363,8 +364,8 @@ static int set_entry_name (qse_fs_t* fs, const qse_mchar_t* name)
|
|||||||
#if defined(QSE_CHAR_IS_MCHAR) || defined(_WIN32)
|
#if defined(QSE_CHAR_IS_MCHAR) || defined(_WIN32)
|
||||||
qse_strcpy (info->name.ptr, name);
|
qse_strcpy (info->name.ptr, name);
|
||||||
#else
|
#else
|
||||||
len++;
|
len++; /* for terminating null */
|
||||||
qse_mbstowcs (name, info->name.ptr, &len);
|
qse_mbstowcs (name, &mlen, info->name.ptr, &len);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
fs->ent.name.base = info->name.ptr;
|
fs->ent.name.base = info->name.ptr;
|
||||||
|
@ -48,31 +48,12 @@ int qse_runmain (
|
|||||||
|
|
||||||
for (i = 0; i < argc; i++)
|
for (i = 0; i < argc; i++)
|
||||||
{
|
{
|
||||||
qse_size_t n, len, nlen;
|
/* TODO: ignore MBWCERR */
|
||||||
qse_size_t mbslen;
|
v[i]= qse_mbstowcsdup (argv[i], mmgr);
|
||||||
|
if (v[i] == QSE_NULL)
|
||||||
mbslen = qse_mbslen (argv[i]);
|
|
||||||
|
|
||||||
n = qse_mbstowcslen (argv[i], &len);
|
|
||||||
if (n < mbslen) { ret = -1; goto oops; }
|
|
||||||
|
|
||||||
len++; /* include the terminating null */
|
|
||||||
|
|
||||||
v[i] = (qse_char_t*) QSE_MMGR_ALLOC (
|
|
||||||
mmgr, len*QSE_SIZEOF(qse_char_t));
|
|
||||||
if (v[i] == QSE_NULL) { ret = -1; goto oops; }
|
|
||||||
|
|
||||||
nlen = len;
|
|
||||||
n = qse_mbstowcs (argv[i], v[i], &nlen);
|
|
||||||
if (nlen >= len)
|
|
||||||
{
|
{
|
||||||
/* no null-termination */
|
ret = -1;
|
||||||
ret = -1; goto oops;
|
goto oops;
|
||||||
}
|
|
||||||
if (argv[i][n] != '\0')
|
|
||||||
{
|
|
||||||
/* partial processing */
|
|
||||||
ret = -1; goto oops;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -117,36 +98,18 @@ int qse_runmainwithenv (
|
|||||||
|
|
||||||
for (i = 0; i < argc + 1 + envc; i++)
|
for (i = 0; i < argc + 1 + envc; i++)
|
||||||
{
|
{
|
||||||
qse_size_t n, len, nlen;
|
|
||||||
qse_size_t mbslen;
|
|
||||||
qse_achar_t* x;
|
qse_achar_t* x;
|
||||||
|
|
||||||
if (i < argc) x = argv[i];
|
if (i < argc) x = argv[i];
|
||||||
else if (i == argc) continue;
|
else if (i == argc) continue;
|
||||||
else x = envp[i - argc - 1];
|
else x = envp[i - argc - 1];
|
||||||
|
|
||||||
mbslen = qse_mbslen (x);
|
/* TODO: ignore MBWCERR */
|
||||||
|
v[i]= qse_mbstowcsdup (x, mmgr);
|
||||||
n = qse_mbstowcslen (x, &len);
|
if (v[i] == QSE_NULL)
|
||||||
if (n < mbslen) { ret = -1; goto oops; }
|
|
||||||
|
|
||||||
len++; /* include the terminating null */
|
|
||||||
|
|
||||||
v[i] = (qse_char_t*) QSE_MMGR_ALLOC (
|
|
||||||
mmgr, len*QSE_SIZEOF(qse_char_t));
|
|
||||||
if (v[i] == QSE_NULL) { ret = -1; goto oops; }
|
|
||||||
|
|
||||||
nlen = len;
|
|
||||||
n = qse_mbstowcs (x, v[i], &nlen);
|
|
||||||
if (nlen >= len)
|
|
||||||
{
|
{
|
||||||
/* no null-termination */
|
ret = -1;
|
||||||
ret = -1; goto oops;
|
goto oops;
|
||||||
}
|
|
||||||
if (x[n] != '\0')
|
|
||||||
{
|
|
||||||
/* partial processing */
|
|
||||||
ret = -1; goto oops;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -838,12 +838,19 @@ int qse_pio_init (
|
|||||||
|
|
||||||
if (oflags & QSE_PIO_SHELL)
|
if (oflags & QSE_PIO_SHELL)
|
||||||
{
|
{
|
||||||
|
#if 0
|
||||||
n = qse_wcstombslen (cmd, &mn);
|
n = qse_wcstombslen (cmd, &mn);
|
||||||
if (cmd[n] != QSE_WT('\0'))
|
if (cmd[n] != QSE_WT('\0'))
|
||||||
{
|
{
|
||||||
/* cmd has illegal sequence */
|
/* cmd has illegal sequence */
|
||||||
goto child_oops;
|
goto child_oops;
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
if (qse_wcstombs (cmd, &wl, QSE_NULL, &mn) <= -1)
|
||||||
|
{
|
||||||
|
/* cmd has illegal sequence */
|
||||||
|
goto child_oops;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@ -864,8 +871,11 @@ int qse_pio_init (
|
|||||||
if (wcmd[wl++] == QSE_T('\0')) n--;
|
if (wcmd[wl++] == QSE_T('\0')) n--;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if 0
|
||||||
n = qse_wcsntombsnlen (wcmd, wl, &mn);
|
n = qse_wcsntombsnlen (wcmd, wl, &mn);
|
||||||
if (n != wl) goto child_oops;
|
if (n != wl) goto child_oops;
|
||||||
|
#endif
|
||||||
|
if (qse_wcsntombsn (wcmd, &wl, QSE_NULL, &mn) <= -1) goto child_oops;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* prepare to reserve 1 more slot for the terminating '\0'
|
/* prepare to reserve 1 more slot for the terminating '\0'
|
||||||
@ -887,16 +897,16 @@ int qse_pio_init (
|
|||||||
if (oflags & QSE_PIO_SHELL)
|
if (oflags & QSE_PIO_SHELL)
|
||||||
{
|
{
|
||||||
/* qse_wcstombs() should succeed as
|
/* qse_wcstombs() should succeed as
|
||||||
* qse_wcstombslen() was successful above */
|
* it was successful above */
|
||||||
qse_wcstombs (cmd, mcmd, &mn);
|
qse_wcstombs (cmd, &wl, mcmd, &mn);
|
||||||
/* qse_wcstombs() null-terminate mcmd */
|
/* qse_wcstombs() null-terminate mcmd */
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
QSE_ASSERT (wcmd != QSE_NULL);
|
QSE_ASSERT (wcmd != QSE_NULL);
|
||||||
/* qse_wcsntombsn() should succeed as
|
/* qse_wcsntombsn() should succeed as
|
||||||
* qse_wcsntombsnlen() was successful above */
|
* it was was successful above */
|
||||||
qse_wcsntombsn (wcmd, wl, mcmd, &mn);
|
qse_wcsntombsn (wcmd, &wl, mcmd, &mn);
|
||||||
/* qse_wcsntombsn() doesn't null-terminate mcmd */
|
/* qse_wcsntombsn() doesn't null-terminate mcmd */
|
||||||
mcmd[mn] = QSE_MT('\0');
|
mcmd[mn] = QSE_MT('\0');
|
||||||
}
|
}
|
||||||
|
@ -138,306 +138,134 @@ qse_ulong_t qse_strxtoulong (const qse_char_t* str, qse_size_t len)
|
|||||||
/*
|
/*
|
||||||
* TODO: fix wrong mbstate handling
|
* TODO: fix wrong mbstate handling
|
||||||
*/
|
*/
|
||||||
qse_size_t qse_mbstowcslen (const qse_mchar_t* mcs, qse_size_t* wcslen)
|
|
||||||
{
|
|
||||||
qse_wchar_t wc;
|
|
||||||
qse_size_t n, ml, wl = 0;
|
|
||||||
const qse_mchar_t* p = mcs;
|
|
||||||
qse_mbstate_t state = {{ 0, }};
|
|
||||||
|
|
||||||
while (*p != '\0') p++;
|
|
||||||
ml = p - mcs;
|
|
||||||
|
|
||||||
for (p = mcs; ml > 0; p += n, ml -= n)
|
int qse_mbstowcs (
|
||||||
{
|
const qse_mchar_t* mbs, qse_size_t* mbslen,
|
||||||
n = qse_mbrtowc (p, ml, &wc, &state);
|
qse_wchar_t* wcs, qse_size_t* wcslen)
|
||||||
/* insufficient input or wrong sequence */
|
{
|
||||||
if (n == 0 || n > ml) break;
|
const qse_mchar_t* mp;
|
||||||
wl++;
|
qse_size_t mlen, wlen;
|
||||||
}
|
int n;
|
||||||
|
|
||||||
if (wcslen) *wcslen = wl;
|
for (mp = mbs; *mp != QSE_MT('\0'); mp++);
|
||||||
return p - mcs;
|
|
||||||
}
|
mlen = mp - mbs; wlen = *wcslen;
|
||||||
|
n = qse_mbsntowcsn (mbs, &mlen, wcs, &wlen);
|
||||||
qse_size_t qse_mbsntowcsnlen (
|
if (wcs)
|
||||||
const qse_mchar_t* mcs, qse_size_t mcslen, qse_size_t* wcslen)
|
{
|
||||||
{
|
if (wlen < *wcslen) wcs[wlen] = QSE_WT('\0');
|
||||||
qse_wchar_t wc;
|
else n = -2; /* buffer too small */
|
||||||
qse_size_t n, ml = mcslen, wl = 0;
|
}
|
||||||
const qse_mchar_t* p = mcs;
|
*mbslen = mlen; *wcslen = wlen;
|
||||||
qse_mbstate_t state = {{ 0, }};
|
|
||||||
|
return n;
|
||||||
for (p = mcs; ml > 0; p += n, ml -= n)
|
}
|
||||||
{
|
|
||||||
n = qse_mbrtowc (p, ml, &wc, &state);
|
int qse_mbsntowcsn (
|
||||||
/* insufficient or invalid sequence */
|
const qse_mchar_t* mbs, qse_size_t* mbslen,
|
||||||
if (n == 0 || n > ml) break;
|
|
||||||
wl++;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (wcslen) *wcslen = wl;
|
|
||||||
return mcslen - ml;
|
|
||||||
}
|
|
||||||
|
|
||||||
qse_size_t qse_mbstowcs (
|
|
||||||
const qse_mchar_t* mbs, qse_wchar_t* wcs, qse_size_t* wcslen)
|
|
||||||
{
|
|
||||||
qse_size_t wlen, mlen;
|
|
||||||
const qse_mchar_t* mp;
|
|
||||||
|
|
||||||
/* get the length of mbs and pass it to qse_mbsntowcsn as
|
|
||||||
* qse_mbtowc called by qse_mbsntowcsn needs it. */
|
|
||||||
wlen = *wcslen;
|
|
||||||
if (wlen <= 0)
|
|
||||||
{
|
|
||||||
/* buffer too small. also cannot null-terminate it */
|
|
||||||
*wcslen = 0;
|
|
||||||
return 0; /* 0 byte processed */
|
|
||||||
}
|
|
||||||
|
|
||||||
for (mp = mbs; *mp != QSE_MT('\0'); mp++);
|
|
||||||
mlen = qse_mbsntowcsn (mbs, mp - mbs, wcs, &wlen);
|
|
||||||
if (wlen < *wcslen)
|
|
||||||
{
|
|
||||||
/* null-terminate wcs if it is large enough. */
|
|
||||||
wcs[wlen] = QSE_WT('\0');
|
|
||||||
}
|
|
||||||
|
|
||||||
/* if null-terminated properly, the input wcslen must be less than
|
|
||||||
* the output wcslen. (input length includes the terminating null
|
|
||||||
* while the output length excludes the terminating null) */
|
|
||||||
*wcslen = wlen;
|
|
||||||
return mlen;
|
|
||||||
}
|
|
||||||
|
|
||||||
qse_size_t qse_mbsntowcsn (
|
|
||||||
const qse_mchar_t* mbs, qse_size_t mbslen,
|
|
||||||
qse_wchar_t* wcs, qse_size_t* wcslen)
|
qse_wchar_t* wcs, qse_size_t* wcslen)
|
||||||
{
|
{
|
||||||
qse_size_t mlen = mbslen, n;
|
|
||||||
const qse_mchar_t* p;
|
const qse_mchar_t* p;
|
||||||
qse_wchar_t* q, * qend ;
|
|
||||||
qse_mbstate_t state = {{ 0, }};
|
qse_mbstate_t state = {{ 0, }};
|
||||||
|
int ret = 0;
|
||||||
|
qse_size_t mlen;
|
||||||
|
|
||||||
|
if (wcs)
|
||||||
|
{
|
||||||
|
qse_wchar_t* q, * qend;
|
||||||
|
|
||||||
|
p = mbs;
|
||||||
|
q = wcs;
|
||||||
qend = wcs + *wcslen;
|
qend = wcs + *wcslen;
|
||||||
|
mlen = *mbslen;
|
||||||
|
|
||||||
for (p = mbs, q = wcs; mlen > 0 && q < qend; p += n, mlen -= n)
|
while (mlen > 0)
|
||||||
{
|
{
|
||||||
|
qse_size_t n;
|
||||||
|
|
||||||
|
if (q >= qend)
|
||||||
|
{
|
||||||
|
/* buffer too small */
|
||||||
|
ret = -2;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
n = qse_mbrtowc (p, mlen, q, &state);
|
n = qse_mbrtowc (p, mlen, q, &state);
|
||||||
if (n == 0 || n > mlen)
|
if (n == 0)
|
||||||
{
|
{
|
||||||
/* wrong sequence or insufficient input */
|
/* invalid sequence */
|
||||||
|
ret = -1;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (n > mlen)
|
||||||
|
{
|
||||||
|
/* incomplete sequence */
|
||||||
|
ret = -3;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
q++;
|
q++;
|
||||||
|
p += n;
|
||||||
|
mlen -= n;
|
||||||
}
|
}
|
||||||
|
|
||||||
*wcslen = q - wcs;
|
*wcslen = q - wcs;
|
||||||
return p - mbs; /* returns the number of bytes processed */
|
*mbslen = p - mbs;
|
||||||
}
|
|
||||||
|
|
||||||
qse_size_t qse_wcstombslen (const qse_wchar_t* wcs, qse_size_t* mbslen)
|
|
||||||
{
|
|
||||||
const qse_wchar_t* p = wcs;
|
|
||||||
qse_mchar_t mbs[QSE_MBLEN_MAX];
|
|
||||||
qse_size_t mlen = 0;
|
|
||||||
qse_mbstate_t state = {{ 0, }};
|
|
||||||
|
|
||||||
while (*p != QSE_WT('\0'))
|
|
||||||
{
|
|
||||||
qse_size_t n = qse_wcrtomb (*p, mbs, QSE_COUNTOF(mbs), &state);
|
|
||||||
if (n == 0) break; /* illegal character */
|
|
||||||
|
|
||||||
/* it assumes that mbs is large enough to hold a character */
|
|
||||||
QSE_ASSERT (n <= QSE_COUNTOF(mbs));
|
|
||||||
|
|
||||||
p++; mlen += n;
|
|
||||||
}
|
}
|
||||||
|
else
|
||||||
/* this length holds the number of resulting multi-byte characters
|
|
||||||
* excluding the terminating null character */
|
|
||||||
*mbslen = mlen;
|
|
||||||
|
|
||||||
/* returns the number of characters handled.
|
|
||||||
* if the function has encountered an illegal character in
|
|
||||||
* the while loop above, wcs[p-wcs] will not be a null character */
|
|
||||||
return p - wcs;
|
|
||||||
}
|
|
||||||
|
|
||||||
qse_size_t qse_wcsntombsnlen (
|
|
||||||
const qse_wchar_t* wcs, qse_size_t wcslen, qse_size_t* mbslen)
|
|
||||||
{
|
|
||||||
const qse_wchar_t* p = wcs;
|
|
||||||
const qse_wchar_t* end = wcs + wcslen;
|
|
||||||
qse_mchar_t mbs[QSE_MBLEN_MAX];
|
|
||||||
qse_size_t mlen = 0;
|
|
||||||
qse_mbstate_t state = {{ 0, }};
|
|
||||||
|
|
||||||
while (p < end)
|
|
||||||
{
|
{
|
||||||
qse_size_t n = qse_wcrtomb (*p, mbs, QSE_COUNTOF(mbs), &state);
|
qse_wchar_t w;
|
||||||
if (n == 0) break; /* illegal character */
|
qse_size_t wlen = 0;
|
||||||
|
|
||||||
/* it assumes that mbs is large enough to hold a character */
|
p = mbs;
|
||||||
QSE_ASSERT (n <= QSE_COUNTOF(mbs));
|
mlen = *mbslen;
|
||||||
|
|
||||||
p++; mlen += n;
|
while (mlen > 0)
|
||||||
|
{
|
||||||
|
qse_size_t n;
|
||||||
|
|
||||||
|
n = qse_mbrtowc (p, mlen, &w, &state);
|
||||||
|
if (n == 0)
|
||||||
|
{
|
||||||
|
/* invalid sequence */
|
||||||
|
ret = -1;
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
if (n > mlen)
|
||||||
/* this length excludes the terminating null character.
|
|
||||||
* this function doesn't event null-terminate the result. */
|
|
||||||
*mbslen = mlen;
|
|
||||||
|
|
||||||
/* returns the number of characters handled.
|
|
||||||
* if the function has encountered an illegal character in
|
|
||||||
* the while loop above, wcs[p-wcs] will not be a null character */
|
|
||||||
return p - wcs;
|
|
||||||
}
|
|
||||||
|
|
||||||
qse_size_t qse_wcstombs (
|
|
||||||
const qse_wchar_t* wcs, qse_mchar_t* mbs, qse_size_t* mbslen)
|
|
||||||
{
|
|
||||||
const qse_wchar_t* p = wcs;
|
|
||||||
qse_size_t rem = *mbslen;
|
|
||||||
qse_mbstate_t state = {{ 0, }};
|
|
||||||
|
|
||||||
while (*p != QSE_WT('\0') && rem > 0)
|
|
||||||
{
|
{
|
||||||
qse_size_t n = qse_wcrtomb (*p, mbs, rem, &state);
|
/* incomplete sequence */
|
||||||
if (n == 0 || n > rem)
|
ret = -3;
|
||||||
{
|
|
||||||
/* illegal character or buffer not enough */
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
mbs += n; rem -= n; p++;
|
p += n;
|
||||||
|
mlen -= n;
|
||||||
|
wlen += 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* update mbslen to the length of the mbs string converted excluding
|
*wcslen = wlen;
|
||||||
* terminating null */
|
*mbslen = p - mbs;
|
||||||
*mbslen -= rem;
|
|
||||||
|
|
||||||
/* null-terminate the multibyte sequence if it has sufficient space */
|
|
||||||
if (rem > 0) *mbs = QSE_MT('\0');
|
|
||||||
|
|
||||||
/* returns the number of characters handled. */
|
|
||||||
return p - wcs;
|
|
||||||
}
|
|
||||||
|
|
||||||
qse_size_t qse_wcsntombsn (
|
|
||||||
const qse_wchar_t* wcs, qse_size_t wcslen,
|
|
||||||
qse_mchar_t* mbs, qse_size_t* mbslen)
|
|
||||||
{
|
|
||||||
const qse_wchar_t* p = wcs;
|
|
||||||
const qse_wchar_t* end = wcs + wcslen;
|
|
||||||
qse_size_t len = *mbslen;
|
|
||||||
qse_mbstate_t state = {{ 0, }};
|
|
||||||
|
|
||||||
while (p < end && len > 0)
|
|
||||||
{
|
|
||||||
qse_size_t n = qse_wcrtomb (*p, mbs, len, &state);
|
|
||||||
if (n == 0 || n > len)
|
|
||||||
{
|
|
||||||
/* illegal character or buffer not enough */
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
mbs += n; len -= n; p++;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
*mbslen -= len;
|
return ret;
|
||||||
|
|
||||||
/* returns the number of characters handled.
|
|
||||||
* the caller can check if the return value is as large is wcslen
|
|
||||||
* for an error. */
|
|
||||||
return p - wcs;
|
|
||||||
}
|
|
||||||
|
|
||||||
int qse_mbstowcsrigid (
|
|
||||||
const qse_mchar_t* mbs, qse_wchar_t* wcs, qse_size_t wcslen)
|
|
||||||
{
|
|
||||||
/* no truncation is allowed in this function for any reasons */
|
|
||||||
qse_size_t n;
|
|
||||||
qse_size_t wn = wcslen;
|
|
||||||
|
|
||||||
n = qse_mbstowcs (mbs, wcs, &wn);
|
|
||||||
if (mbs[n] != QSE_MT('\0'))
|
|
||||||
{
|
|
||||||
/* incomplete sequence or invalid sequence */
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
if (wn >= wcslen)
|
|
||||||
{
|
|
||||||
/* wcs not big enough to be null-terminated.
|
|
||||||
* if it has been null-terminated properly,
|
|
||||||
* wn should be less than wcslen. */
|
|
||||||
return -2;
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
int qse_wcstombsrigid (
|
|
||||||
const qse_wchar_t* wcs, qse_mchar_t* mbs, qse_size_t mbslen)
|
|
||||||
{
|
|
||||||
/* no truncation is allowed in this function for any reasons */
|
|
||||||
qse_size_t n;
|
|
||||||
qse_size_t mn = mbslen;
|
|
||||||
|
|
||||||
n = qse_wcstombs (wcs, mbs, &mn);
|
|
||||||
if (wcs[n] != QSE_WT('\0'))
|
|
||||||
{
|
|
||||||
/* if qse_wcstombs() processed all wide characters,
|
|
||||||
* the character at position 'n' should be a null character
|
|
||||||
* as 'n' is the number of wide characters processed. */
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
if (mn >= mbslen)
|
|
||||||
{
|
|
||||||
/* mbs not big enough to be null-terminated.
|
|
||||||
* if it has been null-terminated properly,
|
|
||||||
* mn should be less than mbslen. */
|
|
||||||
return -2;
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
qse_wchar_t* qse_mbstowcsdup (const qse_mchar_t* mbs, qse_mmgr_t* mmgr)
|
qse_wchar_t* qse_mbstowcsdup (const qse_mchar_t* mbs, qse_mmgr_t* mmgr)
|
||||||
{
|
{
|
||||||
qse_size_t n, req;
|
qse_size_t mbslen, wcslen;
|
||||||
qse_wchar_t* wcs;
|
qse_wchar_t* wcs;
|
||||||
|
|
||||||
n = qse_mbstowcslen (mbs, &req);
|
if (qse_mbstowcs (mbs, &mbslen, QSE_NULL, &wcslen) <= -1) return QSE_NULL;
|
||||||
if (mbs[n] != QSE_WT('\0')) return QSE_NULL;
|
|
||||||
|
|
||||||
req++;
|
wcslen++; /* for terminating null */
|
||||||
|
wcs = QSE_MMGR_ALLOC (mmgr, wcslen * QSE_SIZEOF(*wcs));
|
||||||
wcs = QSE_MMGR_ALLOC (mmgr, req * QSE_SIZEOF(*wcs));
|
|
||||||
if (wcs == QSE_NULL) return QSE_NULL;
|
if (wcs == QSE_NULL) return QSE_NULL;
|
||||||
|
|
||||||
qse_mbstowcs (mbs, wcs, &req);
|
qse_mbstowcs (mbs, &mbslen, wcs, &wcslen);
|
||||||
return wcs;
|
return wcs;
|
||||||
}
|
}
|
||||||
|
|
||||||
qse_mchar_t* qse_wcstombsdup (const qse_wchar_t* wcs, qse_mmgr_t* mmgr)
|
|
||||||
{
|
|
||||||
qse_size_t n, req;
|
|
||||||
qse_mchar_t* mbs;
|
|
||||||
|
|
||||||
n = qse_wcstombslen (wcs, &req);
|
|
||||||
if (wcs[n] != QSE_WT('\0')) return QSE_NULL;
|
|
||||||
|
|
||||||
req++;
|
|
||||||
|
|
||||||
mbs = QSE_MMGR_ALLOC (mmgr, req * QSE_SIZEOF(*mbs));
|
|
||||||
if (mbs == QSE_NULL) return QSE_NULL;
|
|
||||||
|
|
||||||
qse_wcstombs (wcs, mbs, &req);
|
|
||||||
return mbs;
|
|
||||||
}
|
|
||||||
|
|
||||||
qse_wchar_t* qse_mbsatowcsdup (const qse_mchar_t* mbs[], qse_mmgr_t* mmgr)
|
qse_wchar_t* qse_mbsatowcsdup (const qse_mchar_t* mbs[], qse_mmgr_t* mmgr)
|
||||||
{
|
{
|
||||||
qse_wchar_t* buf, * ptr;
|
qse_wchar_t* buf, * ptr;
|
||||||
@ -449,8 +277,7 @@ qse_wchar_t* qse_mbsatowcsdup (const qse_mchar_t* mbs[], qse_mmgr_t* mmgr)
|
|||||||
|
|
||||||
for (i = 0; mbs[i]; i++)
|
for (i = 0; mbs[i]; i++)
|
||||||
{
|
{
|
||||||
ml = qse_mbstowcslen(mbs[i], &wl);
|
if (qse_mbstowcs(mbs[i], &ml, QSE_NULL, &wl) <= -1) return QSE_NULL;
|
||||||
if (mbs[i][ml] != QSE_MT('\0')) return QSE_NULL;
|
|
||||||
capa += wl;
|
capa += wl;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -462,7 +289,7 @@ qse_wchar_t* qse_mbsatowcsdup (const qse_mchar_t* mbs[], qse_mmgr_t* mmgr)
|
|||||||
for (i = 0; mbs[i]; i++)
|
for (i = 0; mbs[i]; i++)
|
||||||
{
|
{
|
||||||
wl = capa + 1;
|
wl = capa + 1;
|
||||||
ml = qse_mbstowcs (mbs[i], ptr, &wl);
|
qse_mbstowcs (mbs[i], &ml, ptr, &wl);
|
||||||
ptr += wl;
|
ptr += wl;
|
||||||
capa -= wl;
|
capa -= wl;
|
||||||
}
|
}
|
||||||
@ -470,19 +297,188 @@ qse_wchar_t* qse_mbsatowcsdup (const qse_mchar_t* mbs[], qse_mmgr_t* mmgr)
|
|||||||
return buf;
|
return buf;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int qse_wcstombs (
|
||||||
|
const qse_wchar_t* wcs, qse_size_t* wcslen,
|
||||||
|
qse_mchar_t* mbs, qse_size_t* mbslen)
|
||||||
|
{
|
||||||
|
const qse_wchar_t* p = wcs;
|
||||||
|
qse_mbstate_t state = {{ 0, }};
|
||||||
|
int ret = 0;
|
||||||
|
|
||||||
|
if (mbs)
|
||||||
|
{
|
||||||
|
qse_size_t rem = *mbslen;
|
||||||
|
|
||||||
|
while (*p != QSE_WT('\0'))
|
||||||
|
{
|
||||||
|
qse_size_t n;
|
||||||
|
|
||||||
|
if (rem <= 0)
|
||||||
|
{
|
||||||
|
ret = -2;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
n = qse_wcrtomb (*p, mbs, rem, &state);
|
||||||
|
if (n == 0)
|
||||||
|
{
|
||||||
|
ret = -1;
|
||||||
|
break; /* illegal character */
|
||||||
|
}
|
||||||
|
if (n > rem)
|
||||||
|
{
|
||||||
|
ret = -2;
|
||||||
|
break; /* buffer too small */
|
||||||
|
}
|
||||||
|
|
||||||
|
mbs += n; rem -= n; p++;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* update mbslen to the length of the mbs string converted excluding
|
||||||
|
* terminating null */
|
||||||
|
*mbslen -= rem;
|
||||||
|
|
||||||
|
/* null-terminate the multibyte sequence if it has sufficient space */
|
||||||
|
if (rem > 0) *mbs = QSE_MT('\0');
|
||||||
|
else
|
||||||
|
{
|
||||||
|
/* if ret is -2 and wcs[wcslen] == QSE_T('\0'),
|
||||||
|
* this means that the mbs buffer was lacking one
|
||||||
|
* slot for the terminating null */
|
||||||
|
ret = -2; /* buffer too small */
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
qse_mchar_t mbsbuf[QSE_MBLEN_MAX];
|
||||||
|
qse_size_t mlen = 0;
|
||||||
|
|
||||||
|
while (*p != QSE_WT('\0'))
|
||||||
|
{
|
||||||
|
qse_size_t n;
|
||||||
|
|
||||||
|
n = qse_wcrtomb (*p, mbsbuf, QSE_COUNTOF(mbsbuf), &state);
|
||||||
|
if (n == 0)
|
||||||
|
{
|
||||||
|
ret = -1;
|
||||||
|
break; /* illegal character */
|
||||||
|
}
|
||||||
|
|
||||||
|
/* it assumes that mbs is large enough to hold a character */
|
||||||
|
QSE_ASSERT (n <= QSE_COUNTOF(mbs));
|
||||||
|
|
||||||
|
p++; mlen += n;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* this length holds the number of resulting multi-byte characters
|
||||||
|
* excluding the terminating null character */
|
||||||
|
*mbslen = mlen;
|
||||||
|
}
|
||||||
|
|
||||||
|
*wcslen = p - wcs; /* the number of wide characters handled. */
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
int qse_wcsntombsn (
|
||||||
|
const qse_wchar_t* wcs, qse_size_t* wcslen,
|
||||||
|
qse_mchar_t* mbs, qse_size_t* mbslen)
|
||||||
|
{
|
||||||
|
const qse_wchar_t* p = wcs;
|
||||||
|
const qse_wchar_t* end = wcs + *wcslen;
|
||||||
|
qse_mbstate_t state = {{ 0, }};
|
||||||
|
int ret = 0;
|
||||||
|
|
||||||
|
if (mbs)
|
||||||
|
{
|
||||||
|
qse_size_t rem = *mbslen;
|
||||||
|
|
||||||
|
while (p < end)
|
||||||
|
{
|
||||||
|
qse_size_t n;
|
||||||
|
|
||||||
|
if (rem <= 0)
|
||||||
|
{
|
||||||
|
ret = -2; /* buffer too small */
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
n = qse_wcrtomb (*p, mbs, rem, &state);
|
||||||
|
if (n == 0)
|
||||||
|
{
|
||||||
|
ret = -1;
|
||||||
|
break; /* illegal character */
|
||||||
|
}
|
||||||
|
if (n > rem)
|
||||||
|
{
|
||||||
|
ret = -2; /* buffer too small */
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
mbs += n; rem -= n; p++;
|
||||||
|
}
|
||||||
|
|
||||||
|
*mbslen -= rem;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
qse_mchar_t mbsbuf[QSE_MBLEN_MAX];
|
||||||
|
qse_size_t mlen = 0;
|
||||||
|
|
||||||
|
while (p < end)
|
||||||
|
{
|
||||||
|
qse_size_t n;
|
||||||
|
|
||||||
|
n = qse_wcrtomb (*p, mbs, QSE_COUNTOF(mbsbuf), &state);
|
||||||
|
if (n == 0)
|
||||||
|
{
|
||||||
|
ret = -1;
|
||||||
|
break; /* illegal character */
|
||||||
|
}
|
||||||
|
|
||||||
|
/* it assumes that mbs is large enough to hold a character */
|
||||||
|
QSE_ASSERT (n <= QSE_COUNTOF(mbsbuf));
|
||||||
|
|
||||||
|
p++; mlen += n;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* this length excludes the terminating null character.
|
||||||
|
* this function doesn't event null-terminate the result. */
|
||||||
|
*mbslen = mlen;
|
||||||
|
}
|
||||||
|
|
||||||
|
*wcslen = p - wcs;
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
qse_mchar_t* qse_wcstombsdup (const qse_wchar_t* wcs, qse_mmgr_t* mmgr)
|
||||||
|
{
|
||||||
|
qse_size_t wcslen, mbslen;
|
||||||
|
qse_mchar_t* mbs;
|
||||||
|
|
||||||
|
if (qse_wcstombs (wcs, &wcslen, QSE_NULL, &mbslen) <= -1) return QSE_NULL;
|
||||||
|
|
||||||
|
mbslen++; /* for the terminating null character */
|
||||||
|
|
||||||
|
mbs = QSE_MMGR_ALLOC (mmgr, mbslen * QSE_SIZEOF(*mbs));
|
||||||
|
if (mbs == QSE_NULL) return QSE_NULL;
|
||||||
|
|
||||||
|
qse_wcstombs (wcs, &wcslen, mbs, &mbslen);
|
||||||
|
return mbs;
|
||||||
|
}
|
||||||
|
|
||||||
qse_mchar_t* qse_wcsatombsdup (const qse_wchar_t* wcs[], qse_mmgr_t* mmgr)
|
qse_mchar_t* qse_wcsatombsdup (const qse_wchar_t* wcs[], qse_mmgr_t* mmgr)
|
||||||
{
|
{
|
||||||
qse_mchar_t* buf, * ptr;
|
qse_mchar_t* buf, * ptr;
|
||||||
qse_size_t i;
|
qse_size_t i;
|
||||||
qse_size_t capa = 0;
|
|
||||||
qse_size_t wl, ml;
|
qse_size_t wl, ml;
|
||||||
|
qse_size_t capa = 0;
|
||||||
|
|
||||||
QSE_ASSERT (mmgr != QSE_NULL);
|
QSE_ASSERT (mmgr != QSE_NULL);
|
||||||
|
|
||||||
for (i = 0; wcs[i]; i++)
|
for (i = 0; wcs[i]; i++)
|
||||||
{
|
{
|
||||||
wl = qse_wcstombslen(wcs[i], &ml);
|
if (qse_wcstombs (wcs[i], &wl, QSE_NULL, &ml) <= -1) return QSE_NULL;
|
||||||
if (wcs[i][wl] != QSE_WT('\0')) return QSE_NULL;
|
|
||||||
capa += ml;
|
capa += ml;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -494,7 +490,7 @@ qse_mchar_t* qse_wcsatombsdup (const qse_wchar_t* wcs[], qse_mmgr_t* mmgr)
|
|||||||
for (i = 0; wcs[i]; i++)
|
for (i = 0; wcs[i]; i++)
|
||||||
{
|
{
|
||||||
ml = capa + 1;
|
ml = capa + 1;
|
||||||
wl = qse_wcstombs (wcs[i], ptr, &ml);
|
qse_wcstombs (wcs[i], &wl, ptr, &ml);
|
||||||
ptr += ml;
|
ptr += ml;
|
||||||
capa -= ml;
|
capa -= ml;
|
||||||
}
|
}
|
||||||
|
@ -101,6 +101,7 @@ qse_ssize_t qse_tio_write (qse_tio_t* tio, const qse_char_t* str, qse_size_t siz
|
|||||||
|
|
||||||
if (size == (qse_size_t)-1)
|
if (size == (qse_size_t)-1)
|
||||||
{
|
{
|
||||||
|
/* TODO: should not write more than than QSE_TYPE_MAX(qse_ssize_t) */
|
||||||
while (*p != QSE_T('\0'))
|
while (*p != QSE_T('\0'))
|
||||||
{
|
{
|
||||||
n = tio_putc (tio, *p, &flush_needed);
|
n = tio_putc (tio, *p, &flush_needed);
|
||||||
@ -111,7 +112,10 @@ qse_ssize_t qse_tio_write (qse_tio_t* tio, const qse_char_t* str, qse_size_t siz
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
const qse_char_t* end = str + size;
|
const qse_char_t* end;
|
||||||
|
|
||||||
|
/* TODO: size should not be longer than QSE_TYPE_MAX(qse_ssize_t) */
|
||||||
|
end = str + size;
|
||||||
while (p < end)
|
while (p < end)
|
||||||
{
|
{
|
||||||
n = tio_putc (tio, *p, &flush_needed);
|
n = tio_putc (tio, *p, &flush_needed);
|
||||||
@ -125,3 +129,84 @@ qse_ssize_t qse_tio_write (qse_tio_t* tio, const qse_char_t* str, qse_size_t siz
|
|||||||
return p - str;
|
return p - str;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
qse_ssize_t qse_tio_writemstr (
|
||||||
|
qse_tio_t* tio, const qse_mchar_t* mptr, qse_size_t mlen)
|
||||||
|
{
|
||||||
|
const qse_mchar_t* xptr, * xend;
|
||||||
|
qse_size_t capa;
|
||||||
|
int nl = 0;
|
||||||
|
|
||||||
|
if (tio->outbuf_len >= QSE_COUNTOF(tio->outbuf))
|
||||||
|
{
|
||||||
|
/* maybe, previous flush operation has failed a few
|
||||||
|
* times previously. so the buffer is full.
|
||||||
|
*/
|
||||||
|
tio->errnum = QSE_TIO_ENOSPC;
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* adjust mlen for the type difference between the parameter
|
||||||
|
* and the return value */
|
||||||
|
if (mlen > QSE_TYPE_MAX(qse_ssize_t)) mlen = QSE_TYPE_MAX(qse_ssize_t);
|
||||||
|
|
||||||
|
/* handle the parts that can't fit into the internal buffer */
|
||||||
|
while (mlen >= (capa = QSE_COUNTOF(tio->outbuf) - tio->outbuf_len))
|
||||||
|
{
|
||||||
|
for (xend = xptr + capa; xptr < xend; xptr++)
|
||||||
|
tio->outbuf[tio->outbuf_len++] = *xptr;
|
||||||
|
if (qse_tio_flush (tio) == -1) return -1;
|
||||||
|
mlen -= capa;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* handle the last part that can fit into the internal buffer */
|
||||||
|
for (xend = xptr + mlen; xptr < xend; xptr++)
|
||||||
|
{
|
||||||
|
/* TODO: support different line terminating characeter */
|
||||||
|
if (*xptr == QSE_MT('\n')) nl = 1;
|
||||||
|
tio->outbuf[tio->outbuf_len++] = *xptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* if the last part contains a new line, flush the internal
|
||||||
|
* buffer. note that this flushes characters after nl also.*/
|
||||||
|
if (nl && qse_tio_flush (tio) == -1) return -1;
|
||||||
|
|
||||||
|
/* returns the number multi-bytes characters handled */
|
||||||
|
return xptr - mptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
#if 0
|
||||||
|
qse_ssize_t qse_tio_writewstr (
|
||||||
|
qse_tio_t* tio, const qse_wchar_t* wptr, qse_size_t wlen)
|
||||||
|
{
|
||||||
|
|
||||||
|
if (wlen > QSE_TYPE_MAX(qse_ssize_t)) wlen = QSE_TYPE_MAX(qse_ssize_t);
|
||||||
|
|
||||||
|
while (1)
|
||||||
|
{
|
||||||
|
qse_size_t capa, mcnt, wcnt;
|
||||||
|
|
||||||
|
capa = QSE_COUNTOF(tio->outbuf) - tio->outbuf_len;
|
||||||
|
mcnt = capa;
|
||||||
|
wcnt = qse_wcsntombsn (wptr, wlen, &tio->outbuf[tio->outbuf_len], &mcnt);
|
||||||
|
|
||||||
|
if (wcnt == wlen)
|
||||||
|
{
|
||||||
|
/* process the all*/
|
||||||
|
}
|
||||||
|
|
||||||
|
if (mcnt >= capa)
|
||||||
|
{
|
||||||
|
/* wcsntombsn doesn't null-terminate the result. * /
|
||||||
|
/* buffer is not large enough. flush first before continuing */
|
||||||
|
if (qse_tio_flush (tio) == -1) return -1;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
else if (wcnt != wlen)
|
||||||
|
{
|
||||||
|
/* invalid wide-character is included. */
|
||||||
|
if (tio->flags & QSE_TIO_IGNOREMBWCERR)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
@ -6,7 +6,7 @@ AM_CPPFLAGS = \
|
|||||||
-I$(includedir)
|
-I$(includedir)
|
||||||
|
|
||||||
|
|
||||||
bin_PROGRAMS = xma fma pma chr str sll dll lda oht htb rbt fio01 fio02 pio sio time main main2 rex01 env path01 tre01 fmt01 fmt02 fs01
|
bin_PROGRAMS = xma fma pma chr str01 str02 str03 sll dll lda oht htb rbt fio01 fio02 pio sio time main main2 rex01 env path01 tre01 fmt01 fmt02 fs01
|
||||||
|
|
||||||
LDFLAGS = -L../../lib/cmn
|
LDFLAGS = -L../../lib/cmn
|
||||||
LDADD = -lqsecmn
|
LDADD = -lqsecmn
|
||||||
@ -15,7 +15,9 @@ xma_SOURCES = xma.c
|
|||||||
fma_SOURCES = fma.c
|
fma_SOURCES = fma.c
|
||||||
pma_SOURCES = pma.c
|
pma_SOURCES = pma.c
|
||||||
chr_SOURCES = chr.c
|
chr_SOURCES = chr.c
|
||||||
str_SOURCES = str.c
|
str01_SOURCES = str01.c
|
||||||
|
str02_SOURCES = str02.c
|
||||||
|
str03_SOURCES = str03.c
|
||||||
sll_SOURCES = sll.c
|
sll_SOURCES = sll.c
|
||||||
dll_SOURCES = dll.c
|
dll_SOURCES = dll.c
|
||||||
lda_SOURCES = lda.c
|
lda_SOURCES = lda.c
|
||||||
|
@ -35,12 +35,12 @@ POST_UNINSTALL = :
|
|||||||
build_triplet = @build@
|
build_triplet = @build@
|
||||||
host_triplet = @host@
|
host_triplet = @host@
|
||||||
bin_PROGRAMS = xma$(EXEEXT) fma$(EXEEXT) pma$(EXEEXT) chr$(EXEEXT) \
|
bin_PROGRAMS = xma$(EXEEXT) fma$(EXEEXT) pma$(EXEEXT) chr$(EXEEXT) \
|
||||||
str$(EXEEXT) sll$(EXEEXT) dll$(EXEEXT) lda$(EXEEXT) \
|
str01$(EXEEXT) str02$(EXEEXT) str03$(EXEEXT) sll$(EXEEXT) \
|
||||||
oht$(EXEEXT) htb$(EXEEXT) rbt$(EXEEXT) fio01$(EXEEXT) \
|
dll$(EXEEXT) lda$(EXEEXT) oht$(EXEEXT) htb$(EXEEXT) \
|
||||||
fio02$(EXEEXT) pio$(EXEEXT) sio$(EXEEXT) time$(EXEEXT) \
|
rbt$(EXEEXT) fio01$(EXEEXT) fio02$(EXEEXT) pio$(EXEEXT) \
|
||||||
main$(EXEEXT) main2$(EXEEXT) rex01$(EXEEXT) env$(EXEEXT) \
|
sio$(EXEEXT) time$(EXEEXT) main$(EXEEXT) main2$(EXEEXT) \
|
||||||
path01$(EXEEXT) tre01$(EXEEXT) fmt01$(EXEEXT) fmt02$(EXEEXT) \
|
rex01$(EXEEXT) env$(EXEEXT) path01$(EXEEXT) tre01$(EXEEXT) \
|
||||||
fs01$(EXEEXT)
|
fmt01$(EXEEXT) fmt02$(EXEEXT) fs01$(EXEEXT)
|
||||||
subdir = samples/cmn
|
subdir = samples/cmn
|
||||||
DIST_COMMON = $(srcdir)/Makefile.am $(srcdir)/Makefile.in
|
DIST_COMMON = $(srcdir)/Makefile.am $(srcdir)/Makefile.in
|
||||||
ACLOCAL_M4 = $(top_srcdir)/aclocal.m4
|
ACLOCAL_M4 = $(top_srcdir)/aclocal.m4
|
||||||
@ -141,10 +141,18 @@ am_sll_OBJECTS = sll.$(OBJEXT)
|
|||||||
sll_OBJECTS = $(am_sll_OBJECTS)
|
sll_OBJECTS = $(am_sll_OBJECTS)
|
||||||
sll_LDADD = $(LDADD)
|
sll_LDADD = $(LDADD)
|
||||||
sll_DEPENDENCIES =
|
sll_DEPENDENCIES =
|
||||||
am_str_OBJECTS = str.$(OBJEXT)
|
am_str01_OBJECTS = str01.$(OBJEXT)
|
||||||
str_OBJECTS = $(am_str_OBJECTS)
|
str01_OBJECTS = $(am_str01_OBJECTS)
|
||||||
str_LDADD = $(LDADD)
|
str01_LDADD = $(LDADD)
|
||||||
str_DEPENDENCIES =
|
str01_DEPENDENCIES =
|
||||||
|
am_str02_OBJECTS = str02.$(OBJEXT)
|
||||||
|
str02_OBJECTS = $(am_str02_OBJECTS)
|
||||||
|
str02_LDADD = $(LDADD)
|
||||||
|
str02_DEPENDENCIES =
|
||||||
|
am_str03_OBJECTS = str03.$(OBJEXT)
|
||||||
|
str03_OBJECTS = $(am_str03_OBJECTS)
|
||||||
|
str03_LDADD = $(LDADD)
|
||||||
|
str03_DEPENDENCIES =
|
||||||
am_time_OBJECTS = time.$(OBJEXT)
|
am_time_OBJECTS = time.$(OBJEXT)
|
||||||
time_OBJECTS = $(am_time_OBJECTS)
|
time_OBJECTS = $(am_time_OBJECTS)
|
||||||
time_LDADD = $(LDADD)
|
time_LDADD = $(LDADD)
|
||||||
@ -176,14 +184,16 @@ SOURCES = $(chr_SOURCES) $(dll_SOURCES) $(env_SOURCES) \
|
|||||||
$(htb_SOURCES) $(lda_SOURCES) $(main_SOURCES) $(main2_SOURCES) \
|
$(htb_SOURCES) $(lda_SOURCES) $(main_SOURCES) $(main2_SOURCES) \
|
||||||
$(oht_SOURCES) $(path01_SOURCES) $(pio_SOURCES) $(pma_SOURCES) \
|
$(oht_SOURCES) $(path01_SOURCES) $(pio_SOURCES) $(pma_SOURCES) \
|
||||||
$(rbt_SOURCES) $(rex01_SOURCES) $(sio_SOURCES) $(sll_SOURCES) \
|
$(rbt_SOURCES) $(rex01_SOURCES) $(sio_SOURCES) $(sll_SOURCES) \
|
||||||
$(str_SOURCES) $(time_SOURCES) $(tre01_SOURCES) $(xma_SOURCES)
|
$(str01_SOURCES) $(str02_SOURCES) $(str03_SOURCES) \
|
||||||
|
$(time_SOURCES) $(tre01_SOURCES) $(xma_SOURCES)
|
||||||
DIST_SOURCES = $(chr_SOURCES) $(dll_SOURCES) $(env_SOURCES) \
|
DIST_SOURCES = $(chr_SOURCES) $(dll_SOURCES) $(env_SOURCES) \
|
||||||
$(fio01_SOURCES) $(fio02_SOURCES) $(fma_SOURCES) \
|
$(fio01_SOURCES) $(fio02_SOURCES) $(fma_SOURCES) \
|
||||||
$(fmt01_SOURCES) $(fmt02_SOURCES) $(fs01_SOURCES) \
|
$(fmt01_SOURCES) $(fmt02_SOURCES) $(fs01_SOURCES) \
|
||||||
$(htb_SOURCES) $(lda_SOURCES) $(main_SOURCES) $(main2_SOURCES) \
|
$(htb_SOURCES) $(lda_SOURCES) $(main_SOURCES) $(main2_SOURCES) \
|
||||||
$(oht_SOURCES) $(path01_SOURCES) $(pio_SOURCES) $(pma_SOURCES) \
|
$(oht_SOURCES) $(path01_SOURCES) $(pio_SOURCES) $(pma_SOURCES) \
|
||||||
$(rbt_SOURCES) $(rex01_SOURCES) $(sio_SOURCES) $(sll_SOURCES) \
|
$(rbt_SOURCES) $(rex01_SOURCES) $(sio_SOURCES) $(sll_SOURCES) \
|
||||||
$(str_SOURCES) $(time_SOURCES) $(tre01_SOURCES) $(xma_SOURCES)
|
$(str01_SOURCES) $(str02_SOURCES) $(str03_SOURCES) \
|
||||||
|
$(time_SOURCES) $(tre01_SOURCES) $(xma_SOURCES)
|
||||||
ETAGS = etags
|
ETAGS = etags
|
||||||
CTAGS = ctags
|
CTAGS = ctags
|
||||||
DISTFILES = $(DIST_COMMON) $(DIST_SOURCES) $(TEXINFOS) $(EXTRA_DIST)
|
DISTFILES = $(DIST_COMMON) $(DIST_SOURCES) $(TEXINFOS) $(EXTRA_DIST)
|
||||||
@ -339,7 +349,9 @@ xma_SOURCES = xma.c
|
|||||||
fma_SOURCES = fma.c
|
fma_SOURCES = fma.c
|
||||||
pma_SOURCES = pma.c
|
pma_SOURCES = pma.c
|
||||||
chr_SOURCES = chr.c
|
chr_SOURCES = chr.c
|
||||||
str_SOURCES = str.c
|
str01_SOURCES = str01.c
|
||||||
|
str02_SOURCES = str02.c
|
||||||
|
str03_SOURCES = str03.c
|
||||||
sll_SOURCES = sll.c
|
sll_SOURCES = sll.c
|
||||||
dll_SOURCES = dll.c
|
dll_SOURCES = dll.c
|
||||||
lda_SOURCES = lda.c
|
lda_SOURCES = lda.c
|
||||||
@ -500,9 +512,15 @@ sio$(EXEEXT): $(sio_OBJECTS) $(sio_DEPENDENCIES)
|
|||||||
sll$(EXEEXT): $(sll_OBJECTS) $(sll_DEPENDENCIES)
|
sll$(EXEEXT): $(sll_OBJECTS) $(sll_DEPENDENCIES)
|
||||||
@rm -f sll$(EXEEXT)
|
@rm -f sll$(EXEEXT)
|
||||||
$(LINK) $(sll_OBJECTS) $(sll_LDADD) $(LIBS)
|
$(LINK) $(sll_OBJECTS) $(sll_LDADD) $(LIBS)
|
||||||
str$(EXEEXT): $(str_OBJECTS) $(str_DEPENDENCIES)
|
str01$(EXEEXT): $(str01_OBJECTS) $(str01_DEPENDENCIES)
|
||||||
@rm -f str$(EXEEXT)
|
@rm -f str01$(EXEEXT)
|
||||||
$(LINK) $(str_OBJECTS) $(str_LDADD) $(LIBS)
|
$(LINK) $(str01_OBJECTS) $(str01_LDADD) $(LIBS)
|
||||||
|
str02$(EXEEXT): $(str02_OBJECTS) $(str02_DEPENDENCIES)
|
||||||
|
@rm -f str02$(EXEEXT)
|
||||||
|
$(LINK) $(str02_OBJECTS) $(str02_LDADD) $(LIBS)
|
||||||
|
str03$(EXEEXT): $(str03_OBJECTS) $(str03_DEPENDENCIES)
|
||||||
|
@rm -f str03$(EXEEXT)
|
||||||
|
$(LINK) $(str03_OBJECTS) $(str03_LDADD) $(LIBS)
|
||||||
time$(EXEEXT): $(time_OBJECTS) $(time_DEPENDENCIES)
|
time$(EXEEXT): $(time_OBJECTS) $(time_DEPENDENCIES)
|
||||||
@rm -f time$(EXEEXT)
|
@rm -f time$(EXEEXT)
|
||||||
$(LINK) $(time_OBJECTS) $(time_LDADD) $(LIBS)
|
$(LINK) $(time_OBJECTS) $(time_LDADD) $(LIBS)
|
||||||
@ -540,7 +558,9 @@ distclean-compile:
|
|||||||
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/rex01.Po@am__quote@
|
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/rex01.Po@am__quote@
|
||||||
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/sio.Po@am__quote@
|
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/sio.Po@am__quote@
|
||||||
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/sll.Po@am__quote@
|
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/sll.Po@am__quote@
|
||||||
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/str.Po@am__quote@
|
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/str01.Po@am__quote@
|
||||||
|
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/str02.Po@am__quote@
|
||||||
|
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/str03.Po@am__quote@
|
||||||
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/time.Po@am__quote@
|
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/time.Po@am__quote@
|
||||||
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/tre01.Po@am__quote@
|
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/tre01.Po@am__quote@
|
||||||
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/xma.Po@am__quote@
|
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/xma.Po@am__quote@
|
||||||
|
@ -7,12 +7,12 @@ static int test_main (int argc, qse_char_t* argv[], qse_char_t* envp[])
|
|||||||
|
|
||||||
for (i = 0; i < argc; i++)
|
for (i = 0; i < argc; i++)
|
||||||
{
|
{
|
||||||
qse_printf (QSE_T("%d => [%s]\n"), i, argv[i]);
|
qse_printf (QSE_T("ARG %d => [%s]\n"), i, argv[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
for (i = 0; envp[i]; i++)
|
for (i = 0; envp[i]; i++)
|
||||||
{
|
{
|
||||||
qse_printf (QSE_T("%d => [%s]\n"), i, envp[i]);
|
qse_printf (QSE_T("ENV %d => [%s]\n"), i, envp[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -161,281 +161,6 @@ static int test4 ()
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int test5 (void)
|
|
||||||
{
|
|
||||||
int i;
|
|
||||||
const qse_mchar_t* x[] =
|
|
||||||
{
|
|
||||||
"\0\0\0",
|
|
||||||
"뛰어 올라봐. 멀리멀리 잘난척하기는",
|
|
||||||
"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 wc;
|
|
||||||
qse_wchar_t buf[10];
|
|
||||||
|
|
||||||
if (k == 0) k = 3; /* for x[0] */
|
|
||||||
|
|
||||||
qse_printf (QSE_T("["));
|
|
||||||
|
|
||||||
while (j < k)
|
|
||||||
{
|
|
||||||
qse_size_t wlen = QSE_COUNTOF(buf);
|
|
||||||
qse_size_t y = qse_mbsntowcsn (&x[i][j], k-j, buf, &wlen);
|
|
||||||
|
|
||||||
if (y <= 0)
|
|
||||||
{
|
|
||||||
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 += y;
|
|
||||||
}
|
|
||||||
|
|
||||||
qse_printf (QSE_T("] => %d bytes\n"), (int)k);
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int test6 (void)
|
|
||||||
{
|
|
||||||
int i;
|
|
||||||
const qse_mchar_t* x[] =
|
|
||||||
{
|
|
||||||
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++)
|
|
||||||
{
|
|
||||||
qse_size_t n;
|
|
||||||
qse_size_t wlen = QSE_COUNTOF(buf);
|
|
||||||
|
|
||||||
n = qse_mbstowcs (x[i], buf, &wlen);
|
|
||||||
|
|
||||||
qse_printf (QSE_T("[%hs]=>"), x[i]);
|
|
||||||
|
|
||||||
/* 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 [%ls]\n"),
|
|
||||||
((x[i][n] == QSE_WT('\0') && wlen < QSE_COUNTOF(buf))? QSE_T("FULL"): QSE_T("PARTIAL")),
|
|
||||||
(int)wlen, (int)n, buf2);
|
|
||||||
qse_fflush (QSE_STDOUT);
|
|
||||||
|
|
||||||
qse_sio_puts (QSE_SIO_OUT, QSE_T("<<"));
|
|
||||||
qse_sio_puts (QSE_SIO_OUT, buf2);
|
|
||||||
qse_sio_puts (QSE_SIO_OUT, QSE_T(">>\n"));
|
|
||||||
qse_sio_flush (QSE_SIO_OUT);
|
|
||||||
}
|
|
||||||
|
|
||||||
qse_printf (QSE_T("-----------------\n"));
|
|
||||||
|
|
||||||
for (i = 0; i < QSE_COUNTOF(x); i++)
|
|
||||||
{
|
|
||||||
qse_size_t n;
|
|
||||||
qse_size_t wlen = QSE_COUNTOF(buf2);
|
|
||||||
n = qse_mbstowcs (x[i], buf2, &wlen);
|
|
||||||
|
|
||||||
qse_printf (QSE_T("%s %d chars %d bytes [%.*ls]\n"),
|
|
||||||
((x[i][n] == QSE_WT('\0') && wlen < QSE_COUNTOF(buf))? QSE_T("FULL"): QSE_T("PARTIAL")),
|
|
||||||
(int)wlen, (int)n, (int)wlen, buf2);
|
|
||||||
|
|
||||||
qse_fflush (QSE_STDOUT);
|
|
||||||
qse_sio_puts (QSE_SIO_OUT, QSE_T("<<"));
|
|
||||||
qse_sio_puts (QSE_SIO_OUT, buf2);
|
|
||||||
qse_sio_puts (QSE_SIO_OUT, QSE_T(">>\n"));
|
|
||||||
qse_sio_flush (QSE_SIO_OUT);
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int test7 (void)
|
|
||||||
{
|
|
||||||
const qse_wchar_t* x[] =
|
|
||||||
{
|
|
||||||
L"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0",
|
|
||||||
L"\uB108 \uBB50\uAC00 \uC798\uB0AC\uC5B4?",
|
|
||||||
L"Fly to the universe, kick you ass"
|
|
||||||
};
|
|
||||||
qse_mchar_t buf[15];
|
|
||||||
int i, j;
|
|
||||||
|
|
||||||
for (i = 0; i < QSE_COUNTOF(x); i++)
|
|
||||||
{
|
|
||||||
size_t len = wcslen(x[i]);
|
|
||||||
qse_size_t n;
|
|
||||||
|
|
||||||
if (len == 0) len = 20; /* for x[0] */
|
|
||||||
|
|
||||||
qse_printf (QSE_T("["));
|
|
||||||
|
|
||||||
for (j = 0; j < len; j += n)
|
|
||||||
{
|
|
||||||
qse_size_t mlen = sizeof(buf);
|
|
||||||
n = qse_wcsntombsn (&x[i][j], len-j, buf, &mlen);
|
|
||||||
|
|
||||||
if (n <= 0)
|
|
||||||
{
|
|
||||||
qse_printf (QSE_T("***illegal character or buffer not large***"));
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (mlen > 0 && buf[0] == QSE_T('\0'))
|
|
||||||
{
|
|
||||||
while (mlen > 0 && buf[--mlen] == QSE_T('\0'))
|
|
||||||
{
|
|
||||||
qse_printf (QSE_T("\\0"));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
qse_printf (QSE_T("%.*hs"), (int)mlen, buf);
|
|
||||||
#if 0
|
|
||||||
#ifdef QSE_CHAR_IS_MCHAR
|
|
||||||
qse_printf (QSE_T("%.*s"), (int)mlen, buf);
|
|
||||||
#else
|
|
||||||
#ifdef _WIN32
|
|
||||||
qse_printf (QSE_T("%.*S"), (int)mlen, buf);
|
|
||||||
#else
|
|
||||||
/* at least on linux and macosx, wprintf seems
|
|
||||||
* to taks preceision as the number of wide
|
|
||||||
* characters with %s. */
|
|
||||||
qse_printf (QSE_T("%.*S"), n, buf);
|
|
||||||
#endif
|
|
||||||
#endif
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
qse_printf (QSE_T("] => %d chars\n"), (int)len);
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int test8 (void)
|
|
||||||
{
|
|
||||||
const qse_wchar_t* x[] =
|
|
||||||
{
|
|
||||||
L"",
|
|
||||||
L"\uB108 \uBB50\uAC00\uC798\uB0AC\uC5B4?",
|
|
||||||
L"Fly to the universe, kick you ass"
|
|
||||||
};
|
|
||||||
qse_mchar_t buf[10];
|
|
||||||
qse_mchar_t buf2[100];
|
|
||||||
int i, j;
|
|
||||||
|
|
||||||
for (i = 0; i < QSE_COUNTOF(x); i++)
|
|
||||||
{
|
|
||||||
qse_size_t n;
|
|
||||||
qse_size_t mlen = sizeof(buf);
|
|
||||||
|
|
||||||
memset (buf, QSE_MT('A'), sizeof(buf));
|
|
||||||
n = qse_wcstombs (x[i], buf, &mlen);
|
|
||||||
|
|
||||||
qse_printf (QSE_T("%s chars=%d bytes=%d [%hs]\n"),
|
|
||||||
((x[i][n] == QSE_WT('\0') && mlen < sizeof(buf))? QSE_T("FULL"): QSE_T("PARTIAL")),
|
|
||||||
(int)n, (int)mlen, buf);
|
|
||||||
}
|
|
||||||
|
|
||||||
qse_printf (QSE_T("-----------------\n"));
|
|
||||||
for (i = 0; i < QSE_COUNTOF(x); i++)
|
|
||||||
{
|
|
||||||
qse_size_t n;
|
|
||||||
qse_size_t mlen = sizeof(buf2);
|
|
||||||
|
|
||||||
memset (buf2, 'A', sizeof(buf2));
|
|
||||||
n = qse_wcstombs (x[i], buf2, &mlen);
|
|
||||||
|
|
||||||
qse_printf (QSE_T("%s chars=%d bytes=%d [%hs]\n"),
|
|
||||||
((x[i][n] == QSE_WT('\0') && mlen < sizeof(buf2))? QSE_T("FULL"): QSE_T("PARTIAL")),
|
|
||||||
(int)n, (int)mlen, buf2);
|
|
||||||
}
|
|
||||||
|
|
||||||
qse_printf (QSE_T("-----------------\n"));
|
|
||||||
for (i = 0; i < QSE_COUNTOF(x); i++)
|
|
||||||
{
|
|
||||||
qse_size_t n;
|
|
||||||
const qse_wchar_t* p = x[i];
|
|
||||||
|
|
||||||
while (1)
|
|
||||||
{
|
|
||||||
qse_size_t mlen;
|
|
||||||
memset (buf, 'A', sizeof(buf));
|
|
||||||
mlen = sizeof(buf);
|
|
||||||
n = qse_wcstombs (p, buf, &mlen);
|
|
||||||
if (n == 0) break;
|
|
||||||
|
|
||||||
qse_printf (QSE_T("%s chars=%d bytes=%d [%hs]\n"),
|
|
||||||
((x[i][n] == QSE_WT('\0') && mlen < sizeof(buf))? QSE_T("FULL"): QSE_T("PARTIAL")),
|
|
||||||
(int)n, (int)mlen, buf);
|
|
||||||
p += n;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int test9 (void)
|
|
||||||
{
|
|
||||||
qse_mchar_t buf[24];
|
|
||||||
int i;
|
|
||||||
|
|
||||||
const qse_wchar_t* x[] =
|
|
||||||
{
|
|
||||||
L"",
|
|
||||||
L"\uB108 \uBB50\uAC00 \uC798\uB0AC\uC5B4?",
|
|
||||||
L"A\uB108 \uBB50\uAC00 \uC798\uB0AC\uC5B4?",
|
|
||||||
L"AB\uB108 \uBB50\uAC00 \uC798\uB0AC\uC5B4?",
|
|
||||||
L"ABC\uB108 \uBB50\uAC00 \uC798\uB0AC\uC5B4?",
|
|
||||||
L"ABCD\uB108 \uBB50\uAC00 \uC798\uB0AC\uC5B4?",
|
|
||||||
L"ABCDEFGHIJKLMNOPQRSTUV",
|
|
||||||
L"ABCDEFGHIJKLMNOPQRSTUVW",
|
|
||||||
L"ABCDEFGHIJKLMNOPQRSTUVWX",
|
|
||||||
L"ABCDEFGHIJKLMNOPQRSTUVWXY",
|
|
||||||
L"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
|
||||||
};
|
|
||||||
|
|
||||||
for (i = 0; i < QSE_COUNTOF(x); i++)
|
|
||||||
{
|
|
||||||
qse_printf (QSE_T("[%ls] => "), x[i]);
|
|
||||||
|
|
||||||
if (qse_wcstombsrigid (x[i], buf, QSE_COUNTOF(buf)) <= -1)
|
|
||||||
{
|
|
||||||
qse_printf (QSE_T("ERROR\n"));
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
qse_printf (QSE_T("[%hs]\n"), buf);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int test10 (void)
|
static int test10 (void)
|
||||||
{
|
{
|
||||||
qse_wchar_t* wa[] = { QSE_WT("hello"), QSE_WT(","), QSE_WT("world"), QSE_NULL };
|
qse_wchar_t* wa[] = { QSE_WT("hello"), QSE_WT(","), QSE_WT("world"), QSE_NULL };
|
||||||
@ -693,11 +418,6 @@ int main ()
|
|||||||
R (test2);
|
R (test2);
|
||||||
R (test3);
|
R (test3);
|
||||||
R (test4);
|
R (test4);
|
||||||
R (test5);
|
|
||||||
R (test6);
|
|
||||||
R (test7);
|
|
||||||
R (test8);
|
|
||||||
R (test9);
|
|
||||||
R (test10);
|
R (test10);
|
||||||
|
|
||||||
R (test11);
|
R (test11);
|
245
qse/samples/cmn/str02.c
Normal file
245
qse/samples/cmn/str02.c
Normal file
@ -0,0 +1,245 @@
|
|||||||
|
#include <qse/cmn/mem.h>
|
||||||
|
#include <qse/cmn/str.h>
|
||||||
|
#include <qse/cmn/stdio.h>
|
||||||
|
#include <qse/cmn/sio.h>
|
||||||
|
|
||||||
|
#include <locale.h>
|
||||||
|
#include <wchar.h>
|
||||||
|
#include <string.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)
|
||||||
|
|
||||||
|
|
||||||
|
static int test1 (void)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
const qse_mchar_t* x[] =
|
||||||
|
{
|
||||||
|
QSE_MT(""),
|
||||||
|
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_sio_puts (QSE_SIO_OUT, QSE_T("<<"));
|
||||||
|
qse_sio_puts (QSE_SIO_OUT, buf2);
|
||||||
|
qse_sio_puts (QSE_SIO_OUT, QSE_T(">>\n"));
|
||||||
|
qse_sio_flush (QSE_SIO_OUT);
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
qse_sio_puts (QSE_SIO_OUT, QSE_T("<<"));
|
||||||
|
qse_sio_puts (QSE_SIO_OUT, buf2);
|
||||||
|
qse_sio_puts (QSE_SIO_OUT, QSE_T(">>\n"));
|
||||||
|
qse_sio_flush (QSE_SIO_OUT);
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int test2 (void)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
const qse_mchar_t* x[] =
|
||||||
|
{
|
||||||
|
QSE_MT("\0\0\0"),
|
||||||
|
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)
|
||||||
|
{
|
||||||
|
wlen = QSE_COUNTOF(buf);
|
||||||
|
mlen = k - j;
|
||||||
|
|
||||||
|
int y = qse_mbsntowcsn (&x[i][j], &mlen, buf, &wlen);
|
||||||
|
|
||||||
|
|
||||||
|
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(""),
|
||||||
|
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;
|
||||||
|
|
||||||
|
wcs = qse_mbstowcsdup (x[i], QSE_MMGR_GETDFL());
|
||||||
|
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"),
|
||||||
|
QSE_MT("이거슨"),
|
||||||
|
QSE_MT("뭐냐이거"),
|
||||||
|
QSE_MT("過去一個月"),
|
||||||
|
QSE_MT("是成功的建商"),
|
||||||
|
QSE_MT("뛰어 올라봐. 멀리멀리 잘난척하기는"),
|
||||||
|
QSE_MT("Fly to the universe"),
|
||||||
|
QSE_NULL
|
||||||
|
};
|
||||||
|
qse_wchar_t* wcs;
|
||||||
|
|
||||||
|
wcs = qse_mbsatowcsdup (x, QSE_MMGR_GETDFL());
|
||||||
|
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)
|
||||||
|
char codepage[100];
|
||||||
|
UINT old_cp = GetConsoleOutputCP();
|
||||||
|
SetConsoleOutputCP (CP_UTF8);
|
||||||
|
|
||||||
|
/* TODO: on windows this set locale only affects those mbcs fucntions in clib.
|
||||||
|
* it doesn't support utf8 i guess find a working way. the following won't work
|
||||||
|
sprintf (codepage, ".%d", GetACP());
|
||||||
|
setlocale (LC_ALL, codepage);
|
||||||
|
*/
|
||||||
|
#else
|
||||||
|
setlocale (LC_ALL, "");
|
||||||
|
#endif
|
||||||
|
|
||||||
|
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);
|
||||||
|
|
||||||
|
#if defined(_WIN32)
|
||||||
|
SetConsoleOutputCP (old_cp);
|
||||||
|
#endif
|
||||||
|
return 0;
|
||||||
|
}
|
324
qse/samples/cmn/str03.c
Normal file
324
qse/samples/cmn/str03.c
Normal file
@ -0,0 +1,324 @@
|
|||||||
|
#include <qse/cmn/mem.h>
|
||||||
|
#include <qse/cmn/str.h>
|
||||||
|
#include <qse/cmn/stdio.h>
|
||||||
|
#include <qse/cmn/sio.h>
|
||||||
|
|
||||||
|
#include <locale.h>
|
||||||
|
#include <wchar.h>
|
||||||
|
#include <string.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)
|
||||||
|
|
||||||
|
static int test1 (void)
|
||||||
|
{
|
||||||
|
const qse_wchar_t unistr[] =
|
||||||
|
{
|
||||||
|
/*L"\uB108 \uBB50\uAC00 \uC798\uB0AC\uC5B4?",*/
|
||||||
|
0xB108,
|
||||||
|
L' ',
|
||||||
|
0xBB50,
|
||||||
|
0xAC00,
|
||||||
|
L' ',
|
||||||
|
0xC798,
|
||||||
|
0xB0AC,
|
||||||
|
0xC5B4,
|
||||||
|
L'?',
|
||||||
|
L'\0'
|
||||||
|
};
|
||||||
|
const qse_wchar_t* x[] =
|
||||||
|
{
|
||||||
|
L"",
|
||||||
|
L"",
|
||||||
|
L"Fly to the universe, kick your ass"
|
||||||
|
};
|
||||||
|
qse_mchar_t buf[10];
|
||||||
|
qse_mchar_t buf2[100];
|
||||||
|
int i;
|
||||||
|
|
||||||
|
x[1] = unistr;
|
||||||
|
|
||||||
|
for (i = 0; i < QSE_COUNTOF(x); i++)
|
||||||
|
{
|
||||||
|
int n;
|
||||||
|
qse_size_t wlen, mlen;
|
||||||
|
|
||||||
|
n = qse_wcstombs (x[i], &wlen, QSE_NULL, &mlen);
|
||||||
|
|
||||||
|
qse_printf (QSE_T("[%ls] n=%d, wcslen()=%d, wlen=%d, mlen=%d\n"),
|
||||||
|
x[i], (int)n, (int)qse_wcslen(x[i]), (int)wlen, (int)mlen);
|
||||||
|
}
|
||||||
|
qse_printf (QSE_T("-----------------\n"));
|
||||||
|
|
||||||
|
for (i = 0; i < QSE_COUNTOF(x); i++)
|
||||||
|
{
|
||||||
|
int n;
|
||||||
|
qse_size_t wlen, mlen;
|
||||||
|
|
||||||
|
mlen = QSE_COUNTOF(buf) - 1;
|
||||||
|
|
||||||
|
qse_mbsset (buf, QSE_MT('A'), QSE_COUNTOF(buf));
|
||||||
|
n = qse_wcstombs (x[i], &wlen, buf, &mlen);
|
||||||
|
|
||||||
|
QSE_ASSERT (buf[QSE_COUNTOF(buf)-1] == QSE_MT('A'));
|
||||||
|
buf[QSE_COUNTOF(buf)-1] = QSE_MT('\0');
|
||||||
|
|
||||||
|
qse_printf (QSE_T("%s chars=%d bytes=%d [%hs]\n"),
|
||||||
|
((n == -2)? QSE_T("BUFFER-SMALL"): (n == -1)? QSE_T("ILLEGAL-CHAR"): QSE_T("FULL")),
|
||||||
|
(int)wlen, (int)mlen, buf);
|
||||||
|
}
|
||||||
|
|
||||||
|
qse_printf (QSE_T("-----------------\n"));
|
||||||
|
for (i = 0; i < QSE_COUNTOF(x); i++)
|
||||||
|
{
|
||||||
|
int n;
|
||||||
|
qse_size_t wlen, mlen;
|
||||||
|
|
||||||
|
mlen = QSE_COUNTOF(buf2);
|
||||||
|
n = qse_wcstombs (x[i], &wlen, buf2, &mlen);
|
||||||
|
|
||||||
|
qse_printf (QSE_T("%s chars=%d bytes=%d [%hs]\n"),
|
||||||
|
((n == -2)? QSE_T("BUFFER-SMALL"): (n == -1)? QSE_T("ILLEGAL-CHAR"): QSE_T("FULL")),
|
||||||
|
(int)wlen, (int)mlen, buf2);
|
||||||
|
}
|
||||||
|
|
||||||
|
qse_printf (QSE_T("-----------------\n"));
|
||||||
|
for (i = 0; i < QSE_COUNTOF(x); i++)
|
||||||
|
{
|
||||||
|
int n;
|
||||||
|
const qse_wchar_t* p = x[i];
|
||||||
|
|
||||||
|
while (*p)
|
||||||
|
{
|
||||||
|
qse_size_t wlen, mlen;
|
||||||
|
|
||||||
|
mlen = QSE_COUNTOF(buf) - 1;
|
||||||
|
n = qse_wcstombs (p, &wlen, buf, &mlen);
|
||||||
|
|
||||||
|
qse_printf (QSE_T("%s chars=%d bytes=%d [%hs]\n"),
|
||||||
|
((n == -2)? QSE_T("BUFFER-SMALL"): (n == -1)? QSE_T("ILLEGAL-CHAR"): QSE_T("FULL")),
|
||||||
|
(int)wlen, (int)mlen, buf);
|
||||||
|
p += wlen;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int test2 (void)
|
||||||
|
{
|
||||||
|
const qse_wchar_t unistr[] =
|
||||||
|
{
|
||||||
|
/*L"\uB108 \uBB50\uAC00 \uC798\uB0AC\uC5B4?",*/
|
||||||
|
0xB108,
|
||||||
|
L' ',
|
||||||
|
0xBB50,
|
||||||
|
0xAC00,
|
||||||
|
L' ',
|
||||||
|
0xC798,
|
||||||
|
0xB0AC,
|
||||||
|
0xC5B4,
|
||||||
|
L'?',
|
||||||
|
L'\0'
|
||||||
|
};
|
||||||
|
const qse_wchar_t* x[] =
|
||||||
|
{
|
||||||
|
L"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0",
|
||||||
|
L"",
|
||||||
|
L"Fly to the universe, kick your ass"
|
||||||
|
};
|
||||||
|
qse_mchar_t buf[9];
|
||||||
|
int i, j;
|
||||||
|
|
||||||
|
x[1] = unistr;
|
||||||
|
|
||||||
|
for (i = 0; i < QSE_COUNTOF(x); i++)
|
||||||
|
{
|
||||||
|
qse_size_t len, wlen, mlen, iter;
|
||||||
|
int n;
|
||||||
|
|
||||||
|
len = qse_wcslen(x[i]);
|
||||||
|
if (len == 0) len = 20; /* for x[0] */
|
||||||
|
|
||||||
|
qse_printf (QSE_T("Converting %d wide-character - "), (int)len);
|
||||||
|
|
||||||
|
wlen = len;
|
||||||
|
n = qse_wcsntombsn (&x[i][j], &wlen, QSE_NULL, &mlen);
|
||||||
|
if (n == -1)
|
||||||
|
{
|
||||||
|
qse_printf (QSE_T("***illegal character[mlen=%d/wlen=%d]*** ["), (int)mlen, (int)wlen);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
/* -2 is never returned if i don't provide the multibyte buffer */
|
||||||
|
qse_printf (QSE_T("%d multibyte characters to be produced from %d wide characters ["), (int)mlen, (int)wlen);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (j = 0, iter = 0; j < len; j += wlen)
|
||||||
|
{
|
||||||
|
iter = iter + 1;
|
||||||
|
wlen = len - j;
|
||||||
|
mlen = QSE_COUNTOF(buf);
|
||||||
|
n = qse_wcsntombsn (&x[i][j], &wlen, buf, &mlen);
|
||||||
|
|
||||||
|
if (n == -1)
|
||||||
|
{
|
||||||
|
qse_printf (QSE_T("***illegal character*** wlen=%d/mlen=%d/n=%d"), (int)(len-j), (int)mlen, (int)n);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
#if 0
|
||||||
|
else if (n == -2)
|
||||||
|
{
|
||||||
|
qse_printf (QSE_T("***buffer not large*** wlen=%d/mlen=%d/n=%d"), (int)(len-j), (int)mlen, (int)n);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
if (mlen > 0 && buf[0] == QSE_T('\0'))
|
||||||
|
{
|
||||||
|
while (mlen > 0 && buf[--mlen] == QSE_T('\0'))
|
||||||
|
{
|
||||||
|
qse_printf (QSE_T("\\0"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
#if defined(QSE_CHAR_IS_MCHAR) || defined(_WIN32)
|
||||||
|
qse_printf (QSE_T("%.*hs"), (int)mlen, buf);
|
||||||
|
#else
|
||||||
|
/* at least on linux and macosx, wprintf seems
|
||||||
|
* to taks preceision as the number of wide
|
||||||
|
* characters with %s. */
|
||||||
|
/* THIS HACK SHOULD BE REMOVED ONCE I IMPLEMENTE
|
||||||
|
* MY OWN qse_printf */
|
||||||
|
qse_printf (QSE_T("%.*hs"), (int)wlen, buf);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
qse_printf (QSE_T("] => %d iterations\n"), (int)iter);
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int test3 (void)
|
||||||
|
{
|
||||||
|
const qse_wchar_t unistr[] =
|
||||||
|
{
|
||||||
|
/*L"\uB108 \uBB50\uAC00 \uC798\uB0AC\uC5B4?",*/
|
||||||
|
0xB108,
|
||||||
|
L' ',
|
||||||
|
0xBB50,
|
||||||
|
0xAC00,
|
||||||
|
L' ',
|
||||||
|
0xC798,
|
||||||
|
0xB0AC,
|
||||||
|
0xC5B4,
|
||||||
|
L'?',
|
||||||
|
L'\0'
|
||||||
|
};
|
||||||
|
const qse_wchar_t* x[] =
|
||||||
|
{
|
||||||
|
L"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0",
|
||||||
|
L"",
|
||||||
|
L"Fly to the universe, kick your ass"
|
||||||
|
};
|
||||||
|
int i;
|
||||||
|
|
||||||
|
x[1] = unistr;
|
||||||
|
|
||||||
|
for (i = 0; i < QSE_COUNTOF(x); i++)
|
||||||
|
{
|
||||||
|
qse_mchar_t* m = qse_wcstombsdup (x[i], QSE_MMGR_GETDFL());
|
||||||
|
if (m == QSE_NULL)
|
||||||
|
{
|
||||||
|
qse_printf (QSE_T("[ERROR]\n"), m);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
qse_printf (QSE_T("[%hs]\n"), m);
|
||||||
|
}
|
||||||
|
|
||||||
|
QSE_MMGR_FREE (QSE_MMGR_GETDFL(), m);
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int test4 (void)
|
||||||
|
{
|
||||||
|
const qse_wchar_t unistr[] =
|
||||||
|
{
|
||||||
|
/*L"\uB108 \uBB50\uAC00 \uC798\uB0AC\uC5B4?",*/
|
||||||
|
0xB108,
|
||||||
|
L' ',
|
||||||
|
0xBB50,
|
||||||
|
0xAC00,
|
||||||
|
L' ',
|
||||||
|
0xC798,
|
||||||
|
0xB0AC,
|
||||||
|
0xC5B4,
|
||||||
|
L'?',
|
||||||
|
L'\0'
|
||||||
|
};
|
||||||
|
const qse_wchar_t* x[] =
|
||||||
|
{
|
||||||
|
L"Good morning angels",
|
||||||
|
L"",
|
||||||
|
L"Fly to the universe, kick your ass",
|
||||||
|
QSE_NULL
|
||||||
|
};
|
||||||
|
qse_mchar_t* m;
|
||||||
|
|
||||||
|
x[1] = unistr;
|
||||||
|
|
||||||
|
m = qse_wcsatombsdup (x, QSE_MMGR_GETDFL());
|
||||||
|
if (m == QSE_NULL)
|
||||||
|
{
|
||||||
|
qse_printf (QSE_T("[ERROR]\n"), m);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
qse_printf (QSE_T("[%hs]\n"), m);
|
||||||
|
}
|
||||||
|
|
||||||
|
QSE_MMGR_FREE (QSE_MMGR_GETDFL(), m);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main ()
|
||||||
|
{
|
||||||
|
#if defined(_WIN32)
|
||||||
|
char codepage[100];
|
||||||
|
UINT old_cp = GetConsoleOutputCP();
|
||||||
|
SetConsoleOutputCP (CP_UTF8);
|
||||||
|
|
||||||
|
/* TODO: on windows this set locale only affects those mbcs fucntions in clib.
|
||||||
|
* it doesn't support utf8 i guess find a working way. the following won't work
|
||||||
|
sprintf (codepage, ".%d", GetACP());
|
||||||
|
setlocale (LC_ALL, codepage);
|
||||||
|
*/
|
||||||
|
#else
|
||||||
|
setlocale (LC_ALL, "");
|
||||||
|
#endif
|
||||||
|
|
||||||
|
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);
|
||||||
|
|
||||||
|
#if defined(_WIN32)
|
||||||
|
SetConsoleOutputCP (old_cp);
|
||||||
|
#endif
|
||||||
|
return 0;
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user