diff --git a/qse/include/qse/cmn/nwio.h b/qse/include/qse/cmn/nwio.h index de2c6889..0a174119 100644 --- a/qse/include/qse/cmn/nwio.h +++ b/qse/include/qse/cmn/nwio.h @@ -194,7 +194,7 @@ QSE_EXPORT qse_ssize_t qse_nwio_flush ( qse_nwio_t* nwio ); -QSE_EXPORT void qse_nwio_purge ( +QSE_EXPORT void qse_nwio_drain ( qse_nwio_t* nwio ); diff --git a/qse/include/qse/cmn/pio.h b/qse/include/qse/cmn/pio.h index 0828de1f..b3a5d0a9 100644 --- a/qse/include/qse/cmn/pio.h +++ b/qse/include/qse/cmn/pio.h @@ -346,10 +346,10 @@ QSE_EXPORT qse_ssize_t qse_pio_flush ( ); /** - * The qse_pio_purge() drops unflushed input and output data in the + * The qse_pio_drain() drops unflushed input and output data in the * buffer. */ -QSE_EXPORT void qse_pio_purge ( +QSE_EXPORT void qse_pio_drain ( qse_pio_t* pio, /**< pio object */ qse_pio_hid_t hid /**< handle ID */ ); diff --git a/qse/include/qse/cmn/sio.h b/qse/include/qse/cmn/sio.h index fce3fb53..e3b97350 100644 --- a/qse/include/qse/cmn/sio.h +++ b/qse/include/qse/cmn/sio.h @@ -88,11 +88,16 @@ typedef enum qse_sio_errnum_t qse_sio_errnum_t; typedef qse_fio_off_t qse_sio_pos_t; typedef qse_fio_hnd_t qse_sio_hnd_t; typedef qse_fio_std_t qse_sio_std_t; +typedef qse_fio_ori_t qse_sio_ori_t; #define QSE_SIO_STDIN QSE_FIO_STDIN #define QSE_SIO_STDOUT QSE_FIO_STDOUT #define QSE_SIO_STDERR QSE_FIO_STDERR +#define QSE_SIO_BEGIN QSE_FIO_BEGIN +#define QSE_SIO_CURRENT QSE_FIO_CURRENT +#define QSE_SIO_END QSE_FIO_END + /** * The qse_sio_t type defines a simple text stream over a file. It also * provides predefined streams for standard input, output, and error. @@ -214,9 +219,9 @@ QSE_EXPORT qse_ssize_t qse_sio_flush ( ); /** - * The qse_sio_purge() funtion purges all buffered data without writing. + * The qse_sio_drain() funtion purges all buffered data without writing. */ -QSE_EXPORT void qse_sio_purge ( +QSE_EXPORT void qse_sio_drain ( qse_sio_t* sio ); @@ -346,7 +351,7 @@ QSE_EXPORT qse_ssize_t qse_sio_putwcsvf ( #endif /** - * The qse_sio_getpos() gets the current position in a stream. + * The qse_sio_getpos() function gets the current position in a stream. * Note that it may not return the desired postion due to buffering. * @return 0 on success, -1 on failure */ @@ -356,7 +361,7 @@ QSE_EXPORT int qse_sio_getpos ( ); /** - * The qse_sio_setpos() changes the current position in a stream. + * The qse_sio_setpos() function changes the current position in a stream. * @return 0 on success, -1 on failure */ QSE_EXPORT int qse_sio_setpos ( @@ -364,11 +369,21 @@ QSE_EXPORT int qse_sio_setpos ( qse_sio_pos_t pos /**< position */ ); -#if 0 -int qse_sio_rewind (qse_sio_t* sio); -int qse_sio_movetoend (qse_sio_t* sio); -#endif +QSE_EXPORT int qse_sio_truncate ( + qse_sio_t* sio, + qse_sio_pos_t pos +); +/** + * The qse_sio_seek() function repositions the current file offset. + * Upon success, \a pos is adjusted to the new offset from the beginning + * of the file. + */ +QSE_EXPORT int qse_sio_seek ( + qse_sio_t* sio, + qse_sio_pos_t* pos, + qse_sio_ori_t origin +); QSE_EXPORT int qse_openstdsios (void); QSE_EXPORT void qse_closestdsios (void); diff --git a/qse/include/qse/cmn/tio.h b/qse/include/qse/cmn/tio.h index dd244685..1a1cbdbe 100644 --- a/qse/include/qse/cmn/tio.h +++ b/qse/include/qse/cmn/tio.h @@ -247,9 +247,9 @@ QSE_EXPORT qse_ssize_t qse_tio_flush ( ); /** - * The qse_tio_purge() function empties input and output buffers. + * The qse_tio_drain() function empties input and output buffers. */ -QSE_EXPORT void qse_tio_purge ( +QSE_EXPORT void qse_tio_drain ( qse_tio_t* tio ); diff --git a/qse/lib/cmn/nwio.c b/qse/lib/cmn/nwio.c index a3f6ad3e..0e0ca29c 100644 --- a/qse/lib/cmn/nwio.c +++ b/qse/lib/cmn/nwio.c @@ -908,9 +908,9 @@ qse_ssize_t qse_nwio_flush (qse_nwio_t* nwio) return n; } -void qse_nwio_purge (qse_nwio_t* nwio) +void qse_nwio_drain (qse_nwio_t* nwio) { - if (nwio->tio) qse_tio_purge (nwio->tio); + if (nwio->tio) qse_tio_drain (nwio->tio); } /* ---------------------------------------------------------- */ diff --git a/qse/lib/cmn/pio.c b/qse/lib/cmn/pio.c index 62488012..2e491767 100644 --- a/qse/lib/cmn/pio.c +++ b/qse/lib/cmn/pio.c @@ -2183,9 +2183,9 @@ qse_ssize_t qse_pio_flush (qse_pio_t* pio, qse_pio_hid_t hid) return n; } -void qse_pio_purge (qse_pio_t* pio, qse_pio_hid_t hid) +void qse_pio_drain (qse_pio_t* pio, qse_pio_hid_t hid) { - if (pio->pin[hid].tio) qse_tio_purge (pio->pin[hid].tio); + if (pio->pin[hid].tio) qse_tio_drain (pio->pin[hid].tio); } void qse_pio_end (qse_pio_t* pio, qse_pio_hid_t hid) diff --git a/qse/lib/cmn/sio.c b/qse/lib/cmn/sio.c index 2c803fba..bba55d32 100644 --- a/qse/lib/cmn/sio.c +++ b/qse/lib/cmn/sio.c @@ -312,9 +312,9 @@ qse_ssize_t qse_sio_flush (qse_sio_t* sio) return n; } -void qse_sio_purge (qse_sio_t* sio) +void qse_sio_drain (qse_sio_t* sio) { - qse_tio_purge (&sio->tio.io); + qse_tio_drain (&sio->tio.io); } qse_ssize_t qse_sio_getmb (qse_sio_t* sio, qse_mchar_t* c) @@ -725,6 +725,8 @@ int qse_sio_getpos (qse_sio_t* sio, qse_sio_pos_t* pos) { qse_fio_off_t off; + if (qse_sio_flush(sio) <= -1) return -1; + off = qse_fio_seek (&sio->file, 0, QSE_FIO_CURRENT); if (off == (qse_fio_off_t)-1) { @@ -741,6 +743,7 @@ int qse_sio_setpos (qse_sio_t* sio, qse_sio_pos_t pos) qse_fio_off_t off; if (qse_sio_flush(sio) <= -1) return -1; + off = qse_fio_seek (&sio->file, pos, QSE_FIO_BEGIN); if (off == (qse_fio_off_t)-1) { @@ -751,23 +754,23 @@ int qse_sio_setpos (qse_sio_t* sio, qse_sio_pos_t pos) return 0; } -#if 0 -int qse_sio_seek (qse_sio_t* sio, qse_sio_seek_t pos) +int qse_sio_truncate (qse_sio_t* sio, qse_sio_pos_t pos) { - /* TODO: write this function - more flexible positioning .... - * can move to the end of the stream also.... */ - if (qse_sio_flush(sio) <= -1) return -1; - return (qse_fio_seek (&sio->file, - 0, QSE_FIO_END) == (qse_fio_off_t)-1)? -1: 0; - - /* TODO: write this function */ - if (qse_sio_flush(sio) <= -1) return -1; - return (qse_fio_seek (&sio->file, - 0, QSE_FIO_BEGIN) == (qse_fio_off_t)-1)? -1: 0; - + return qse_fio_truncate (&sio->file, pos); +} + +int qse_sio_seek (qse_sio_t* sio, qse_sio_pos_t* pos, qse_sio_ori_t origin) +{ + qse_fio_off_t x; + + if (qse_sio_flush(sio) <= -1) return -1; + x = qse_fio_seek (&sio->file, *pos, origin); + if (x == (qse_fio_off_t)-1) return -1; + + *pos = x; + return 0; } -#endif static qse_ssize_t file_input ( qse_tio_t* tio, qse_tio_cmd_t cmd, void* buf, qse_size_t size) diff --git a/qse/lib/cmn/tio.c b/qse/lib/cmn/tio.c index 1ea7b779..3289c875 100644 --- a/qse/lib/cmn/tio.c +++ b/qse/lib/cmn/tio.c @@ -343,7 +343,7 @@ qse_ssize_t qse_tio_flush (qse_tio_t* tio) return (qse_ssize_t)count; } -void qse_tio_purge (qse_tio_t* tio) +void qse_tio_drain (qse_tio_t* tio) { tio->status &= ~(STATUS_INPUT_ILLSEQ | STATUS_INPUT_EOF); tio->inbuf_cur = 0; diff --git a/qse/samples/cmn/sio01.c b/qse/samples/cmn/sio01.c index 63eecc60..93516c3f 100644 --- a/qse/samples/cmn/sio01.c +++ b/qse/samples/cmn/sio01.c @@ -3,7 +3,6 @@ #include #include #include -#include #include @@ -110,7 +109,7 @@ int main () qse_setdflcmgrbyid (QSE_CMGR_SLMB); } #else - setlocale (LC_ALL, ""); + setlocale (LC_ALL, ""); qse_setdflcmgrbyid (QSE_CMGR_SLMB); #endif diff --git a/qse/samples/cmn/sio02.c b/qse/samples/cmn/sio02.c index abe654f2..3d41c4cb 100644 --- a/qse/samples/cmn/sio02.c +++ b/qse/samples/cmn/sio02.c @@ -2,7 +2,6 @@ #include #include #include -#include #include #if defined(_WIN32) @@ -160,7 +159,7 @@ int main () qse_setdflcmgrbyid (QSE_CMGR_SLMB); } #else - setlocale (LC_ALL, ""); + setlocale (LC_ALL, ""); qse_setdflcmgrbyid (QSE_CMGR_SLMB); #endif diff --git a/qse/samples/cmn/sio03.c b/qse/samples/cmn/sio03.c index 475b2ba1..6700c095 100644 --- a/qse/samples/cmn/sio03.c +++ b/qse/samples/cmn/sio03.c @@ -1,7 +1,6 @@ #include #include #include -#include #include @@ -9,9 +8,11 @@ # include #endif +static qse_sio_t* g_out; + #define R(f) \ do { \ - qse_printf (QSE_T("== %s ==\n"), QSE_T(#f)); \ + qse_sio_putstrf (g_out, QSE_T("== %s ==\n"), QSE_T(#f)); \ if (f() == -1) return -1; \ } while (0) @@ -57,7 +58,8 @@ static int test1 (void) x[1] = unistr; x[2] = unistr2; - sio = qse_sio_openstd (QSE_MMGR_GETDFL(), 0, QSE_SIO_STDOUT, QSE_SIO_READ | QSE_SIO_IGNOREMBWCERR | QSE_SIO_NOAUTOFLUSH); + sio = qse_sio_openstd (QSE_MMGR_GETDFL(), 0, + QSE_SIO_STDOUT, QSE_SIO_READ | QSE_SIO_IGNOREMBWCERR | QSE_SIO_NOAUTOFLUSH); if (sio == QSE_NULL) return -1; for (i = 0; i < QSE_COUNTOF(x); i++) @@ -74,16 +76,16 @@ static int test2 (void) { /* this file is in utf8, the following strings may not be shown properly * if your locale/codepage is not utf8 */ - 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") - }; + 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") + }; int i; qse_sio_t* sio; @@ -104,20 +106,21 @@ static int test3 (void) { /* this file is in utf8, the following strings may not be shown properly * if your locale/codepage is not utf8 */ - 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") - }; + 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") + }; int i; qse_sio_t* sio; - sio = qse_sio_openstd (QSE_MMGR_GETDFL(), 0, QSE_SIO_STDOUT, QSE_SIO_READ | QSE_SIO_IGNOREMBWCERR | QSE_SIO_NOAUTOFLUSH); + sio = qse_sio_openstd (QSE_MMGR_GETDFL(), 0, + QSE_SIO_STDOUT, QSE_SIO_READ | QSE_SIO_IGNOREMBWCERR | QSE_SIO_NOAUTOFLUSH); if (sio == QSE_NULL) return -1; for (i = 0; i < QSE_COUNTOF(x); i++) @@ -130,6 +133,37 @@ static int test3 (void) return 0; } +static int test4 (void) +{ + qse_sio_t* sio; + qse_sio_pos_t pos; + + sio = qse_sio_open (QSE_MMGR_GETDFL(), 0, QSE_T("sio03.txt"), + QSE_SIO_WRITE | QSE_SIO_READ | QSE_SIO_TRUNCATE | QSE_SIO_CREATE); + if (sio == QSE_NULL) return -1; + + qse_sio_putstr (sio, QSE_T("이거 좋다. this is good")); + qse_sio_getpos (sio, &pos); + qse_sio_putstrf (g_out, QSE_T("position = %lld\n"), (long long int)pos); + + qse_sio_setpos (sio, pos - 2); + qse_sio_putstrf (sio, QSE_T("wonderful")); + + qse_sio_getpos (sio, &pos); + qse_sio_truncate (sio, pos - 2); + + pos = 0; + qse_sio_seek (sio, &pos, QSE_SIO_BEGIN); + qse_sio_putstr (sio, QSE_T("오홍")); + + qse_sio_putstrf (g_out, QSE_T("position returned by seek = %lld\n"), (long long int)pos); + qse_sio_getpos (sio, &pos); + qse_sio_putstrf (g_out, QSE_T("position returned by getpos = %lld\n"), (long long int)pos); + + qse_sio_close (sio); + return 0; +} + int main () { @@ -152,13 +186,18 @@ int main () qse_setdflcmgrbyid (QSE_CMGR_SLMB); #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")); + g_out = qse_sio_openstd (QSE_MMGR_GETDFL(), 0, QSE_SIO_STDOUT, QSE_SIO_WRITE | QSE_SIO_IGNOREMBWCERR); + + qse_sio_putstr (g_out, QSE_T("--------------------------------------------------------------------------------\n")); + qse_sio_putstr (g_out, 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_sio_putstr (g_out, QSE_T("--------------------------------------------------------------------------------\n")); R (test1); R (test2); R (test3); + R (test4); + + qse_sio_close (g_out); return 0; }