Reorganized the directory structure
This commit is contained in:
		
							
								
								
									
										73
									
								
								samples/si/Makefile.am
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										73
									
								
								samples/si/Makefile.am
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,73 @@ | ||||
| AUTOMAKE_OPTIONS = nostdinc | ||||
|  | ||||
| AM_CPPFLAGS = \ | ||||
| 	-I$(top_builddir)/include \ | ||||
| 	-I$(top_srcdir)/include \ | ||||
| 	-I$(includedir) | ||||
|  | ||||
| bin_PROGRAMS = \ | ||||
| 	dir01 \ | ||||
| 	fio01 \ | ||||
| 	fio02 \ | ||||
| 	fs01 \ | ||||
| 	fs02 \ | ||||
| 	fs03 \ | ||||
| 	glob01 \ | ||||
| 	log01 \ | ||||
| 	nwad01 \ | ||||
| 	nwif01 \ | ||||
| 	nwif02 \ | ||||
| 	pio01 \ | ||||
| 	rwl01 \ | ||||
| 	sck01 \ | ||||
| 	sio01 \ | ||||
| 	sio02 \ | ||||
| 	sio03  \ | ||||
| 	spl01 \ | ||||
| 	task01 \ | ||||
| 	thr01 | ||||
| 	 | ||||
| AM_LDFLAGS = -L../../lib/si  -L../../lib/cmn | ||||
| LDADD = -lqsesi -lqsecmn  | ||||
|  | ||||
| if WIN32  | ||||
| LDADD += $(UNICOWS_LIBS) | ||||
| endif | ||||
|  | ||||
| dir01_SOURCES = dir01.c | ||||
| fio01_SOURCES = fio01.c | ||||
| fio02_SOURCES = fio02.c | ||||
| fs01_SOURCES = fs01.c | ||||
| fs02_SOURCES = fs02.c | ||||
| fs03_SOURCES = fs03.c | ||||
| glob01_SOURCES = glob01.c | ||||
| log01_SOURCES = log01.c | ||||
| nwad01_SOURCES = nwad01.c | ||||
| nwif01_SOURCES = nwif01.c | ||||
| nwif02_SOURCES = nwif02.c | ||||
| pio01_SOURCES = pio01.c | ||||
| rwl01_SOURCES = rwl01.c | ||||
| sio01_SOURCES = sio01.c | ||||
| sio02_SOURCES = sio02.c | ||||
| sio03_SOURCES = sio03.c | ||||
| spl01_SOURCES = spl01.c  | ||||
| task01_SOURCES = task01.c | ||||
| thr01_SOURCES = thr01.c | ||||
|  | ||||
|  | ||||
|  | ||||
| if ENABLE_CXX | ||||
|  | ||||
| bin_PROGRAMS += sck01 spl02 tcpsvr01 tcpsvr02 thr02 thr03 | ||||
|  | ||||
| sck01_SOURCES = sck01.cpp | ||||
| spl02_SOURCES = spl02.cpp | ||||
| tcpsvr01_SOURCES = tcpsvr01.cpp | ||||
| tcpsvr02_SOURCES = tcpsvr02.cpp | ||||
| thr02_SOURCES = thr02.cpp | ||||
| thr03_SOURCES = thr03.cpp | ||||
|  | ||||
| tcpsvr02_LDADD = $(LDADD) -L../../lib/sttp -lqsesttp | ||||
|  | ||||
| endif | ||||
|  | ||||
							
								
								
									
										1060
									
								
								samples/si/Makefile.in
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1060
									
								
								samples/si/Makefile.in
									
									
									
									
									
										Normal file
									
								
							
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							
							
								
								
									
										131
									
								
								samples/si/dir01.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										131
									
								
								samples/si/dir01.c
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,131 @@ | ||||
| #include <qse/si/dir.h> | ||||
| #include <qse/cmn/main.h> | ||||
| #include <qse/cmn/mbwc.h> | ||||
| #include <qse/si/sio.h> | ||||
| #include <qse/cmn/mem.h> | ||||
| #include <qse/cmn/str.h> | ||||
| #include <qse/cmn/fmt.h> | ||||
| #include <locale.h> | ||||
|  | ||||
| #if defined(_WIN32) | ||||
| #	include <windows.h> | ||||
| #endif | ||||
|  | ||||
| static int list_dir (int argc, qse_char_t* argv[], int flags) | ||||
| { | ||||
| 	qse_dir_t* dir = QSE_NULL; | ||||
| 	qse_dir_ent_t dirent; | ||||
| 	void* xpath = QSE_NULL; | ||||
| 	void* xdirpath; | ||||
|  | ||||
| 	if (argc < 2) | ||||
| 	{ | ||||
| 		if (flags & QSE_DIR_MBSPATH) | ||||
| 			xdirpath = QSE_MT("."); | ||||
| 		else | ||||
| 			xdirpath = QSE_WT("."); | ||||
|  | ||||
| 		dir = qse_dir_open (QSE_MMGR_GETDFL(), 0, xdirpath, flags, QSE_NULL); | ||||
| 		if (!dir) | ||||
| 		{ | ||||
| 			qse_printf (QSE_T("Cannot open .\n")); | ||||
| 			goto oops; | ||||
| 		} | ||||
| 	} | ||||
| 	else | ||||
| 	{ | ||||
| 		if (flags & QSE_DIR_MBSPATH) | ||||
| 			xpath = qse_strtombsdup (argv[1], QSE_MMGR_GETDFL()); | ||||
| 		else  | ||||
| 			xpath = qse_strtowcsdup (argv[1], QSE_MMGR_GETDFL()); | ||||
|  | ||||
| 		xdirpath = xpath; | ||||
| 		dir = qse_dir_open (QSE_MMGR_GETDFL(), 0, xdirpath, flags, QSE_NULL); | ||||
| 		if (!dir) | ||||
| 		{ | ||||
| 			qse_printf (QSE_T("Cannot open %s\n"), argv[1]); | ||||
| 			goto oops; | ||||
| 		} | ||||
| 	} | ||||
| 	 | ||||
|  | ||||
| 	while (qse_dir_read (dir, &dirent) > 0) | ||||
| 	{ | ||||
| 		if (flags & QSE_DIR_MBSPATH) | ||||
| 			qse_printf (QSE_T("%hs\n"), dirent.name); | ||||
| 		else | ||||
| 			qse_printf (QSE_T("%ls\n"), dirent.name); | ||||
| 	} | ||||
|  | ||||
| 	qse_printf (QSE_T("----------------------------------------\n")); | ||||
| 	if (qse_dir_reset (dir, xdirpath) <= -1) | ||||
| 	{ | ||||
| 		qse_printf (QSE_T("Cannot reset\n")); | ||||
| 		goto oops; | ||||
| 	} | ||||
| 	 | ||||
|  | ||||
| 	while (qse_dir_read (dir, &dirent) > 0) | ||||
| 	{ | ||||
| 		if (flags & QSE_DIR_MBSPATH) | ||||
| 			qse_printf (QSE_T("%hs\n"), dirent.name); | ||||
| 		else | ||||
| 			qse_printf (QSE_T("%ls\n"), dirent.name); | ||||
| 	} | ||||
|  | ||||
| 	QSE_MMGR_FREE(QSE_MMGR_GETDFL(), xpath); | ||||
| 	qse_dir_close (dir); | ||||
| 	return 0; | ||||
|  | ||||
| oops: | ||||
| 	if (xpath) QSE_MMGR_FREE(QSE_MMGR_GETDFL(), xpath); | ||||
| 	if (dir) qse_dir_close(dir); | ||||
| 	return -1; | ||||
| } | ||||
|  | ||||
| static int dir_main (int argc, qse_char_t* argv[]) | ||||
| { | ||||
| 	if (list_dir (argc, argv, QSE_DIR_MBSPATH) <= -1) return -1; | ||||
| 	qse_printf (QSE_T("===============================\n")); | ||||
| 	if (list_dir (argc, argv, QSE_DIR_MBSPATH | QSE_DIR_SORT) <= -1) return -1; | ||||
| 	qse_printf (QSE_T("===============================\n")); | ||||
| 	if (list_dir (argc, argv, QSE_DIR_WCSPATH) <= -1) return -1; | ||||
| 	qse_printf (QSE_T("===============================\n")); | ||||
| 	if (list_dir (argc, argv, QSE_DIR_WCSPATH | QSE_DIR_SORT) <= -1) return -1; | ||||
| 	return 0; | ||||
| } | ||||
|  | ||||
| int qse_main (int argc, qse_achar_t* argv[]) | ||||
| { | ||||
| 	int x; | ||||
|  | ||||
| #if defined(_WIN32) | ||||
|  	char locale[100]; | ||||
| 	UINT codepage = GetConsoleOutputCP(); | ||||
| 	if (codepage == CP_UTF8) | ||||
| 	{ | ||||
| 		/*SetConsoleOUtputCP (CP_UTF8);*/ | ||||
| 		qse_setdflcmgrbyid (QSE_CMGR_UTF8); | ||||
| 	} | ||||
| 	else | ||||
| 	{ | ||||
| 		/* .codepage */ | ||||
| 		qse_fmtuintmaxtombs (locale, QSE_COUNTOF(locale), | ||||
| 			codepage, 10, -1, QSE_MT('\0'), QSE_MT(".")); | ||||
| 		setlocale (LC_ALL, locale); | ||||
| 		/*qse_setdflcmgrbyid (QSE_CMGR_SLMB);*/ | ||||
| 	} | ||||
| #else | ||||
| 	setlocale (LC_ALL, ""); | ||||
| 	/*qse_setdflcmgrbyid (QSE_CMGR_SLMB);*/ | ||||
| #endif | ||||
|  | ||||
| 	qse_open_stdsios (); | ||||
|  | ||||
| 	x = qse_run_main (argc, argv, dir_main); | ||||
|  | ||||
| 	qse_close_stdsios (); | ||||
|  | ||||
| 	return x; | ||||
| } | ||||
|  | ||||
							
								
								
									
										320
									
								
								samples/si/fio01.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										320
									
								
								samples/si/fio01.c
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,320 @@ | ||||
| #include <qse/si/fio.h> | ||||
| #include <qse/cmn/mem.h> | ||||
| #include <qse/cmn/str.h> | ||||
| #include <qse/si/sio.h> | ||||
|  | ||||
| #include <locale.h> | ||||
|  | ||||
| #define R(f) \ | ||||
| 	do { \ | ||||
| 		qse_printf (QSE_T("== %s ==\n"), QSE_T(#f)); \ | ||||
| 		if (f() == -1) return -1; \ | ||||
| 	} while (0) | ||||
|  | ||||
| static int test1 (void) | ||||
| { | ||||
| 	qse_fio_t* fio; | ||||
| 	qse_ssize_t n; | ||||
| 	char x[] = "fio test"; | ||||
| 	char x2[] = "fio test2"; | ||||
| 	qse_fio_off_t off; | ||||
| 	char buf[1000]; | ||||
|  | ||||
| 	fio = qse_fio_open ( | ||||
| 		QSE_MMGR_GETDFL(), | ||||
| 		0, | ||||
| 		QSE_T("fio01-1.txt"),  | ||||
| 		QSE_FIO_READ|QSE_FIO_WRITE|QSE_FIO_CREATE|QSE_FIO_TRUNCATE,  | ||||
| 		QSE_FIO_RUSR|QSE_FIO_WUSR|QSE_FIO_RGRP|QSE_FIO_ROTH | ||||
| 	); | ||||
| 	if (fio == QSE_NULL) | ||||
| 	{ | ||||
| 		qse_printf (QSE_T("cannot open file\n")); | ||||
| 		return -1; | ||||
| 	} | ||||
|  | ||||
| 	n = qse_fio_write (fio, x, qse_mbslen(x)); | ||||
| 	qse_printf (QSE_T("written %d bytes\n"), (int)n); | ||||
|  | ||||
|  | ||||
| 	off = qse_fio_seek (fio, 0, QSE_FIO_CURRENT); | ||||
| 	if (off == (qse_fio_off_t)-1) | ||||
| 	{ | ||||
| 		qse_printf (QSE_T("failed to get file offset\n")); | ||||
| 	} | ||||
| 	else | ||||
| 	{ | ||||
| #if (QSE_SIZEOF_LONG_LONG > 0) | ||||
| 		qse_printf (QSE_T("file offset at %lld\n"), (long long)off); | ||||
| #else | ||||
| 		qse_printf (QSE_T("file offset at %ld\n"), (long)off); | ||||
| #endif | ||||
| 	} | ||||
|  | ||||
| 	off = qse_fio_seek (fio, 0, QSE_FIO_BEGIN); | ||||
| 	if (off == (qse_fio_off_t)-1) | ||||
| 	{ | ||||
| 		qse_printf (QSE_T("failed to set file offset\n")); | ||||
| 	} | ||||
| 	else | ||||
| 	{ | ||||
| #if (QSE_SIZEOF_LONG_LONG > 0) | ||||
| 		qse_printf (QSE_T("moved file offset to %lld\n"), (long long)off); | ||||
| #else | ||||
| 		qse_printf (QSE_T("moved file offset to %ld\n"), (long)off); | ||||
| #endif | ||||
| 	} | ||||
|  | ||||
| 	n = qse_fio_read (fio, buf, sizeof(buf)); | ||||
| 	qse_printf (QSE_T("read %d bytes \n"), (int)n); | ||||
| 	if (n > 0) | ||||
| 	{ | ||||
| 	#ifdef QSE_CHAR_IS_MCHAR | ||||
| 	qse_printf (QSE_T("buf => [%.*s]\n"), (int)n, buf); | ||||
| 	#else | ||||
| 	qse_printf (QSE_T("buf => [%.*S]\n"), (int)n, buf); | ||||
| 	#endif | ||||
| 	} | ||||
|  | ||||
| #if (QSE_SIZEOF_LONG_LONG > 0) | ||||
| 	off = qse_fio_seek (fio, QSE_TYPE_MAX(int) * 3llu, QSE_FIO_BEGIN); | ||||
| #else | ||||
| 	off = qse_fio_seek (fio, QSE_TYPE_MAX(int), QSE_FIO_BEGIN); | ||||
| #endif | ||||
| 	if (off == (qse_fio_off_t)-1) | ||||
| 	{ | ||||
| 		qse_printf (QSE_T("failed to set file offset\n")); | ||||
| 	} | ||||
| 	else | ||||
| 	{ | ||||
| #if (QSE_SIZEOF_LONG_LONG > 0) | ||||
| 		qse_printf (QSE_T("moved file offset to %lld\n"), (long long)off); | ||||
| #else | ||||
| 		qse_printf (QSE_T("moved file offset to %ld\n"), (long)off); | ||||
| #endif | ||||
| 	} | ||||
|  | ||||
| 	n = qse_fio_write (fio, x2, qse_mbslen(x2)); | ||||
| 	qse_printf (QSE_T("written %d bytes\n"), (int)n); | ||||
|  | ||||
| 	if (qse_fio_chmod (fio, QSE_FIO_RUSR|QSE_FIO_RGRP) <= -1) | ||||
| 	{ | ||||
| 		qse_printf (QSE_T("failed to change mode\n")); | ||||
| 	} | ||||
| 	else | ||||
| 	{ | ||||
| 		qse_printf (QSE_T("changed mode\n")); | ||||
| 	} | ||||
|  | ||||
| 	qse_fio_close (fio); | ||||
|  | ||||
| 	return 0; | ||||
| } | ||||
|  | ||||
| static int test2 (void) | ||||
| { | ||||
| 	qse_fio_t* fio; | ||||
| 	qse_ssize_t n; | ||||
| 	char x[] = "fio test"; | ||||
| 	char x2[] = "fio test2"; | ||||
| 	qse_fio_off_t off; | ||||
| 	char buf[1000]; | ||||
| 	int i; | ||||
|  | ||||
| 	fio = qse_fio_open ( | ||||
| 		QSE_MMGR_GETDFL(),  | ||||
| 		0,  | ||||
| 		QSE_T("fio01-2.txt"),  | ||||
| 		QSE_FIO_CREATE | QSE_FIO_TRUNCATE | QSE_FIO_APPEND,  | ||||
| 		QSE_FIO_RUSR|QSE_FIO_WUSR|QSE_FIO_RGRP|QSE_FIO_ROTH | ||||
| 	); | ||||
| 	if (fio == QSE_NULL) | ||||
| 	{ | ||||
| 		qse_printf (QSE_T("cannot open file\n")); | ||||
| 		return -1; | ||||
| 	} | ||||
|  | ||||
| 	for (i = 0; i < 2; i++) | ||||
| 	{ | ||||
| 		n = qse_fio_write (fio, x, qse_mbslen(x)); | ||||
| 		qse_printf (QSE_T("written %d bytes\n"), (int)n); | ||||
|  | ||||
| 		off = qse_fio_seek (fio, 0, QSE_FIO_CURRENT); | ||||
| 		if (off == (qse_fio_off_t)-1) | ||||
| 		{ | ||||
| 			qse_printf (QSE_T("failed to get file offset\n")); | ||||
| 		} | ||||
| 		else | ||||
| 		{ | ||||
| #if (QSE_SIZEOF_LONG_LONG > 0) | ||||
| 			qse_printf (QSE_T("file offset at %lld\n"), (long long)off); | ||||
| #else | ||||
| 			qse_printf (QSE_T("file offset at %ld\n"), (long)off); | ||||
| #endif | ||||
| 		} | ||||
|  | ||||
| 		off = qse_fio_seek (fio, 0, QSE_FIO_BEGIN); | ||||
| 		if (off == (qse_fio_off_t)-1) | ||||
| 		{ | ||||
| 			qse_printf (QSE_T("failed to set file offset\n")); | ||||
| 		} | ||||
| 		else | ||||
| 		{ | ||||
| #if (QSE_SIZEOF_LONG_LONG > 0) | ||||
| 			qse_printf (QSE_T("moved file offset to %lld\n"), (long long)off); | ||||
| #else | ||||
| 			qse_printf (QSE_T("moved file offset to %ld\n"), (long)off); | ||||
| #endif | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
|  | ||||
| 	n = qse_fio_read (fio, buf, sizeof(buf)); | ||||
| 	qse_printf (QSE_T("read %d bytes \n"), (int)n); | ||||
| 	if (n > 0) | ||||
| 	{ | ||||
| 	#ifdef QSE_CHAR_IS_MCHAR | ||||
| 	qse_printf (QSE_T("buf => [%.*s]\n"), (int)n, buf); | ||||
| 	#else | ||||
| 	qse_printf (QSE_T("buf => [%.*S]\n"), (int)n, buf); | ||||
| 	#endif | ||||
| 	} | ||||
|  | ||||
| #if (QSE_SIZEOF_LONG_LONG > 0) | ||||
| 	off = qse_fio_seek (fio, QSE_TYPE_MAX(int) * 2llu, QSE_FIO_BEGIN); | ||||
| #else | ||||
| 	off = qse_fio_seek (fio, QSE_TYPE_MAX(int) + 99999lu, QSE_FIO_BEGIN); | ||||
| #endif | ||||
| 	if (off == (qse_fio_off_t)-1) | ||||
| 	{ | ||||
| 		qse_printf (QSE_T("failed to set file offset\n")); | ||||
| 	} | ||||
| 	else | ||||
| 	{ | ||||
| #if (QSE_SIZEOF_LONG_LONG > 0) | ||||
| 		qse_printf (QSE_T("moved file offset to %lld\n"), (long long)off); | ||||
| #else | ||||
| 		qse_printf (QSE_T("moved file offset to %ld\n"), (long)off); | ||||
| #endif | ||||
| 	} | ||||
|  | ||||
| 	n = qse_fio_write (fio, x2, qse_mbslen(x2)); | ||||
| 	qse_printf (QSE_T("written %d bytes\n"), (int)n); | ||||
|  | ||||
| 	off = qse_fio_seek (fio, 0, QSE_FIO_CURRENT); | ||||
| 	if (off == (qse_fio_off_t)-1) | ||||
| 	{ | ||||
| 		qse_printf (QSE_T("failed to get file offset\n")); | ||||
| 	} | ||||
| 	else | ||||
| 	{ | ||||
| #if (QSE_SIZEOF_LONG_LONG > 0) | ||||
| 		qse_printf (QSE_T("file offset at %lld\n"), (long long)off); | ||||
| #else | ||||
| 		qse_printf (QSE_T("file offset at %ld\n"), (long)off); | ||||
| #endif | ||||
| 	} | ||||
|  | ||||
| 	if (qse_fio_truncate (fio, 20000) == -1) | ||||
| 	{ | ||||
| 		qse_printf (QSE_T("failed to truncate the file\n")); | ||||
| 	} | ||||
|  | ||||
| 	n = qse_fio_write (fio, x2, qse_mbslen(x2)); | ||||
| 	qse_printf (QSE_T("written %d bytes\n"), (int)n); | ||||
|  | ||||
| 	off = qse_fio_seek (fio, 0, QSE_FIO_CURRENT); | ||||
| 	if (off == (qse_fio_off_t)-1) | ||||
| 	{ | ||||
| 		qse_printf (QSE_T("failed to get file offset\n")); | ||||
| 	} | ||||
| 	else | ||||
| 	{ | ||||
| #if (QSE_SIZEOF_LONG_LONG > 0) | ||||
| 		qse_printf (QSE_T("file offset at %lld\n"), (long long)off); | ||||
| #else | ||||
| 		qse_printf (QSE_T("file offset at %ld\n"), (long)off); | ||||
| #endif | ||||
| 	} | ||||
|  | ||||
| 	/* on _WIN32, this will fail as this file is opened without  | ||||
| 	 * QSE_FIO_READ. */ | ||||
| 	if (qse_fio_chmod (fio, QSE_FIO_RUSR|QSE_FIO_RGRP) <= -1) | ||||
| 	{ | ||||
| 		qse_printf (QSE_T("failed to change mode\n")); | ||||
| 	} | ||||
| 	else | ||||
| 	{ | ||||
| 		qse_printf (QSE_T("changed mode\n")); | ||||
| 	} | ||||
|  | ||||
| 	qse_fio_close (fio); | ||||
|  | ||||
| 	return 0; | ||||
| } | ||||
|  | ||||
| static int test3 (void) | ||||
| { | ||||
| 	qse_fio_t* fio; | ||||
| 	qse_ssize_t n; | ||||
| 	const qse_mchar_t* x = QSE_MT("this is a test string"); | ||||
| 	qse_fio_off_t off; | ||||
| 	qse_mchar_t buf[1000]; | ||||
|  | ||||
| 	fio = qse_fio_open ( | ||||
| 		QSE_MMGR_GETDFL(), | ||||
| 		0, | ||||
| 		QSE_T("fio01-3.txt"),  | ||||
|  | ||||
| 		QSE_FIO_READ | QSE_FIO_WRITE | | ||||
| 		QSE_FIO_CREATE | QSE_FIO_TRUNCATE,  | ||||
|  | ||||
| 		QSE_FIO_RUSR|QSE_FIO_WUSR|QSE_FIO_RGRP|QSE_FIO_ROTH | ||||
| 	); | ||||
| 	if (fio == QSE_NULL) | ||||
| 	{ | ||||
| 		qse_printf (QSE_T("cannot open file\n")); | ||||
| 		return -1; | ||||
| 	} | ||||
|  | ||||
| 	n = qse_fio_write (fio, x, qse_mbslen(x)); | ||||
| 	qse_printf (QSE_T("written %d chars\n"), (int)n); | ||||
|  | ||||
| 	off = qse_fio_seek (fio, 0, QSE_FIO_BEGIN); | ||||
| 	if (off == (qse_fio_off_t)-1) | ||||
| 	{ | ||||
| 		qse_printf (QSE_T("failed to get file offset\n")); | ||||
| 	} | ||||
|  | ||||
| 	n = qse_fio_read (fio, buf, QSE_COUNTOF(buf)); | ||||
| 	qse_printf (QSE_T("read %d chars\n"), (int)n); | ||||
| 	if (n > 0) | ||||
| 	{ | ||||
| 		qse_printf (QSE_T("[%.*hs]\n"), (int)n,  buf); | ||||
| 	} | ||||
|  | ||||
| 	 | ||||
| 	qse_fio_close (fio); | ||||
| 	return 0; | ||||
| } | ||||
|  | ||||
| int main () | ||||
| { | ||||
| 	qse_open_stdsios (); | ||||
| 	setlocale (LC_ALL, ""); | ||||
|  | ||||
| 	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); | ||||
|  | ||||
| 	qse_printf (QSE_T("--------------------------------------------------------------------------------\n")); | ||||
| 	qse_printf (QSE_T("Run \"rm -f fio01-?.txt\" to delete garbages\n")); | ||||
| 	qse_printf (QSE_T("--------------------------------------------------------------------------------\n")); | ||||
|  | ||||
| 	qse_close_stdsios (); | ||||
| 	return 0; | ||||
| } | ||||
							
								
								
									
										86
									
								
								samples/si/fio02.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										86
									
								
								samples/si/fio02.c
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,86 @@ | ||||
| #include <qse/si/fio.h> | ||||
| #include <qse/cmn/mem.h> | ||||
| #include <qse/cmn/str.h> | ||||
| #include <qse/si/sio.h> | ||||
|  | ||||
| #define R(f) \ | ||||
| 	do { \ | ||||
| 		qse_printf (QSE_T("== %s ==\n"), QSE_T(#f)); \ | ||||
| 		if (f() == -1) return -1; \ | ||||
| 	} while (0) | ||||
|  | ||||
| static int test1 (void) | ||||
| { | ||||
| 	qse_fio_t* fio; | ||||
| 	qse_ssize_t n; | ||||
| 	char x[] = "fio test"; | ||||
| 	qse_fio_off_t off; | ||||
| 	char buf[1000]; | ||||
| 	int i; | ||||
| 	qse_char_t file[] = QSE_T("fio02-XXXX"); | ||||
|  | ||||
| 	fio = qse_fio_open ( | ||||
| 		QSE_MMGR_GETDFL(),  | ||||
| 		0,  | ||||
| 		file, | ||||
| 		QSE_FIO_CREATE | QSE_FIO_EXCLUSIVE | QSE_FIO_TEMPORARY | QSE_FIO_READ | QSE_FIO_WRITE, | ||||
| 		QSE_FIO_RUSR|QSE_FIO_WUSR|QSE_FIO_RGRP|QSE_FIO_ROTH | ||||
| 	); | ||||
| 	if (fio == QSE_NULL) | ||||
| 	{ | ||||
| 		qse_printf (QSE_T("cannot open file - last file tried [%s]\n"), file); | ||||
| 		return -1; | ||||
| 	} | ||||
|  | ||||
| 	qse_printf (QSE_T("file opened: [%s]\n"), file); | ||||
|  | ||||
| 	for (i = 0; i < 2; i++) | ||||
| 	{ | ||||
| 		n = qse_fio_write (fio, x, qse_mbslen(x)); | ||||
| 		qse_printf (QSE_T("written %d bytes\n"), (int)n); | ||||
|  | ||||
| 		off = qse_fio_seek (fio, 0, QSE_FIO_CURRENT); | ||||
| 		if (off == (qse_fio_off_t)-1) | ||||
| 		{ | ||||
| 			qse_printf (QSE_T("failed to get file offset\n")); | ||||
| 		} | ||||
| 		else | ||||
| 		{ | ||||
| 			qse_printf (QSE_T("file offset at %ld\n"), (long)off); | ||||
| 		} | ||||
|  | ||||
| 		off = qse_fio_seek (fio, 0, QSE_FIO_BEGIN); | ||||
| 		if (off == (qse_fio_off_t)-1) | ||||
| 		{ | ||||
| 			qse_printf (QSE_T("failed to set file offset\n")); | ||||
| 		} | ||||
| 		else | ||||
| 		{ | ||||
| 			qse_printf (QSE_T("moved file offset to %ld\n"), (long)off); | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
|  | ||||
| 	n = qse_fio_read (fio, buf, sizeof(buf)); | ||||
| 	qse_printf (QSE_T("read %d bytes \n"), (int)n); | ||||
| 	if (n > 0) | ||||
| 	{ | ||||
| 	#ifdef QSE_CHAR_IS_MCHAR | ||||
| 	qse_printf (QSE_T("buf => [%.*s]\n"), (int)n, buf); | ||||
| 	#else | ||||
| 	qse_printf (QSE_T("buf => [%.*S]\n"), (int)n, buf); | ||||
| 	#endif | ||||
| 	} | ||||
|  | ||||
| 	qse_fio_close (fio); | ||||
|  | ||||
| 	return 0; | ||||
| } | ||||
|  | ||||
| int main () | ||||
| { | ||||
| 	qse_open_stdsios(); | ||||
| 	R (test1); | ||||
| 	qse_close_stdsios(); | ||||
| 	return 0; | ||||
| } | ||||
							
								
								
									
										108
									
								
								samples/si/fs01.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										108
									
								
								samples/si/fs01.c
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,108 @@ | ||||
| #include <qse/si/fs.h> | ||||
| #include <qse/si/sio.h> | ||||
|  | ||||
| #include <qse/cmn/mem.h> | ||||
| #include <qse/cmn/main.h> | ||||
| #include <qse/cmn/mbwc.h> | ||||
|  | ||||
| #include <locale.h> | ||||
| #if defined(_WIN32) | ||||
| #	include <windows.h> | ||||
| #endif | ||||
|  | ||||
|  | ||||
| static void list (qse_fs_t* fs, const qse_char_t* name) | ||||
| { | ||||
| 	qse_fs_ent_t* ent; | ||||
|  | ||||
| 	if (qse_fs_chdir (fs, name) <= -1) | ||||
| 	{ | ||||
| 		qse_fprintf (QSE_STDERR, QSE_T("Error: Cannot change directory to %s - code %d\n"), name, (int)qse_fs_geterrnum(fs)); | ||||
| 		return; | ||||
| 	}	 | ||||
|  | ||||
| 	qse_printf (QSE_T("----------------------------------------------------------------\n"), fs->curdir); | ||||
| 	qse_printf (QSE_T("CURRENT DIRECTORY: [%s]\n"), fs->curdir); | ||||
| 	qse_printf (QSE_T("----------------------------------------------------------------\n"), fs->curdir); | ||||
|  | ||||
| 	do | ||||
| 	{ | ||||
| 		qse_btime_t bt; | ||||
|  | ||||
| 		ent = qse_fs_read (fs, QSE_FS_ENT_SIZE | QSE_FS_ENT_TYPE | QSE_FS_ENT_TIME); | ||||
| 		if (ent == QSE_NULL)  | ||||
| 		{ | ||||
| 			qse_fs_errnum_t e = qse_fs_geterrnum(fs); | ||||
| 			if (e != QSE_FS_ENOERR) | ||||
| 				qse_fprintf (QSE_STDERR, QSE_T("Error: Read error - code %d\n"), (int)qse_fs_geterrnum(fs)); | ||||
| 			break; | ||||
| 		} | ||||
|  | ||||
| 		qse_localtime (&ent->time.modify, &bt); | ||||
| 		qse_printf (QSE_T("%s %16lu %04d-%02d-%02d %02d:%02d %s\n"),  | ||||
| 			((ent->type == QSE_FS_ENT_SUBDIR)? QSE_T("<D>"): QSE_T("   ")), | ||||
| 			(unsigned long)ent->size,  | ||||
| 			bt.year + QSE_BTIME_YEAR_BASE, bt.mon+1, bt.mday, bt.hour, bt.min, | ||||
| 			ent->name.base | ||||
| 		); | ||||
| 	} | ||||
| 	while (1); | ||||
|  | ||||
| } | ||||
|  | ||||
| int fs_main (int argc, qse_char_t* argv[]) | ||||
| { | ||||
| 	qse_fs_t* fs; | ||||
|  | ||||
| 	if (argc != 2) | ||||
| 	{ | ||||
| 		qse_fprintf (QSE_STDERR, QSE_T("Usage: %s <directory>\n"), argv[0]); | ||||
| 		return -1; | ||||
| 	} | ||||
|  | ||||
| 	fs = qse_fs_open (QSE_MMGR_GETDFL(), 0); | ||||
| 	if (fs == QSE_NULL) | ||||
| 	{ | ||||
| 		qse_fprintf (QSE_STDERR, QSE_T("Error: Cannot open directory\n"), argv[1]); | ||||
| 		return -1; | ||||
| 	} | ||||
|  | ||||
| 	list (fs, argv[1]); | ||||
| 	list (fs, QSE_T("..")); | ||||
|  | ||||
| 	qse_fs_close (fs); | ||||
| 	return 0; | ||||
| } | ||||
|  | ||||
| int qse_main (int argc, qse_achar_t* argv[]) | ||||
| { | ||||
| 	int x; | ||||
|  | ||||
| #if defined(_WIN32) | ||||
| 	char locale[100]; | ||||
| 	UINT codepage = GetConsoleOutputCP(); | ||||
| 	if (codepage == CP_UTF8) | ||||
| 	{ | ||||
| 		/*SetConsoleOUtputCP (CP_UTF8);*/ | ||||
| 		qse_setdflcmgrbyid (QSE_CMGR_UTF8); | ||||
| 	} | ||||
| 	else | ||||
| 	{ | ||||
| 		sprintf (locale, ".%u", (unsigned int)codepage); | ||||
| 		setlocale (LC_ALL, locale); | ||||
| 		/*qse_setdflcmgrbyid (QSE_CMGR_SLMB);*/ | ||||
| 	} | ||||
| #else | ||||
| 	setlocale (LC_ALL, ""); | ||||
| 	/*qse_setdflcmgrbyid (QSE_CMGR_SLMB);*/ | ||||
| #endif | ||||
|  | ||||
| 	qse_open_stdsios (); | ||||
|  | ||||
| 	x = qse_run_main (argc, argv, fs_main); | ||||
|  | ||||
| 	qse_close_stdsios (); | ||||
|  | ||||
| 	return x; | ||||
| } | ||||
|  | ||||
							
								
								
									
										163
									
								
								samples/si/fs02.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										163
									
								
								samples/si/fs02.c
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,163 @@ | ||||
| #include <qse/si/fs.h> | ||||
| #include <qse/cmn/mem.h> | ||||
| #include <qse/cmn/str.h> | ||||
| #include <qse/si/sio.h> | ||||
| #include <qse/cmn/path.h> | ||||
| #include <qse/cmn/main.h> | ||||
| #include <qse/cmn/mbwc.h> | ||||
| #include <qse/cmn/fmt.h> | ||||
| #include <locale.h> | ||||
|  | ||||
| #if defined(_WIN32) | ||||
| #	include <windows.h> | ||||
| #endif | ||||
|  | ||||
| static int fs_actcb (qse_fs_t* fs, qse_fs_action_t act, const qse_char_t* srcpath, const qse_char_t* dstpath, qse_uintmax_t total, qse_uintmax_t done) | ||||
| { | ||||
| 	switch (act) | ||||
| 	{ | ||||
| 		case QSE_FS_CPFILE: | ||||
| 			if (total == done) qse_printf (QSE_T("Copied [%s] to [%s]\n"), srcpath, dstpath); | ||||
| 			break; | ||||
|  | ||||
| 		case QSE_FS_RMFILE: | ||||
| 			qse_printf (QSE_T("Removed file [%s]\n"), srcpath); | ||||
| 			break; | ||||
|  | ||||
| 		case QSE_FS_SYMLINK: | ||||
| 			qse_printf (QSE_T("Symlinked [%s] to [%s]\n"), srcpath, dstpath); | ||||
| 			break; | ||||
|  | ||||
| 		case QSE_FS_MKDIR: | ||||
| 			qse_printf (QSE_T("Made directory [%s]\n"), srcpath); | ||||
| 			break; | ||||
|  | ||||
| 		case QSE_FS_RMDIR: | ||||
| 			qse_printf (QSE_T("Removed directory [%s]\n"), srcpath); | ||||
| 			break; | ||||
|  | ||||
| 		case QSE_FS_RENFILE: | ||||
| 			qse_printf (QSE_T("renamed  [%s] to [%s]\n"), srcpath); | ||||
| 			break; | ||||
| 	} | ||||
|  | ||||
| /*if (qse_strcmp(path, QSE_T("b/c")) == 0) return 0;*/ | ||||
|         return 1; | ||||
| } | ||||
|  | ||||
|  | ||||
| static void print_usage (const qse_char_t* argv0) | ||||
| { | ||||
| 	qse_fprintf (QSE_STDERR, QSE_T("Usage: %s command filename\n"), qse_basename(argv0)); | ||||
| 	qse_fprintf (QSE_STDERR, QSE_T("Command is one of rmfile | rmfile-r | rmdir | rmdir-r | mkdir | mkdir-p\n")); | ||||
| 	qse_fprintf (QSE_STDERR, QSE_T("Filename is a pattern for delXXX\n")); | ||||
| } | ||||
|  | ||||
| static int fs_main (int argc, qse_char_t* argv[]) | ||||
| { | ||||
| 	qse_fs_t* fs; | ||||
| 	qse_fs_cbs_t cbs; | ||||
| 	int ret = 0; | ||||
|  | ||||
| 	if (argc != 3) | ||||
| 	{ | ||||
| 		print_usage (argv[0]); | ||||
| 		return -1; | ||||
| 	} | ||||
| 	fs = qse_fs_open (QSE_MMGR_GETDFL(), 0); | ||||
|  | ||||
| 	qse_memset (&cbs, 0, QSE_SIZEOF(cbs)); | ||||
| 	cbs.actcb = fs_actcb; | ||||
| 	qse_fs_setopt (fs, QSE_FS_CBS, &cbs); | ||||
|  | ||||
| 	if (qse_strcmp(argv[1], QSE_T("rmfile")) == 0) | ||||
| 	{ | ||||
| 		if (qse_fs_rmfile (fs, argv[2], QSE_FS_RMFILEMBS_GLOB) <= -1) | ||||
| 		{ | ||||
| 			qse_fprintf (QSE_STDERR, QSE_T("cannot delete files - %d\n"), qse_fs_geterrnum(fs)); | ||||
| 			ret = -1; | ||||
| 		} | ||||
| 	} | ||||
| 	else if (qse_strcmp(argv[1], QSE_T("rmfile-r")) == 0) | ||||
| 	{ | ||||
| 		if (qse_fs_rmfile (fs, argv[2], QSE_FS_RMFILE_GLOB | QSE_FS_RMFILE_RECURSIVE) <= -1) | ||||
| 		{ | ||||
| 			qse_fprintf (QSE_STDERR, QSE_T("cannot delete files - %d\n"), qse_fs_geterrnum(fs)); | ||||
| 			ret = -1; | ||||
| 		} | ||||
| 	} | ||||
| 	else if (qse_strcmp (argv[1], QSE_T("rmdir")) == 0) | ||||
| 	{ | ||||
| 		if (qse_fs_rmdir (fs, argv[2], QSE_FS_RMDIR_GLOB) <= -1) | ||||
| 		{ | ||||
| 			qse_fprintf (QSE_STDERR, QSE_T("cannot delete directories - %d\n"), qse_fs_geterrnum(fs)); | ||||
| 			ret = -1; | ||||
| 		} | ||||
| 	} | ||||
| 	else if (qse_strcmp (argv[1], QSE_T("rmdir-r")) == 0) | ||||
| 	{ | ||||
| 		if (qse_fs_rmdir (fs, argv[2], QSE_FS_RMDIR_GLOB | QSE_FS_RMDIR_RECURSIVE) <= -1) | ||||
| 		{ | ||||
| 			qse_fprintf (QSE_STDERR, QSE_T("cannot delete directories - %d\n"), qse_fs_geterrnum(fs)); | ||||
| 			ret = -1; | ||||
| 		} | ||||
| 	} | ||||
| 	else if (qse_strcmp (argv[1], QSE_T("mkdir")) == 0) | ||||
| 	{ | ||||
| 		if (qse_fs_mkdir (fs, argv[2], 0755, 0) <= -1) | ||||
| 		{ | ||||
| 			qse_fprintf (QSE_STDERR, QSE_T("cannot make directory - %d\n"), qse_fs_geterrnum(fs)); | ||||
| 			ret = -1; | ||||
| 		} | ||||
| 	} | ||||
| 	else if (qse_strcmp (argv[1], QSE_T("mkdir-p")) == 0) | ||||
| 	{ | ||||
| 		if (qse_fs_mkdir (fs, argv[2], 0755, QSE_FS_MKDIR_PARENT) <= -1) | ||||
| 		{ | ||||
| 			qse_fprintf (QSE_STDERR, QSE_T("cannot make directory - %d\n"), qse_fs_geterrnum(fs)); | ||||
| 			ret = -1; | ||||
| 		} | ||||
| 	} | ||||
| 	else | ||||
| 	{ | ||||
| 		print_usage (argv[0]); | ||||
| 		ret = -1; | ||||
| 	} | ||||
|  | ||||
| 	qse_fs_close (fs); | ||||
| 	return ret; | ||||
| } | ||||
|  | ||||
| int main (int argc, qse_achar_t* argv[]) | ||||
| { | ||||
| 	int x; | ||||
| #if defined(_WIN32) | ||||
|  	char locale[100]; | ||||
| 	UINT codepage = GetConsoleOutputCP(); | ||||
| 	if (codepage == CP_UTF8) | ||||
| 	{ | ||||
| 		/*SetConsoleOUtputCP (CP_UTF8);*/ | ||||
| 		qse_setdflcmgrbyid (QSE_CMGR_UTF8); | ||||
| 	} | ||||
| 	else | ||||
| 	{ | ||||
| 		/* .codepage */ | ||||
| 		qse_fmtuintmaxtombs (locale, QSE_COUNTOF(locale), | ||||
| 			codepage, 10, -1, QSE_MT('\0'), QSE_MT(".")); | ||||
| 		setlocale (LC_ALL, locale); | ||||
| 		/*qse_setdflcmgrbyid (QSE_CMGR_SLMB);*/ | ||||
| 	} | ||||
| #else | ||||
| 	setlocale (LC_ALL, ""); | ||||
| 	/*qse_setdflcmgrbyid (QSE_CMGR_SLMB);*/ | ||||
| #endif | ||||
|  | ||||
| 	qse_open_stdsios (); | ||||
|  | ||||
| 	x = qse_run_main (argc, argv, fs_main); | ||||
|  | ||||
| 	qse_close_stdsios (); | ||||
|  | ||||
| 	return x; | ||||
| } | ||||
|  | ||||
							
								
								
									
										193
									
								
								samples/si/fs03.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										193
									
								
								samples/si/fs03.c
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,193 @@ | ||||
| #include <qse/si/fs.h> | ||||
| #include <qse/cmn/mem.h> | ||||
| #include <qse/cmn/str.h> | ||||
| #include <qse/si/sio.h> | ||||
| #include <qse/cmn/path.h> | ||||
| #include <qse/cmn/main.h> | ||||
| #include <qse/cmn/mbwc.h> | ||||
| #include <qse/cmn/opt.h> | ||||
| #include <qse/cmn/fmt.h> | ||||
| #include <locale.h> | ||||
|  | ||||
| #if defined(_WIN32) | ||||
| #	include <windows.h> | ||||
| #endif | ||||
|  | ||||
| static void print_usage (const qse_char_t* argv0) | ||||
| { | ||||
| 	qse_fprintf (QSE_STDERR, QSE_T("Usage: %s [options] source-filename target-filename\n"), qse_basename(argv0)); | ||||
| 	qse_fprintf (QSE_STDERR, QSE_T("Options include:\n")); | ||||
| 	qse_fprintf (QSE_STDERR, QSE_T("  -f            force\n")); | ||||
| 	qse_fprintf (QSE_STDERR, QSE_T("  -o            overwrite\n")); | ||||
| 	qse_fprintf (QSE_STDERR, QSE_T("  -p            preserve\n")); | ||||
| 	qse_fprintf (QSE_STDERR, QSE_T("  -r            recursive\n")); | ||||
| 	qse_fprintf (QSE_STDERR, QSE_T("  -s            symlink\n")); | ||||
| 	qse_fprintf (QSE_STDERR, QSE_T("  -g            glob\n")); | ||||
| 	qse_fprintf (QSE_STDERR, QSE_T("  -w            chown instead. wins over other options. honors -s\n")); | ||||
| } | ||||
|  | ||||
| static int fs_actcb (qse_fs_t* fs, qse_fs_action_t act, const qse_char_t* srcpath, const qse_char_t* dstpath, qse_uintmax_t total, qse_uintmax_t done) | ||||
| { | ||||
| 	switch (act) | ||||
| 	{ | ||||
| 		case QSE_FS_CPFILE: | ||||
| 			if (total == done) qse_printf (QSE_T("Copied [%s] to [%s]\n"), srcpath, dstpath); | ||||
| 			break; | ||||
|  | ||||
| 		case QSE_FS_RMFILE: | ||||
| 			qse_printf (QSE_T("Removed file [%s]\n"), srcpath); | ||||
| 			break; | ||||
|  | ||||
| 		case QSE_FS_SYMLINK: | ||||
| 			qse_printf (QSE_T("Symlinked [%s] to [%s]\n"), srcpath, dstpath); | ||||
| 			break; | ||||
|  | ||||
| 		case QSE_FS_MKDIR: | ||||
| 			qse_printf (QSE_T("Made directory [%s]\n"), srcpath); | ||||
| 			break; | ||||
|  | ||||
| 		case QSE_FS_RMDIR: | ||||
| 			qse_printf (QSE_T("Removed directory [%s]\n"), srcpath); | ||||
| 			break; | ||||
|  | ||||
| 		case QSE_FS_RENFILE: | ||||
| 			qse_printf (QSE_T("renamed  [%s] to [%s]\n"), srcpath); | ||||
| 			break; | ||||
| 	} | ||||
|  | ||||
| /*if (qse_strcmp(path, QSE_T("b/c")) == 0) return 0;*/ | ||||
|         return 1; | ||||
| } | ||||
|  | ||||
|  | ||||
| static int fs_main (int argc, qse_char_t* argv[]) | ||||
| { | ||||
| 	qse_fs_t* fs; | ||||
| 	qse_fs_cbs_t cbs; | ||||
| 	int ret = 0; | ||||
| 	qse_cint_t c; | ||||
| 	int cpfile_flags = 0; | ||||
| 	int act_chown = 0; | ||||
|  | ||||
| 	static qse_opt_t opt =  | ||||
| 	{ | ||||
| 		QSE_T("foprsgw"), | ||||
| 		QSE_NULL | ||||
| 	}; | ||||
|  | ||||
| 	while ((c = qse_getopt (argc, argv, &opt)) != QSE_CHAR_EOF) | ||||
| 	{ | ||||
| 		switch (c) | ||||
| 		{ | ||||
| 			case QSE_T('f'): | ||||
| 				cpfile_flags |= QSE_FS_CPFILE_FORCE; | ||||
| 				break; | ||||
|  | ||||
| 			case QSE_T('o'): | ||||
| 				cpfile_flags |= QSE_FS_CPFILE_REPLACE; | ||||
| 				break; | ||||
|  | ||||
| 			case QSE_T('p'): | ||||
| 				cpfile_flags |= QSE_FS_CPFILE_PRESERVE; | ||||
| 				break; | ||||
|  | ||||
| 			case QSE_T('r'): | ||||
| 				cpfile_flags |= QSE_FS_CPFILE_RECURSIVE; | ||||
| 				break; | ||||
|  | ||||
| 			case QSE_T('s'): | ||||
| 				cpfile_flags |= QSE_FS_CPFILE_SYMLINK; | ||||
| 				break; | ||||
|  | ||||
| 			case QSE_T('g'): | ||||
| 				cpfile_flags |= QSE_FS_CPFILE_GLOB; | ||||
| 				break; | ||||
|  | ||||
| 			case QSE_T('w'): | ||||
| 				act_chown = 1; | ||||
| 				break; | ||||
|  | ||||
| 			case QSE_T('?'): | ||||
| 				qse_fprintf (QSE_STDERR, QSE_T("illegal option - '%c'\n"), opt.opt); | ||||
| 				goto wrong_usage; | ||||
|  | ||||
| 			case QSE_T(':'): | ||||
| 				qse_fprintf (QSE_STDERR, QSE_T("bad argument for '%c'\n"), opt.opt); | ||||
| 				goto wrong_usage; | ||||
|  | ||||
| 			default: | ||||
| 				goto wrong_usage; | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	if (opt.ind  + 2 != argc) goto wrong_usage; | ||||
|  | ||||
| 	fs = qse_fs_open (QSE_MMGR_GETDFL(), 0); | ||||
|  | ||||
| 	if (act_chown) | ||||
| 	{ | ||||
| 		qse_fattr_t attr; | ||||
| 		int attr_flags = QSE_FILE_ATTR_OWNER; | ||||
| 		if (cpfile_flags & QSE_FS_CPFILE_SYMLINK) attr_flags |= QSE_FILE_ATTR_SYMLINK; | ||||
| 		attr.uid = qse_strtoulong(argv[opt.ind], 10, QSE_NULL); | ||||
| 		attr.gid = -1; | ||||
| 		if (qse_fs_setattr(fs, argv[opt.ind + 1], &attr, attr_flags) <= -1) | ||||
| 		{ | ||||
| 			qse_fprintf (QSE_STDERR, QSE_T("cannot change file uid - %d\n"), qse_fs_geterrnum(fs)); | ||||
| 			ret = -1; | ||||
| 		} | ||||
| 	} | ||||
| 	else | ||||
| 	{ | ||||
| 		qse_memset (&cbs, 0, QSE_SIZEOF(cbs)); | ||||
| 		cbs.actcb = fs_actcb; | ||||
| 		qse_fs_setopt (fs, QSE_FS_CBS, &cbs); | ||||
|  | ||||
| 		if (qse_fs_cpfile(fs, argv[opt.ind], argv[opt.ind + 1], cpfile_flags) <= -1) | ||||
| 		{ | ||||
| 			qse_fprintf (QSE_STDERR, QSE_T("cannot copy file - %d\n"), qse_fs_geterrnum(fs)); | ||||
| 			ret = -1; | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	qse_fs_close (fs); | ||||
| 	return ret; | ||||
|  | ||||
| wrong_usage: | ||||
| 	print_usage (argv[0]); | ||||
| 	return -1; | ||||
| } | ||||
|  | ||||
| int main (int argc, qse_achar_t* argv[]) | ||||
| { | ||||
| 	int x; | ||||
| #if defined(_WIN32) | ||||
|  	char locale[100]; | ||||
| 	UINT codepage = GetConsoleOutputCP(); | ||||
| 	if (codepage == CP_UTF8) | ||||
| 	{ | ||||
| 		/*SetConsoleOUtputCP (CP_UTF8);*/ | ||||
| 		qse_setdflcmgrbyid (QSE_CMGR_UTF8); | ||||
| 	} | ||||
| 	else | ||||
| 	{ | ||||
| 		/* .codepage */ | ||||
| 		qse_fmtuintmaxtombs (locale, QSE_COUNTOF(locale), | ||||
| 			codepage, 10, -1, QSE_MT('\0'), QSE_MT(".")); | ||||
| 		setlocale (LC_ALL, locale); | ||||
| 		/*qse_setdflcmgrbyid (QSE_CMGR_SLMB);*/ | ||||
| 	} | ||||
| #else | ||||
| 	setlocale (LC_ALL, ""); | ||||
| 	/*qse_setdflcmgrbyid (QSE_CMGR_SLMB);*/ | ||||
| #endif | ||||
|  | ||||
| 	qse_open_stdsios (); | ||||
|  | ||||
| 	x = qse_run_main (argc, argv, fs_main); | ||||
|  | ||||
| 	qse_close_stdsios (); | ||||
|  | ||||
| 	return x; | ||||
| } | ||||
|  | ||||
							
								
								
									
										72
									
								
								samples/si/glob01.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										72
									
								
								samples/si/glob01.c
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,72 @@ | ||||
| #include <qse/si/glob.h> | ||||
| #include <qse/si/sio.h> | ||||
| #include <qse/cmn/main.h> | ||||
| #include <qse/cmn/mbwc.h> | ||||
| #include <qse/cmn/str.h> | ||||
| #include <qse/cmn/mem.h> | ||||
| #include <qse/cmn/fmt.h> | ||||
| #include <qse/cmn/path.h> | ||||
|  | ||||
| #include <locale.h> | ||||
| #if defined(_WIN32) | ||||
| #	include <windows.h> | ||||
| #endif | ||||
|  | ||||
| static int print (const qse_cstr_t* path, void* ctx) | ||||
| { | ||||
| 	qse_printf  (QSE_T("[%.*s]\n"), (int)path->len, path->ptr); | ||||
| 	return 0; | ||||
| } | ||||
|  | ||||
| static int glob_main (int argc, qse_char_t* argv[]) | ||||
| { | ||||
| 	int i; | ||||
|  | ||||
| 	if (argc <= 1) | ||||
| 	{ | ||||
| 		qse_fprintf (QSE_STDERR, QSE_T("Usage: %s file-pattern ...\n"), qse_basename(argv[0])); | ||||
| 		return -1; | ||||
| 	} | ||||
|  | ||||
| 	for (i = 1; i < argc; i++) | ||||
| 	{ | ||||
| 		if (qse_glob (argv[i], print, QSE_NULL, QSE_GLOB_PERIOD,  | ||||
| 		              qse_getdflmmgr(), qse_getdflcmgr()) <= -1) return -1; | ||||
| 	} | ||||
|  | ||||
| 	return 0; | ||||
| } | ||||
|  | ||||
| int qse_main (int argc, qse_achar_t* argv[]) | ||||
| { | ||||
| 	int x; | ||||
| #if defined(_WIN32) | ||||
|  	char locale[100]; | ||||
| 	UINT codepage = GetConsoleOutputCP(); | ||||
| 	if (codepage == CP_UTF8) | ||||
| 	{ | ||||
| 		/*SetConsoleOUtputCP (CP_UTF8);*/ | ||||
| 		qse_setdflcmgrbyid (QSE_CMGR_UTF8); | ||||
| 	} | ||||
| 	else | ||||
| 	{ | ||||
| 		/* .codepage */ | ||||
| 		qse_fmtuintmaxtombs (locale, QSE_COUNTOF(locale), | ||||
| 			codepage, 10, -1, QSE_MT('\0'), QSE_MT(".")); | ||||
| 		setlocale (LC_ALL, locale); | ||||
| 		/*qse_setdflcmgrbyid (QSE_CMGR_SLMB);*/ | ||||
| 	} | ||||
| #else | ||||
| 	setlocale (LC_ALL, ""); | ||||
| 	/*qse_setdflcmgrbyid (QSE_CMGR_SLMB);*/ | ||||
| #endif | ||||
|  | ||||
| 	qse_open_stdsios (); | ||||
|  | ||||
| 	x = qse_run_main (argc, argv, glob_main); | ||||
|  | ||||
| 	qse_close_stdsios (); | ||||
|  | ||||
| 	return x; | ||||
| } | ||||
|  | ||||
							
								
								
									
										117
									
								
								samples/si/log01.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										117
									
								
								samples/si/log01.c
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,117 @@ | ||||
| #include <qse/cmn/mem.h> | ||||
| #include <qse/cmn/hwad.h> | ||||
| #include <qse/si/log.h> | ||||
| #include <qse/si/nwad.h> | ||||
| #include <stdio.h> | ||||
|  | ||||
| static void t1_sub001 (qse_log_t* log) | ||||
| { | ||||
| 	static struct | ||||
| 	{ | ||||
| 		const qse_char_t* name; | ||||
| 		int pri; | ||||
| 	} xtab[] = | ||||
| 	{ | ||||
| 		{ QSE_T(""),                          0                                             }, | ||||
| 		{ QSE_T("   "),                       0                                             }, | ||||
| 		{ QSE_T("|"),                         0                                             }, | ||||
| 		{ QSE_T("debug|xinfo|panic"),         0                                             }, | ||||
| 		{ QSE_T("debug|info|panic"),          (QSE_LOG_DEBUG | QSE_LOG_INFO | QSE_LOG_PANIC) }, | ||||
| 		{ QSE_T("debug|notice|debug|panic"),  (QSE_LOG_DEBUG | QSE_LOG_NOTICE | QSE_LOG_PANIC) }, | ||||
| 		{ QSE_T("notice"),                    QSE_LOG_NOTICE                                 } | ||||
| 	}; | ||||
| 	qse_size_t i; | ||||
| 	qse_char_t buf[QSE_LOG_PRIORITY_LEN_MAX + 1]; | ||||
| 	qse_size_t len; | ||||
|  | ||||
| 	for (i = 0; i < QSE_COUNTOF(xtab); i++) | ||||
| 	{ | ||||
| 		len = qse_make_log_priority_name (xtab[i].pri, QSE_T("|"), buf, QSE_COUNTOF(buf)); | ||||
| 		QSE_LOG3 (log, QSE_NULL, QSE_LOG_INFO, QSE_T("%x => %s [%d]"), xtab[i].pri, buf, len); | ||||
| 	} | ||||
| 	 | ||||
| 	for  (i = 0; i < QSE_COUNTOF(xtab); i++) | ||||
| 	{ | ||||
| 		int pri; | ||||
| 		if ((pri = qse_get_log_priority_by_name(xtab[i].name, QSE_T("|"))) == xtab[i].pri) | ||||
| 		{ | ||||
| 			QSE_LOG2 (log, QSE_NULL, QSE_LOG_INFO, QSE_T("SUCCESS: %s => %x"), xtab[i].name, pri); | ||||
| 		} | ||||
| 		else | ||||
| 		{ | ||||
| 			QSE_LOG3 (log, QSE_NULL, QSE_LOG_INFO, QSE_T("FAILURE: %s => got %x expected %x"), xtab[i].name, pri, xtab[i].pri); | ||||
| 		} | ||||
| 	} | ||||
| } | ||||
|  | ||||
| static void t1 (void) | ||||
| { | ||||
| 	qse_log_t* log; | ||||
| 	qse_log_target_data_t t; | ||||
| #if defined(QSE_HAVE_INT128_T) | ||||
| 	qse_int128_t q = 0x1234567890; | ||||
| #elif defined(QSE_HAVE_INT64_T) | ||||
| 	qse_int64_t q = 0x1234567890; | ||||
| #else | ||||
| 	qse_int32_t q = 0x12345678; | ||||
| #endif | ||||
| 	int i; | ||||
| 	qse_nwad_t nwad; | ||||
|  | ||||
| 	t.file = QSE_T("/tmp/t3.log"); | ||||
| 	/*qse_strtonwad ("127.0.0.1:514", &nwad);*/ | ||||
| 	/*qse_strtonwad ("@/var/run/log", &nwad);*/ | ||||
| 	qse_strtonwad (QSE_T("@/dev/log"), &nwad); | ||||
| 	qse_nwadtoskad (&nwad, &t.syslog_remote); | ||||
|  | ||||
| 	log = qse_log_open (QSE_MMGR_GETDFL(), 0, QSE_T("t3"),  | ||||
| 		QSE_LOG_INCLUDE_PID | QSE_LOG_HOST_IN_REMOTE_SYSLOG | | ||||
| 		QSE_LOG_DEBUG | | ||||
| 		QSE_LOG_CONSOLE | QSE_LOG_FILE | QSE_LOG_SYSLOG | QSE_LOG_SYSLOG_REMOTE, &t); | ||||
|  | ||||
| 	QSE_ASSERT (qse_log_getoption (log) == (QSE_LOG_INCLUDE_PID | QSE_LOG_HOST_IN_REMOTE_SYSLOG)); | ||||
| 	QSE_ASSERT (qse_log_gettarget (log, QSE_NULL) == (QSE_LOG_CONSOLE | QSE_LOG_FILE | QSE_LOG_SYSLOG | QSE_LOG_SYSLOG_REMOTE)); | ||||
|  | ||||
| 	t1_sub001 (log); | ||||
|  | ||||
| 	for (i = 0; i < 10; i++) | ||||
| 	{ | ||||
|  | ||||
| 		if (i == 4) | ||||
| 		{ | ||||
| 			qse_log_target_data_t t2; | ||||
|  | ||||
| 			qse_log_gettarget (log, &t2); | ||||
| 			qse_strtonwad (QSE_T("127.0.0.1:514"), &nwad); | ||||
| 			qse_nwadtoskad (&nwad, &t2.syslog_remote); | ||||
| 			qse_log_settarget (log, QSE_LOG_CONSOLE | QSE_LOG_FILE | QSE_LOG_SYSLOG_REMOTE, &t2); | ||||
|  | ||||
| 			qse_log_setoption (log, qse_log_getoption(log) | QSE_LOG_KEEP_FILE_OPEN); | ||||
|  | ||||
| 			QSE_ASSERT (qse_log_getoption (log) == (QSE_LOG_INCLUDE_PID | QSE_LOG_HOST_IN_REMOTE_SYSLOG | QSE_LOG_KEEP_FILE_OPEN)); | ||||
| 			QSE_ASSERT (qse_log_gettarget (log, QSE_NULL) == (QSE_LOG_CONSOLE | QSE_LOG_FILE | QSE_LOG_SYSLOG_REMOTE)); | ||||
| 		} | ||||
|  | ||||
| 	#if defined(QSE_HAVE_INT128_T) | ||||
| 		QSE_LOG4 (log, QSE_T("test"), QSE_LOG_DEBUG, QSE_T("MSG %d %I128x %#0128I128b %l20d >>"), 10 * i , q, q, (long)45); | ||||
| 	#elif defined(QSE_HAVE_INT64_T) | ||||
| 		QSE_LOG4 (log, QSE_T("test"), QSE_LOG_DEBUG, QSE_T("MSG %d %I64x %#064I64b %l20d >>"), 10 * i , q, q, (long)45); | ||||
| 	#else | ||||
| 		QSE_LOG4 (log, QSE_T("test"), QSE_LOG_DEBUG, QSE_T("MSG %d %I32x %#032I32b %l20d >>"), 10 * i , q, q, (long)45); | ||||
| 	#endif | ||||
| 	} | ||||
|  | ||||
|  | ||||
| #if defined(QSE_LOG) | ||||
| 	QSE_LOG (log, QSE_T("var"), QSE_LOG_INFO, QSE_T("variadic QSE_LOG() supported - no argument")); | ||||
| 	QSE_LOG (log, QSE_T("var"), QSE_LOG_ERROR, QSE_T("variadic QSE_LOG() supported %d %d"), 1, 2); | ||||
| 	QSE_LOG (log, QSE_T("var"), QSE_LOG_PANIC, QSE_T("variadic QSE_LOG() supported %10s"), QSE_T("panic")); | ||||
| #endif | ||||
| 	qse_log_close (log); | ||||
| } | ||||
|  | ||||
| int main (int argc, char* argv[]) | ||||
| { | ||||
| 	t1 (); | ||||
| 	return 0; | ||||
| } | ||||
							
								
								
									
										181
									
								
								samples/si/nwad01.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										181
									
								
								samples/si/nwad01.c
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,181 @@ | ||||
| #include <qse/si/nwad.h> | ||||
| #include <qse/cmn/main.h> | ||||
| #include <qse/cmn/mbwc.h> | ||||
| #include <qse/si/sio.h> | ||||
|  | ||||
| #include <locale.h> | ||||
| #if defined(_WIN32) | ||||
| #	include <windows.h> | ||||
| #endif | ||||
|  | ||||
| static int test_main (int argc, qse_char_t* argv[]) | ||||
| { | ||||
| 	qse_nwad_t nwad; | ||||
| 	qse_char_t buf[64]; | ||||
| 	qse_mchar_t mbsbuf[64]; | ||||
| 	qse_wchar_t wcsbuf[64]; | ||||
| 	qse_size_t i; | ||||
|  | ||||
| 	static qse_char_t* ipstr[] = | ||||
| 	{ | ||||
| 		QSE_T("192.168.1.1"), | ||||
| 		QSE_T("255.255.255.255"), | ||||
| 		QSE_T("4.3.0.0"), | ||||
| 		QSE_T("4.3.0.0X"), | ||||
| 		QSE_T("65.1234.11.34"), | ||||
| 		QSE_T("65.123.11.34"), | ||||
| 		QSE_T("1.1.1.1"), | ||||
| 		QSE_T("::"), | ||||
| 		QSE_T("::1"), | ||||
| 		QSE_T("fe80::f27b:cbff:fea3:f40c"), | ||||
| 		QSE_T("2001:0db8:85a3:0000:0000:8a2e:0370:7334"), | ||||
| 		QSE_T("2001:db8:1234:ffff:ffff:ffff:ffff:ffff"), | ||||
| 		QSE_T("::ffff:0:0"), | ||||
| 		QSE_T("::ffff:192.168.1.1"), | ||||
| 		QSE_T("::ffff:192.168.1.1%88"), | ||||
| 		QSE_T("::ffff:192.168.1.1%eth0"), | ||||
| 		QSE_T("::ffff:192.168.1.1%eth1"), | ||||
| 		QSE_T("[::]:10"), | ||||
| 		QSE_T("[::1]:20"), | ||||
| 		QSE_T("[fe80::f27b:cbff:fea3:f40c]:30"), | ||||
| 		QSE_T("[2001:0db8:85a3:0000:0000:8a2e:0370:7334]:60"), | ||||
| 		QSE_T("[2001:db8:1234:ffff:ffff:ffff:ffff:ffff]:50"), | ||||
| 		QSE_T("[::ffff:0:0]:60"), | ||||
| 		QSE_T("[::ffff:192.168.1.1]:70"), | ||||
| 		QSE_T("[::ffff:192.168.1.1%999]:70"), | ||||
| 		QSE_T("[::ffff:192.168.1.1%eth0]:70") | ||||
| 	}; | ||||
|  | ||||
| 	static qse_mchar_t* ipstr_mbs[] = | ||||
| 	{ | ||||
| 		QSE_MT("192.168.1.1"), | ||||
| 		QSE_MT("255.255.255.255"), | ||||
| 		QSE_MT("4.3.0.0"), | ||||
| 		QSE_MT("4.3.0.0X"), | ||||
| 		QSE_MT("65.1234.11.34"), | ||||
| 		QSE_MT("65.123.11.34"), | ||||
| 		QSE_MT("1.1.1.1"), | ||||
| 		QSE_MT("::"), | ||||
| 		QSE_MT("::1"), | ||||
| 		QSE_MT("fe80::f27b:cbff:fea3:f40c"), | ||||
| 		QSE_MT("2001:0db8:85a3:0000:0000:8a2e:0370:7334"), | ||||
| 		QSE_MT("2001:db8:1234:ffff:ffff:ffff:ffff:ffff"), | ||||
| 		QSE_MT("::ffff:0:0"), | ||||
| 		QSE_MT("::ffff:192.168.1.1"), | ||||
| 		QSE_MT("::ffff:192.168.1.1%88"), | ||||
| 		QSE_MT("::ffff:192.168.1.1%eth0"), | ||||
| 		QSE_MT("::ffff:192.168.1.1%eth1"), | ||||
| 		QSE_MT("[::]:10"), | ||||
| 		QSE_MT("[::1]:20"), | ||||
| 		QSE_MT("[fe80::f27b:cbff:fea3:f40c]:30"), | ||||
| 		QSE_MT("[2001:0db8:85a3:0000:0000:8a2e:0370:7334]:60"), | ||||
| 		QSE_MT("[2001:db8:1234:ffff:ffff:ffff:ffff:ffff]:50"), | ||||
| 		QSE_MT("[::ffff:0:0]:60"), | ||||
| 		QSE_MT("[::ffff:192.168.1.1]:70"), | ||||
| 		QSE_MT("[::ffff:192.168.1.1%999]:70"), | ||||
| 		QSE_MT("[::ffff:192.168.1.1%eth0]:70") | ||||
| 	}; | ||||
|  | ||||
| 	static qse_wchar_t* ipstr_wcs[] = | ||||
| 	{ | ||||
| 		QSE_WT("192.168.1.1"), | ||||
| 		QSE_WT("255.255.255.255"), | ||||
| 		QSE_WT("4.3.0.0"), | ||||
| 		QSE_WT("4.3.0.0X"), | ||||
| 		QSE_WT("65.1234.11.34"), | ||||
| 		QSE_WT("65.123.11.34"), | ||||
| 		QSE_WT("1.1.1.1"), | ||||
| 		QSE_WT("::"), | ||||
| 		QSE_WT("::1"), | ||||
| 		QSE_WT("fe80::f27b:cbff:fea3:f40c"), | ||||
| 		QSE_WT("2001:0db8:85a3:0000:0000:8a2e:0370:7334"), | ||||
| 		QSE_WT("2001:db8:1234:ffff:ffff:ffff:ffff:ffff"), | ||||
| 		QSE_WT("::ffff:0:0"), | ||||
| 		QSE_WT("::ffff:192.168.1.1"), | ||||
| 		QSE_WT("::ffff:192.168.1.1%88"), | ||||
| 		QSE_WT("::ffff:192.168.1.1%eth0"), | ||||
| 		QSE_WT("::ffff:192.168.1.1%eth1"), | ||||
| 		QSE_WT("[::]:10"), | ||||
| 		QSE_WT("[::1]:20"), | ||||
| 		QSE_WT("[fe80::f27b:cbff:fea3:f40c]:30"), | ||||
| 		QSE_WT("[2001:0db8:85a3:0000:0000:8a2e:0370:7334]:60"), | ||||
| 		QSE_WT("[2001:db8:1234:ffff:ffff:ffff:ffff:ffff]:50"), | ||||
| 		QSE_WT("[::ffff:0:0]:60"), | ||||
| 		QSE_WT("[::ffff:192.168.1.1]:70"), | ||||
| 		QSE_WT("[::ffff:192.168.1.1%999]:70"), | ||||
| 		QSE_WT("[::ffff:192.168.1.1%eth0]:70"), | ||||
| 		QSE_WT("[::ffff:192.168.1.1%1]:70") | ||||
| 	}; | ||||
|  | ||||
| 	for (i = 0; i < QSE_COUNTOF(ipstr); i++) | ||||
| 	{ | ||||
| 		if (qse_strtonwad (ipstr[i], &nwad) <= -1) | ||||
| 		{ | ||||
| 			qse_printf (QSE_T("Failed to convert <%s>\n"), ipstr[i]); | ||||
| 		} | ||||
| 		else | ||||
| 		{ | ||||
| 			qse_nwadtostr (&nwad, buf, QSE_COUNTOF(buf), QSE_NWADTOSTR_ALL); | ||||
| 			qse_printf (QSE_T("Converted <%s> to <%s>\n"), ipstr[i], buf); | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	qse_printf (QSE_T("-------------------\n")); | ||||
| 	for (i = 0; i < QSE_COUNTOF(ipstr_mbs); i++) | ||||
| 	{ | ||||
| 		if (qse_mbstonwad (ipstr_mbs[i], &nwad) <= -1) | ||||
| 		{ | ||||
| 			qse_printf (QSE_T("Failed to convert <%hs>\n"), ipstr_mbs[i]); | ||||
| 		} | ||||
| 		else | ||||
| 		{ | ||||
| 			qse_nwadtombs (&nwad, mbsbuf, QSE_COUNTOF(mbsbuf), QSE_NWADTOMBS_ALL); | ||||
| 			qse_printf (QSE_T("Converted <%hs> to <%hs>\n"), ipstr_mbs[i], mbsbuf); | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	qse_printf (QSE_T("-------------------\n")); | ||||
| 	for (i = 0; i < QSE_COUNTOF(ipstr_wcs); i++) | ||||
| 	{ | ||||
| 		if (qse_wcstonwad (ipstr_wcs[i], &nwad) <= -1) | ||||
| 		{ | ||||
| 			qse_printf (QSE_T("Failed to convert <%ls>\n"), ipstr_wcs[i]); | ||||
| 		} | ||||
| 		else | ||||
| 		{ | ||||
| 			qse_nwadtowcs (&nwad, wcsbuf, QSE_COUNTOF(wcsbuf), QSE_NWADTOWCS_ALL); | ||||
| 			qse_printf (QSE_T("Converted <%ls> to <%ls>\n"), ipstr_wcs[i], wcsbuf); | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	qse_printf (QSE_T("================================================\n")); | ||||
| 	return 0; | ||||
| } | ||||
|  | ||||
| int qse_main (int argc, qse_achar_t* argv[]) | ||||
| { | ||||
| 	int x; | ||||
| #if defined(_WIN32) | ||||
| 	char locale[100]; | ||||
| 	UINT codepage = GetConsoleOutputCP(); | ||||
| 	if (codepage == CP_UTF8) | ||||
| 	{ | ||||
| 		/*SetConsoleOUtputCP (CP_UTF8);*/ | ||||
| 		qse_setdflcmgrbyid (QSE_CMGR_UTF8); | ||||
| 	} | ||||
| 	else | ||||
| 	{ | ||||
| 		sprintf (locale, ".%u", (unsigned int)codepage); | ||||
| 		setlocale (LC_ALL, locale); | ||||
| 		/*qse_setdflcmgrbyid (QSE_CMGR_SLMB);*/ | ||||
| 	} | ||||
| #else | ||||
|      setlocale (LC_ALL, ""); | ||||
| 	/*qse_setdflcmgrbyid (QSE_CMGR_SLMB);*/ | ||||
| #endif | ||||
| 	qse_open_stdsios (); | ||||
| 	x = qse_run_main (argc, argv, test_main); | ||||
| 	qse_close_stdsios (); | ||||
| 	return x; | ||||
| } | ||||
|  | ||||
							
								
								
									
										97
									
								
								samples/si/nwif01.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										97
									
								
								samples/si/nwif01.c
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,97 @@ | ||||
| #include <qse/si/nwif.h> | ||||
| #include <qse/cmn/mbwc.h> | ||||
| #include <qse/cmn/main.h> | ||||
| #include <qse/cmn/mem.h> | ||||
| #include <qse/cmn/str.h> | ||||
| #include <qse/si/sio.h> | ||||
| #include <qse/cmn/fmt.h> | ||||
|  | ||||
| #include <locale.h> | ||||
| #if defined(_WIN32) | ||||
| #	include <windows.h> | ||||
| #endif | ||||
|  | ||||
| static void print_nwifcfg (qse_nwifcfg_t* ptr) | ||||
| { | ||||
| 	qse_char_t tmp[128]; | ||||
|  | ||||
| 	qse_printf (QSE_T("[%s] ifindex=[%u] "), ptr->name, ptr->index); | ||||
| 		 | ||||
| 	qse_nwadtostr (&ptr->addr, tmp, QSE_COUNTOF(tmp), QSE_NWADTOSTR_ALL); | ||||
| 	qse_printf (QSE_T("addr=[%s] "), tmp); | ||||
| 	qse_nwadtostr (&ptr->mask, tmp, QSE_COUNTOF(tmp), QSE_NWADTOSTR_ALL); | ||||
| 	qse_printf (QSE_T("mask=[%s] "), tmp); | ||||
|  | ||||
| 	if (ptr->flags & QSE_NWIFCFG_BCAST) | ||||
| 	{ | ||||
| 		qse_nwadtostr (&ptr->bcast, tmp, QSE_COUNTOF(tmp), QSE_NWADTOSTR_ALL); | ||||
| 		qse_printf (QSE_T("bcast=[%s] "), tmp); | ||||
| 	} | ||||
|  | ||||
| 	qse_printf (QSE_T("mtu=[%d] "), (int)ptr->mtu); | ||||
| 	qse_printf (QSE_T("\n")); | ||||
| } | ||||
|  | ||||
| static int test_main (int argc, qse_char_t* argv[]) | ||||
| { | ||||
| 	qse_char_t name[100]; | ||||
| 	unsigned int index; | ||||
| 	unsigned int i; | ||||
|  | ||||
| 	for (i = 1; ;i++) | ||||
| 	{ | ||||
| 		if (qse_nwifindextostr (i, name, QSE_COUNTOF(name)) <= -1)  | ||||
| 		{ | ||||
| 			qse_printf (QSE_T("ifindex %d failed\n"), i); | ||||
| 			break; | ||||
| 		} | ||||
| 		 | ||||
| 		if (qse_nwifstrtoindex (name, &index) <= -1) | ||||
| 		{ | ||||
| 			qse_printf (QSE_T("ifname %s failed\n"), name); | ||||
| 			break; | ||||
| 		} | ||||
| 		 | ||||
| 		if (i != index) | ||||
| 		{ | ||||
| 			qse_printf (QSE_T("index mismatch %u %u %s\n"), i, index, name); | ||||
| 			break; | ||||
| 		} | ||||
|  | ||||
| 		qse_printf (QSE_T("OK %u %s\n"), index, name); | ||||
| 	} | ||||
|  | ||||
| 	qse_printf (QSE_T("================================================\n")); | ||||
| 	return 0; | ||||
| } | ||||
|  | ||||
| int qse_main (int argc, qse_achar_t* argv[]) | ||||
| { | ||||
| 	int ret; | ||||
|  | ||||
| #if defined(_WIN32) | ||||
| 	char locale[100]; | ||||
| 	UINT codepage = GetConsoleOutputCP();	 | ||||
| 	if (codepage == CP_UTF8) | ||||
| 	{ | ||||
| 		/*SetConsoleOutputCP (CP_UTF8);*/ | ||||
| 		qse_setdflcmgrbyid (QSE_CMGR_UTF8); | ||||
| 	} | ||||
| 	else | ||||
| 	{ | ||||
| 		qse_fmtuintmaxtombs (locale, QSE_COUNTOF(locale), | ||||
| 			codepage, 10, -1, QSE_MT('\0'), QSE_MT(".")); | ||||
| 		setlocale (LC_ALL, locale); | ||||
| 		/*qse_setdflcmgrbyid (QSE_CMGR_SLMB);*/ | ||||
| 	} | ||||
| #else | ||||
| 	setlocale (LC_ALL, ""); | ||||
| 	/*qse_setdflcmgrbyid (QSE_CMGR_SLMB);*/ | ||||
| #endif | ||||
| 	qse_open_stdsios (); | ||||
| 	ret =  qse_run_main (argc, argv, test_main); | ||||
| 	qse_close_stdsios (); | ||||
|  | ||||
| 	return ret; | ||||
| } | ||||
|  | ||||
							
								
								
									
										109
									
								
								samples/si/nwif02.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										109
									
								
								samples/si/nwif02.c
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,109 @@ | ||||
| #include <qse/si/nwif.h> | ||||
| #include <qse/cmn/mbwc.h> | ||||
| #include <qse/cmn/main.h> | ||||
| #include <qse/si/sio.h> | ||||
| #include <qse/cmn/mem.h> | ||||
| #include <qse/cmn/str.h> | ||||
| #include <qse/cmn/fmt.h> | ||||
|  | ||||
| #include <locale.h> | ||||
| #if defined(_WIN32) | ||||
| #	include <windows.h> | ||||
| #endif | ||||
|  | ||||
| static void print_nwifcfg (qse_nwifcfg_t* ptr) | ||||
| { | ||||
| 	qse_char_t tmp[128]; | ||||
|  | ||||
| 	if (ptr->flags & QSE_NWIFCFG_UP) qse_printf (QSE_T("UP ")); | ||||
| 	if (ptr->flags & QSE_NWIFCFG_LINKUP) qse_printf (QSE_T("LINKUP ")); | ||||
| 	if (ptr->flags & QSE_NWIFCFG_LINKDOWN) qse_printf (QSE_T("LINKDOWN ")); | ||||
| 	qse_printf (QSE_T("[%s] ifindex=[%u] "), ptr->name, ptr->index); | ||||
| 		 | ||||
| 	qse_nwadtostr (&ptr->addr, tmp, QSE_COUNTOF(tmp), QSE_NWADTOSTR_ALL); | ||||
| 	qse_printf (QSE_T("addr=[%s] "), tmp); | ||||
| 	qse_nwadtostr (&ptr->mask, tmp, QSE_COUNTOF(tmp), QSE_NWADTOSTR_ALL); | ||||
| 	qse_printf (QSE_T("mask=[%s] "), tmp); | ||||
|  | ||||
| 	if (ptr->flags & QSE_NWIFCFG_BCAST) | ||||
| 	{ | ||||
| 		qse_nwadtostr (&ptr->bcast, tmp, QSE_COUNTOF(tmp), QSE_NWADTOSTR_ALL); | ||||
| 		qse_printf (QSE_T("bcast=[%s] "), tmp); | ||||
| 	} | ||||
| 	if (ptr->flags & QSE_NWIFCFG_PTOP) | ||||
| 	{ | ||||
| 		qse_nwadtostr (&ptr->ptop, tmp, QSE_COUNTOF(tmp), QSE_NWADTOSTR_ALL); | ||||
| 		qse_printf (QSE_T("ptop=[%s] "), tmp); | ||||
| 	} | ||||
|  | ||||
| 	qse_printf (QSE_T("mtu=[%d] "), (int)ptr->mtu); | ||||
| 	qse_printf (QSE_T("hwaddr=[%02X:%02X:%02X:%02X:%02X:%02X] "), ptr->ethw[0], ptr->ethw[1], ptr->ethw[2], ptr->ethw[3], ptr->ethw[4], ptr->ethw[5]); | ||||
| 	qse_printf (QSE_T("\n")); | ||||
| } | ||||
|  | ||||
| static int test_main (int argc, qse_char_t* argv[]) | ||||
| { | ||||
| 	qse_nwifcfg_t cfg; | ||||
| 	int i; | ||||
|  | ||||
| 	for (i = 1; ;i++) | ||||
| 	{ | ||||
| 		if (qse_nwifindextostr (i, cfg.name, QSE_COUNTOF(cfg.name)) <= -1)  | ||||
| 		{ | ||||
| 			qse_printf (QSE_T("ifindex %d failed for IN4\n"), i); | ||||
| 			break; | ||||
| 		} | ||||
| 		 | ||||
| 		cfg.type = QSE_NWIFCFG_IN4; | ||||
| 		if (qse_getnwifcfg (&cfg) <= -1) | ||||
| 			qse_printf (QSE_T("Cannot get v4 configuration - %s\n"), cfg.name); | ||||
| 		else print_nwifcfg (&cfg); | ||||
| 	} | ||||
|  | ||||
| 	for (i = 1; ;i++) | ||||
| 	{ | ||||
| 		if (qse_nwifindextostr (i, cfg.name, QSE_COUNTOF(cfg.name)) <= -1)  | ||||
| 		{ | ||||
| 			qse_printf (QSE_T("ifindex %d failed for IN6\n"), i); | ||||
| 			break; | ||||
| 		} | ||||
|  | ||||
| 		cfg.type = QSE_NWIFCFG_IN6; | ||||
| 		if (qse_getnwifcfg (&cfg) <= -1) | ||||
| 			qse_printf (QSE_T("Cannot get v6 configuration - %s\n"), cfg.name); | ||||
| 		else print_nwifcfg (&cfg); | ||||
| 	} | ||||
| 	qse_printf (QSE_T("================================================\n")); | ||||
| 	return 0; | ||||
| } | ||||
|  | ||||
| int qse_main (int argc, qse_achar_t* argv[]) | ||||
| { | ||||
| 	int ret; | ||||
|  | ||||
| #if defined(_WIN32) | ||||
| 	char locale[100]; | ||||
| 	UINT codepage = GetConsoleOutputCP();	 | ||||
| 	if (codepage == CP_UTF8) | ||||
| 	{ | ||||
| 		/*SetConsoleOutputCP (CP_UTF8);*/ | ||||
| 		qse_setdflcmgrbyid (QSE_CMGR_UTF8); | ||||
| 	} | ||||
| 	else | ||||
| 	{ | ||||
| 		qse_fmtuintmaxtombs (locale, QSE_COUNTOF(locale), | ||||
| 			codepage, 10, -1, QSE_MT('\0'), QSE_MT(".")); | ||||
| 		setlocale (LC_ALL, locale); | ||||
| 		/*qse_setdflcmgrbyid (QSE_CMGR_SLMB);*/ | ||||
| 	} | ||||
| #else | ||||
| 	setlocale (LC_ALL, ""); | ||||
| 	/*qse_setdflcmgrbyid (QSE_CMGR_SLMB);*/ | ||||
| #endif | ||||
| 	qse_open_stdsios (); | ||||
| 	ret = qse_run_main (argc, argv, test_main); | ||||
| 	qse_close_stdsios (); | ||||
|  | ||||
| 	return ret; | ||||
| } | ||||
|  | ||||
							
								
								
									
										420
									
								
								samples/si/pio01.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										420
									
								
								samples/si/pio01.c
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,420 @@ | ||||
| #include <qse/si/pio.h> | ||||
| #include <qse/cmn/mem.h> | ||||
| #include <qse/cmn/env.h> | ||||
| #include <qse/si/sio.h> | ||||
|  | ||||
| #include <locale.h> | ||||
|  | ||||
| #if defined(_WIN32) | ||||
| #	include <windows.h> | ||||
| #elif defined(__OS2__) | ||||
| #	define INCL_DOSPROCESS | ||||
| #	define INCL_DOSERRORS | ||||
| #	define INCL_DOSDATETIME | ||||
| #	include <os2.h> | ||||
| #else | ||||
| #	include <unistd.h> | ||||
| #	include <sys/wait.h> | ||||
| #endif | ||||
|  | ||||
| #define R(f) \ | ||||
| 	do { \ | ||||
| 		qse_printf (QSE_T("== %s ==\n"), QSE_T(#f)); \ | ||||
| 		if (f() == -1) return -1; \ | ||||
| 	} while (0) | ||||
|  | ||||
| static int pio1 (const qse_char_t* cmd, qse_env_t* env, int oflags, qse_pio_hid_t rhid) | ||||
| { | ||||
| 	qse_pio_t pio; | ||||
| 	int x; | ||||
|  | ||||
| 	if (qse_pio_init (&pio, QSE_MMGR_GETDFL(), cmd, env, oflags) <= -1) | ||||
| 	{ | ||||
| 		qse_printf (QSE_T("cannot open program through pipe - %d\n"),  | ||||
| 			(int)qse_pio_geterrnum(&pio)); | ||||
| 		return -1; | ||||
| 	} | ||||
|  | ||||
| 	while (1) | ||||
| 	{ | ||||
| 		qse_byte_t buf[128]; | ||||
| 		qse_ssize_t i; | ||||
|  | ||||
| 		/*qse_pio_canread (&pio, QSE_PIO_ERR, 1000)*/ | ||||
| 		qse_ssize_t n = qse_pio_read (&pio, rhid, buf, QSE_SIZEOF(buf)); | ||||
| 		if (n == 0) break; | ||||
| 		if (n <= -1) | ||||
| 		{ | ||||
| 			qse_printf ( | ||||
| 				QSE_T("qse_pio_read() returned error - %d\n"), | ||||
| 				(int)qse_pio_geterrnum(&pio) | ||||
| 			); | ||||
| 			break; | ||||
| 		}	 | ||||
|  | ||||
| 		qse_printf (QSE_T("N===> %d buf => ["), (int)n); | ||||
| 		for (i = 0; i < n; i++) | ||||
| 		{ | ||||
| 		#ifdef QSE_CHAR_IS_MCHAR | ||||
| 			qse_printf (QSE_T("%c"), buf[i]); | ||||
| 		#else | ||||
| 			qse_printf (QSE_T("%C"), buf[i]); | ||||
| 		#endif | ||||
| 		}	 | ||||
| 		qse_printf (QSE_T("]\n")); | ||||
| 	} | ||||
|  | ||||
| 	x = qse_pio_wait (&pio); | ||||
| 	qse_printf (QSE_T("qse_pio_wait returns %d\n"), x); | ||||
| 	if (x <= -1) | ||||
| 	{ | ||||
| 		qse_printf (QSE_T("error code : %d\n"), (int)qse_pio_geterrnum(&pio)); | ||||
| 	} | ||||
|  | ||||
| 	qse_pio_fini (&pio); | ||||
|  | ||||
| 	return 0; | ||||
| } | ||||
|  | ||||
| static int pio2 (const qse_char_t* cmd, qse_env_t* env, int oflags, qse_pio_hid_t rhid) | ||||
| { | ||||
| 	qse_pio_t* pio; | ||||
| 	int x; | ||||
|  | ||||
| 	pio = qse_pio_open ( | ||||
| 		QSE_MMGR_GETDFL(), | ||||
| 		0, | ||||
| 		cmd, | ||||
| 		env, | ||||
| 		oflags | QSE_PIO_TEXT | ||||
| 	); | ||||
| 	if (pio == QSE_NULL) | ||||
| 	{ | ||||
| 		qse_printf (QSE_T("cannot open program through pipe\n")); | ||||
| 		return -1; | ||||
| 	} | ||||
|  | ||||
| 	while (1) | ||||
| 	{ | ||||
| 		qse_char_t buf[128]; | ||||
| 		qse_ssize_t i; | ||||
|  | ||||
| 		qse_ssize_t n = qse_pio_read (pio, rhid, buf, QSE_COUNTOF(buf)); | ||||
| 		if (n == 0) break; | ||||
| 		if (n < 0) | ||||
| 		{ | ||||
| 			qse_printf ( | ||||
| 				QSE_T("qse_pio_read() returned error - %d\n"), | ||||
| 				(int)qse_pio_geterrnum(pio) | ||||
| 			); | ||||
| 			break; | ||||
| 		}	 | ||||
|  | ||||
| 		qse_printf (QSE_T("N===> %d buf => ["), (int)n); | ||||
| 		for (i = 0; i < n; i++) | ||||
| 		{ | ||||
| 			qse_printf (QSE_T("%c"), buf[i]); | ||||
| 		} | ||||
| 		qse_printf (QSE_T("]\n")); | ||||
| 	} | ||||
|  | ||||
| 	x = qse_pio_wait (pio); | ||||
| 	qse_printf (QSE_T("qse_pio_wait returns %d\n"), x); | ||||
| 	if (x <= -1) | ||||
| 	{ | ||||
| 		qse_printf (QSE_T("error code : %d\n"), (int)qse_pio_geterrnum(pio)); | ||||
| 	} | ||||
|  | ||||
| 	qse_pio_close (pio); | ||||
|  | ||||
| 	return 0; | ||||
| } | ||||
|  | ||||
|  | ||||
| static int test1 (void) | ||||
| { | ||||
|  | ||||
| 	return pio1 ( | ||||
| #if defined(_WIN32) || defined(__OS2__) || defined(__DOS__) | ||||
| 		QSE_T("dir /a"),  | ||||
| #else | ||||
| 		QSE_T("ls -laF"), | ||||
| #endif | ||||
| 		QSE_NULL, | ||||
| 		QSE_PIO_READOUT|QSE_PIO_WRITEIN|QSE_PIO_SHELL, | ||||
| 		QSE_PIO_OUT | ||||
| 	); | ||||
| } | ||||
|  | ||||
| static int test2 (void) | ||||
| { | ||||
| 	return pio1 ( | ||||
| #if defined(_WIN32) || defined(__OS2__) || defined(__DOS__) | ||||
| 		QSE_T("dir /a"),  | ||||
| #else | ||||
| 		QSE_T("ls -laF"), | ||||
| #endif | ||||
| 		QSE_NULL, | ||||
| 		QSE_PIO_READERR|QSE_PIO_OUTTOERR|QSE_PIO_WRITEIN|QSE_PIO_SHELL, | ||||
| 		QSE_PIO_ERR | ||||
| 	); | ||||
| } | ||||
|  | ||||
| static int test3 (void) | ||||
| { | ||||
| 	return pio1 ( | ||||
| #if defined(_WIN32) || defined(__OS2__) || defined(__DOS__) | ||||
| 		QSE_T("tree.com"),  | ||||
| #else | ||||
| 		QSE_T("/bin/ls -laF"),  | ||||
| #endif | ||||
| 		QSE_NULL, | ||||
| 		QSE_PIO_READERR|QSE_PIO_OUTTOERR|QSE_PIO_WRITEIN, | ||||
| 		QSE_PIO_ERR | ||||
| 	); | ||||
| } | ||||
|  | ||||
| static int test4 (void) | ||||
| { | ||||
| 	return pio2 ( | ||||
| #if defined(_WIN32) || defined(__OS2__) || defined(__DOS__) | ||||
| 		QSE_T("dir /a"),  | ||||
| #else | ||||
| 		QSE_T("ls -laF"), | ||||
| #endif | ||||
| 		QSE_NULL, | ||||
| 		QSE_PIO_READOUT|QSE_PIO_WRITEIN|QSE_PIO_SHELL,  | ||||
| 		QSE_PIO_OUT | ||||
| 	); | ||||
| } | ||||
|  | ||||
| static int test5 (void) | ||||
| { | ||||
| 	return pio2 ( | ||||
| #if defined(_WIN32) || defined(__OS2__) || defined(__DOS__) | ||||
| 		QSE_T("dir /a"),  | ||||
| #else | ||||
| 		QSE_T("ls -laF"),  | ||||
| #endif | ||||
| 		QSE_NULL, | ||||
| 		QSE_PIO_READERR|QSE_PIO_OUTTOERR|QSE_PIO_WRITEIN|QSE_PIO_SHELL, | ||||
| 		QSE_PIO_ERR | ||||
| 	); | ||||
| } | ||||
|  | ||||
| static int test6 (void) | ||||
| { | ||||
| 	return pio2 ( | ||||
| #if defined(_WIN32) || defined(__OS2__) || defined(__DOS__) | ||||
| 		QSE_T("tree.com"),  | ||||
| #else | ||||
| 		QSE_T("/bin/ls -laF"), | ||||
| #endif | ||||
| 		QSE_NULL, | ||||
| 		QSE_PIO_READERR|QSE_PIO_OUTTOERR|QSE_PIO_WRITEIN, | ||||
| 		QSE_PIO_ERR | ||||
| 	); | ||||
| } | ||||
|  | ||||
| static int test7 (void) | ||||
| { | ||||
| 	return pio1 ( | ||||
| #if defined(_WIN32) || defined(__OS2__) || defined(__DOS__) | ||||
| 		QSE_T("tree.com"),  | ||||
| #else | ||||
| 		QSE_T("/bin/ls -laF"),  | ||||
| #endif | ||||
| 		QSE_NULL, | ||||
| 		QSE_PIO_READOUT|QSE_PIO_ERRTOOUT|QSE_PIO_WRITEIN, | ||||
| 		QSE_PIO_OUT | ||||
| 	); | ||||
| } | ||||
|  | ||||
| static int test8 (void) | ||||
| { | ||||
| 	return pio1 ( | ||||
| #if defined(_WIN32) || defined(__OS2__) || defined(__DOS__) | ||||
| 		QSE_T("tree.com"),  | ||||
| #else | ||||
| 		QSE_T("/bin/ls -laF"),  | ||||
| #endif | ||||
| 		QSE_NULL, | ||||
| 		QSE_PIO_READOUT|QSE_PIO_WRITEIN| | ||||
| 		QSE_PIO_OUTTONUL|QSE_PIO_ERRTONUL|QSE_PIO_INTONUL, | ||||
| 		QSE_PIO_OUT | ||||
| 	); | ||||
| } | ||||
|  | ||||
| static int test9 (void) | ||||
| { | ||||
| 	return pio1 ( | ||||
| #if defined(_WIN32) || defined(__OS2__) || defined(__DOS__) | ||||
| 		(const qse_char_t*)"tree.com",  | ||||
| #else | ||||
| 		(const qse_char_t*)"/bin/ls -laF",  | ||||
| #endif | ||||
| 		QSE_NULL, | ||||
| 		QSE_PIO_MBSCMD|QSE_PIO_READOUT|QSE_PIO_WRITEIN, | ||||
| 		QSE_PIO_OUT | ||||
| 	); | ||||
| } | ||||
|  | ||||
| static int test10 (void) | ||||
| { | ||||
| 	return pio1 ( | ||||
| #if defined(_WIN32) || defined(__OS2__) || defined(__DOS__) | ||||
| 		(const qse_char_t*)"dir /a", | ||||
| #else | ||||
| 		(const qse_char_t*)"ls -laF",  | ||||
| #endif | ||||
| 		QSE_NULL, | ||||
| 		QSE_PIO_MBSCMD|QSE_PIO_READOUT|QSE_PIO_WRITEIN|QSE_PIO_SHELL, | ||||
| 		QSE_PIO_OUT | ||||
| 	); | ||||
| } | ||||
|  | ||||
| static int test11 (void) | ||||
| { | ||||
| 	return pio1 ( | ||||
| #if defined(_WIN32) || defined(__OS2__) || defined(__DOS__) | ||||
| 		(const qse_char_t*)"dir /a", | ||||
| #else | ||||
| 		(const qse_char_t*)"ls -laF",  | ||||
| #endif | ||||
| 		QSE_NULL, | ||||
| 		QSE_PIO_MBSCMD|QSE_PIO_READOUT|QSE_PIO_WRITEIN|QSE_PIO_DROPERR|QSE_PIO_INTONUL|QSE_PIO_SHELL, | ||||
| 		QSE_PIO_OUT | ||||
| 	); | ||||
| } | ||||
|  | ||||
|  | ||||
| static int test12 (void) | ||||
| { | ||||
| 	qse_env_t* env; | ||||
| 	int n; | ||||
| 	 | ||||
| 	env = qse_env_open (QSE_MMGR_GETDFL(), 0, 0); | ||||
| 	if (env == QSE_NULL) return -1; | ||||
|  | ||||
| 	qse_env_insert (env, QSE_T("PATH"), QSE_NULL); | ||||
| 	qse_env_insert (env, QSE_T("HELLO"), QSE_T("WORLD")); | ||||
|  | ||||
| 	n = pio1 ( | ||||
| #if defined(_WIN32) || defined(__OS2__) || defined(__DOS__) | ||||
| 		QSE_T("set"), | ||||
| #else | ||||
| 		QSE_T("printenv"), | ||||
| #endif | ||||
| 		env, | ||||
| 		QSE_PIO_READOUT|QSE_PIO_WRITEIN|QSE_PIO_SHELL, | ||||
| 		QSE_PIO_OUT | ||||
| 	); | ||||
|  | ||||
| 	qse_env_close (env); | ||||
| 	return n; | ||||
| } | ||||
|  | ||||
| static int test13 (void) | ||||
| { | ||||
| 	qse_pio_t* pio; | ||||
| 	int x; | ||||
|  | ||||
| 	pio = qse_pio_open ( | ||||
| 		QSE_MMGR_GETDFL(), | ||||
| 		0, | ||||
| #if defined(_WIN32) || defined(__OS2__) || defined(__DOS__) | ||||
| 		QSE_T("tree.com"), | ||||
| #else | ||||
| 		QSE_T("/bin/ls -laF"), | ||||
| #endif | ||||
| 		QSE_NULL, | ||||
| 		QSE_PIO_READOUT|QSE_PIO_READERR|QSE_PIO_WRITEIN | ||||
| 	); | ||||
| 	if (pio == QSE_NULL) | ||||
| 	{ | ||||
| 		qse_printf (QSE_T("cannot open program through pipe\n")); | ||||
| 		return -1; | ||||
| 	} | ||||
|  | ||||
| #if defined(_WIN32) | ||||
| 	{ | ||||
| 		int n = 5; | ||||
|  | ||||
| 		qse_printf (QSE_T("sleeping for %d seconds\n"), n); | ||||
| 		Sleep (n * 1000); | ||||
| 		qse_printf (QSE_T("WaitForSingleObject....%d\n"), | ||||
| 			(int)WaitForSingleObject (pio->child, INFINITE)); | ||||
| 	} | ||||
| #elif defined(__OS2__) | ||||
| 	{ | ||||
| 		int n = 5; | ||||
| 		RESULTCODES result; | ||||
| 		PID pid; | ||||
|  | ||||
| 		qse_printf (QSE_T("sleeping for %d seconds\n"), n); | ||||
| 		DosSleep (n * 1000); | ||||
|  | ||||
| 		/* it doesn't seem to proceed if the pipe is not read out. | ||||
| 		 * maybe the OS2 pipe buffer is too smally?? */ | ||||
| 		while (1) | ||||
| 		{ | ||||
| 			qse_mchar_t buf[100]; | ||||
| 			qse_ssize_t x = qse_pio_read (pio, QSE_PIO_OUT, buf, QSE_SIZEOF(buf)); | ||||
| 			if (x <= 0) break; | ||||
| 		} | ||||
|  | ||||
| 		qse_printf (QSE_T("DosWaitChild....%d\n"), | ||||
| 			(int)DosWaitChild (DCWA_PROCESS, DCWW_WAIT, &result, &pid, pio->child)); | ||||
| 	} | ||||
|  | ||||
| #elif defined(__DOS__) | ||||
|  | ||||
| 	#error NOT SUPPORTED | ||||
|  | ||||
| #else | ||||
| 	{ | ||||
| 		int status; | ||||
| 		int n = 5; | ||||
|  | ||||
| 		qse_printf (QSE_T("sleeping for %d seconds\n"), n); | ||||
| 		sleep (n); | ||||
| 		qse_printf (QSE_T("waitpid...%d\n"),  (int)waitpid (-1, &status, 0)); | ||||
| 	} | ||||
| #endif | ||||
|  | ||||
| 	x = qse_pio_wait (pio); | ||||
| 	qse_printf (QSE_T("qse_pio_wait returns %d\n"), x); | ||||
| 	if (x == -1) | ||||
| 	{ | ||||
| 		qse_printf (QSE_T("error code : %d\n"), (int)QSE_PIO_ERRNUM(pio)); | ||||
| 	} | ||||
|  | ||||
| 	qse_pio_close (pio); | ||||
| 	return 0; | ||||
| } | ||||
|  | ||||
| int main () | ||||
| { | ||||
| 	qse_open_stdsios (); | ||||
| 	setlocale (LC_ALL, ""); | ||||
|  | ||||
| 	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); | ||||
| 	R (test5); | ||||
| 	R (test6); | ||||
| 	R (test7); | ||||
| 	R (test8); | ||||
| 	R (test9); | ||||
| 	R (test10); | ||||
| 	R (test11); | ||||
| 	R (test12); | ||||
| 	R (test13); | ||||
| 	qse_close_stdsios (); | ||||
| 	return 0; | ||||
| } | ||||
							
								
								
									
										138
									
								
								samples/si/rwl01.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										138
									
								
								samples/si/rwl01.c
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,138 @@ | ||||
| #include <qse/si/rwl.h> | ||||
| #include <qse/si/thr.h> | ||||
| #include <qse/si/sio.h> | ||||
| #include <qse/si/intr.h> | ||||
| #include <qse/cmn/mem.h> | ||||
|  | ||||
| #include <locale.h> | ||||
|  | ||||
| #if defined(_WIN32) | ||||
| #	include <windows.h> | ||||
| #endif | ||||
|  | ||||
| struct thr_xtn_t | ||||
| { | ||||
| 	int id; | ||||
| }; | ||||
| typedef struct thr_xtn_t thr_xtn_t; | ||||
|  | ||||
| static qse_rwl_t* rwl; | ||||
| static qse_mtx_t* mtx; | ||||
| static int stop_req = 0; | ||||
|  | ||||
| #define OUTMSG(msg,id)  do { \ | ||||
| 	qse_ntime_t now; \ | ||||
| 	qse_gettime(&now); \ | ||||
| 	qse_mtx_lock (mtx, QSE_NULL); \ | ||||
| 	qse_printf (QSE_T("%10ld.%7ld thread %d: %s\n"), (long int)now.sec, (long int)now.nsec, id, msg); \ | ||||
| 	qse_mtx_unlock (mtx); \ | ||||
| } while(0) | ||||
|  | ||||
| static int thr_exec (qse_thr_t* thr, void* ctx) | ||||
| { | ||||
| 	thr_xtn_t* xtn = qse_thr_getxtn(thr); | ||||
|  | ||||
| 	OUTMSG (QSE_T("starting"), xtn->id); | ||||
| 	while (!stop_req) | ||||
| 	{ | ||||
| 		if (xtn->id % 2) | ||||
| 		/*if (xtn->id > 0)*/ | ||||
| 		{ | ||||
| 			OUTMSG (QSE_T("read waiting"), xtn->id); | ||||
|  | ||||
| 			if (qse_rwl_lockr (rwl, QSE_NULL) >= 0) | ||||
| 			{ | ||||
| 				OUTMSG (QSE_T("read start"), xtn->id); | ||||
| 				/*sleep (1);*/ | ||||
| 				OUTMSG (QSE_T("read done"), xtn->id); | ||||
| 				qse_rwl_unlockr (rwl); | ||||
| 			} | ||||
| 			else | ||||
| 			{ | ||||
| 				OUTMSG (QSE_T("read fail"), xtn->id); | ||||
| 			} | ||||
| 		} | ||||
| 		else | ||||
| 		{ | ||||
| 			qse_ntime_t x; | ||||
| 			OUTMSG (QSE_T("write waiting"), xtn->id); | ||||
|  | ||||
| 			qse_init_ntime (&x, 3, 0); | ||||
| 			/*if (qse_rwl_lockw (rwl, QSE_NULL) >= 0)*/ | ||||
| 			if (qse_rwl_lockw (rwl, &x) >= 0) | ||||
| 			{ | ||||
| 				OUTMSG (QSE_T("write start"), xtn->id); | ||||
| 				/*sleep (1);*/ | ||||
| 				OUTMSG (QSE_T("write done"), xtn->id); | ||||
| 				qse_rwl_unlockw (rwl); | ||||
| 				/*sleep (2);*/ | ||||
| 			} | ||||
| 			else | ||||
| 			{ | ||||
| 				OUTMSG (QSE_T("write fail"), xtn->id); | ||||
| 			} | ||||
|  | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	OUTMSG (QSE_T("exiting"), xtn->id); | ||||
| 	return 0; | ||||
| } | ||||
|  | ||||
|  | ||||
| static void test_001 (void) | ||||
| { | ||||
| 	qse_mmgr_t* mmgr; | ||||
| 	qse_thr_t* t[6]; | ||||
| 	qse_size_t i; | ||||
| 	thr_xtn_t* xtn; | ||||
|  | ||||
| 	mmgr = QSE_MMGR_GETDFL(); | ||||
|  | ||||
| 	mtx = qse_mtx_open (mmgr, 0); | ||||
| 	rwl = qse_rwl_open (mmgr, 0, 0/*QSE_RWL_PREFER_WRITER*/); | ||||
|  | ||||
| 	for (i = 0; i < QSE_COUNTOF(t); i++) | ||||
| 	{ | ||||
| 		t[i] = qse_thr_open (mmgr, QSE_SIZEOF(thr_xtn_t)); | ||||
| 		xtn = qse_thr_getxtn(t[i]); | ||||
| 		xtn->id = i; | ||||
| 	} | ||||
|  | ||||
| 	for (i = 0; i < QSE_COUNTOF(t); i++) qse_thr_start (t[i], thr_exec, QSE_NULL, 0); | ||||
| 	for (i = 0; i < QSE_COUNTOF(t); i++) qse_thr_join (t[i]); | ||||
| 	for (i = 0; i < QSE_COUNTOF(t); i++) qse_thr_close (t[i]); | ||||
|  | ||||
| 	qse_rwl_close (rwl); | ||||
| 	qse_mtx_close (mtx); | ||||
| } | ||||
|  | ||||
| static void stop_run (void* arg) | ||||
| { | ||||
| 	stop_req = 1; | ||||
| } | ||||
|  | ||||
| int main () | ||||
| { | ||||
| #if defined(_WIN32) | ||||
| 	char codepage[100]; | ||||
| 	UINT old_cp = GetConsoleOutputCP(); | ||||
| 	SetConsoleOutputCP (CP_UTF8); | ||||
|  | ||||
| #else | ||||
| 	setlocale (LC_ALL, ""); | ||||
| #endif | ||||
|  | ||||
| 	qse_open_stdsios (); | ||||
|  | ||||
| 	qse_set_intr_handler (stop_run, QSE_NULL); | ||||
| 	test_001 (); | ||||
| 	qse_clear_intr_handler (); | ||||
|  | ||||
| 	qse_close_stdsios (); | ||||
|  | ||||
| #if defined(_WIN32) | ||||
| 	SetConsoleOutputCP (old_cp); | ||||
| #endif | ||||
| 	return 0; | ||||
| } | ||||
							
								
								
									
										121
									
								
								samples/si/sck01.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										121
									
								
								samples/si/sck01.cpp
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,121 @@ | ||||
| #include <qse/si/Socket.hpp> | ||||
| #include <qse/si/mtx.h> | ||||
| #include <qse/si/sio.h> | ||||
| #include <qse/cmn/mem.h> | ||||
|  | ||||
| #include <locale.h> | ||||
| #if defined(_WIN32) | ||||
| #	include <windows.h> | ||||
| #endif | ||||
|  | ||||
| #include <unistd.h> | ||||
| #include <signal.h> | ||||
| #include <string.h> | ||||
|  | ||||
| static int test1 () | ||||
| { | ||||
| 	QSE::Socket s; | ||||
| 	QSE::SocketAddress addr; | ||||
|  | ||||
| 	if (s.open (QSE_AF_INET6, QSE_SOCK_STREAM, 0) <= -1) | ||||
| 	{ | ||||
| 		qse_printf (QSE_T("cannot open socket\n")); | ||||
| 		return -1; | ||||
| 	} | ||||
|  | ||||
| 	//if (addr.resolve(QSE_T("0"), QSE_T("tango6.miflux.com"), QSE_SOCK_STREAM) >= 0) | ||||
| 	if (addr.resolve(QSE_T("0"), QSE_T("code.miflux.com"), QSE_SOCK_STREAM) >= 0) | ||||
| 	{ | ||||
| 		qse_printf (QSE_T("code.miflux.com ===> [%js]\n"), (const qse_char_t*)addr.toString()); | ||||
| 	} | ||||
|  | ||||
| 	// if 'addr' is set to IPv6 above, this resolve() will fail if the given host doesn't have an IPv6 address | ||||
| 	// specifying the family to QSE_AF_INET overrides this behavior. | ||||
| 	if (addr.resolve(QSE_T("https"), QSE_T("code.miflux.com"), QSE_AF_INET, QSE_SOCK_STREAM) >= 0) | ||||
| 	{ | ||||
| 		qse_printf (QSE_T("code.miflux.com:https ===> [%js]\n"), (const qse_char_t*)addr.toString()); | ||||
| 	} | ||||
|  | ||||
| 	qse_printf (QSE_T("lo ifindex ===> %d\n"), s.getIfceIndex(QSE_MT("lo"))); | ||||
| 	if (s.getIfceAddress(QSE_T("lo"), &addr) >= 0) | ||||
| 	{ | ||||
| 		qse_char_t buf[256]; | ||||
| 		qse_printf (QSE_T("lo ifaddr ===> [%s]\n"), addr.toStrBuf(buf, QSE_COUNTOF(buf))); | ||||
| 	} | ||||
| 	if (s.getIfceNetmask(QSE_T("lo"), &addr) >= 0) | ||||
| 	{ | ||||
| 		qse_char_t buf[256]; | ||||
| 		qse_printf (QSE_T("lo netmask ===> [%s]\n"), addr.toStrBuf(buf, QSE_COUNTOF(buf))); | ||||
| 	} | ||||
| 	if (s.getIfceBroadcast(QSE_T("lo"), &addr) >= 0) | ||||
| 	{ | ||||
| 		qse_char_t buf[256]; | ||||
| 		qse_printf (QSE_T("lo broadcast ===> [%s]\n"), addr.toStrBuf(buf, QSE_COUNTOF(buf))); | ||||
| 	} | ||||
|  | ||||
|  | ||||
| 	qse_printf (QSE_T("eth0 ifindex ===> %d\n"), s.getIfceIndex(QSE_T("eth0"))); | ||||
| 	if (s.getIfceAddress(QSE_T("eth0"), &addr) >= 0) | ||||
| 	{ | ||||
| 		qse_char_t buf[256]; | ||||
| 		qse_printf (QSE_T("eth0 ifaddr ===> [%s]\n"), addr.toStrBuf(buf, QSE_COUNTOF(buf))); | ||||
| 	} | ||||
| 	if (s.getIfceNetmask(QSE_T("eth0"), &addr) >= 0) | ||||
| 	{ | ||||
| 		qse_char_t buf[256]; | ||||
| 		qse_printf (QSE_T("eth0 netmask ===> [%s]\n"), addr.toStrBuf(buf, QSE_COUNTOF(buf))); | ||||
| 	} | ||||
| 	if (s.getIfceBroadcast(QSE_T("eth0"), &addr) >= 0) | ||||
| 	{ | ||||
| 		qse_char_t buf[256]; | ||||
| 		qse_printf (QSE_T("eth0 broadcast ===> [%s]\n"), addr.toStrBuf(buf, QSE_COUNTOF(buf))); | ||||
| 	} | ||||
|  | ||||
|  | ||||
| 	addr.set ("[::1]:9999"); | ||||
| 	if (s.connect(addr) <= -1) | ||||
| 	{ | ||||
| 		qse_printf (QSE_T("unable to connect to [::1]:9999\n")); | ||||
| 	} | ||||
| 	else | ||||
| 	{ | ||||
| 		qse_ioptl_t k[3]; | ||||
| 		k[0].ptr = (void*)"hello"; | ||||
| 		k[0].len = 5; | ||||
| 		k[1].ptr = (void*)"world"; | ||||
| 		k[1].len = 5; | ||||
| 		k[2].ptr = (void*)"forever"; | ||||
| 		k[2].len = 7; | ||||
| 		s.sendx (k, 3); | ||||
| 	} | ||||
|  | ||||
| 	return 0; | ||||
| } | ||||
|  | ||||
| int main () | ||||
| { | ||||
| #if defined(_WIN32) | ||||
|  	char locale[100]; | ||||
| 	UINT codepage = GetConsoleOutputCP(); | ||||
| 	if (codepage == CP_UTF8) | ||||
| 	{ | ||||
| 		/*SetConsoleOUtputCP (CP_UTF8);*/ | ||||
| 		qse_setdflcmgrbyid (QSE_CMGR_UTF8); | ||||
| 	} | ||||
| 	else | ||||
| 	{ | ||||
| 		sprintf (locale, ".%u", (unsigned int)codepage); | ||||
| 		setlocale (LC_ALL, locale); | ||||
| 		/*qse_setdflcmgrbyid (QSE_CMGR_SLMB);*/ | ||||
| 	} | ||||
| #else | ||||
| 	setlocale (LC_ALL, ""); | ||||
| 	/*qse_setdflcmgrbyid (QSE_CMGR_SLMB);*/ | ||||
| #endif | ||||
|  | ||||
| 	qse_open_stdsios (); | ||||
| 	test1(); | ||||
| 	qse_close_stdsios (); | ||||
|  | ||||
| 	return 0; | ||||
| } | ||||
							
								
								
									
										122
									
								
								samples/si/sio01.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										122
									
								
								samples/si/sio01.c
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,122 @@ | ||||
| #include <qse/si/sio.h> | ||||
| #include <qse/cmn/mbwc.h> | ||||
| #include <qse/cmn/mem.h> | ||||
| #include <qse/cmn/mem.h> | ||||
| #include <qse/cmn/fmt.h> | ||||
|  | ||||
|  | ||||
| #include <locale.h> | ||||
| #if defined(_WIN32) | ||||
| #	include <windows.h> | ||||
| #endif | ||||
|  | ||||
| static qse_sio_t* g_out; | ||||
|  | ||||
| #define R(f) \ | ||||
| 	do { \ | ||||
| 		qse_sio_putstr (g_out,QSE_T("== ")); \ | ||||
| 		qse_sio_putstr (g_out,QSE_T(#f)); \ | ||||
| 		qse_sio_putstr (g_out,QSE_T(" ==\n")); \ | ||||
| 		qse_sio_flush (g_out); \ | ||||
| 		if (f() == -1) return -1; \ | ||||
| 	} while (0) | ||||
|  | ||||
| static int test1 (void) | ||||
| { | ||||
|      const qse_wchar_t unistr[] = | ||||
|      { | ||||
| 		0x00A2, | ||||
|  | ||||
|           /*L"\uB108 \uBB50\uAC00 \uC798\uB0AC\uC5B4!",*/ | ||||
|           0xB108, | ||||
|           L' ', | ||||
|           0xBB50, | ||||
|           0xAC00, | ||||
|           L' ', | ||||
|           0xC798, | ||||
|           0xB0AC, | ||||
|           0xC5B4, | ||||
|           L'!', | ||||
|  | ||||
|           L'\n', | ||||
|           L'\0' | ||||
|      }; | ||||
|  | ||||
| 	qse_sio_t* out; | ||||
| 	 | ||||
| 	out = qse_sio_openstd ( | ||||
| 		QSE_MMGR_GETDFL(), 0, QSE_SIO_STDOUT,  | ||||
| 		QSE_SIO_WRITE | QSE_SIO_IGNOREMBWCERR); | ||||
| 	qse_sio_putwcs (out, unistr); | ||||
| 	qse_sio_putwcsf (out, QSE_WT ("[%.*s] [%.*s]\n"), (int)qse_wcslen(unistr) - 1, unistr, (int)qse_wcslen(unistr) - 1, unistr); | ||||
| 	qse_sio_close (out); | ||||
| 	return 0; | ||||
| } | ||||
|  | ||||
| static int test2 (void) | ||||
| { | ||||
|      const qse_wchar_t unistr[] = | ||||
|      { | ||||
| 		0x00A2, | ||||
|  | ||||
|           /*L"\uB108 \uBB50\uAC00 \uC798\uB0AC\uC5B4!",*/ | ||||
|           0xB108, | ||||
|           L' ', | ||||
|           0xBB50, | ||||
|           0xAC00, | ||||
|           L' ', | ||||
|           0xC798, | ||||
|           0xB0AC, | ||||
|           0xC5B4, | ||||
|           L'!', | ||||
|  | ||||
|           L'\n', | ||||
|           L'\0' | ||||
|      }; | ||||
|  | ||||
| 	qse_mchar_t mbsbuf[100]; | ||||
| 	qse_sio_t* out; | ||||
| 	qse_size_t wlen, mlen; | ||||
| 	 | ||||
| 	out = qse_sio_openstd ( | ||||
| 		QSE_MMGR_GETDFL(), 0, QSE_SIO_STDOUT,  | ||||
| 		QSE_SIO_WRITE | QSE_SIO_IGNOREMBWCERR); | ||||
|  | ||||
| 	mlen = QSE_COUNTOF(mbsbuf); | ||||
| 	qse_wcstombs (unistr, &wlen, mbsbuf, &mlen); | ||||
|  | ||||
| 	qse_sio_putmbs (out, mbsbuf); | ||||
|  | ||||
| 	qse_sio_putmbsf (out, QSE_MT ("[%.*s] [%.*s]\n"), (int)qse_mbslen(mbsbuf) - 1, mbsbuf, (int)qse_mbslen(mbsbuf) - 1, mbsbuf); | ||||
| 	qse_sio_close (out); | ||||
| 	return 0; | ||||
| } | ||||
|  | ||||
| int main () | ||||
| { | ||||
| #if defined(_WIN32) | ||||
|  	char locale[100]; | ||||
| 	UINT codepage = GetConsoleOutputCP();	 | ||||
| 	if (codepage == CP_UTF8) | ||||
| 	{ | ||||
| 		/*SetConsoleOUtputCP (CP_UTF8);*/ | ||||
| 		qse_setdflcmgrbyid (QSE_CMGR_UTF8); | ||||
| 	} | ||||
| 	else | ||||
| 	{ | ||||
|      	sprintf (locale, ".%u", (unsigned int)codepage); | ||||
|      	setlocale (LC_ALL, locale); | ||||
| 		/*qse_setdflcmgrbyid (QSE_CMGR_SLMB);*/ | ||||
| 	} | ||||
| #else | ||||
| 	setlocale (LC_ALL, ""); | ||||
| 	/*qse_setdflcmgrbyid (QSE_CMGR_SLMB);*/ | ||||
| #endif | ||||
|  | ||||
| 	g_out = qse_sio_openstd (QSE_MMGR_GETDFL(), 0, QSE_SIO_STDOUT, QSE_SIO_WRITE | QSE_SIO_IGNOREMBWCERR); | ||||
| 	R (test1); | ||||
| 	R (test2); | ||||
| 	qse_sio_close (g_out); | ||||
|  | ||||
| 	return 0; | ||||
| } | ||||
							
								
								
									
										176
									
								
								samples/si/sio02.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										176
									
								
								samples/si/sio02.c
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,176 @@ | ||||
| #include <qse/si/sio.h> | ||||
| #include <qse/cmn/mbwc.h> | ||||
| #include <qse/cmn/mem.h> | ||||
| #include <qse/cmn/fmt.h> | ||||
| #include <locale.h> | ||||
|  | ||||
| #if defined(_WIN32) | ||||
| #	include <windows.h> | ||||
| #endif | ||||
|  | ||||
| static qse_sio_t* g_out; | ||||
|  | ||||
| #define R(f) \ | ||||
| 	do { \ | ||||
| 		qse_sio_putstr (g_out,QSE_T("== ")); \ | ||||
| 		qse_sio_putstr (g_out,QSE_T(#f)); \ | ||||
| 		qse_sio_putstr (g_out,QSE_T(" ==\n")); \ | ||||
| 		qse_sio_flush (g_out); \ | ||||
| 		if (f() == -1) return -1; \ | ||||
| 	} while (0) | ||||
|  | ||||
| static int test1 (void) | ||||
| { | ||||
| 	qse_sio_t* sio; | ||||
| 	int i; | ||||
|  | ||||
| 	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"Fly to the universe, kick you ass", | ||||
| 		L"", | ||||
| 		L"Fly to the universe, kick you ass", | ||||
| 		L"Fly to the universe, kick you ass", | ||||
| 		L"" | ||||
|         }; | ||||
|  | ||||
| 	x[0] = unistr; | ||||
| 	x[2] = unistr; | ||||
| 	x[5] = unistr; | ||||
| 	sio = qse_sio_open (QSE_MMGR_GETDFL(), 0, QSE_T("sio.txt"),  | ||||
| 		QSE_SIO_WRITE | QSE_SIO_CREATE | QSE_SIO_TRUNCATE); | ||||
|  | ||||
| 	if (sio == QSE_NULL) | ||||
| 	{ | ||||
| 		qse_printf (QSE_T("cannot open file\n")); | ||||
| 		return -1; | ||||
| 	} | ||||
|  | ||||
| 	for (i = 0; i < QSE_COUNTOF(x); i++) | ||||
| 	{ | ||||
| 		qse_sio_putstr (g_out, QSE_T("Written [")); | ||||
| 		qse_sio_putwcs (g_out, x[i]); | ||||
| 		qse_sio_putstr (g_out, QSE_T("]\n")); | ||||
|  | ||||
| 		qse_sio_putwcs (sio, x[i]); | ||||
| 		qse_sio_putc (sio, QSE_T('\n')); | ||||
| 	} | ||||
|  | ||||
| 	qse_sio_close (sio); | ||||
|  | ||||
| 	return 0; | ||||
| } | ||||
|  | ||||
| static int test2 (void) | ||||
| { | ||||
| 	qse_ssize_t n; | ||||
| 	qse_wchar_t buf[20]; | ||||
| 	qse_sio_t* in, * out; | ||||
| 	 | ||||
| 	in = qse_sio_openstd (QSE_MMGR_GETDFL(), 0, QSE_SIO_STDIN, QSE_SIO_READ | QSE_SIO_IGNOREMBWCERR); | ||||
| 	out = qse_sio_openstd (QSE_MMGR_GETDFL(), 0, QSE_SIO_STDOUT, QSE_SIO_WRITE | QSE_SIO_IGNOREMBWCERR); | ||||
| 	 | ||||
| 	qse_sio_putstr (out, QSE_T("Type something here:\n")); | ||||
| 	while (1) | ||||
| 	{ | ||||
| 		n = qse_sio_getwcs (in, buf, QSE_COUNTOF(buf)); | ||||
| 		if (n == 0) break; | ||||
| 		if (n <= -1)  | ||||
| 		{ | ||||
| 			qse_char_t buf[32]; | ||||
| 			qse_fmtintmax (buf, QSE_COUNTOF(buf), qse_sio_geterrnum(in), 10, -1, QSE_T('\0'), QSE_NULL); | ||||
| 			qse_sio_putstr (out, QSE_T("ERROR .... ")); | ||||
| 			qse_sio_putstr (out, buf); | ||||
| 			qse_sio_putstr (out, QSE_T("\n")); | ||||
| 			break; | ||||
| 		} | ||||
|  | ||||
| 		qse_sio_putwcs (out, buf); | ||||
| 	} | ||||
|  | ||||
| 	qse_sio_close (out); | ||||
| 	qse_sio_close (in); | ||||
| 	return 0; | ||||
| } | ||||
|  | ||||
| static int test3 (void) | ||||
| { | ||||
| 	qse_ssize_t n; | ||||
| 	qse_mchar_t buf[20]; | ||||
| 	qse_sio_t* in, * out; | ||||
| 	 | ||||
| 	in = qse_sio_openstd (QSE_MMGR_GETDFL(), 0, QSE_SIO_STDIN, QSE_SIO_READ | QSE_SIO_IGNOREMBWCERR); | ||||
| 	out = qse_sio_openstd (QSE_MMGR_GETDFL(), 0, QSE_SIO_STDOUT, QSE_SIO_WRITE | QSE_SIO_IGNOREMBWCERR); | ||||
|  | ||||
| 	qse_sio_putstr (out, QSE_T("Type something here:\n")); | ||||
| 	while (1) | ||||
| 	{ | ||||
| 		n = qse_sio_getmbs (in, buf, QSE_COUNTOF(buf)); | ||||
| 		if (n == 0) break; | ||||
| 		if (n < 0)  | ||||
| 		{ | ||||
| 			qse_char_t buf[32]; | ||||
| 			qse_fmtintmax (buf, QSE_COUNTOF(buf), qse_sio_geterrnum(in), 10, -1, QSE_T('\0'), QSE_NULL); | ||||
| 			qse_sio_putstr (out, QSE_T("error .... ")); | ||||
| 			qse_sio_putstr (out, buf); | ||||
| 			qse_sio_putstr (out, QSE_T("\n")); | ||||
| 			break; | ||||
| 		} | ||||
|  | ||||
| 		qse_sio_putmbs (out, buf); | ||||
| 	} | ||||
|  | ||||
| 	qse_sio_close (out); | ||||
| 	qse_sio_close (in); | ||||
| 	return 0; | ||||
| } | ||||
|  | ||||
|  | ||||
| int main () | ||||
| { | ||||
|  | ||||
| #if defined(_WIN32) | ||||
|  	char locale[100]; | ||||
| 	UINT codepage = GetConsoleOutputCP();	 | ||||
| 	if (codepage == CP_UTF8) | ||||
| 	{ | ||||
| 		/*SetConsoleOutputCP (CP_UTF8);*/ | ||||
| 		qse_setdflcmgrbyid (QSE_CMGR_UTF8); | ||||
| 	} | ||||
| 	else | ||||
| 	{ | ||||
|      	sprintf (locale, ".%u", (unsigned int)codepage); | ||||
|      	setlocale (LC_ALL, locale); | ||||
| 		/*qse_setdflcmgrbyid (QSE_CMGR_SLMB);*/ | ||||
| 	} | ||||
| #else | ||||
| 	setlocale (LC_ALL, ""); | ||||
| 	/*qse_setdflcmgrbyid (QSE_CMGR_SLMB);*/ | ||||
| #endif | ||||
|  | ||||
| 	setlocale (LC_ALL, ""); | ||||
|  | ||||
| 	g_out = qse_sio_openstd (QSE_MMGR_GETDFL(), 0, QSE_SIO_STDOUT, QSE_SIO_WRITE | QSE_SIO_IGNOREMBWCERR); | ||||
|  | ||||
| 	R (test1); | ||||
| 	R (test2); | ||||
| 	R (test3); | ||||
|  | ||||
| 	qse_sio_close (g_out); | ||||
| 	return 0; | ||||
| } | ||||
							
								
								
									
										203
									
								
								samples/si/sio03.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										203
									
								
								samples/si/sio03.c
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,203 @@ | ||||
| #include <qse/si/sio.h> | ||||
| #include <qse/cmn/mbwc.h> | ||||
| #include <qse/cmn/mem.h> | ||||
|  | ||||
| #include <locale.h> | ||||
|  | ||||
| #if defined(_WIN32) | ||||
| #	include <windows.h> | ||||
| #endif | ||||
|  | ||||
| static qse_sio_t* g_out; | ||||
|  | ||||
| #define R(f) \ | ||||
| 	do { \ | ||||
| 		qse_sio_putstrf (g_out, QSE_T("== %s ==\n"), QSE_T(#f)); \ | ||||
| 		if (f() == -1) return -1; \ | ||||
| 	} while (0) | ||||
|  | ||||
| static int test1 (void) | ||||
| { | ||||
| 	const qse_wchar_t unistr[] =  | ||||
| 	{ | ||||
| 		/* ugly hack for old compilers that don't support \u */ | ||||
| 		/*L"\uB108 \uBB50\uAC00 \uC798\uB0AC\uC5B4!",*/ | ||||
| 		0xB108, | ||||
| 		L' ', | ||||
| 		0xBB50, | ||||
| 		0xAC00, | ||||
| 		L' ', | ||||
| 		0xC798, | ||||
| 		0xB0AC, | ||||
| 		0xC5B4, | ||||
| 		L'!', | ||||
| 		L'\0' | ||||
| 	}; | ||||
|  | ||||
| 	const qse_wchar_t unistr2[] =  | ||||
| 	{ | ||||
| 		/* this include an illegal unicode character. | ||||
| 		 * a strict converter should return an error so a question mark  | ||||
| 		 * should be printed for such a character */ | ||||
| 		0xFFFF53C0u, | ||||
| 		0xFFFF4912u, | ||||
| 		0xBA00u, | ||||
| 		0xFFFF1234u, | ||||
| 		L'\0' | ||||
| 	}; | ||||
| 	const qse_wchar_t* x[] = | ||||
| 	{ | ||||
| 		L"", | ||||
| 		L"", | ||||
| 		L"", | ||||
| 		L"Fly to the universe, kick your ass" | ||||
| 	}; | ||||
| 	int i; | ||||
| 	qse_sio_t* sio; | ||||
|  | ||||
| 	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); | ||||
| 	if (sio == QSE_NULL) return -1; | ||||
|  | ||||
| 	for (i = 0; i < QSE_COUNTOF(x); i++) | ||||
| 	{ | ||||
| 		qse_sio_putwcs (sio, x[i]); | ||||
| 		qse_sio_putwc (sio, QSE_WT('\n')); | ||||
| 	} | ||||
|  | ||||
| 	qse_sio_close (sio); | ||||
| 	return 0; | ||||
| } | ||||
|  | ||||
| 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") | ||||
| 	}; | ||||
| 	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); | ||||
| 	if (sio == QSE_NULL) return -1; | ||||
|  | ||||
| 	for (i = 0; i < QSE_COUNTOF(x); i++) | ||||
| 	{ | ||||
| 		qse_sio_putmbs (sio, x[i]); | ||||
| 		qse_sio_putmb (sio, QSE_MT('\n')); | ||||
| 	} | ||||
|  | ||||
| 	qse_sio_close (sio); | ||||
| 	return 0; | ||||
| } | ||||
|  | ||||
| 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") | ||||
| 	}; | ||||
| 	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); | ||||
| 	if (sio == QSE_NULL) return -1; | ||||
|  | ||||
| 	for (i = 0; i < QSE_COUNTOF(x); i++) | ||||
| 	{ | ||||
| 		qse_sio_putmbsn (sio, x[i], qse_mbslen(x[i])); | ||||
| 		qse_sio_putmb (sio, QSE_MT('\n')); | ||||
| 	} | ||||
|  | ||||
| 	qse_sio_close (sio); | ||||
| 	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 () | ||||
| { | ||||
| #if defined(_WIN32) | ||||
|  	char locale[100]; | ||||
| 	UINT codepage = GetConsoleOutputCP();	 | ||||
| 	if (codepage == CP_UTF8) | ||||
| 	{ | ||||
| 		/*SetConsoleOUtputCP (CP_UTF8);*/ | ||||
| 		qse_setdflcmgrbyid (QSE_CMGR_UTF8); | ||||
| 	} | ||||
| 	else | ||||
| 	{ | ||||
| 		sprintf (locale, ".%u", (unsigned int)codepage); | ||||
| 		setlocale (LC_ALL, locale); | ||||
| 		/*qse_setdflcmgrbyid (QSE_CMGR_SLMB);*/ | ||||
| 	} | ||||
| #else | ||||
| 	setlocale (LC_ALL, ""); | ||||
| 	/*qse_setdflcmgrbyid (QSE_CMGR_SLMB);*/ | ||||
| #endif | ||||
|  | ||||
| 	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; | ||||
| } | ||||
							
								
								
									
										175
									
								
								samples/si/spl01.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										175
									
								
								samples/si/spl01.c
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,175 @@ | ||||
| #include <qse/si/spl.h> | ||||
| #include <qse/si/thr.h> | ||||
| #include <qse/si/sio.h> | ||||
| #include <qse/cmn/mem.h> | ||||
|  | ||||
| #include <locale.h> | ||||
| #if defined(_WIN32) | ||||
| #	include <windows.h> | ||||
| #endif | ||||
|  | ||||
| #include <signal.h> | ||||
| #include <string.h> | ||||
| #include <sched.h> | ||||
|  | ||||
| static int g_stopreq = 0; | ||||
| static qse_ntime_t sleep_interval = { 1, 0 }; | ||||
|  | ||||
| struct thr_xtn_t | ||||
| { | ||||
| 	int stopreq; | ||||
| 	qse_char_t name[32]; | ||||
| 	qse_spl_t* spl; | ||||
| }; | ||||
| typedef struct thr_xtn_t thr_xtn_t; | ||||
|  | ||||
| static int thr_func (qse_thr_t* thr, void* ctx) | ||||
| { | ||||
| 	int i = 0; | ||||
| 	thr_xtn_t* xtn = qse_thr_getxtn(thr); | ||||
|  | ||||
| 	while (!xtn->stopreq) | ||||
| 	{ | ||||
| 		qse_spl_lock (xtn->spl); | ||||
| 		qse_printf (QSE_T("%s: [% 16d] [% 16d] [% 16d]\n"), xtn->name, i, i, i); | ||||
| 		qse_spl_unlock (xtn->spl); | ||||
| 		i++; | ||||
| 		if (!(i % 15)) sched_yield(); | ||||
| 		/*qse_sleep (&sleep_interval);*/ | ||||
| 	} | ||||
|  | ||||
| 	return i; | ||||
| } | ||||
|  | ||||
| static int test1 (void) | ||||
| { | ||||
| 	qse_thr_t* thr1, * thr2; | ||||
| 	qse_spl_t spl; | ||||
| 	thr_xtn_t* xtn1, * xtn2; | ||||
|  | ||||
| 	qse_spl_init (&spl); | ||||
|  | ||||
| 	thr1 = qse_thr_open (QSE_MMGR_GETDFL(), QSE_SIZEOF(thr_xtn_t)); | ||||
| 	if (!thr1) | ||||
| 	{ | ||||
| 		qse_printf (QSE_T("cannot open thread1\n")); | ||||
| 		return -1; | ||||
| 	} | ||||
|  | ||||
| 	thr2 = qse_thr_open (QSE_MMGR_GETDFL(), QSE_SIZEOF(thr_xtn_t)); | ||||
| 	if (!thr2) | ||||
| 	{ | ||||
| 		qse_printf (QSE_T("cannot open thread2\n")); | ||||
| 		return -1; | ||||
| 	} | ||||
|  | ||||
| 	xtn1 = qse_thr_getxtn(thr1); | ||||
| 	xtn2 = qse_thr_getxtn(thr2); | ||||
|  | ||||
| 	qse_strcpy (xtn1->name, QSE_T("Thr-X")); | ||||
| 	qse_strcpy (xtn2->name, QSE_T("Thr-Y")); | ||||
| 	xtn1->spl = &spl; | ||||
| 	xtn2->spl = &spl; | ||||
|  | ||||
| 	qse_thr_setstacksize (thr1, 64000); | ||||
| 	qse_thr_setstacksize (thr2, 64000); | ||||
|  | ||||
| 	if (qse_thr_start(thr1, thr_func, QSE_NULL, QSE_THR_SIGNALS_BLOCKED) <= -1) | ||||
| 	{ | ||||
| 		qse_printf (QSE_T("cannot start thread1\n")); | ||||
| 		qse_thr_close (thr1); | ||||
| 		return -1; | ||||
| 	} | ||||
|  | ||||
| 	if (qse_thr_start(thr2, thr_func, QSE_NULL, QSE_THR_SIGNALS_BLOCKED) <= -1) | ||||
| 	{ | ||||
| 		qse_printf (QSE_T("cannot start thread1\n")); | ||||
| 		qse_thr_close (thr1); | ||||
| 		return -1; | ||||
| 	} | ||||
|  | ||||
| 	while (!g_stopreq) | ||||
| 	{ | ||||
| 		if (qse_thr_getstate(thr1) == QSE_THR_TERMINATED && | ||||
| 		    qse_thr_getstate(thr2) == QSE_THR_TERMINATED) break; | ||||
| 		sleep (1); | ||||
| 	} | ||||
|  | ||||
| 	if (g_stopreq)  | ||||
| 	{ | ||||
| 		xtn1->stopreq = 1; | ||||
| 		xtn2->stopreq = 1; | ||||
| 	} | ||||
|  | ||||
| 	qse_thr_join (thr1); | ||||
| 	qse_thr_join (thr2); | ||||
|  | ||||
| 	qse_printf (QSE_T("thread1 ended with retcode %d\n"), qse_thr_getretcode(thr1)); | ||||
| 	qse_printf (QSE_T("thread2 ended with retcode %d\n"), qse_thr_getretcode(thr2)); | ||||
| 	qse_thr_close (thr1); | ||||
| 	qse_thr_close (thr2); | ||||
|  | ||||
| 	return 0; | ||||
| } | ||||
|  | ||||
| static void handle_sigint (int sig, siginfo_t* siginfo, void* ctx) | ||||
| { | ||||
| 	g_stopreq = 1; | ||||
| } | ||||
|  | ||||
| static void set_signal (int sig, void(*handler)(int, siginfo_t*, void*)) | ||||
| { | ||||
| 	struct sigaction sa; | ||||
|   | ||||
| 	memset (&sa, 0, sizeof(sa)); | ||||
| 	/*sa.sa_handler = handler;*/ | ||||
| 	sa.sa_flags = SA_SIGINFO; | ||||
| 	sa.sa_sigaction = handler; | ||||
| 	sigemptyset (&sa.sa_mask); | ||||
|   | ||||
| 	sigaction (sig, &sa, NULL); | ||||
| } | ||||
|   | ||||
| static void set_signal_to_default (int sig) | ||||
| { | ||||
| 	struct sigaction sa; | ||||
|   | ||||
| 	memset (&sa, 0, sizeof(sa)); | ||||
| 	sa.sa_handler = SIG_DFL; | ||||
| 	sa.sa_flags = 0; | ||||
| 	sigemptyset (&sa.sa_mask); | ||||
|   | ||||
| 	sigaction (sig, &sa, NULL); | ||||
| } | ||||
|   | ||||
| int main () | ||||
| { | ||||
| #if defined(_WIN32) | ||||
|  	char locale[100]; | ||||
| 	UINT codepage = GetConsoleOutputCP(); | ||||
| 	if (codepage == CP_UTF8) | ||||
| 	{ | ||||
| 		/*SetConsoleOutputCP (CP_UTF8);*/ | ||||
| 		qse_setdflcmgrbyid (QSE_CMGR_UTF8); | ||||
| 	} | ||||
| 	else | ||||
| 	{ | ||||
| 		qse_mbsxfmt (locale, QSE_COUNTOF(locale), ".%u", (unsigned int)codepage); | ||||
| 		setlocale (LC_ALL, locale); | ||||
| 		/*qse_setdflcmgrbyid (QSE_CMGR_SLMB);*/ | ||||
| 	} | ||||
| #else | ||||
| 	setlocale (LC_ALL, ""); | ||||
| 	/*qse_setdflcmgrbyid (QSE_CMGR_SLMB);*/ | ||||
| #endif | ||||
|  | ||||
| 	set_signal (SIGINT, handle_sigint); | ||||
|  | ||||
| 	qse_open_stdsios_with_flags (QSE_SIO_LINEBREAK); /* to disable mutex protection on stdout & stderr */ | ||||
| 	test1(); | ||||
| 	qse_close_stdsios (); | ||||
|  | ||||
| 	set_signal_to_default (SIGINT); | ||||
|  | ||||
| 	return 0; | ||||
| } | ||||
							
								
								
									
										300
									
								
								samples/si/spl02.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										300
									
								
								samples/si/spl02.cpp
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,300 @@ | ||||
| #include <qse/si/SpinLock.hpp> | ||||
| #include <qse/si/Thread.hpp> | ||||
| #include <qse/si/mtx.h> | ||||
| #include <qse/si/sio.h> | ||||
| #include <qse/si/os.h> | ||||
| #include <qse/cmn/mem.h> | ||||
| #include <qse/cmn/str.h> | ||||
|  | ||||
| #include <locale.h> | ||||
| #if defined(_WIN32) | ||||
| #	include <windows.h> | ||||
| #endif | ||||
|  | ||||
| #include <signal.h> | ||||
| #include <string.h> | ||||
|  | ||||
| static int g_stopreq = 0; | ||||
| static qse_ntime_t sleep_interval = { 1, 0 }; | ||||
|  | ||||
| static QSE::SpinLock g_prmtx; | ||||
|  | ||||
| class MyThread: public QSE::Thread | ||||
| { | ||||
| public: | ||||
| 	MyThread(): stopreq(0) {} | ||||
|  | ||||
| 	int main () | ||||
| 	{ | ||||
| 		int i = 0; | ||||
|  | ||||
| 		while (!this->stopreq) | ||||
| 		{ | ||||
| 			g_prmtx.lock (); | ||||
| 			qse_printf (QSE_T("m %p -> %d\n"), this, i); | ||||
| 			g_prmtx.unlock (); | ||||
| 			i++; | ||||
| 			qse_sleep (&sleep_interval); | ||||
| 		} | ||||
|  | ||||
| 		return i; | ||||
| 	} | ||||
|  | ||||
| 	int stopreq; | ||||
| }; | ||||
|  | ||||
| class Functor | ||||
| { | ||||
| public: | ||||
| 	int operator() (QSE::Thread* thr) | ||||
| 	{ | ||||
| 		int i = 0; | ||||
| 		int* stopreqptr = (int*)thr->getContext(); | ||||
|  | ||||
| 		while (!*stopreqptr) | ||||
| 		{ | ||||
| 			g_prmtx.lock (); | ||||
| 			qse_printf (QSE_T("fc %p -> %d\n"), this, i); | ||||
| 			g_prmtx.unlock (); | ||||
| 			i++; | ||||
| 			qse_sleep (&sleep_interval); | ||||
| 		} | ||||
|  | ||||
| 		return i; | ||||
| 	} | ||||
| }; | ||||
|  | ||||
| static int func_ptr (QSE::Thread* thr) | ||||
| { | ||||
| 	int i = 0; | ||||
| 	int* stopreqptr = (int*)thr->getContext(); | ||||
|  | ||||
| 	while (!*stopreqptr) | ||||
| 	{ | ||||
| 		g_prmtx.lock (); | ||||
| 		qse_printf (QSE_T("fp %p -> %d\n"), thr, i); | ||||
| 		g_prmtx.unlock (); | ||||
| 		i++; | ||||
| 		qse_sleep (&sleep_interval); | ||||
| 	} | ||||
|  | ||||
| 	return i; | ||||
| } | ||||
|  | ||||
| static int test1 (void) | ||||
| { | ||||
| 	int localstopreq = 0; | ||||
|  | ||||
| #if defined(QSE_LANG_CPP11) | ||||
| 	auto lambda = [](QSE::Thread* thr)->int  | ||||
| 	{  | ||||
| 		int i = 0; | ||||
| 		int* stopreqptr = (int*)thr->getContext(); | ||||
|  | ||||
| 		while (!*stopreqptr) | ||||
| 		{ | ||||
| 			g_prmtx.lock (); | ||||
| 			qse_printf (QSE_T("l %p -> %d\n"), thr, i); | ||||
| 			g_prmtx.unlock (); | ||||
| 			i++; | ||||
| 			qse_sleep (&sleep_interval); | ||||
| 		} | ||||
|  | ||||
| 		return i; | ||||
| 	}; | ||||
|  | ||||
| 	auto lambda_with_capture = [&localstopreq](QSE::Thread* thr)->int | ||||
| 	{  | ||||
| 		int i = 0; | ||||
|  | ||||
| 		while (!localstopreq) | ||||
| 		{ | ||||
| 			g_prmtx.lock (); | ||||
| 			qse_printf (QSE_T("lc %p -> %d\n"), thr, i); | ||||
| 			g_prmtx.unlock (); | ||||
| 			i++; | ||||
| 			qse_sleep (&sleep_interval); | ||||
| 		} | ||||
|  | ||||
| 		return i; | ||||
| 	}; | ||||
| #endif | ||||
|  | ||||
| 	MyThread thr1; | ||||
| 	thr1.setStackSize (64000); | ||||
| 	if (thr1.start(QSE::Thread::SIGNALS_BLOCKED) <= -1) | ||||
| 	{ | ||||
| 		qse_printf (QSE_T("cannot start thread1\n")); | ||||
| 		return -1; | ||||
| 	} | ||||
|  | ||||
| 	QSE::ThreadR thr2; | ||||
| 	thr2.setStackSize (64000); | ||||
| 	thr2.setContext (&localstopreq); | ||||
| #if defined(QSE_LANG_CPP11) | ||||
| 	// the lambda expression with no capture can be passed as a function pointer | ||||
| 	// as long as the signature matches QSE::Thread::ThreadRoutine. | ||||
| 	if (thr2.start(lambda, QSE::Thread::SIGNALS_BLOCKED) <= -1) | ||||
| #else | ||||
| 	if (thr2.start(func_ptr, QSE::Thread::SIGNALS_BLOCKED) <= -1) | ||||
| #endif | ||||
| 	{ | ||||
| 		qse_printf (QSE_T("cannot start thread2\n")); | ||||
| 		return -1; | ||||
| 	} | ||||
|  | ||||
| #if defined(QSE_LANG_CPP11) | ||||
| 	QSE::ThreadF<decltype(lambda)> thr3 (lambda); | ||||
| 	thr3.setStackSize (64000); | ||||
| 	thr3.setContext (&localstopreq); | ||||
| 	if (thr3.start(QSE::Thread::SIGNALS_BLOCKED) <= -1) | ||||
| 	{ | ||||
| 		qse_printf (QSE_T("cannot start thread3\n")); | ||||
| 		return -1; | ||||
| 	} | ||||
|  | ||||
| 	// turn a lambda with capture to a thread | ||||
| 	QSE::ThreadF<decltype(lambda_with_capture)> thr4 (lambda_with_capture); | ||||
| 	thr4.setStackSize (64000); | ||||
| 	if (thr4.start(QSE::Thread::SIGNALS_BLOCKED) <= -1) | ||||
| 	{ | ||||
| 		qse_printf (QSE_T("cannot start thread4\n")); | ||||
| 		return -1; | ||||
| 	} | ||||
|  | ||||
| 	QSE::ThreadL<int(QSE::Thread*)> thr5; | ||||
| 	thr5.setStackSize (64000); | ||||
| 	if (thr5.start( | ||||
| 		([&localstopreq, &thr5](QSE::Thread* thr) {  | ||||
| 			int i = 0; | ||||
|  | ||||
| 			while (!localstopreq) | ||||
| 			{ | ||||
| 				g_prmtx.lock (); | ||||
| 				qse_printf (QSE_T("tl %p -> %d\n"), thr, i); | ||||
| 				g_prmtx.unlock (); | ||||
| 				i++; | ||||
| 				qse_sleep (&sleep_interval); | ||||
| 			} | ||||
|  | ||||
| 			return i; | ||||
| 		}),  | ||||
| 		QSE::Thread::SIGNALS_BLOCKED) <= -1) | ||||
| 	{ | ||||
| 		qse_printf (QSE_T("cannot start thread5\n")); | ||||
| 		return -1; | ||||
| 	} | ||||
| #endif | ||||
|  | ||||
| 	// turn a functor to a thread | ||||
| 	QSE::ThreadF<Functor> thr6; | ||||
| 	thr6.setStackSize (64000); | ||||
| 	thr6.setContext (&localstopreq); | ||||
| 	if (thr6.start(QSE::Thread::SIGNALS_BLOCKED) <= -1) | ||||
| 	{ | ||||
| 		qse_printf (QSE_T("cannot start thread6\n")); | ||||
| 		return -1; | ||||
| 	} | ||||
|  | ||||
| 	while (!g_stopreq) | ||||
| 	{ | ||||
| 		if (thr1.getState() == QSE::Thread::TERMINATED &&  | ||||
| 		    thr2.getState() == QSE::Thread::TERMINATED && | ||||
| #if defined(QSE_LANG_CPP11) | ||||
| 		    thr3.getState() == QSE::Thread::TERMINATED && | ||||
| 		    thr4.getState() == QSE::Thread::TERMINATED && | ||||
| 		    thr5.getState() == QSE::Thread::TERMINATED && | ||||
| #endif | ||||
| 		    thr6.getState() == QSE::Thread::TERMINATED) break; | ||||
| 		qse_sleep (&sleep_interval); | ||||
| 	} | ||||
|  | ||||
| 	if (g_stopreq)  | ||||
| 	{ | ||||
| 		localstopreq = 1; | ||||
| 		thr1.stopreq = 1; | ||||
| 	} | ||||
|  | ||||
| 	thr1.join (); | ||||
| 	thr2.join (); | ||||
| #if defined(QSE_LANG_CPP11) | ||||
| 	thr3.join (); | ||||
| 	thr4.join (); | ||||
| 	thr5.join (); | ||||
| #endif | ||||
| 	thr6.join (); | ||||
|  | ||||
| 	qse_printf (QSE_T("thread1 ended with retcode %d\n"), thr1.getReturnCode()); | ||||
| 	qse_printf (QSE_T("thread2 ended with retcode %d\n"), thr2.getReturnCode()); | ||||
| #if defined(QSE_LANG_CPP11) | ||||
| 	qse_printf (QSE_T("thread3 ended with retcode %d\n"), thr3.getReturnCode()); | ||||
| 	qse_printf (QSE_T("thread4 ended with retcode %d\n"), thr4.getReturnCode()); | ||||
| 	qse_printf (QSE_T("thread5 ended with retcode %d\n"), thr5.getReturnCode()); | ||||
| #endif | ||||
| 	qse_printf (QSE_T("thread6 ended with retcode %d\n"), thr6.getReturnCode()); | ||||
|  | ||||
| 	return 0; | ||||
| } | ||||
|  | ||||
|  | ||||
| static void handle_sigint (int sig, siginfo_t* siginfo, void* ctx) | ||||
| { | ||||
| 	g_stopreq = 1; | ||||
| } | ||||
|  | ||||
| static void set_signal (int sig, void(*handler)(int, siginfo_t*, void*)) | ||||
| { | ||||
| 	struct sigaction sa; | ||||
|   | ||||
| 	memset (&sa, 0, sizeof(sa)); | ||||
| 	/*sa.sa_handler = handler;*/ | ||||
| 	sa.sa_flags = SA_SIGINFO; | ||||
| 	sa.sa_sigaction = handler; | ||||
| 	sigemptyset (&sa.sa_mask); | ||||
|   | ||||
| 	sigaction (sig, &sa, NULL); | ||||
| } | ||||
|   | ||||
| static void set_signal_to_default (int sig) | ||||
| { | ||||
| 	struct sigaction sa; | ||||
|   | ||||
| 	memset (&sa, 0, sizeof(sa)); | ||||
| 	sa.sa_handler = SIG_DFL; | ||||
| 	sa.sa_flags = 0; | ||||
| 	sigemptyset (&sa.sa_mask); | ||||
|   | ||||
| 	sigaction (sig, &sa, NULL); | ||||
| } | ||||
|   | ||||
| int main () | ||||
| { | ||||
| #if defined(_WIN32) | ||||
|  	char locale[100]; | ||||
| 	UINT codepage = GetConsoleOutputCP(); | ||||
| 	if (codepage == CP_UTF8) | ||||
| 	{ | ||||
| 		/*SetConsoleOutputCP (CP_UTF8);*/ | ||||
| 		qse_setdflcmgrbyid (QSE_CMGR_UTF8); | ||||
| 	} | ||||
| 	else | ||||
| 	{ | ||||
| 		qse_mbsxfmt (locale, QSE_COUNTOF(locale), ".%u", (unsigned int)codepage); | ||||
| 		setlocale (LC_ALL, locale); | ||||
| 		/*qse_setdflcmgrbyid (QSE_CMGR_SLMB);*/ | ||||
| 	} | ||||
| #else | ||||
| 	setlocale (LC_ALL, ""); | ||||
| 	/*qse_setdflcmgrbyid (QSE_CMGR_SLMB);*/ | ||||
| #endif | ||||
|  | ||||
| 	set_signal (SIGINT, handle_sigint); | ||||
|  | ||||
| 	qse_open_stdsios_with_flags (QSE_SIO_LINEBREAK); | ||||
| 	test1(); | ||||
| 	qse_close_stdsios (); | ||||
|  | ||||
| 	set_signal_to_default (SIGINT); | ||||
|  | ||||
| 	return 0; | ||||
| } | ||||
							
								
								
									
										111
									
								
								samples/si/task01.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										111
									
								
								samples/si/task01.c
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,111 @@ | ||||
| #include <qse/si/task.h> | ||||
| #include <qse/cmn/main.h> | ||||
| #include <qse/cmn/mbwc.h> | ||||
| #include <qse/cmn/mem.h> | ||||
| #include <qse/si/sio.h> | ||||
|  | ||||
| #include <locale.h> | ||||
| #if defined(_WIN32) | ||||
| #    include <windows.h> | ||||
| #endif | ||||
|  | ||||
| static qse_task_slice_t* print ( | ||||
| 	qse_task_t* task, qse_task_slice_t* slice, void* ctx) | ||||
| { | ||||
| 	int i; | ||||
| 	int num = (int)ctx; | ||||
|  | ||||
| 	qse_printf (QSE_T("task[%03d] => starting\n"), num); | ||||
| 	for (i = 0; i < 5; i++)  | ||||
| 	{  | ||||
| 		qse_printf (QSE_T("task[%03d] => %03d\n"), num, i);  | ||||
|  | ||||
| 		qse_task_schedule (task, slice, QSE_NULL);	 | ||||
|  | ||||
| 		if (i == 2 && num == 1) | ||||
| 		{ | ||||
| 			qse_task_create (task, print, (void*)99, 40000); | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	qse_printf (QSE_T("task[%03d] => exiting\n"), num); | ||||
| 	return QSE_NULL;  | ||||
| } | ||||
|  | ||||
| static int start_task (qse_task_fnc_t fnc) | ||||
| { | ||||
| 	qse_task_t* task; | ||||
| 	qse_task_slice_t* slice; | ||||
|  | ||||
| 	task = qse_task_open (QSE_MMGR_GETDFL(), 0); | ||||
| 	if (task == NULL) | ||||
| 	{ | ||||
| 		qse_printf (QSE_T("cannot initialize tasking system\n")); | ||||
| 		return -1; | ||||
| 	} | ||||
|  | ||||
| 	qse_printf (QSE_T("== END ==\n")); | ||||
|  | ||||
| 	if (qse_task_create (task, fnc, (void*)1, 40000) == QSE_NULL || | ||||
| 	    (slice = qse_task_create (task, fnc, (void*)2, 40000)) == QSE_NULL || | ||||
| 	    qse_task_create (task, fnc, (void*)3, 40000) == QSE_NULL || | ||||
| 	    qse_task_create (task, fnc, (void*)4, 40000) == QSE_NULL) | ||||
| 	{ | ||||
| 		qse_printf (QSE_T("cannot create task slice\n")); | ||||
| 		qse_task_close (task); | ||||
| 		return -1; | ||||
| 	} | ||||
|  | ||||
| 	if (qse_task_boot (task, slice) <= -1) | ||||
| 	{ | ||||
| 		qse_printf (QSE_T("cannot start task\n")); | ||||
| 		qse_task_close (task); | ||||
| 		return -1; | ||||
| 	} | ||||
|  | ||||
| 	qse_printf (QSE_T("== END ==\n")); | ||||
|  | ||||
| 	qse_task_close (task); | ||||
| 	return 0; | ||||
| } | ||||
|  | ||||
| static int test_main (int argc, qse_char_t* argv[]) | ||||
| { | ||||
| 	int ret; | ||||
|  | ||||
| 	ret  = start_task (print); | ||||
| 	qse_printf (QSE_T("== END ==\n")); | ||||
|  | ||||
| 	return ret; | ||||
| } | ||||
|  | ||||
| int qse_main (int argc, qse_achar_t* argv[]) | ||||
| { | ||||
| 	int x; | ||||
|  | ||||
| #if defined(_WIN32) | ||||
| 	char locale[100]; | ||||
| 	UINT codepage = GetConsoleOutputCP(); | ||||
| 	if (codepage == CP_UTF8) | ||||
| 	{ | ||||
| 		/*SetConsoleOUtputCP (CP_UTF8);*/ | ||||
| 		qse_setdflcmgrbyid (QSE_CMGR_UTF8); | ||||
| 	} | ||||
| 	else | ||||
| 	{ | ||||
| 		sprintf (locale, ".%u", (unsigned int)codepage); | ||||
| 		setlocale (LC_ALL, locale); | ||||
| 		/*qse_setdflcmgrbyid (QSE_CMGR_SLMB);*/ | ||||
| 	} | ||||
| #else | ||||
| 	setlocale (LC_ALL, ""); | ||||
| 	/*qse_setdflcmgrbyid (QSE_CMGR_SLMB);*/ | ||||
| #endif | ||||
|  | ||||
| 	qse_open_stdsios (); | ||||
| 	x = qse_run_main (argc, argv, test_main); | ||||
| 	qse_close_stdsios (); | ||||
|  | ||||
| 	return x; | ||||
| } | ||||
|  | ||||
							
								
								
									
										167
									
								
								samples/si/tcpsvr01.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										167
									
								
								samples/si/tcpsvr01.cpp
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,167 @@ | ||||
| #include <qse/si/TcpServer.hpp> | ||||
| #include <qse/si/Mutex.hpp> | ||||
| #include <qse/si/sio.h> | ||||
| #include <qse/si/os.h> | ||||
| #include <qse/cmn/mem.h> | ||||
| #include <qse/cmn/str.h> | ||||
| #include <qse/cmn/HeapMmgr.hpp> | ||||
| #include <qse/si/App.hpp> | ||||
|  | ||||
|  | ||||
| #include <locale.h> | ||||
| #if defined(_WIN32) | ||||
| #	include <windows.h> | ||||
| #endif | ||||
|  | ||||
| #include <signal.h> | ||||
| #include <string.h> | ||||
| #include <unistd.h> | ||||
| #include <sys/wait.h> | ||||
|  | ||||
| #if defined(__linux) | ||||
| #include <sys/prctl.h> | ||||
| #endif | ||||
|  | ||||
| class ClientHandler | ||||
| { | ||||
| public: | ||||
| 	int operator() (QSE::TcpServer* server, QSE::TcpServer::Connection* connection) | ||||
| 	{ | ||||
| 		qse_char_t addrbuf[128]; | ||||
| 		qse_uint8_t bb[256]; | ||||
| 		qse_ssize_t n; | ||||
|  | ||||
| 		connection->address.toStrBuf(addrbuf, QSE_COUNTOF(addrbuf)); | ||||
| 		qse_printf (QSE_T("hello word..from %s -> wid %zu\n"), addrbuf, connection->getWid()); | ||||
|  | ||||
| 		while (!server->isHaltRequested()) | ||||
| 		{ | ||||
| 			if ((n = connection->socket.receive(bb, QSE_COUNTOF(bb))) <= 0)  | ||||
| 			{ | ||||
| 				qse_printf (QSE_T("%zd bytes received from %s\n"), n, addrbuf); | ||||
| 				break; | ||||
| 			} | ||||
| 			connection->socket.send (bb, n); | ||||
| 		} | ||||
|  | ||||
| 		qse_printf (QSE_T("byte to %s -> wid %zu\n"), addrbuf, connection->getWid()); | ||||
| 		return 0; | ||||
| 	} | ||||
| }; | ||||
|  | ||||
| #if defined(QSE_LANG_CPP11) | ||||
| static QSE::TcpServerL<int(QSE::TcpServer::Connection*)>* g_server; | ||||
| #else | ||||
| static QSE::TcpServerF<ClientHandler>* g_server; | ||||
| #endif | ||||
|  | ||||
|  | ||||
| class MyApp: public QSE::App | ||||
| { | ||||
| public: | ||||
| 	MyApp(QSE::Mmgr* mmgr): App(mmgr), server(mmgr) {} | ||||
|  | ||||
| 	void on_signal (int sig) | ||||
| 	{ | ||||
| 		switch (sig) | ||||
| 		{ | ||||
| 			case SIGINT: | ||||
| 			case SIGTERM: | ||||
| 			case SIGHUP: | ||||
| 				// TODO: don't call stop() if this processs is a guardian | ||||
| 				//       though it's no harm to call stop(). | ||||
| 				QSE_APP_LOG3 (this, QSE_LOG_INFO, QSE_T("requesting to stop server...app %p server %p - pid %d\n"), this, &this->server, (int)getpid()); | ||||
| 				this->server.halt(); | ||||
| 				break; | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	int run () | ||||
| 	{ | ||||
| 		QSE::App::SignalSet signals; | ||||
| 		signals.set (SIGINT); | ||||
| 		signals.set (SIGHUP); | ||||
| 		signals.set (SIGTERM); | ||||
| 		signals.set (SIGUSR1); | ||||
| 		signals.set (SIGUSR2); | ||||
| 		if (this->guardProcess(signals) > 0) | ||||
| 		{ | ||||
| 			int target_flags; | ||||
| 			qse_log_target_data_t target_data;  | ||||
| 			target_data.file = QSE_T("tcpsvr01.log"); | ||||
|  | ||||
| 			this->setName (QSE_T("tcpsvr01")); | ||||
| 			this->setLogTarget (QSE_LOG_CONSOLE | QSE_LOG_FILE, target_data); | ||||
| 			this->setLogPriorityMask (QSE_LOG_ALL_PRIORITIES); | ||||
| 			this->setLogOption (QSE_LOG_KEEP_FILE_OPEN | QSE_LOG_INCLUDE_PID); | ||||
|  | ||||
| 			QSE_APP_LOG0 (this, QSE_LOG_DEBUG, QSE_T("Starting server\n")); | ||||
| 			QSE_APP_LOG0 (this, QSE_LOG_DEBUG, QSE_T("hello")); | ||||
| 			QSE_APP_LOG0 (this, QSE_LOG_INFO, QSE_T(" <tcpsvr01r> ")); | ||||
| 			QSE_APP_LOG0 (this, QSE_LOG_INFO, QSE_T("started\n")); | ||||
| 			this->server.setThreadStackSize (256000); | ||||
| 			return this->server.execute(QSE_T("[::]:9998,0.0.0.0:9998")); | ||||
| 		} | ||||
| 		return -1; | ||||
| 	} | ||||
|  | ||||
| protected: | ||||
| 	QSE::TcpServerF<ClientHandler> server; | ||||
| }; | ||||
|  | ||||
| static int test1() | ||||
| { | ||||
| 	QSE::HeapMmgr heap_mmgr (30000, QSE::Mmgr::getDFL()); | ||||
| 	MyApp app (&heap_mmgr); | ||||
|  | ||||
| MyApp app2 (&heap_mmgr); | ||||
| MyApp app3 (&heap_mmgr); | ||||
| MyApp app4 (&heap_mmgr); | ||||
|  | ||||
| 	app.acceptSignal (SIGINT); | ||||
| 	app.acceptSignal (SIGTERM); | ||||
|  | ||||
| app4.acceptSignal (SIGINT); | ||||
| app3.acceptSignal (SIGINT); | ||||
| app2.acceptSignal (SIGINT); | ||||
|  | ||||
| 	int n = app.run(); | ||||
| 	app.discardSignal (SIGTERM); | ||||
| 	app.discardSignal (SIGINT); | ||||
|  | ||||
| app4.neglectSignal (SIGINT);  | ||||
| app3.neglectSignal (SIGINT); | ||||
| app2.neglectSignal (SIGINT); | ||||
| QSE_APP_LOG1 (&app, QSE_LOG_INFO, QSE_T("END OF %d\n"), (int)getpid()); | ||||
|  | ||||
| 	return n; | ||||
| } | ||||
|  | ||||
| int main () | ||||
| { | ||||
| #if defined(_WIN32) | ||||
|  	char locale[100]; | ||||
| 	UINT codepage = GetConsoleOutputCP(); | ||||
| 	if (codepage == CP_UTF8) | ||||
| 	{ | ||||
| 		/*SetConsoleOutputCP (CP_UTF8);*/ | ||||
| 		qse_setdflcmgrbyid (QSE_CMGR_UTF8); | ||||
| 	} | ||||
| 	else | ||||
| 	{ | ||||
| 		qse_mbsxfmt (locale, QSE_COUNTOF(locale), ".%u", (unsigned int)codepage); | ||||
| 		setlocale (LC_ALL, locale); | ||||
| 		/*qse_setdflcmgrbyid (QSE_CMGR_SLMB);*/ | ||||
| 	} | ||||
| #else | ||||
| 	setlocale (LC_ALL, ""); | ||||
| 	/*qse_setdflcmgrbyid (QSE_CMGR_SLMB);*/ | ||||
| #endif | ||||
|  | ||||
|  | ||||
| 	qse_open_stdsios (); | ||||
| 	test1(); | ||||
| 	qse_close_stdsios (); | ||||
|  | ||||
| 	return 0; | ||||
| } | ||||
							
								
								
									
										179
									
								
								samples/si/tcpsvr02.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										179
									
								
								samples/si/tcpsvr02.cpp
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,179 @@ | ||||
| #include <qse/si/TcpServer.hpp> | ||||
| #include <qse/si/Mutex.hpp> | ||||
| #include <qse/si/sio.h> | ||||
| #include <qse/si/os.h> | ||||
| #include <qse/cmn/mem.h> | ||||
| #include <qse/cmn/str.h> | ||||
| #include <qse/cmn/HeapMmgr.hpp> | ||||
| #include <qse/sttp/Sttp.hpp> | ||||
| #include <qse/si/App.hpp> | ||||
|  | ||||
| #include <locale.h> | ||||
| #if defined(_WIN32) | ||||
| #	include <windows.h> | ||||
| #endif | ||||
|  | ||||
| #include <unistd.h> | ||||
| #include <signal.h> | ||||
| #include <string.h> | ||||
|  | ||||
| class Proto: public QSE::Sttp | ||||
| { | ||||
| public: | ||||
| 	Proto (QSE::Socket* sck): sck(sck) {} | ||||
|  | ||||
| 	int handle_command (const QSE::SttpCmd& cmd) | ||||
| 	{ | ||||
| 		qse_printf (QSE_T("command -> %js\n"), cmd.name.getData()); | ||||
| 		int n = this->sendCmd(QSE_T("OK"), 1, cmd.name.getData()); | ||||
| 		if (cmd.name == QSE_T("quit")) | ||||
| 		{ | ||||
| 			sck->shutdown (); | ||||
| 		} | ||||
| 		return n; | ||||
| 	} | ||||
|  | ||||
| 	int write_bytes (const qse_uint8_t* data, qse_size_t len) | ||||
| 	{ | ||||
| 		int n = this->sck->sendx(data, len, QSE_NULL); | ||||
| 		if (QSE_UNLIKELY(n <= -1)) this->setErrorFmt(E_ESYSERR, QSE_T("%js"), sck->getErrorMsg()); | ||||
| 		return n; | ||||
| 	} | ||||
|  | ||||
| protected: | ||||
| 	QSE::Socket* sck; | ||||
| }; | ||||
|  | ||||
| #if defined(QSE_LANG_CPP11) | ||||
| QSE::TcpServerL<int(QSE::TcpServer::Connection*)>* g_server; | ||||
| #else | ||||
|  | ||||
| class ClientHandler | ||||
| { | ||||
| public: | ||||
| 	int operator() (QSE::TcpServer* server, QSE::TcpServer::Connection* connection) | ||||
| 	{ | ||||
| 		qse_char_t addrbuf[128]; | ||||
| 		qse_uint8_t bb[256]; | ||||
| 		qse_ssize_t n; | ||||
|  | ||||
| 		connection->address.toStrBuf(addrbuf, QSE_COUNTOF(addrbuf)); | ||||
| 		qse_printf (QSE_T("hello word..from %s[%zu]\n"), addrbuf, connection->getWid()); | ||||
|  | ||||
| 		Proto proto (&connection->socket); | ||||
|  | ||||
| 		while (!server->isHaltRequested()) | ||||
| 		{ | ||||
| 			if ((n = connection->socket.receive(bb, QSE_COUNTOF(bb))) <= 0) | ||||
| 			{ | ||||
| 				if (n <= -1) | ||||
| 					qse_printf (QSE_T("%s[%zu] -> got error\n"), addrbuf, connection->getWid()); | ||||
| 				break; | ||||
| 			} | ||||
|  | ||||
| 			if (proto.feed(bb, n, QSE_NULL) <= -1) | ||||
| 			{ | ||||
| 				qse_printf (QSE_T("%s[%zu] -> protocol error - %js\n"), addrbuf, connection->getWid(), proto.getErrorMsg()); | ||||
| 				break; | ||||
| 			} | ||||
| 		} | ||||
|  | ||||
| 		qse_printf (QSE_T("byte to %s -> wid %zu\n"), addrbuf, connection->getWid()); | ||||
| 		return 0; | ||||
| 	} | ||||
| }; | ||||
|  | ||||
| static QSE::TcpServerF<ClientHandler>* g_server; | ||||
| #endif | ||||
|  | ||||
|  | ||||
| static int test1 (void) | ||||
| { | ||||
| 	QSE::HeapMmgr heap_mmgr (30000, QSE::Mmgr::getDFL()); | ||||
|  | ||||
| #if defined(QSE_LANG_CPP11) | ||||
| 	QSE::TcpServerL<int(QSE::TcpServer::Connection*)> server ( | ||||
|  | ||||
| 		// workload by lambda | ||||
| 		([&server](QSE::TcpServer::Connection* connection) { | ||||
| 			qse_char_t addrbuf[128]; | ||||
| 			qse_uint8_t bb[256]; | ||||
| 			qse_ssize_t n; | ||||
|  | ||||
| 			connection->address.toStrBuf(addrbuf, QSE_COUNTOF(addrbuf)); | ||||
| 			qse_printf (QSE_T("hello word..from %s --> wid %zu\n"), addrbuf, connection->getWid()); | ||||
|  | ||||
| 			Proto proto (&connection->socket); | ||||
|  | ||||
| 			while (!server.isHaltRequested()) | ||||
| 			{ | ||||
| 				if ((n = connection->socket.receive(bb, QSE_COUNTOF(bb))) <= 0) | ||||
| 				{ | ||||
| 					if (n <= -1) | ||||
| 						qse_printf (QSE_T("%s[%zu] -> got error\n"), addrbuf, connection->getWid()); | ||||
| 					break; | ||||
| 				} | ||||
|  | ||||
| 				if (proto.feed(bb, n, QSE_NULL) <= -1) | ||||
| 				{ | ||||
| 					qse_printf (QSE_T("%s[%zu] -> protocol error - %js\n"), addrbuf, connection->getWid(), proto.getErrorMsg()); | ||||
| 					break; | ||||
| 				} | ||||
| 			} | ||||
|  | ||||
| 			qse_printf (QSE_T("byte to %s -> wid %zu\n"), addrbuf, connection->getWid()); | ||||
| 			return 0; | ||||
| 		}), | ||||
|  | ||||
| 		&heap_mmgr | ||||
|  | ||||
| 	); | ||||
| #else | ||||
| 	QSE::TcpServerF<ClientHandler> server (&heap_mmgr); | ||||
| #endif | ||||
|  | ||||
| 	server.setThreadStackSize (256000); | ||||
| 	g_server = &server; | ||||
| 	//server.execute (QSE_T("0.0.0.0:9998")); | ||||
| 	server.execute (QSE_T("[::]:9998,0.0.0.0:9998")); | ||||
| 	//server.execute (QSE_T("[fe80::1c4:a90d:a0f0:d52%wlan0]:9998,0.0.0.0:9998")); | ||||
| 	g_server = QSE_NULL; | ||||
| 	return 0; | ||||
| } | ||||
|  | ||||
| static void handle_sigint (int sig) | ||||
| { | ||||
| 	if (g_server) g_server->halt (); | ||||
| } | ||||
|  | ||||
| int main () | ||||
| { | ||||
| #if defined(_WIN32) | ||||
|  	char locale[100]; | ||||
| 	UINT codepage = GetConsoleOutputCP(); | ||||
| 	if (codepage == CP_UTF8) | ||||
| 	{ | ||||
| 		/*SetConsoleOutputCP (CP_UTF8);*/ | ||||
| 		qse_setdflcmgrbyid (QSE_CMGR_UTF8); | ||||
| 	} | ||||
| 	else | ||||
| 	{ | ||||
| 		qse_mbsxfmt (locale, QSE_COUNTOF(locale), ".%u", (unsigned int)codepage); | ||||
| 		setlocale (LC_ALL, locale); | ||||
| 		/*qse_setdflcmgrbyid (QSE_CMGR_SLMB);*/ | ||||
| 	} | ||||
| #else | ||||
| 	setlocale (LC_ALL, ""); | ||||
| 	/*qse_setdflcmgrbyid (QSE_CMGR_SLMB);*/ | ||||
| #endif | ||||
|  | ||||
| 	qse_open_stdsios (); | ||||
|  | ||||
| 	QSE::App::setSignalHandler (SIGINT, handle_sigint); | ||||
| 	test1(); | ||||
| 	QSE::App::unsetSignalHandler (SIGINT); | ||||
|  | ||||
| 	qse_close_stdsios (); | ||||
|  | ||||
| 	return 0; | ||||
| } | ||||
							
								
								
									
										143
									
								
								samples/si/thr01.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										143
									
								
								samples/si/thr01.c
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,143 @@ | ||||
| #include <qse/si/thr.h> | ||||
| #include <qse/si/sio.h> | ||||
| #include <qse/si/os.h> | ||||
| #include <qse/cmn/mem.h> | ||||
| #include <qse/cmn/str.h> | ||||
|  | ||||
| #if defined(_WIN32) | ||||
| #	include <windows.h> | ||||
| #endif | ||||
|  | ||||
| #include <locale.h> | ||||
| #include <signal.h> | ||||
| #include <string.h> | ||||
|  | ||||
| static int g_stopreq = 0; | ||||
| static qse_ntime_t sleep_interval = { 1, 0 }; | ||||
|  | ||||
| struct thr_xtn_t | ||||
| { | ||||
| 	int stopreq; | ||||
| }; | ||||
| typedef struct thr_xtn_t thr_xtn_t; | ||||
|  | ||||
|  | ||||
|  | ||||
| static int thr_func (qse_thr_t* thr, void* ctx) | ||||
| { | ||||
| 	int i = 0; | ||||
| 	thr_xtn_t* xtn = qse_thr_getxtn(thr); | ||||
|  | ||||
|  | ||||
| 	while (!xtn->stopreq) | ||||
| 	{ | ||||
| 		qse_printf (QSE_T("%d\n"), i); | ||||
| 		i++; | ||||
| 		qse_sleep (&sleep_interval); | ||||
| 	} | ||||
|  | ||||
| 	return i; | ||||
| } | ||||
|  | ||||
| static int test1 (void) | ||||
| { | ||||
| 	qse_thr_t* thr; | ||||
|  | ||||
| 	thr = qse_thr_open (QSE_MMGR_GETDFL(), QSE_SIZEOF(thr_xtn_t)); | ||||
| 	if (!thr) | ||||
| 	{ | ||||
| 		qse_printf (QSE_T("cannot open thread\n")); | ||||
| 		return -1; | ||||
| 	} | ||||
|  | ||||
| 	qse_thr_setstacksize (thr, 64000); | ||||
|  | ||||
| 	if (qse_thr_start(thr, thr_func, QSE_NULL, QSE_THR_SIGNALS_BLOCKED) <= -1) | ||||
| 	{ | ||||
| 		qse_printf (QSE_T("cannot start thread\n")); | ||||
| 		qse_thr_close (thr); | ||||
| 		return -1; | ||||
| 	} | ||||
|  | ||||
| 	while (!g_stopreq) | ||||
| 	{ | ||||
| 		if (qse_thr_getstate(thr) == QSE_THR_TERMINATED) break; | ||||
| 		qse_sleep (&sleep_interval); | ||||
| 	} | ||||
|  | ||||
| 	if (g_stopreq)  | ||||
| 	{ | ||||
| 		/*qse_thr_stop (thr);*/ | ||||
| 		thr_xtn_t* xtn = qse_thr_getxtn(thr); | ||||
| 		xtn->stopreq = 1; | ||||
| 	} | ||||
|  | ||||
| 	qse_thr_join (thr); | ||||
|  | ||||
| 	qse_printf (QSE_T("thread ended with retcode %d\n"), qse_thr_getretcode(thr)); | ||||
| 	qse_thr_close (thr); | ||||
|  | ||||
| 	return 0; | ||||
| } | ||||
|  | ||||
| static void handle_sigint (int sig, siginfo_t* siginfo, void* ctx) | ||||
| { | ||||
| 	g_stopreq = 1; | ||||
| } | ||||
|  | ||||
| static void set_signal (int sig, void(*handler)(int, siginfo_t*, void*)) | ||||
| { | ||||
| 	struct sigaction sa; | ||||
|   | ||||
| 	memset (&sa, 0, sizeof(sa)); | ||||
| 	/*sa.sa_handler = handler;*/ | ||||
| 	sa.sa_flags = SA_SIGINFO; | ||||
| 	sa.sa_sigaction = handler; | ||||
| 	sigemptyset (&sa.sa_mask); | ||||
|   | ||||
| 	sigaction (sig, &sa, NULL); | ||||
| } | ||||
|   | ||||
| static void set_signal_to_default (int sig) | ||||
| { | ||||
| 	struct sigaction sa; | ||||
|   | ||||
| 	memset (&sa, 0, sizeof(sa)); | ||||
| 	sa.sa_handler = SIG_DFL; | ||||
| 	sa.sa_flags = 0; | ||||
| 	sigemptyset (&sa.sa_mask); | ||||
|   | ||||
| 	sigaction (sig, &sa, NULL); | ||||
| } | ||||
|   | ||||
| int main () | ||||
| { | ||||
| #if defined(_WIN32) | ||||
|  	char locale[100]; | ||||
| 	UINT codepage = GetConsoleOutputCP(); | ||||
| 	if (codepage == CP_UTF8) | ||||
| 	{ | ||||
| 		/*SetConsoleOutputCP (CP_UTF8);*/ | ||||
| 		qse_setdflcmgrbyid (QSE_CMGR_UTF8); | ||||
| 	} | ||||
| 	else | ||||
| 	{ | ||||
| 		qse_mbsxfmt (locale, QSE_COUNTOF(locale), ".%u", (unsigned int)codepage); | ||||
| 		setlocale (LC_ALL, locale); | ||||
| 		/*qse_setdflcmgrbyid (QSE_CMGR_SLMB);*/ | ||||
| 	} | ||||
| #else | ||||
| 	setlocale (LC_ALL, ""); | ||||
| 	/*qse_setdflcmgrbyid (QSE_CMGR_SLMB);*/ | ||||
| #endif | ||||
|  | ||||
| 	set_signal (SIGINT, handle_sigint); | ||||
|  | ||||
| 	qse_open_stdsios (); | ||||
| 	test1(); | ||||
| 	qse_close_stdsios (); | ||||
|  | ||||
| 	set_signal_to_default (SIGINT); | ||||
|  | ||||
| 	return 0; | ||||
| } | ||||
							
								
								
									
										301
									
								
								samples/si/thr02.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										301
									
								
								samples/si/thr02.cpp
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,301 @@ | ||||
| #include <qse/si/Thread.hpp> | ||||
| #include <qse/si/mtx.h> | ||||
| #include <qse/si/sio.h> | ||||
| #include <qse/si/os.h> | ||||
| #include <qse/cmn/mem.h> | ||||
| #include <qse/cmn/str.h> | ||||
| #include <qse/cmn/HeapMmgr.hpp> | ||||
|  | ||||
| #include <locale.h> | ||||
| #if defined(_WIN32) | ||||
| #	include <windows.h> | ||||
| #endif | ||||
|  | ||||
| #include <signal.h> | ||||
| #include <string.h> | ||||
|  | ||||
| static int g_stopreq = 0; | ||||
| static qse_ntime_t sleep_interval = { 1, 0 }; | ||||
|  | ||||
|  | ||||
| QSE::HeapMmgr g_heap_mmgr (30000, QSE::Mmgr::getDFL()); | ||||
|  | ||||
| class MyThread: public QSE::Thread | ||||
| { | ||||
| public: | ||||
| 	MyThread(): stopreq(0) {} | ||||
|  | ||||
| 	int main () | ||||
| 	{ | ||||
| 		int i = 0; | ||||
|  | ||||
| 		while (!this->stopreq) | ||||
| 		{ | ||||
| 			qse_printf (QSE_T("m %p -> %d\n"), this, i); | ||||
| 			i++; | ||||
| 			qse_sleep (&sleep_interval); | ||||
| 		} | ||||
|  | ||||
| 		return i; | ||||
| 	} | ||||
|  | ||||
| 	int stopreq; | ||||
| }; | ||||
|  | ||||
| class Functor | ||||
| { | ||||
| public: | ||||
| 	int operator() (QSE::Thread* thr) | ||||
| 	{ | ||||
| 		int i = 0; | ||||
| 		int* stopreqptr = (int*)thr->getContext(); | ||||
|  | ||||
| 		while (!*stopreqptr) | ||||
| 		{ | ||||
| 			qse_printf (QSE_T("fc %p -> %d\n"), this, i); | ||||
| 			i++; | ||||
| 			qse_sleep (&sleep_interval); | ||||
| 		} | ||||
|  | ||||
| 		return i; | ||||
| 	} | ||||
| }; | ||||
|  | ||||
| class FunctorWithI: public Functor | ||||
| { | ||||
| public: | ||||
| 	FunctorWithI (int* x)  {} | ||||
| }; | ||||
|  | ||||
| static int func_ptr (QSE::Thread* thr) | ||||
| { | ||||
| 	int i = 0; | ||||
| 	int* stopreqptr = (int*)thr->getContext(); | ||||
|  | ||||
| 	while (!*stopreqptr) | ||||
| 	{ | ||||
| 		qse_printf (QSE_T("fp %p -> %d\n"), thr, i); | ||||
| 		i++; | ||||
| 		qse_sleep (&sleep_interval); | ||||
| 	} | ||||
|  | ||||
| 	return i; | ||||
| } | ||||
|  | ||||
| static int test1 (void) | ||||
| { | ||||
| 	int localstopreq = 0; | ||||
|  | ||||
| #if defined(QSE_LANG_CPP11) | ||||
| 	auto lambda = [](QSE::Thread* thr)->int  | ||||
| 	{  | ||||
| 		int i = 0; | ||||
| 		int* stopreqptr = (int*)thr->getContext(); | ||||
|  | ||||
| 		while (!*stopreqptr) | ||||
| 		{ | ||||
| 			qse_printf (QSE_T("l %p -> %d\n"), thr, i); | ||||
| 			i++; | ||||
| 			qse_sleep (&sleep_interval); | ||||
| 		} | ||||
|  | ||||
| 		return i; | ||||
| 	}; | ||||
|  | ||||
| 	auto lambda_with_capture = [&localstopreq](QSE::Thread* thr)->int | ||||
| 	{  | ||||
| 		int i = 0; | ||||
|  | ||||
| 		while (!localstopreq) | ||||
| 		{ | ||||
| 			qse_printf (QSE_T("lc %p -> %d\n"), thr, i); | ||||
| 			i++; | ||||
| 			qse_sleep (&sleep_interval); | ||||
| 		} | ||||
|  | ||||
| 		return i; | ||||
| 	}; | ||||
| #endif | ||||
|  | ||||
| 	MyThread thr1; | ||||
| 	thr1.setStackSize (64000); | ||||
| 	if (thr1.start(QSE::Thread::SIGNALS_BLOCKED) <= -1) | ||||
| 	{ | ||||
| 		qse_printf (QSE_T("cannot start thread1\n")); | ||||
| 		return -1; | ||||
| 	} | ||||
|  | ||||
| 	QSE::ThreadR thr2 (&g_heap_mmgr); | ||||
| 	thr2.setStackSize (64000); | ||||
| 	thr2.setContext (&localstopreq); | ||||
| #if defined(QSE_LANG_CPP11) | ||||
| 	// the lambda expression with no capture can be passed as a function pointer | ||||
| 	// as long as the signature matches QSE::Thread::ThreadRoutine. | ||||
| 	if (thr2.start(lambda, QSE::Thread::SIGNALS_BLOCKED) <= -1) | ||||
| #else | ||||
| 	if (thr2.start(func_ptr, QSE::Thread::SIGNALS_BLOCKED) <= -1) | ||||
| #endif | ||||
| 	{ | ||||
| 		qse_printf (QSE_T("cannot start thread2\n")); | ||||
| 		return -1; | ||||
| 	} | ||||
|  | ||||
| #if defined(QSE_LANG_CPP11) | ||||
| 	QSE::ThreadF<decltype(lambda)> thr3 (lambda); | ||||
| 	thr3.setStackSize (64000); | ||||
| 	thr3.setContext (&localstopreq); | ||||
| 	if (thr3.start(QSE::Thread::SIGNALS_BLOCKED) <= -1) | ||||
| 	{ | ||||
| 		qse_printf (QSE_T("cannot start thread3\n")); | ||||
| 		return -1; | ||||
| 	} | ||||
|  | ||||
| 	// turn a lambda with capture to a thread | ||||
| 	QSE::ThreadF<decltype(lambda_with_capture)> thr4 (lambda_with_capture); | ||||
| 	thr4.setStackSize (64000); | ||||
| 	if (thr4.start(QSE::Thread::SIGNALS_BLOCKED) <= -1) | ||||
| 	{ | ||||
| 		qse_printf (QSE_T("cannot start thread4\n")); | ||||
| 		return -1; | ||||
| 	} | ||||
|  | ||||
| 	QSE::ThreadL<int(QSE::Thread*)> thr5; | ||||
| 	thr5.setStackSize (64000); | ||||
| 	if (thr5.start( | ||||
| 		([&localstopreq, &thr5](QSE::Thread* thr) {  | ||||
| 			int i = 0; | ||||
|  | ||||
| 			while (!localstopreq) | ||||
| 			{ | ||||
| 				qse_printf (QSE_T("tl %p -> %d\n"), thr, i); | ||||
| 				i++; | ||||
| 				qse_sleep (&sleep_interval); | ||||
| 			} | ||||
|  | ||||
| 			return i; | ||||
| 		}),  | ||||
| 		QSE::Thread::SIGNALS_BLOCKED) <= -1) | ||||
| 	{ | ||||
| 		qse_printf (QSE_T("cannot start thread5\n")); | ||||
| 		return -1; | ||||
| 	} | ||||
| #endif | ||||
|  | ||||
| 	// turn a functor to a thread | ||||
| 	QSE::ThreadF<Functor> thr6; | ||||
| 	thr6.setStackSize (64000); | ||||
| 	thr6.setContext (&localstopreq); | ||||
| 	if (thr6.start(QSE::Thread::SIGNALS_BLOCKED) <= -1) | ||||
| 	{ | ||||
| 		qse_printf (QSE_T("cannot start thread6\n")); | ||||
| 		return -1; | ||||
| 	} | ||||
|  | ||||
| 	{ | ||||
| 		int t = 20; | ||||
| 		QSE::ThreadFD<FunctorWithI, int*> thr7 (&t); | ||||
| 		// just keep this here to see if QSE::ThreadFD<> can be instantiated syntatically | ||||
| 	} | ||||
|  | ||||
| 	while (!g_stopreq) | ||||
| 	{ | ||||
| 		if (thr1.getState() == QSE::Thread::TERMINATED &&  | ||||
| 		    thr2.getState() == QSE::Thread::TERMINATED && | ||||
| #if defined(QSE_LANG_CPP11) | ||||
| 		    thr3.getState() == QSE::Thread::TERMINATED && | ||||
| 		    thr4.getState() == QSE::Thread::TERMINATED && | ||||
| 		    thr5.getState() == QSE::Thread::TERMINATED && | ||||
| #endif | ||||
| 		    thr6.getState() == QSE::Thread::TERMINATED) break; | ||||
| 		qse_sleep (&sleep_interval); | ||||
| 	} | ||||
|  | ||||
| 	if (g_stopreq)  | ||||
| 	{ | ||||
| 		localstopreq = 1; | ||||
| 		thr1.stopreq = 1; | ||||
| 	} | ||||
|  | ||||
| 	thr1.join (); | ||||
| 	thr2.join (); | ||||
| #if defined(QSE_LANG_CPP11) | ||||
| 	thr3.join (); | ||||
| 	thr4.join (); | ||||
| 	thr5.join (); | ||||
| #endif | ||||
| 	thr6.join (); | ||||
|  | ||||
| 	qse_printf (QSE_T("thread1 ended with retcode %d\n"), thr1.getReturnCode()); | ||||
| 	qse_printf (QSE_T("thread2 ended with retcode %d\n"), thr2.getReturnCode()); | ||||
| #if defined(QSE_LANG_CPP11) | ||||
| 	qse_printf (QSE_T("thread3 ended with retcode %d\n"), thr3.getReturnCode()); | ||||
| 	qse_printf (QSE_T("thread4 ended with retcode %d\n"), thr4.getReturnCode()); | ||||
| 	qse_printf (QSE_T("thread5 ended with retcode %d\n"), thr5.getReturnCode()); | ||||
| #endif | ||||
| 	qse_printf (QSE_T("thread6 ended with retcode %d\n"), thr6.getReturnCode()); | ||||
|  | ||||
| 	return 0; | ||||
| } | ||||
|  | ||||
|  | ||||
| static void handle_sigint (int sig, siginfo_t* siginfo, void* ctx) | ||||
| { | ||||
| 	g_stopreq = 1; | ||||
| } | ||||
|  | ||||
| static void set_signal (int sig, void(*handler)(int, siginfo_t*, void*)) | ||||
| { | ||||
| 	struct sigaction sa; | ||||
|   | ||||
| 	memset (&sa, 0, sizeof(sa)); | ||||
| 	/*sa.sa_handler = handler;*/ | ||||
| 	sa.sa_flags = SA_SIGINFO; | ||||
| 	sa.sa_sigaction = handler; | ||||
| 	sigemptyset (&sa.sa_mask); | ||||
|   | ||||
| 	sigaction (sig, &sa, NULL); | ||||
| } | ||||
|   | ||||
| static void set_signal_to_default (int sig) | ||||
| { | ||||
| 	struct sigaction sa; | ||||
|   | ||||
| 	memset (&sa, 0, sizeof(sa)); | ||||
| 	sa.sa_handler = SIG_DFL; | ||||
| 	sa.sa_flags = 0; | ||||
| 	sigemptyset (&sa.sa_mask); | ||||
|   | ||||
| 	sigaction (sig, &sa, NULL); | ||||
| } | ||||
|   | ||||
| int main () | ||||
| { | ||||
| #if defined(_WIN32) | ||||
|  	char locale[100]; | ||||
| 	UINT codepage = GetConsoleOutputCP(); | ||||
| 	if (codepage == CP_UTF8) | ||||
| 	{ | ||||
| 		/*SetConsoleOutputCP (CP_UTF8);*/ | ||||
| 		qse_setdflcmgrbyid (QSE_CMGR_UTF8); | ||||
| 	} | ||||
| 	else | ||||
| 	{ | ||||
| 		qse_mbsxfmt (locale, QSE_COUNTOF(locale), ".%u", (unsigned int)codepage); | ||||
| 		setlocale (LC_ALL, locale); | ||||
| 		/*qse_setdflcmgrbyid (QSE_CMGR_SLMB);*/ | ||||
| 	} | ||||
| #else | ||||
| 	setlocale (LC_ALL, ""); | ||||
| 	/*qse_setdflcmgrbyid (QSE_CMGR_SLMB);*/ | ||||
| #endif | ||||
|  | ||||
| 	set_signal (SIGINT, handle_sigint); | ||||
|  | ||||
| 	qse_open_stdsios (); | ||||
| 	test1(); | ||||
| 	qse_close_stdsios (); | ||||
|  | ||||
| 	set_signal_to_default (SIGINT); | ||||
|  | ||||
| 	return 0; | ||||
| } | ||||
							
								
								
									
										184
									
								
								samples/si/thr03.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										184
									
								
								samples/si/thr03.cpp
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,184 @@ | ||||
| #include <qse/si/Thread.hpp> | ||||
| #include <qse/si/Condition.hpp> | ||||
| #include <qse/si/mtx.h> | ||||
| #include <qse/si/sio.h> | ||||
| #include <qse/si/os.h> | ||||
| #include <qse/cmn/mem.h> | ||||
| #include <qse/cmn/str.h> | ||||
| #include <qse/cmn/HeapMmgr.hpp> | ||||
|  | ||||
|  | ||||
| #include <locale.h> | ||||
| #if defined(_WIN32) | ||||
| #	include <windows.h> | ||||
| #endif | ||||
|  | ||||
| #include <signal.h> | ||||
| #include <string.h> | ||||
|  | ||||
| static int g_stopreq = 0; | ||||
| static qse_ntime_t sleep_interval = { 1, 0 }; | ||||
|  | ||||
| QSE::HeapMmgr g_heap_mmgr (30000, QSE::Mmgr::getDFL()); | ||||
|  | ||||
| struct rq_data_t | ||||
| { | ||||
| 	rq_data_t(): stop(0) {} | ||||
| 	int stop; | ||||
| 	QSE::Mutex mtx; | ||||
| 	QSE::Condition cnd; | ||||
| }; | ||||
|  | ||||
| class Waiter | ||||
| { | ||||
| public: | ||||
| 	int operator() (QSE::Thread* thr) | ||||
| 	{ | ||||
| 		int i = 0; | ||||
| 		rq_data_t* rqdata = (rq_data_t*)thr->getContext(); | ||||
|  | ||||
| 		while (1) | ||||
| 		{ | ||||
| 		#if 0 | ||||
| 			rqdata->mtx.lock (); | ||||
| 		#else | ||||
| 			while (!rqdata->mtx.trylock()) | ||||
| 			{ | ||||
| 				qse_printf (QSE_T("[%p] -> retrying to lock\n"), this, i); | ||||
| 			} | ||||
| 		#endif | ||||
|  | ||||
| 			if (rqdata->stop) | ||||
| 			{ | ||||
| 				rqdata->mtx.unlock (); | ||||
| 				break; | ||||
| 			} | ||||
| 			rqdata->cnd.wait(rqdata->mtx); | ||||
| 			rqdata->mtx.unlock (); | ||||
|  | ||||
| 			qse_printf (QSE_T("[%p] -> loop %d\n"), this, i); | ||||
| 			i++; | ||||
| 		} | ||||
|  | ||||
| 		qse_printf (QSE_T("[%p] -> exiting\n"), this); | ||||
| 		return i; | ||||
| 	} | ||||
| }; | ||||
|  | ||||
| static int test1 (void) | ||||
| { | ||||
| 	rq_data_t rqdata; | ||||
| 	QSE::ThreadF<Waiter> thr[3]; | ||||
|  | ||||
| 	for (int i = 0; i < 3; i++) | ||||
| 	{ | ||||
| 		thr[i].setStackSize (64000); | ||||
| 		thr[i].setContext (&rqdata); | ||||
| 		if (thr[i].start(QSE::Thread::SIGNALS_BLOCKED) <= -1) | ||||
| 		{ | ||||
| 			qse_printf (QSE_T("cannot start thread%d\n"), i); | ||||
| 			return -1; | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	while (1) | ||||
| 	{ | ||||
| 		if (g_stopreq)  | ||||
| 		{ | ||||
| 			qse_printf (QSE_T("broadcasting stop ---> 1\n")); | ||||
|  | ||||
| 			rqdata.mtx.lock (); | ||||
| 			rqdata.stop = 1; | ||||
| 			rqdata.mtx.unlock (); | ||||
| 			rqdata.cnd.broadcast (); | ||||
| 		} | ||||
|  | ||||
| 		int nterm = 0; | ||||
| 		for (int i = 0; i < 3; i++) | ||||
| 		{ | ||||
| 			if (thr[i].getState() == QSE::Thread::TERMINATED) nterm++; | ||||
| 		} | ||||
| 		if (nterm == 3) break; | ||||
|  | ||||
| 		qse_printf (QSE_T("signalling ....(nterm = %d)\n"), nterm); | ||||
|  | ||||
| 		rqdata.cnd.signal (); | ||||
| 		qse_sleep (&sleep_interval); | ||||
| 	} | ||||
|  | ||||
|  | ||||
| 	for (int i = 0; i < 3; i++) | ||||
| 	{ | ||||
| 		thr[i].join(); | ||||
| 	} | ||||
|  | ||||
| 	for (int i = 0; i < 3; i++) | ||||
| 	{ | ||||
| 		qse_printf (QSE_T("thread%d ended with retcode %d\n"), i, thr[i].getReturnCode()); | ||||
| 	} | ||||
|  | ||||
| 	return 0; | ||||
| } | ||||
|  | ||||
|  | ||||
| static void handle_sigint (int sig, siginfo_t* siginfo, void* ctx) | ||||
| { | ||||
| 	g_stopreq = 1; | ||||
| } | ||||
|  | ||||
| static void set_signal (int sig, void(*handler)(int, siginfo_t*, void*)) | ||||
| { | ||||
| 	struct sigaction sa; | ||||
|   | ||||
| 	memset (&sa, 0, sizeof(sa)); | ||||
| 	/*sa.sa_handler = handler;*/ | ||||
| 	sa.sa_flags = SA_SIGINFO; | ||||
| 	sa.sa_sigaction = handler; | ||||
| 	sigemptyset (&sa.sa_mask); | ||||
|   | ||||
| 	sigaction (sig, &sa, NULL); | ||||
| } | ||||
|   | ||||
| static void set_signal_to_default (int sig) | ||||
| { | ||||
| 	struct sigaction sa; | ||||
|   | ||||
| 	memset (&sa, 0, sizeof(sa)); | ||||
| 	sa.sa_handler = SIG_DFL; | ||||
| 	sa.sa_flags = 0; | ||||
| 	sigemptyset (&sa.sa_mask); | ||||
|   | ||||
| 	sigaction (sig, &sa, NULL); | ||||
| } | ||||
|   | ||||
| int main () | ||||
| { | ||||
| #if defined(_WIN32) | ||||
|  	char locale[100]; | ||||
| 	UINT codepage = GetConsoleOutputCP(); | ||||
| 	if (codepage == CP_UTF8) | ||||
| 	{ | ||||
| 		/*SetConsoleOutputCP (CP_UTF8);*/ | ||||
| 		qse_setdflcmgrbyid (QSE_CMGR_UTF8); | ||||
| 	} | ||||
| 	else | ||||
| 	{ | ||||
| 		qse_mbsxfmt (locale, QSE_COUNTOF(locale), ".%u", (unsigned int)codepage); | ||||
| 		setlocale (LC_ALL, locale); | ||||
| 		/*qse_setdflcmgrbyid (QSE_CMGR_SLMB);*/ | ||||
| 	} | ||||
| #else | ||||
| 	setlocale (LC_ALL, ""); | ||||
| 	/*qse_setdflcmgrbyid (QSE_CMGR_SLMB);*/ | ||||
| #endif | ||||
|  | ||||
| 	set_signal (SIGINT, handle_sigint); | ||||
|  | ||||
| 	qse_open_stdsios (); | ||||
| 	test1(); | ||||
| 	qse_close_stdsios (); | ||||
|  | ||||
| 	set_signal_to_default (SIGINT); | ||||
|  | ||||
| 	return 0; | ||||
| } | ||||
		Reference in New Issue
	
	Block a user