added qse_sio_truncate(), qse_sio_seek(),

changed qse_sio_getpos() to flush first.
renamed qse_Xio_purge() to qse_Xio_drain().
This commit is contained in:
hyung-hwan 2014-04-28 14:18:06 +00:00
parent e26b1a2a68
commit 45b2bc5e57
11 changed files with 120 additions and 65 deletions

View File

@ -194,7 +194,7 @@ QSE_EXPORT qse_ssize_t qse_nwio_flush (
qse_nwio_t* nwio qse_nwio_t* nwio
); );
QSE_EXPORT void qse_nwio_purge ( QSE_EXPORT void qse_nwio_drain (
qse_nwio_t* nwio qse_nwio_t* nwio
); );

View File

@ -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. * buffer.
*/ */
QSE_EXPORT void qse_pio_purge ( QSE_EXPORT void qse_pio_drain (
qse_pio_t* pio, /**< pio object */ qse_pio_t* pio, /**< pio object */
qse_pio_hid_t hid /**< handle ID */ qse_pio_hid_t hid /**< handle ID */
); );

View File

@ -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_off_t qse_sio_pos_t;
typedef qse_fio_hnd_t qse_sio_hnd_t; typedef qse_fio_hnd_t qse_sio_hnd_t;
typedef qse_fio_std_t qse_sio_std_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_STDIN QSE_FIO_STDIN
#define QSE_SIO_STDOUT QSE_FIO_STDOUT #define QSE_SIO_STDOUT QSE_FIO_STDOUT
#define QSE_SIO_STDERR QSE_FIO_STDERR #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 * The qse_sio_t type defines a simple text stream over a file. It also
* provides predefined streams for standard input, output, and error. * 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 qse_sio_t* sio
); );
@ -346,7 +351,7 @@ QSE_EXPORT qse_ssize_t qse_sio_putwcsvf (
#endif #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. * Note that it may not return the desired postion due to buffering.
* @return 0 on success, -1 on failure * @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 * @return 0 on success, -1 on failure
*/ */
QSE_EXPORT int qse_sio_setpos ( QSE_EXPORT int qse_sio_setpos (
@ -364,11 +369,21 @@ QSE_EXPORT int qse_sio_setpos (
qse_sio_pos_t pos /**< position */ qse_sio_pos_t pos /**< position */
); );
#if 0 QSE_EXPORT int qse_sio_truncate (
int qse_sio_rewind (qse_sio_t* sio); qse_sio_t* sio,
int qse_sio_movetoend (qse_sio_t* sio); qse_sio_pos_t pos
#endif );
/**
* 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 int qse_openstdsios (void);
QSE_EXPORT void qse_closestdsios (void); QSE_EXPORT void qse_closestdsios (void);

View File

@ -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 qse_tio_t* tio
); );

View File

@ -908,9 +908,9 @@ qse_ssize_t qse_nwio_flush (qse_nwio_t* nwio)
return n; 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);
} }
/* ---------------------------------------------------------- */ /* ---------------------------------------------------------- */

View File

@ -2183,9 +2183,9 @@ qse_ssize_t qse_pio_flush (qse_pio_t* pio, qse_pio_hid_t hid)
return n; 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) void qse_pio_end (qse_pio_t* pio, qse_pio_hid_t hid)

View File

@ -312,9 +312,9 @@ qse_ssize_t qse_sio_flush (qse_sio_t* sio)
return n; 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) 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; qse_fio_off_t off;
if (qse_sio_flush(sio) <= -1) return -1;
off = qse_fio_seek (&sio->file, 0, QSE_FIO_CURRENT); off = qse_fio_seek (&sio->file, 0, QSE_FIO_CURRENT);
if (off == (qse_fio_off_t)-1) 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; qse_fio_off_t off;
if (qse_sio_flush(sio) <= -1) return -1; if (qse_sio_flush(sio) <= -1) return -1;
off = qse_fio_seek (&sio->file, pos, QSE_FIO_BEGIN); off = qse_fio_seek (&sio->file, pos, QSE_FIO_BEGIN);
if (off == (qse_fio_off_t)-1) 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; return 0;
} }
#if 0 int qse_sio_truncate (qse_sio_t* sio, qse_sio_pos_t pos)
int qse_sio_seek (qse_sio_t* sio, qse_sio_seek_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; if (qse_sio_flush(sio) <= -1) return -1;
return (qse_fio_seek (&sio->file, return qse_fio_truncate (&sio->file, pos);
0, QSE_FIO_END) == (qse_fio_off_t)-1)? -1: 0; }
/* TODO: write this function */ int qse_sio_seek (qse_sio_t* sio, qse_sio_pos_t* pos, qse_sio_ori_t origin)
if (qse_sio_flush(sio) <= -1) return -1; {
return (qse_fio_seek (&sio->file, qse_fio_off_t x;
0, QSE_FIO_BEGIN) == (qse_fio_off_t)-1)? -1: 0;
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 ( static qse_ssize_t file_input (
qse_tio_t* tio, qse_tio_cmd_t cmd, void* buf, qse_size_t size) qse_tio_t* tio, qse_tio_cmd_t cmd, void* buf, qse_size_t size)

View File

@ -343,7 +343,7 @@ qse_ssize_t qse_tio_flush (qse_tio_t* tio)
return (qse_ssize_t)count; 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->status &= ~(STATUS_INPUT_ILLSEQ | STATUS_INPUT_EOF);
tio->inbuf_cur = 0; tio->inbuf_cur = 0;

View File

@ -3,7 +3,6 @@
#include <qse/cmn/mem.h> #include <qse/cmn/mem.h>
#include <qse/cmn/mem.h> #include <qse/cmn/mem.h>
#include <qse/cmn/fmt.h> #include <qse/cmn/fmt.h>
#include <qse/cmn/stdio.h>
#include <locale.h> #include <locale.h>
@ -110,7 +109,7 @@ int main ()
qse_setdflcmgrbyid (QSE_CMGR_SLMB); qse_setdflcmgrbyid (QSE_CMGR_SLMB);
} }
#else #else
setlocale (LC_ALL, ""); setlocale (LC_ALL, "");
qse_setdflcmgrbyid (QSE_CMGR_SLMB); qse_setdflcmgrbyid (QSE_CMGR_SLMB);
#endif #endif

