diff --git a/qse/include/qse/cmn/fio.h b/qse/include/qse/cmn/fio.h index 972a0535..0dfb2ce1 100644 --- a/qse/include/qse/cmn/fio.h +++ b/qse/include/qse/cmn/fio.h @@ -60,13 +60,18 @@ enum qse_fio_open_flag_t QSE_FIO_NOFOLLOW = (1 << 15), /* for WIN32 only. harmless(no effect) when used on other platforms */ - QSE_FIO_NOSHRD = (1 << 24), - QSE_FIO_NOSHWR = (1 << 25), - QSE_FIO_NOSHDL = (1 << 26), + QSE_FIO_NOSHRD = (1 << 20), + QSE_FIO_NOSHWR = (1 << 21), + QSE_FIO_NOSHDL = (1 << 22), /* hints to OS. harmless(no effect) when used on unsupported platforms */ - QSE_FIO_RANDOM = (1 << 27), /* hint that access be random */ - QSE_FIO_SEQUENTIAL = (1 << 28) /* hint that access is sequential */ + QSE_FIO_RANDOM = (1 << 23), /* hint that access be random */ + QSE_FIO_SEQUENTIAL = (1 << 24) /* hint that access is sequential */ + + /* NOTE: + * NEVER (1 << 31) since QSE_SIO_NOAUTOFLUSH is defined to + * that value in sio.h FIO doesn't have any bufferring. + */ }; enum qse_fio_std_t diff --git a/qse/include/qse/cmn/sio.h b/qse/include/qse/cmn/sio.h index 9ea57d39..e1379de6 100644 --- a/qse/include/qse/cmn/sio.h +++ b/qse/include/qse/cmn/sio.h @@ -53,7 +53,11 @@ enum qse_sio_open_flag_t QSE_SIO_NOSHDL = QSE_FIO_NOSHDL, QSE_SIO_RANDOM = QSE_FIO_RANDOM, - QSE_SIO_SEQUENTIAL = QSE_FIO_SEQUENTIAL + QSE_SIO_SEQUENTIAL = QSE_FIO_SEQUENTIAL, + + /* Beware that this should not overlap with any QSE_FIO_XXXX. + * See NOTE in fio.h */ + QSE_SIO_NOAUTOFLUSH = (1 << 31) }; typedef qse_fio_off_t qse_sio_pos_t; @@ -62,7 +66,7 @@ typedef qse_fio_std_t qse_sio_std_t; #define QSE_SIO_STDIN QSE_FIO_STDIN #define QSE_SIO_STDOUT QSE_FIO_STDOUT -#define QSE_SIO_STDER QSE_FIO_STDERR +#define QSE_SIO_STDERR QSE_FIO_STDERR /** * The qse_sio_t type defines a simple text stream over a file. It also @@ -165,17 +169,37 @@ qse_ssize_t qse_sio_putc ( qse_char_t c ); -qse_ssize_t qse_sio_puts ( - qse_sio_t* sio, - const qse_char_t* str +qse_ssize_t qse_sio_putms ( + qse_sio_t* sio, + const qse_mchar_t* str ); -qse_ssize_t qse_sio_putsn ( - qse_sio_t* sio, - const qse_char_t* str, - qse_size_t size +qse_ssize_t qse_sio_putws ( + qse_sio_t* sio, + const qse_wchar_t* str ); + +qse_ssize_t qse_sio_putmsn ( + qse_sio_t* sio, + const qse_mchar_t* str, + qse_size_t size +); + +qse_ssize_t qse_sio_putwsn ( + qse_sio_t* sio, + const qse_wchar_t* str, + qse_size_t size +); + +#if defined(QSE_CHAR_IS_MCHAR) +# define qse_sio_puts(sio,str) qse_sio_putms(sio,str) +# define qse_sio_putsn(sio,str,size) qse_sio_putmsn(sio,str,size) +#else +# define qse_sio_puts(sio,str) qse_sio_putws(sio,str) +# define qse_sio_putsn(sio,str,size) qse_sio_putwsn(sio,str,size) +#endif + /** * The qse_sio_getpos() gets the current position in a stream. * Note that it may not return the desired postion due to buffering. diff --git a/qse/lib/cmn/assert.c b/qse/lib/cmn/assert.c index 076a1a27..06bf7ac9 100644 --- a/qse/lib/cmn/assert.c +++ b/qse/lib/cmn/assert.c @@ -23,7 +23,9 @@ #ifndef NDEBUG +#include #include +#include "mem.h" #ifdef HAVE_EXECINFO_H # include @@ -43,14 +45,14 @@ # include "syscall.h" #endif -#define NTOC(n) (QSE_T("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ")[n]) -#define WRITE_CHAR(c) \ +#define NTOC(n) (QSE_MT("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ")[n]) +#define WRITE_CHAR(sio,c) \ do { \ - qse_char_t __xxx_c = c; \ - if (qse_sio_putsn (QSE_SIO_ERR, &__xxx_c, 1) != 1) return -1; \ + qse_mchar_t __xxx_c = c; \ + if (qse_sio_putmsn (sio, &__xxx_c, 1) != 1) return -1; \ } while (0) -static int write_num (qse_size_t x, int base) +static int write_num (qse_sio_t* sio, qse_size_t x, int base) { qse_size_t last = x % base; qse_size_t y = 0; @@ -67,7 +69,7 @@ static int write_num (qse_size_t x, int base) while (y > 0) { - WRITE_CHAR (NTOC(y % base)); + WRITE_CHAR (sio, NTOC(y % base)); y = y / base; dig--; } @@ -75,9 +77,9 @@ static int write_num (qse_size_t x, int base) while (dig > 0) { dig--; - WRITE_CHAR (QSE_T('0')); + WRITE_CHAR (sio, QSE_T('0')); } - WRITE_CHAR (NTOC(last)); + WRITE_CHAR (sio, NTOC(last)); return 0; } @@ -90,38 +92,45 @@ void qse_assert_failed ( qse_size_t btsize, i; char **btsyms; #endif + qse_sio_t* sio, siobuf; - qse_sio_puts (QSE_SIO_ERR, QSE_T("=[ASSERTION FAILURE]============================================================\n")); + sio = &siobuf; + if (qse_sio_initstd (sio, QSE_MMGR_GETDFL(), QSE_SIO_STDERR, QSE_SIO_NOAUTOFLUSH) <= -1) + sio = QSE_SIO_ERR; + + qse_sio_putms (sio, QSE_MT("=[ASSERTION FAILURE]============================================================\n")); #if 1 - qse_sio_puts (QSE_SIO_ERR, QSE_T(" __ \n")); - qse_sio_puts (QSE_SIO_ERR, QSE_T(" _____ _____ _____ _____| |\n")); - qse_sio_puts (QSE_SIO_ERR, QSE_T("| | | _ | __| |\n")); - qse_sio_puts (QSE_SIO_ERR, QSE_T("| | | | | __|__ |__|\n")); - qse_sio_puts (QSE_SIO_ERR, QSE_T("|_____|_____|__| |_____|__|\n")); + qse_sio_putms (sio, QSE_MT(" __ \n")); + qse_sio_putms (sio, QSE_MT(" _____ _____ _____ _____| |\n")); + qse_sio_putms (sio, QSE_MT("| | | _ | __| |\n")); + qse_sio_putms (sio, QSE_MT("| | | | | __|__ |__|\n")); + qse_sio_putms (sio, QSE_MT("|_____|_____|__| |_____|__|\n")); + qse_sio_putms (sio, QSE_MT(" \n")); #else - qse_sio_puts (QSE_SIO_ERR, QSE_T(" __ \n")); - qse_sio_puts (QSE_SIO_ERR, QSE_T(" _____ _____ _____ _____ | |\n")); - qse_sio_puts (QSE_SIO_ERR, QSE_T("| | | _ | __| | |\n")); - qse_sio_puts (QSE_SIO_ERR, QSE_T("| | | | | __|__ | |__|\n")); - qse_sio_puts (QSE_SIO_ERR, QSE_T("|_____|_____|__| |_____| |__|\n")); + qse_sio_putms (sio, QSE_MT(" __ \n")); + qse_sio_putms (sio, QSE_MT(" _____ _____ _____ _____ | |\n")); + qse_sio_putms (sio, QSE_MT("| | | _ | __| | |\n")); + qse_sio_putms (sio, QSE_MT("| | | | | __|__ | |__|\n")); + qse_sio_putms (sio, QSE_MT("|_____|_____|__| |_____| |__|\n")); + qse_sio_putms (sio, QSE_MT(" __ \n")); #endif - qse_sio_puts (QSE_SIO_ERR, QSE_T("FILE ")); - qse_sio_puts (QSE_SIO_ERR, file); - qse_sio_puts (QSE_SIO_ERR, QSE_T(" LINE ")); + qse_sio_putms (sio, QSE_MT("FILE: ")); + qse_sio_puts (sio, file); + qse_sio_putms (sio, QSE_MT(" LINE: ")); - write_num (line, 10); + write_num (sio, line, 10); - qse_sio_puts (QSE_SIO_ERR, QSE_T(": ")); - qse_sio_puts (QSE_SIO_ERR, expr); - qse_sio_puts (QSE_SIO_ERR, QSE_T("\n")); + qse_sio_putms (sio, QSE_MT("\nEXPRESSION: ")); + qse_sio_puts (sio, expr); + qse_sio_putms (sio, QSE_MT("\n")); if (desc != QSE_NULL) { - qse_sio_puts (QSE_SIO_ERR, QSE_T("DESCRIPTION: ")); - qse_sio_puts (QSE_SIO_ERR, desc); - qse_sio_puts (QSE_SIO_ERR, QSE_T("\n")); + qse_sio_putms (sio, QSE_MT("DESCRIPTION: ")); + qse_sio_puts (sio, desc); + qse_sio_putms (sio, QSE_MT("\n")); } #ifdef HAVE_BACKTRACE @@ -129,30 +138,21 @@ void qse_assert_failed ( btsyms = backtrace_symbols (btarray, btsize); if (btsyms != QSE_NULL) { - qse_sio_puts (QSE_SIO_ERR, QSE_T("=[BACKTRACES]===================================================================\n")); + qse_sio_putms (sio, QSE_MT("=[BACKTRACES]===================================================================\n")); for (i = 0; i < btsize; i++) { - /* TODO: call qse_sio_putms() instead of using ifdef */ - #ifdef QSE_CHAR_IS_MCHAR - qse_sio_puts (QSE_SIO_ERR, btsyms[i]); - #else - qse_wchar_t wcs[256]; - qse_size_t wcslen = QSE_COUNTOF(wcs); - 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); - #endif - qse_sio_puts (QSE_SIO_ERR, QSE_T("\n")); + qse_sio_putms (sio, btsyms[i]); + qse_sio_putms (sio, QSE_MT("\n")); } free (btsyms); } #endif - qse_sio_puts (QSE_SIO_ERR, QSE_T("================================================================================\n")); - qse_sio_flush (QSE_SIO_ERR); + qse_sio_putms (sio, QSE_MT("================================================================================\n")); + qse_sio_flush (sio); + if (sio != QSE_SIO_ERR) qse_sio_fini (sio); #if defined(_WIN32) ExitProcess (249); diff --git a/qse/lib/cmn/sio.c b/qse/lib/cmn/sio.c index 66839f72..481ad821 100644 --- a/qse/lib/cmn/sio.c +++ b/qse/lib/cmn/sio.c @@ -232,6 +232,7 @@ int qse_sio_init ( if (qse_fio_init (&sio->fio, mmgr, file, flags, mode) <= -1) return -1; if (flags & QSE_SIO_IGNOREMBWCERR) topt |= QSE_TIO_IGNOREMBWCERR; + if (flags & QSE_SIO_NOAUTOFLUSH) topt |= QSE_TIO_NOAUTOFLUSH; if (qse_tio_init(&sio->tio, mmgr, topt) <= -1) { @@ -315,25 +316,33 @@ qse_ssize_t qse_sio_putc (qse_sio_t* sio, qse_char_t c) return qse_tio_write (&sio->tio, &c, 1); } +#if 0 qse_ssize_t qse_sio_puts (qse_sio_t* sio, const qse_char_t* str) { return qse_tio_write (&sio->tio, str, (qse_size_t)-1); } +#endif -qse_size_t qse_sio_putms (qse_sio_t* sio, const qse_mchar_t* str) +qse_ssize_t qse_sio_putms (qse_sio_t* sio, const qse_mchar_t* str) { - return qse_tio_write (&sio->tio, str, qse_mbslen(str)); + return qse_tio_writem (&sio->tio, str, qse_mbslen(str)); } -qse_size_t qse_sio_putws (qse_sio_t* sio, const qse_wchar_t* str) +qse_ssize_t qse_sio_putws (qse_sio_t* sio, const qse_wchar_t* str) { - return qse_tio_write (&sio->tio, str, qse_wcslen(str)); + return qse_tio_writew (&sio->tio, str, qse_wcslen(str)); } -qse_ssize_t qse_sio_putsn ( - qse_sio_t* sio, const qse_char_t* str, qse_size_t size) +qse_ssize_t qse_sio_putmsn ( + qse_sio_t* sio, const qse_mchar_t* str, qse_size_t size) { - return qse_tio_write (&sio->tio, str, size); + return qse_tio_writem (&sio->tio, str, size); +} + +qse_ssize_t qse_sio_putwsn ( + qse_sio_t* sio, const qse_wchar_t* str, qse_size_t size) +{ + return qse_tio_writew (&sio->tio, str, size); } int qse_sio_getpos (qse_sio_t* sio, qse_sio_pos_t* pos) diff --git a/qse/lib/cmn/tio-put.c b/qse/lib/cmn/tio-put.c index 8f72c7ec..bde319a3 100644 --- a/qse/lib/cmn/tio-put.c +++ b/qse/lib/cmn/tio-put.c @@ -199,6 +199,8 @@ qse_ssize_t qse_tio_writew ( wcnt = xwlen; mcnt = capa; n = qse_wcsntombsn (wptr, &wcnt, &tio->outbuf[tio->outbuf_len], &mcnt); + tio->outbuf_len += mcnt; + if (n == -2) { /* the buffer is not large enough to @@ -234,10 +236,9 @@ qse_ssize_t qse_tio_writew ( for (i = 0; i < wcnt; i++) if (wptr[i] == QSE_WT('\n')) nl = 1; } - } + } wptr += wcnt; xwlen -= wcnt; - tio->outbuf_len += mcnt; } if (nl && qse_tio_flush (tio) == -1) return -1; diff --git a/qse/samples/cmn/str03.c b/qse/samples/cmn/str03.c index efdaf8ea..a30b82f6 100644 --- a/qse/samples/cmn/str03.c +++ b/qse/samples/cmn/str03.c @@ -67,7 +67,7 @@ static int test1 (void) 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')); + 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"),