View File

@ -2,7 +2,6 @@
#include <qse/cmn/mbwc.h> #include <qse/cmn/mbwc.h>
#include <qse/cmn/mem.h> #include <qse/cmn/mem.h>
#include <qse/cmn/fmt.h> #include <qse/cmn/fmt.h>
#include <qse/cmn/stdio.h>
#include <locale.h> #include <locale.h>
#if defined(_WIN32) #if defined(_WIN32)
@ -160,7 +159,7 @@ int main ()
qse_setdflcmgrbyid (QSE_CMGR_SLMB); qse_setdflcmgrbyid (QSE_CMGR_SLMB);
} }
#else #else
setlocale (LC_ALL, ""); setlocale (LC_ALL, "");
qse_setdflcmgrbyid (QSE_CMGR_SLMB); qse_setdflcmgrbyid (QSE_CMGR_SLMB);
#endif #endif

View File

@ -1,7 +1,6 @@
#include <qse/cmn/sio.h> #include <qse/cmn/sio.h>
#include <qse/cmn/mbwc.h> #include <qse/cmn/mbwc.h>
#include <qse/cmn/mem.h> #include <qse/cmn/mem.h>
#include <qse/cmn/stdio.h>
#include <locale.h> #include <locale.h>
@ -9,9 +8,11 @@
# include <windows.h> # include <windows.h>
#endif #endif
static qse_sio_t* g_out;
#define R(f) \ #define R(f) \
do { \ 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; \ if (f() == -1) return -1; \
} while (0) } while (0)
@ -57,7 +58,8 @@ static int test1 (void)
x[1] = unistr; x[1] = unistr;
x[2] = unistr2; 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; if (sio == QSE_NULL) return -1;
for (i = 0; i < QSE_COUNTOF(x); i++) 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 /* this file is in utf8, the following strings may not be shown properly
* if your locale/codepage is not utf8 */ * if your locale/codepage is not utf8 */
const qse_mchar_t* x[] = const qse_mchar_t* x[] =
{ {
QSE_MT("\0\0\0"), QSE_MT("\0\0\0"),
QSE_MT("이거슨"), QSE_MT("이거슨"),
QSE_MT("뭐냐이거"), QSE_MT("뭐냐이거"),
QSE_MT("過去一個月"), QSE_MT("過去一個月"),
QSE_MT("是成功的建商"), QSE_MT("是成功的建商"),
QSE_MT("뛰어 올라봐. 멀리멀리 잘난척하기는"), QSE_MT("뛰어 올라봐. 멀리멀리 잘난척하기는"),
QSE_MT("Fly to the universe") QSE_MT("Fly to the universe")
}; };
int i; int i;
qse_sio_t* sio; 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 /* this file is in utf8, the following strings may not be shown properly
* if your locale/codepage is not utf8 */ * if your locale/codepage is not utf8 */
const qse_mchar_t* x[] = const qse_mchar_t* x[] =
{ {
QSE_MT("\0\0\0"), QSE_MT("\0\0\0"),
QSE_MT("이거슨"), QSE_MT("이거슨"),
QSE_MT("뭐냐이거"), QSE_MT("뭐냐이거"),
QSE_MT("過去一個月"), QSE_MT("過去一個月"),
QSE_MT("是成功的建商"), QSE_MT("是成功的建商"),
QSE_MT("뛰어 올라봐. 멀리멀리 잘난척하기는"), QSE_MT("뛰어 올라봐. 멀리멀리 잘난척하기는"),
QSE_MT("Fly to the universe") QSE_MT("Fly to the universe")
}; };
int i; int i;
qse_sio_t* sio; 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; if (sio == QSE_NULL) return -1;
for (i = 0; i < QSE_COUNTOF(x); i++) for (i = 0; i < QSE_COUNTOF(x); i++)
@ -130,6 +133,37 @@ static int test3 (void)
return 0; 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 () int main ()
{ {
@ -152,13 +186,18 @@ int main ()
qse_setdflcmgrbyid (QSE_CMGR_SLMB); qse_setdflcmgrbyid (QSE_CMGR_SLMB);
#endif #endif
qse_printf (QSE_T("--------------------------------------------------------------------------------\n")); g_out = qse_sio_openstd (QSE_MMGR_GETDFL(), 0, QSE_SIO_STDOUT, QSE_SIO_WRITE | QSE_SIO_IGNOREMBWCERR);
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")); 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 (test1);
R (test2); R (test2);
R (test3); R (test3);
R (test4);
qse_sio_close (g_out);
return 0; return 0;
} }