Reorganized the directory structure

This commit is contained in:
2022-09-25 09:23:29 +09:00
parent 1bac167e2d
commit 84d1c4c55f
864 changed files with 11 additions and 12 deletions

103
samples/cmn/Makefile.am Normal file
View File

@ -0,0 +1,103 @@
AUTOMAKE_OPTIONS = nostdinc
AM_CPPFLAGS = \
-I$(top_builddir)/include \
-I$(top_srcdir)/include \
-I$(includedir)
bin_PROGRAMS = \
arr01 \
chr01 \
dll \
env01 \
fma \
fmt01 \
fmt02 \
htb01 \
htl01 \
ipad01 \
main01 \
main02 \
mbwc01 \
mbwc02 \
oht \
path01 \
pma \
rex01 \
rbt01 \
sll \
slmb01 \
str01 \
str02 \
time \
tre01 \
uri01 \
xma
AM_LDFLAGS = -L../../lib/si -L../../lib/cmn
LDADD = -lqsesi -lqsecmn $(LIBM)
if WIN32
LDADD += $(UNICOWS_LIBS)
endif
chr01_SOURCES = chr01.c
env01_SOURCES = env01.c
dll_SOURCES = dll.c
fma_SOURCES = fma.c
fmt01_SOURCES = fmt01.c
fmt02_SOURCES = fmt02.c
htb01_SOURCES = htb01.c
ipad01_SOURCES = ipad01.c
arr01_SOURCES = arr01.c
main01_SOURCES = main01.c
main02_SOURCES = main02.c
mbwc01_SOURCES = mbwc01.c
mbwc02_SOURCES = mbwc02.c
oht_SOURCES = oht.c
path01_SOURCES = path01.c
pma_SOURCES = pma.c
rex01_SOURCES = rex01.c
rbt01_SOURCES = rbt01.c
sll_SOURCES = sll.c
slmb01_SOURCES = slmb01.c
str01_SOURCES = str01.c
time_SOURCES = time.c
tre01_SOURCES = tre01.c
xma_SOURCES = xma.c
if ENABLE_CXX
#bin_PROGRAMS += rex02
#rex02_SOURCES = rex02.cpp
#rex02_CXXFLAGS = -I/usr/lib/wx/include/gtk2-unicode-release-2.8 -I/usr/include/wx-2.8 -D_FILE_OFFSET_BITS=64 -D_LARGE_FILES -D__WXGTK__ -pthread
#rex02_LDFLAGS = -pthread -Wl,-Bsymbolic-functions -lwx_gtk2ud_richtext-2.8 -lwx_gtk2ud_aui-2.8 -lwx_gtk2ud_xrc-2.8 -lwx_gtk2ud_qa-2.8 -lwx_gtk2ud_html-2.8 -lwx_gtk2ud_adv-2.8 -lwx_gtk2ud_core-2.8 -lwx_baseud_xml-2.8 -lwx_baseud_net-2.8 -lwx_baseud-2.8
bin_PROGRAMS += arr02 arr03 bh01 bh02 bh03 hl01 htb02 rbt02 rbt03 sp01 sp02 str02
arr02_SOURCES = arr02.cpp
arr03_SOURCES = arr03.cpp
bh01_SOURCES = bh01.cpp
bh02_SOURCES = bh02.cpp
bh03_SOURCES = bh03.cpp
hl01_SOURCES = hl01.cpp
htb02_SOURCES = htb02.cpp
rbt02_SOURCES = rbt02.cpp #RedBlackTree
rbt03_SOURCES = rbt03.cpp #RedBlackTable
sp01_SOURCES = sp01.cpp # ScopedPtr
sp02_SOURCES = sp02.cpp # SharedPtr
str02_SOURCES = str02.cpp # SharedPtr
endif

1240
samples/cmn/Makefile.in Normal file

File diff suppressed because it is too large Load Diff

597
samples/cmn/arr01.c Normal file
View File

@ -0,0 +1,597 @@
#include <qse/cmn/mem.h>
#include <qse/cmn/str.h>
#include <qse/cmn/arr.h>
#include <qse/si/sio.h>
#include <stdlib.h>
#define R(f) \
do { \
qse_printf (QSE_T("== %s ==\n"), QSE_T(#f)); \
if (f() == -1) return -1; \
} while (0)
void keeper1 (qse_arr_t* arr, void* dptr, qse_size_t dlen)
{
qse_printf (QSE_T("[%.*s] has been kept\n"), (int)dlen, dptr);
}
qse_arr_walk_t walker1 (qse_arr_t* arr, qse_size_t index, void* arg)
{
qse_printf (QSE_T("%d => [%.*s]\n"),
(int)index, (int)QSE_ARR_DLEN(arr,index), QSE_ARR_DPTR(arr,index));
return QSE_ARR_WALK_FORWARD;
}
qse_arr_walk_t rwalker1 (qse_arr_t* arr, qse_size_t index, void* arg)
{
qse_printf (QSE_T("%d => [%.*s]\n"),
(int)index, (int)QSE_ARR_DLEN(arr,index), QSE_ARR_DPTR(arr,index));
return QSE_ARR_WALK_BACKWARD;
}
qse_arr_walk_t walker3 (qse_arr_t* arr, qse_size_t index, void* arg)
{
qse_printf (QSE_T("%d => [%d]\n"),
(int)index, *(int*)QSE_ARR_DPTR(arr,index));
return QSE_ARR_WALK_FORWARD;
}
static int test1 ()
{
qse_arr_t* s1;
qse_char_t* x[] =
{
QSE_T("this is so good"),
QSE_T("what the fuck"),
QSE_T("do you like it?"),
QSE_T("oopsy!")
};
int i;
s1 = qse_arr_open (QSE_MMGR_GETDFL(), 0, 0);
if (s1 == QSE_NULL)
{
qse_printf (QSE_T("cannot open a string\n"));
return -1;
}
qse_arr_setcopier (s1, QSE_ARR_COPIER_INLINE);
qse_arr_setkeeper (s1, keeper1);
qse_arr_setscale (s1, QSE_SIZEOF(qse_char_t));
for (i = 0; i < QSE_COUNTOF(x); i++)
{
if (qse_arr_insert (s1, 0, x[i], qse_strlen(x[i])) == QSE_ARR_NIL)
{
qse_printf (QSE_T("failed to add at 0 => [%.*s]\n"),
(int)qse_strlen(x[i]), x[i]);
}
else
{
qse_printf (QSE_T("add at 0 => [%.*s]\n"),
(int)qse_strlen(x[i]), x[i]);
}
}
if (qse_arr_update (s1, 0, QSE_ARR_DPTR(s1,0), QSE_ARR_DLEN(s1,0)) == QSE_ARR_NIL)
{
qse_printf (QSE_T("failed to update index 0 with [%.*s]\n"), (int)QSE_ARR_DLEN(s1,0), QSE_ARR_DPTR(s1,0));
}
else
{
qse_printf (QSE_T("updated index 0 with [%.*s]\n"), (int)QSE_ARR_DLEN(s1,0), QSE_ARR_DPTR(s1,0));
}
if (qse_arr_update (s1, 0, QSE_ARR_DPTR(s1,1), QSE_ARR_DLEN(s1,1)) == QSE_ARR_NIL)
{
qse_printf (QSE_T("updated index 0 with [%.*s]\n"), (int)QSE_ARR_DLEN(s1,1), QSE_ARR_DPTR(s1,1));
}
else
{
qse_printf (QSE_T("updated index 0 with [%.*s]\n"), (int)QSE_ARR_DLEN(s1,0), QSE_ARR_DPTR(s1,0));
}
for (i = 0; i < QSE_COUNTOF(x); i++)
{
if (qse_arr_insert (s1, 10, x[i], qse_strlen(x[i])) == QSE_ARR_NIL)
{
qse_printf (QSE_T("failed to add at 10 => [%.*s]\n"),
(int)qse_strlen(x[i]), x[i]);
}
else
{
qse_printf (QSE_T("add at 10 => [%.*s]\n"),
(int)qse_strlen(x[i]), x[i]);
}
}
qse_printf (QSE_T("arr size => %lu\n"), QSE_ARR_SIZE(s1));
qse_arr_walk (s1, walker1, QSE_NULL);
qse_printf (QSE_T("arr size => %lu\n"), QSE_ARR_SIZE(s1));
qse_arr_rwalk (s1, rwalker1, QSE_NULL);
qse_arr_setcapa (s1, 3);
qse_arr_close (s1);
return 0;
}
static int test2 ()
{
qse_arr_t* s1;
qse_arr_slot_t* p;
const qse_char_t* x[] =
{
QSE_T("this is so good"),
QSE_T("what the fuck"),
QSE_T("do you like it?"),
QSE_T("oopsy!"),
QSE_T("hello hello!"),
QSE_T("oopsy!")
};
const qse_char_t* y[] =
{
QSE_T("tipsy!"),
QSE_T("oopsy!")
};
int i, j;
s1 = qse_arr_open (QSE_MMGR_GETDFL(), 0, 0);
if (s1 == QSE_NULL)
{
qse_printf (QSE_T("cannot open a string\n"));
return -1;
}
qse_arr_setcopier (s1, QSE_ARR_COPIER_INLINE);
qse_arr_setscale (s1, QSE_SIZEOF(qse_char_t));
for (j = 0; j < 20; j++)
{
qse_size_t index;
for (i = 0; i < QSE_COUNTOF(x); i++)
{
if (qse_arr_insert (s1, (i + 1) * j - 1, x[i], qse_strlen(x[i])) == QSE_ARR_NIL)
{
qse_printf (QSE_T("failed to add at %u => [%.*s]\n"),
(unsigned int)((i + 1) * j - 1),
(int)qse_strlen(x[i]), x[i]);
}
else
{
qse_printf (QSE_T("add at %u => [%.*s]\n"),
(unsigned int)((i + 1) * j - 1),
(int)qse_strlen(x[i]), x[i]);
}
}
for (i = 0; i < QSE_ARR_SIZE(s1); i++)
{
if (QSE_ARR_SLOT(s1,i))
{
qse_printf (QSE_T("[%d] %d => [%.*s]\n"),
j, i, (int)QSE_ARR_DLEN(s1,i), QSE_ARR_DPTR(s1,i));
}
}
for (i = 0; i < QSE_COUNTOF(y); i++)
{
index = qse_arr_search (s1, 0, y[i], qse_strlen(y[i]));
if (index == QSE_ARR_NIL)
{
qse_printf (QSE_T("failed to find [%s]\n"), y[i]);
}
else
{
qse_printf (QSE_T("found [%.*s] at %lu\n"),
(int)QSE_ARR_DLEN(s1,index), QSE_ARR_DPTR(s1,index), (unsigned long)index);
}
index = qse_arr_rsearch (s1, QSE_ARR_SIZE(s1), y[i], qse_strlen(y[i]));
if (index == QSE_ARR_NIL)
{
qse_printf (QSE_T("failed to find [%s]\n"), y[i]);
}
else
{
qse_printf (QSE_T("found [%.*s] at %lu\n"),
(int)QSE_ARR_DLEN(s1,index), QSE_ARR_DPTR(s1,index), (unsigned long)index);
}
}
qse_arr_clear (s1);
qse_printf (QSE_T("~~~~~~~~\n"));
}
qse_arr_close (s1);
return 0;
}
static int test3 ()
{
qse_arr_t* s1;
const qse_char_t* x[] =
{
QSE_T("this is so good"),
QSE_T("what the fuck"),
QSE_T("do you like it?"),
QSE_T("oopsy!")
};
const qse_char_t* y =
QSE_T("We Accept MasterCard, VISA, JCB, Dinner & eCheck");
int i, j;
s1 = qse_arr_open (QSE_MMGR_GETDFL(), 0, 0);
if (s1 == QSE_NULL)
{
qse_printf (QSE_T("cannot open a string\n"));
return -1;
}
qse_arr_setcopier (s1, QSE_ARR_COPIER_INLINE);
qse_arr_setscale (s1, QSE_SIZEOF(qse_char_t));
for (j = 0; j < 20; j++)
{
for (i = 0; i < QSE_COUNTOF(x); i++)
{
if (qse_arr_insert (s1, (i + 1) * j - 1, x[i], qse_strlen(x[i])) == QSE_ARR_NIL)
{
qse_printf (QSE_T("failed to add at %u => [%.*s]\n"),
(unsigned int)((i + 1) * j - 1),
(int)qse_strlen(x[i]), x[i]);
}
else
{
qse_printf (QSE_T("add at %u => [%.*s]\n"),
(unsigned int)((i + 1) * j - 1),
(int)qse_strlen(x[i]), x[i]);
}
}
for (i = 2; i < 3; i++)
{
if (i < QSE_ARR_SIZE(s1))
{
if (QSE_ARR_SLOT(s1,i))
{
qse_printf (QSE_T("deleted at %d => [%.*s]\n"),
i, (int)QSE_ARR_DLEN(s1,i), QSE_ARR_DPTR(s1,i));
}
qse_arr_delete (s1, i, 1);
}
if (i < QSE_ARR_SIZE(s1))
{
if (qse_arr_update (s1, i, y, qse_strlen(y)) == QSE_ARR_NIL)
{
qse_printf (QSE_T("failed to update at %d => [%.*s]\n"),
i, (int)qse_strlen(y), y);
}
else
{
qse_printf (QSE_T("updated at %d => [%.*s]\n"),
i, (int)QSE_ARR_DLEN(s1,i), QSE_ARR_DPTR(s1,i));
}
}
}
qse_printf (QSE_T("array size => %lu\n"), (unsigned long)QSE_ARR_SIZE(s1));
for (i = 0; i < QSE_ARR_SIZE(s1); i++)
{
if (QSE_ARR_SLOT(s1,i))
{
qse_printf (QSE_T("[%d] %d => [%.*s]\n"),
j, i, (int)QSE_ARR_DLEN(s1,i), QSE_ARR_DPTR(s1,i));
}
}
{
qse_size_t count = qse_arr_uplete (s1, 3, 20);
qse_printf (QSE_T("upleted %lu items from index 3\n"), (unsigned long)count);
}
qse_printf (QSE_T("array size => %lu\n"), (unsigned long)QSE_ARR_SIZE(s1));
for (i = 0; i < QSE_ARR_SIZE(s1); i++)
{
if (QSE_ARR_SLOT(s1,i))
{
qse_printf (QSE_T("[%d] %d => [%.*s]\n"),
j, i, (int)QSE_ARR_DLEN(s1,i), QSE_ARR_DPTR(s1,i));
}
}
qse_arr_clear (s1);
qse_printf (QSE_T("~~~~~~~~\n"));
}
qse_arr_close (s1);
return 0;
}
qse_size_t sizer1 (qse_arr_t* arr, qse_size_t hint)
{
return 2;
}
static int test4 ()
{
int i;
qse_arr_t* s1;
const qse_char_t* x[] =
{
QSE_T("this is so good"),
QSE_T("what the fuck"),
QSE_T("do you like it?"),
QSE_T("oopsy!")
};
s1 = qse_arr_open (QSE_MMGR_GETDFL(), 0, 3);
if (s1 == QSE_NULL)
{
qse_printf (QSE_T("cannot open an array\n"));
return -1;
}
qse_arr_setcopier (s1, QSE_ARR_COPIER_INLINE);
qse_arr_setsizer (s1, sizer1);
qse_arr_setscale (s1, QSE_SIZEOF(qse_char_t));
for (i = 0; i < QSE_COUNTOF(x); i++)
{
if (qse_arr_insert (s1, 0, x[i], qse_strlen(x[i])) == QSE_ARR_NIL)
{
qse_printf (QSE_T("failed to add at 0 => [%.*s]\n"),
(int)qse_strlen(x[i]), x[i]);
}
else
{
qse_printf (QSE_T("add at 0 => [%.*s]\n"),
(int)qse_strlen(x[i]), x[i]);
}
}
qse_printf (QSE_T("arr size => %lu\n"), QSE_ARR_SIZE(s1));
qse_arr_walk (s1, walker1, QSE_NULL);
qse_printf (QSE_T("arr size => %lu\n"), QSE_ARR_SIZE(s1));
qse_arr_rwalk (s1, rwalker1, QSE_NULL);
qse_arr_close (s1);
return 0;
}
qse_arr_comper_t default_comparator;
static int integer_comparator (qse_arr_t* arr,
const void* dptr1, size_t dlen1,
const void* dptr2, size_t dlen2)
{
return (*(int*)dptr1 > *(int*)dptr2)? 1:
(*(int*)dptr1 < *(int*)dptr2)? -1: 0;
}
static int inverse_comparator (qse_arr_t* arr,
const void* dptr1, size_t dlen1,
const void* dptr2, size_t dlen2)
{
return -default_comparator (arr, dptr1, dlen1, dptr2, dlen2);
}
static int test5 ()
{
qse_arr_t* s1;
int i, j, oldv, newv;
s1 = qse_arr_open (QSE_MMGR_GETDFL(), 0, 3);
if (s1 == QSE_NULL)
{
qse_printf (QSE_T("cannot open an array\n"));
return -1;
}
qse_arr_setcopier (s1, QSE_ARR_COPIER_INLINE);
qse_arr_setscale (s1, QSE_SIZEOF(i));
qse_arr_setcomper (s1, integer_comparator);
/* inverse the comparator to implement min-heap */
default_comparator = qse_arr_getcomper (s1);
qse_arr_setcomper (s1, inverse_comparator);
for (i = 0; i < 2500; i++)
{
j = rand () % 1000;
qse_arr_pushheap (s1, &j, 1);
}
qse_printf (QSE_T("arr size => %lu\n"), QSE_ARR_SIZE(s1));
qse_arr_walk (s1, walker3, QSE_NULL);
oldv = 0;
while (QSE_ARR_SIZE(s1) > 10 )
{
newv = *(int*)QSE_ARR_DPTR(s1,0);
qse_printf (QSE_T("top => %d prevtop => %d\n"), newv, oldv);
QSE_ASSERT (newv >= oldv);
qse_arr_popheap (s1);
oldv = newv;
}
for (i = 0; i < 2500; i++)
{
j = rand () % 1000;
qse_arr_pushheap (s1, &j, 1);
}
qse_printf (QSE_T("arr size => %lu\n"), QSE_ARR_SIZE(s1));
qse_arr_walk (s1, walker3, QSE_NULL);
oldv = 0;
while (QSE_ARR_SIZE(s1) > 0)
{
newv = *(int*)QSE_ARR_DPTR(s1,0);
qse_printf (QSE_T("top => %d prevtop => %d\n"), newv, oldv);
QSE_ASSERT (newv >= oldv);
qse_arr_popheap (s1);
oldv = newv;
}
/* back to max-heap */
qse_arr_setcomper (s1, default_comparator);
for (i = 0; i < 2500; i++)
{
j = rand () % 1000;
qse_arr_pushheap (s1, &j, 1);
}
j = 88888888;
qse_arr_updateheap (s1, QSE_ARR_SIZE(s1) / 2, &j, 1);
j = -123;
qse_arr_updateheap (s1, QSE_ARR_SIZE(s1) / 2, &j, 1);
qse_printf (QSE_T("arr size => %lu\n"), QSE_ARR_SIZE(s1));
qse_arr_walk (s1, walker3, QSE_NULL);
oldv = 99999999;
while (QSE_ARR_SIZE(s1) > 0)
{
newv = *(int*)QSE_ARR_DPTR(s1,0);
qse_printf (QSE_T("top => %d prevtop => %d\n"), newv, oldv);
QSE_ASSERT (newv <= oldv);
qse_arr_popheap (s1);
oldv = newv;
}
qse_arr_close (s1);
return 0;
}
struct test6_data_t
{
int v;
qse_size_t pos;
};
typedef struct test6_data_t test6_data_t;
static int test6_comparator (qse_arr_t* arr,
const void* dptr1, size_t dlen1,
const void* dptr2, size_t dlen2)
{
return ((test6_data_t*)dptr1)->v > ((test6_data_t*)dptr2)->v? 1:
((test6_data_t*)dptr1)->v < ((test6_data_t*)dptr2)->v? -1: 0;
}
qse_arr_walk_t test6_walker (qse_arr_t* arr, qse_size_t index, void* arg)
{
test6_data_t* x;
x = QSE_ARR_DPTR(arr,index);
qse_printf (QSE_T("%d => [%d] pos=%d\n"), (int)index, (int)x->v, (int)x->pos);
QSE_ASSERT (index == x->pos);
return QSE_ARR_WALK_FORWARD;
}
static int test6 ()
{
qse_arr_t* s1;
int i, oldv, newv;
test6_data_t j;
s1 = qse_arr_open (QSE_MMGR_GETDFL(), 0, 3);
if (s1 == QSE_NULL)
{
qse_printf (QSE_T("cannot open an array\n"));
return -1;
}
qse_arr_setcopier (s1, QSE_ARR_COPIER_INLINE);
qse_arr_setscale (s1, QSE_SIZEOF(j));
qse_arr_setcomper (s1, test6_comparator);
qse_arr_setheapposoffset (s1, QSE_OFFSETOF(test6_data_t, pos));
for (i = 0; i < 100; i++)
{
j.v = rand () % 65535;
qse_arr_pushheap (s1, &j, 1);
}
j.v = 88888888;
qse_arr_updateheap (s1, QSE_ARR_SIZE(s1) / 2, &j, 1);
j.v = -123;
qse_arr_updateheap (s1, QSE_ARR_SIZE(s1) / 2, &j, 1);
qse_printf (QSE_T("arr size => %lu\n"), QSE_ARR_SIZE(s1));
qse_arr_walk (s1, test6_walker, QSE_NULL);
oldv = 99999999;
while (QSE_ARR_SIZE(s1) > 50)
{
newv = ((test6_data_t*)QSE_ARR_DPTR(s1,0))->v;
qse_printf (QSE_T("top => %d prevtop => %d\n"), newv, oldv);
QSE_ASSERT (newv <= oldv);
qse_arr_popheap (s1);
oldv = newv;
}
qse_printf (QSE_T("arr size => %lu\n"), QSE_ARR_SIZE(s1));
qse_arr_walk (s1, test6_walker, QSE_NULL);
while (QSE_ARR_SIZE(s1) > 10)
{
newv = ((test6_data_t*)QSE_ARR_DPTR(s1,0))->v;
qse_printf (QSE_T("top => %d prevtop => %d\n"), newv, oldv);
QSE_ASSERT (newv <= oldv);
qse_arr_popheap (s1);
oldv = newv;
}
qse_printf (QSE_T("arr size => %lu\n"), QSE_ARR_SIZE(s1));
qse_arr_walk (s1, test6_walker, QSE_NULL);
while (QSE_ARR_SIZE(s1) > 1)
{
newv = ((test6_data_t*)QSE_ARR_DPTR(s1,0))->v;
qse_printf (QSE_T("top => %d prevtop => %d\n"), newv, oldv);
QSE_ASSERT (newv <= oldv);
qse_arr_popheap (s1);
oldv = newv;
}
qse_printf (QSE_T("arr size => %lu\n"), QSE_ARR_SIZE(s1));
qse_arr_walk (s1, test6_walker, QSE_NULL);
while (QSE_ARR_SIZE(s1) > 0)
{
newv = ((test6_data_t*)QSE_ARR_DPTR(s1,0))->v;
qse_printf (QSE_T("top => %d prevtop => %d\n"), newv, oldv);
QSE_ASSERT (newv <= oldv);
qse_arr_popheap (s1);
oldv = newv;
}
qse_arr_close (s1);
return 0;
}
int main ()
{
qse_open_stdsios ();
R (test1);
R (test2);
R (test3);
R (test4);
R (test5);
R (test6);
qse_close_stdsios ();
return 0;
}

148
samples/cmn/arr02.cpp Normal file
View File

@ -0,0 +1,148 @@
#include <stdio.h>
#include <string.h>
#include <qse/cmn/Array.hpp>
#include <qse/cmn/String.hpp>
/*
typedef QSE::Array<int,int> IntArray;
struct IntClass
{
IntClass (int x = 0): x (x) {}
int x;
};
struct IntClassComparator
{
int operator() (const IntClass& v, int y) const
{
IntTable::DefaultComparator comp;
return comp (v.x, y);
}
};
*/
#if 1
class PosStr: public QSE::MbString
{
public:
PosStr (const char* str = ""): QSE::MbString(str), pos((qse_size_t)-1) {};
PosStr (const PosStr& ps): QSE::MbString (ps), pos(ps.pos)
{
}
~PosStr()
{
}
qse_size_t pos;
};
#else
class PosStr
{
public:
PosStr (const char* str = "")
{
strcpy (buf, str);
}
const char* getBuffer() const { return this->buf; }
const char* data() const { return this->buf; }
char buf[512];
};
#endif
struct cstr_comparator
{
int operator() (const char* v1, const QSE::MbString& v2) const
{
return strcmp (v1, v2.getBuffer());
}
};
typedef QSE::Array<PosStr> StrArray;
int main ()
{
//StrArray h (100);
StrArray h (15);
char buf[20];
for (int i = 0; i < 20; i++)
{
sprintf (buf, "hello %d", i);
PosStr x (buf);
h.insert (0, x);
}
#if 0
printf ("%s\n", h.get(3).getBuffer());
h.remove (3, 5);
printf ("%s\n", h.get(3).getBuffer());
h.remove (3, 5);
printf ("%s\n", h.get(3).getBuffer());
#endif
printf ("--------------------\n");
for (qse_size_t i = 0; i < h.getSize(); i++)
{
printf ("[%s] at [%lu]\n", h[i].getBuffer(), (unsigned long int)h.getIndex(h[i]));
}
printf ("--------------------\n");
StrArray h2 (h);
StrArray h3;
h3 = h2;
h.clear (true);
printf ("--------------------\n");
printf ("%d\n", (int)h.getSize());
printf ("--------------------\n");
for (qse_size_t i = 0; i < h2.getSize(); i++)
{
printf ("[%s] at [%lu]\n", h2[i].getBuffer(), (unsigned long int)h2.getIndex(h2[i]));
}
printf ("--------------------\n");
h3.insert (21, "this is a large string");
printf ("%d %d\n", (int)h2.getSize(), (int)h3.getSize());
printf ("--------------------\n");
h3.insert (21, "mystery!");
for (qse_size_t i = 0; i < h3.getSize(); i++)
{
printf ("[%s] at [%lu]\n", h3[i].getBuffer(), (unsigned long int)h3.getIndex(h3[i]));
}
printf ("--------------------\n");
h3.setCapacity (6);
h3.insert (6, "good?");
h3.rotate (StrArray::ROTATE_RIGHT, h3.getSize() / 2);
printf ("[%s] [%s]\n", h3.getValueAt(5).getBuffer(), h3.getValueAt(6).getBuffer());
printf ("%d\n", (int)h3.getSize());
printf ("--------------------\n");
h3.insert (1, "bad?");
for (qse_size_t i = 0; i < h3.getSize(); i++)
{
printf ("[%s] at [%lu]\n", h3[i].getBuffer(), (unsigned long int)h3.getIndex(h3[i]));
}
printf ("--------------------\n");
QSE::Array<int> a;
a.insert (0, 10);
a.insert (0, 20);
a.insert (0, 30);
const int& t = a[2u];
printf ("%lu\n", (unsigned long int)a.getIndex(t));
return 0;
}

108
samples/cmn/arr03.cpp Normal file
View File

@ -0,0 +1,108 @@
#include <stdio.h>
#include <string.h>
#include <qse/cmn/Array.hpp>
#include <qse/cmn/String.hpp>
class Julia
{
public:
Julia (int q = 0): x(QSE_NULL)
{
this->x = new int (q);
printf ("constructor %d\n", q);
}
Julia (const Julia& q): x(QSE_NULL)
{
this->x = new int (*q.x);
printf ("copy constructor %d\n", *q.x);
}
#if defined(QSE_CPP_ENABLE_CPP11_MOVE)
Julia (Julia&& q)
{
printf ("move constructor %d\n", *q.x);
this->x = q.x;
q.x = QSE_NULL;
}
#endif
~Julia()
{
if (this->x)
{
printf ("deleting %p %d\n", this, *x);
delete this->x;
}
}
Julia& operator= (const Julia& q)
{
if (this != &q)
{
if (this->x) { delete this->x; this->x = QSE_NULL; }
this->x = new int (*q.x);
printf ("operator= %d\n", *q.x);
}
return *this;
}
#if defined(QSE_CPP_ENABLE_CPP11_MOVE)
Julia& operator= (Julia&& q)
{
if (this != &q)
{
if (this->x) { delete this->x; this->x = QSE_NULL; }
printf ("move operator= %d\n", *q.x);
this->x = q.x;
q.x = QSE_NULL;
}
return *this;
}
#endif
int* x;
};
typedef QSE::Array<Julia> JuliaArray;
int main ()
{
JuliaArray a0;
for (int i = 0; i < 256; i++)
{
a0.insert (0, Julia(i));
}
a0.setCapacity (1024);
a0.insert (512, Julia (512));
a0.remove (2, 5);
a0.update (5, Julia(9999));
#if defined(QSE_CPP_ENABLE_CPP11_MOVE)
JuliaArray a1 ((JuliaArray&&)a0);
#else
JuliaArray a1 (a0);
#endif
printf ("OK: %d %d\n", (int)a0.getSize(), (int)a1.getSize());
for (qse_size_t i = 0; i < a1.getSize(); i++)
{
printf ("ITEM: %d => %d\n", (int)i, *a1[i].x);
}
printf ("----------------\n");
a1.rotate (JuliaArray::ROTATE_LEFT, 2);
for (qse_size_t i = 0; i < a1.getSize(); i++)
{
printf ("ITEM: %d => %d\n", (int)i, *a1[i].x);
}
return 0;
}

299
samples/cmn/bh01.cpp Normal file
View File

@ -0,0 +1,299 @@
#include <stdio.h>
#include <string>
#include <qse/cmn/BinaryHeap.hpp>
#include <qse/cmn/LinkedList.hpp>
#include <string.h>
class PosStr: public std::string
{
public:
PosStr (const char* str = ""): std::string(str), heap_pos((qse_size_t)-1) {};
PosStr (const PosStr& ps): std::string (ps), heap_pos(ps.heap_pos)
{
}
~PosStr()
{
}
qse_size_t heap_pos;
};
struct cstr_comparator
{
bool operator() (const char* v1, const PosStr& v2) const
{
//return strcmp (v1, v2.c_str()) > 0;
return strcmp (v1, v2.c_str()) < 0;
}
};
struct str_comparator
{
bool operator() (const PosStr& v1, const PosStr& v2) const
{
//return strcmp (v1.c_str(), v2.c_str()) > 0;
return strcmp (v1.c_str(), v2.c_str()) < 0;
}
};
typedef QSE::BinaryHeap<PosStr,str_comparator> StrHeap;
typedef QSE::LinkedList<PosStr> StrList;
class Container
{
public:
struct str_ptr_greater_than
{
bool operator() (const StrList::Node* v1, const StrList::Node* v2) const
{
//return strcmp (v1.c_str(), v2.c_str()) > 0;
return strcmp (v1->value.c_str(), v2->value.c_str()) > 0;
}
};
struct str_ptr_positioner
{
void operator() (StrList::Node* v, qse_size_t index) const
{
v->value.heap_pos = index;
}
};
typedef QSE::BinaryHeap<StrList::Node*,str_ptr_greater_than,str_ptr_positioner> StrPtrHeap;
StrList::Node* insert (const char* str)
{
StrList::Node* node = this->str_list.append (PosStr(str));
qse_size_t heap_pos = this->str_heap.insert (node);
return node;
}
bool isEmpty () const
{
QSE_ASSERT (this->str_list.isEmpty() == this->str_heap.isEmpty());
return this->str_list.isEmpty();
}
qse_size_t getSize() const
{
QSE_ASSERT (this->str_list.getSize() == this->str_heap.getSize());
return this->str_list.getSize();
}
const PosStr& getLargest () const
{
StrList::Node* node = this->str_heap.getValueAt(0);
return node->value;
}
void removeLargest ()
{
StrList::Node* node = this->str_heap.getValueAt(0);
this->str_heap.remove (0);
this->str_list.remove (node);
}
void remove (StrList::Node* node)
{
this->str_heap.remove (node->value.heap_pos);
this->str_list.remove (node);
}
StrList::Node* getHeapValueAt (qse_size_t index)
{
return this->str_heap.getValueAt(index);
}
StrList str_list;
StrPtrHeap str_heap;
};
struct IntComparator
{
bool operator() (int v1, int v2) const
{
//return !(v1 > v2);
//return v1 > v2;
return v1 < v2;
}
};
static StrHeap getStrHeap ()
{
char buf[100];
StrHeap h;
for (int i = 0; i < 25; i++)
{
sprintf (buf, "binary heap item %d", i);
h.insert (buf);
}
return h;
}
int main ()
{
char buf[20];
StrHeap h;
#define TOTAL 22
printf ("[TEST #1 - %d strings must be shown]\n", TOTAL * 2);
for (int i = 0; i < TOTAL; i++)
{
sprintf (buf, "hello %d", i);
h.insert (buf);
h.insert (buf);
}
if (h.getSize() != TOTAL * 2)
{
printf ("[FAILURE] ------------------------ \n");
}
else
{
for (qse_size_t i = 0; i < h.getSize(); i++)
{
printf ("%05d %s\n", (int)h.getIndex(h[i]), h[i].c_str());
}
}
printf ("\n");
//////////////////////////////////////////////////////////////////////////////
printf ("[TEST #2 - strings must be shown in ascending order]\n");
const char* x = h[0u].c_str();
std::string oldx;
while (true)
{
oldx = x;
printf ("%s\n", x);
h.remove (0);
if (h.isEmpty()) break;
x = h[0u].c_str();
if (strcmp (x, oldx.c_str()) < 0)
{
printf ("[FAILURE] ------------------------ \n");
break;
}
}
printf ("\n");
//////////////////////////////////////////////////////////////////////////////
printf ("[TEST #3 - integers must be shown in ascending order]\n");
{
QSE::BinaryHeap<int,IntComparator> h2;
h2.insert (70);
h2.insert (90);
h2.insert (10);
h2.insert (5);
h2.insert (88);
h2.insert (87);
h2.insert (300);
h2.insert (91);
h2.insert (100);
h2.insert (200);
int x = h2.getValueAt(0);
int oldx;
while (true)
{
oldx = x;
printf ("%d\n", x);
h2.remove (0);
if (h2.getSize() <= 0) break;
x = h2.getValueAt(0);
if (x < oldx)
{
printf ("[FAILURE] ------------------------ \n");
break;
}
}
}
printf ("\n");
//////////////////////////////////////////////////////////////////////////////
printf ("[TEST #4 - strings must be shown in the ascending order]\n");
{
StrHeap h (getStrHeap ());
const char* x = h.getValueAt(0).c_str();
std::string oldx;
while (true)
{
oldx = x;
printf ("%s\n", x);
h.remove (0);
if (h.isEmpty()) break;
x = h.getValueAt(0).c_str();
if (strcmp (x, oldx.c_str()) < 0)
{
printf ("[FAILURE] ------------------------ \n");
break;
}
}
}
printf ("\n");
//////////////////////////////////////////////////////////////////////////////
printf ("[TEST #5 - random test]\n");
{
Container c;
StrList::Node* node2, * node14;
for (qse_size_t i = 0; i < TOTAL; i++)
{
sprintf (buf, "hello %d", (int)i);
//c.insert (buf);
if (i == 2)
{
node2 = c.insert (buf);
printf ("2nd => %s\n", node2->value.c_str());
}
else if (i == 14)
{
node14 = c.insert (buf);
printf ("14th => %s\n", node14->value.c_str());
}
else c.insert (buf);
}
/*
for (int i = 0; i < c.getSize(); i++)
{
StrList::Node* node = c.getHeapValueAt(i);
printf ("%s %d\n", node->value.c_str(), (int)node->value.heap_pos);
}
*/
for (int i = 0; c.getSize() > 0; i++)
{
if (i == 3) c.remove (node2);
if (i == 5) c.remove (node14);
const char* largest = c.getLargest().c_str();
printf ("%s\n", largest);
c.removeLargest ();
}
}
return 0;
}

113
samples/cmn/bh02.cpp Normal file
View File

@ -0,0 +1,113 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <qse/cmn/BinaryHeap.hpp>
#include <qse/cmn/String.hpp>
#include <qse/cmn/alg.h>
#include <qse/cmn/time.h>
//#define MAX_HEAP
class Julia
{
public:
Julia (int q = 0): x(QSE_NULL)
{
this->x = new int (q);
}
Julia (const Julia& q): x(QSE_NULL)
{
this->x = new int (*q.x);
}
#if defined(QSE_CPP_ENABLE_CPP11_MOVE)
Julia (Julia&& q)
{
this->x = q.x;
q.x = QSE_NULL;
}
#endif
~Julia()
{
if (this->x)
{
delete this->x;
}
}
Julia& operator= (const Julia& q)
{
if (this != &q)
{
if (this->x) { delete this->x; this->x = QSE_NULL; }
this->x = new int (*q.x);
}
return *this;
}
#if defined(QSE_CPP_ENABLE_CPP11_MOVE)
Julia& operator= (Julia&& q)
{
if (this != &q)
{
if (this->x) { delete this->x; this->x = QSE_NULL; }
this->x = q.x;
q.x = QSE_NULL;
}
return *this;
}
#endif
#if defined(MAX_HEAP)
bool operator> (const Julia& q) const { return *this->x > *q.x; }
#else
bool operator> (const Julia& q) const { return *this->x < *q.x; }
#endif
int* x;
};
typedef QSE::BinaryHeap<Julia> JuliaHeap;
int main ()
{
JuliaHeap jh;
qse_uint32_t x;
qse_ntime_t nt;
int oldval, newval;
qse_gettime (&nt);
x = nt.sec + nt.nsec;
for (int i = 0; i < 2500; i++)
{
//x = qse_rand31(x);
x = rand();
jh.insert (Julia((int)x % 1000));
}
#if defined(MAX_HEAP)
oldval = 9999999;
#else
oldval = 0;
#endif
while (!jh.isEmpty())
{
newval = *jh.getValueAt(0).x;
printf ("%d oldval => %d\n", newval, oldval);
#if defined(MAX_HEAP)
QSE_ASSERT (newval <= oldval);
#else
QSE_ASSERT (newval >= oldval);
#endif
jh.remove (0);
oldval = newval;
}
return 0;
}

122
samples/cmn/bh03.cpp Normal file
View File

@ -0,0 +1,122 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <qse/cmn/BinaryHeap.hpp>
#include <qse/cmn/String.hpp>
#include <qse/cmn/alg.h>
#include <qse/cmn/time.h>
//#define MAX_HEAP
class Julia
{
public:
struct julia_t
{
julia_t(int v): v(v), heap_pos(~(qse_size_t)0) {}
int v;
qse_size_t heap_pos;
};
Julia (int q = 0): x(QSE_NULL)
{
this->x = new julia_t (q);
}
Julia (const Julia& q): x(QSE_NULL)
{
this->x = new julia_t (*q.x);
}
#if defined(QSE_CPP_ENABLE_CPP11_MOVE)
Julia (Julia&& q)
{
this->x = q.x;
q.x = QSE_NULL;
}
#endif
~Julia()
{
if (this->x) delete this->x;
}
Julia& operator= (const Julia& q)
{
if (this != &q)
{
if (this->x) { delete this->x; this->x = QSE_NULL; }
this->x = new julia_t (*q.x);
}
return *this;
}
#if defined(QSE_CPP_ENABLE_CPP11_MOVE)
Julia& operator= (Julia&& q)
{
if (this != &q)
{
if (this->x) { delete this->x; this->x = QSE_NULL; }
this->x = q.x;
q.x = QSE_NULL;
}
return *this;
}
#endif
#if defined(MAX_HEAP)
bool operator> (const Julia& q) const { return this->x->v > q.x->v; }
#else
bool operator> (const Julia& q) const { return this->x->v < q.x->v; }
#endif
julia_t* x;
};
struct JuliaGreaterThan
{
bool operator() (const Julia& b1, const Julia& b2) const
{
return b1 > b2;
}
};
struct JuliaOnAssign
{
void operator() (Julia& b, qse_size_t index) const
{
b.x->heap_pos = index;
//printf ("%p[%d] to position %zd\n", &b, b.x->v, index);
}
};
typedef QSE::BinaryHeap<Julia,JuliaGreaterThan,JuliaOnAssign> JuliaHeap;
int main ()
{
{
JuliaHeap jh;
jh.insert (Julia(10));
jh.insert (Julia(20));
jh.insert (Julia(30));
jh.insert (Julia(40));
jh.insert (Julia(50));
jh.remove (1);
// test if the positioner(JuliaOnAssign) is called properly.
QSE_ASSERT (jh.getValueAt(0).x->heap_pos == 0);
QSE_ASSERT (jh.getValueAt(1).x->heap_pos == 1);
QSE_ASSERT (jh.getValueAt(2).x->heap_pos == 2);
QSE_ASSERT (jh.getValueAt(3).x->heap_pos == 3);
printf ("%zd\n", jh.getValueAt(0).x->heap_pos);
printf ("%zd\n", jh.getValueAt(1).x->heap_pos);
printf ("%zd\n", jh.getValueAt(2).x->heap_pos);
printf ("%zd\n", jh.getValueAt(3).x->heap_pos);
}
return 0;
}

58
samples/cmn/chr01.c Normal file
View File

@ -0,0 +1,58 @@
#include <qse/cmn/chr.h>
#include <qse/cmn/mbwc.h>
#include <qse/si/sio.h>
#include <locale.h>
#if defined(_WIN32)
# include <windows.h>
#endif
#define R(f) \
do { \
qse_printf (QSE_T("== %s ==\n"), QSE_T(#f)); \
if (f() == -1) return -1; \
} while (0)
static int test1 (void)
{
qse_char_t c;
for (c = QSE_T('a'); c <= QSE_T('z'); c++)
{
qse_printf (QSE_T("%c => %c\n"), c, QSE_TOUPPER(c));
}
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 ();
R (test1);
qse_close_stdsios ();
return 0;
}

118
samples/cmn/dll.c Normal file
View File

@ -0,0 +1,118 @@
#include <qse/cmn/mem.h>
#include <qse/cmn/str.h>
#include <qse/cmn/dll.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 qse_dll_walk_t walk_dll (qse_dll_t* dll, qse_dll_node_t* n, void* arg)
{
qse_printf (QSE_T("[%.*s]\n"), (int)QSE_DLL_DLEN(n), QSE_DLL_DPTR(n));
return QSE_DLL_WALK_FORWARD;
}
static qse_dll_walk_t rwalk_dll (qse_dll_t* dll, qse_dll_node_t* n, void* arg)
{
qse_printf (QSE_T("[%.*s]\n"), (int)QSE_DLL_DLEN(n), QSE_DLL_DPTR(n));
return QSE_DLL_WALK_BACKWARD;
}
static int test1 ()
{
qse_dll_t* s1;
qse_dll_node_t* p;
qse_char_t* x[] =
{
QSE_T("this is so good"),
QSE_T("what the hack"),
QSE_T("do you like it?")
};
int i;
s1 = qse_dll_open (QSE_MMGR_GETDFL(), 0);
if (s1 == QSE_NULL)
{
qse_printf (QSE_T("cannot open a string\n"));
return -1;
}
qse_dll_setcopier (s1, QSE_DLL_COPIER_INLINE);
qse_dll_setscale (s1, QSE_SIZEOF(qse_char_t));
for (i = 0; i < QSE_COUNTOF(x); i++)
{
qse_dll_pushtail (s1, x[i], qse_strlen(x[i]));
}
qse_printf (QSE_T("s1 holding [%zu] nodes\n"), QSE_DLL_SIZE(s1));
qse_dll_walk (s1, walk_dll, QSE_NULL);
p = qse_dll_search (s1, QSE_NULL, x[0], qse_strlen(x[0]));
if (p != QSE_NULL)
{
qse_dll_delete (s1, p);
}
qse_printf (QSE_T("s1 holding [%zu] nodes\n"), QSE_DLL_SIZE(s1));
qse_dll_walk (s1, walk_dll, QSE_NULL);
qse_dll_close (s1);
return 0;
}
static int test2 ()
{
qse_dll_t* s1;
qse_dll_node_t* p;
qse_char_t* x[] =
{
QSE_T("this is so good"),
QSE_T("what the hack"),
QSE_T("do you like it?")
};
int i;
s1 = qse_dll_open (QSE_MMGR_GETDFL(), 0);
if (s1 == QSE_NULL)
{
qse_printf (QSE_T("cannot open a string\n"));
return -1;
}
qse_dll_setcopier (s1, QSE_DLL_COPIER_INLINE);
qse_dll_setscale (s1, QSE_SIZEOF(qse_char_t));
for (i = 0; i < QSE_COUNTOF(x); i++)
{
qse_dll_pushtail (s1, x[i], qse_strlen(x[i]));
}
qse_printf (QSE_T("s1 holding [%zu] nodes\n"), QSE_DLL_SIZE(s1));
qse_dll_rwalk (s1, rwalk_dll, QSE_NULL);
p = qse_dll_search (s1, QSE_NULL, x[0], qse_strlen(x[0]));
if (p != QSE_NULL)
{
qse_dll_delete (s1, p);
}
qse_printf (QSE_T("s1 holding [%zu] nodes\n"), QSE_DLL_SIZE(s1));
qse_dll_rwalk (s1, rwalk_dll, QSE_NULL);
qse_dll_close (s1);
return 0;
}
int main ()
{
qse_open_stdsios ();
R (test1);
R (test2);
qse_close_stdsios ();
return 0;
}

135
samples/cmn/env01.c Normal file
View File

@ -0,0 +1,135 @@
#include <qse/cmn/env.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 void dump (qse_env_t* env)
{
const qse_env_char_t* envstr;
qse_env_char_t** envarr;
envstr = qse_env_getstr (env);
#if (defined(QSE_ENV_CHAR_IS_WCHAR) && defined(QSE_CHAR_IS_WCHAR)) || \
(defined(QSE_ENV_CHAR_IS_MCHAR) && defined(QSE_CHAR_IS_MCHAR))
while (*envstr != QSE_T('\0'))
{
qse_printf (QSE_T("%p [%s]\n"), envstr, envstr);
envstr += qse_strlen(envstr) + 1;
}
#elif defined(QSE_ENV_CHAR_IS_WCHAR)
while (*envstr != QSE_WT('\0'))
{
qse_printf (QSE_T("%p [%S]\n"), envstr, envstr);
envstr += qse_wcslen(envstr) + 1;
}
#else
while (*envstr != QSE_MT('\0'))
{
qse_printf (QSE_T("%p [%S]\n"), envstr, envstr);
envstr += qse_mbslen(envstr) + 1;
}
#endif
qse_printf (QSE_T("-------------\n"));
envarr = qse_env_getarr (env);
while (*envarr)
{
#if (defined(QSE_ENV_CHAR_IS_WCHAR) && defined(QSE_CHAR_IS_WCHAR)) || \
(defined(QSE_ENV_CHAR_IS_MCHAR) && defined(QSE_CHAR_IS_MCHAR))
qse_printf (QSE_T("%p [%s]\n"), *envarr, *envarr);
#else
qse_printf (QSE_T("%p [%S]\n"), *envarr, *envarr);
#endif
envarr++;
}
}
static int test1 (void)
{
qse_env_t* env;
env = qse_env_open (QSE_MMGR_GETDFL(), 0, 0);
qse_env_clear (env);
qse_env_insert (env, QSE_T("alice"), QSE_T("wonderland"));
qse_env_insert (env, QSE_T("cool"), QSE_T("mint"));
qse_env_insert (env, QSE_T("smurf"), QSE_T("happy song"));
qse_env_insert (env, QSE_T("donkey"), QSE_T("mule"));
qse_env_insert (env, QSE_T("lily"), QSE_T("rose"));
qse_env_clear (env);
qse_env_insert (env, QSE_T("alice"), QSE_T("wonderland"));
qse_env_insert (env, QSE_T("cool"), QSE_T("mint"));
qse_env_insert (env, QSE_T("smurf"), QSE_T("happy song"));
qse_env_insert (env, QSE_T("donkey"), QSE_T("mule"));
qse_env_insert (env, QSE_T("lily"), QSE_T("rose"));
qse_env_delete (env, QSE_T("cool"));
qse_env_insert (env, QSE_T("spider"), QSE_T("man"));
dump (env);
qse_env_close (env);
return 0;
}
static int test2 (void)
{
qse_env_t* env;
env = qse_env_open (QSE_MMGR_GETDFL(), 0, 1);
qse_printf (QSE_T("DELETING HOME => %s\n"),
(qse_env_delete (env, QSE_T("HOME")) == 0?
QSE_T("SUCCESS"): QSE_T("FAILURE"))
);
qse_printf (QSE_T("DELETING wolf => %s\n"),
(qse_env_delete (env, QSE_T("wolf")) == 0?
QSE_T("SUCCESS"): QSE_T("FAILURE"))
);
dump (env);
qse_env_close (env);
return 0;
}
static int test3 (void)
{
qse_env_t* env;
env = qse_env_open (QSE_MMGR_GETDFL(), 0, 0);
qse_printf (QSE_T("appending to PATH => %d\n"), qse_env_append (env, QSE_T("xxx"))); /* this should fail as there's no item in the environment */
qse_printf (QSE_T("inserting PATH => %d\n"), qse_env_insert (env, QSE_T("PATH"), QSE_NULL));
qse_printf (QSE_T("appending to PATH => %d\n"), qse_env_append (env, QSE_T(":/usr/xxx/bin:/tmp/bin:/var/lib/qse/bin/sbin/test/bin")));
qse_printf (QSE_T("appending to PATH => %d\n"), qse_env_append (env, QSE_T("")));
qse_printf (QSE_T("inserting HOME => %d\n"), qse_env_insertmbs (env, QSE_MT("HOME"), QSE_NULL));
qse_printf (QSE_T("inserting USER => %d\n"), qse_env_insertwcs (env, QSE_WT("USER"), QSE_NULL));
qse_printf (QSE_T("inserting WHAT => %d\n"), qse_env_insert (env, QSE_T("WHAT"), QSE_NULL));
qse_printf (QSE_T("inserting an empty string => %d\n"), qse_env_insert (env, QSE_T(""), QSE_NULL));
dump (env);
qse_env_close (env);
return 0;
}
int main ()
{
qse_open_stdsios();
R (test1);
R (test2);
R (test3);
qse_close_stdsios();
return 0;
}

194
samples/cmn/fma.c Normal file
View File

@ -0,0 +1,194 @@
#include <qse/cmn/fma.h>
#include <qse/cmn/rbt.h>
#include <qse/cmn/mem.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 qse_rbt_style_t style =
{
{
QSE_RBT_COPIER_INLINE,
QSE_RBT_COPIER_INLINE
},
{
QSE_RBT_FREEER_DEFAULT,
QSE_RBT_FREEER_DEFAULT
},
QSE_RBT_COMPER_DEFAULT,
QSE_RBT_KEEPER_DEFAULT
};
static int test1 ()
{
int i;
int* ptr[100];
qse_fma_t* fma = qse_fma_open (QSE_MMGR_GETDFL(), 0, sizeof(int), 10 /* max block size */, 5 /* max chunks */);
if (fma == QSE_NULL)
{
qse_printf (QSE_T("cannot open fma\n"));
return -1;
}
/* max 50 (10 * 5) allocations should be possible */
for (i = 0; i < 100; i++)
{
ptr[i] = qse_fma_alloc (fma, sizeof(int));
if (i < 50) QSE_ASSERT (ptr[i] != QSE_NULL);
else QSE_ASSERT (ptr[i] == QSE_NULL);
if (ptr[i])
{
qse_printf (QSE_T("%d %p\n"), i, ptr[i]);
*(ptr[i]) = i;
}
else qse_printf (QSE_T("%d FAIL\n"), i);
}
for (i = 0; i < 30; i+=2)
{
if (ptr[i])
{
qse_fma_free (fma, ptr[i]);
ptr[i] = QSE_NULL;
}
}
for (i = 0; i < 100; i++)
{
if (ptr[i])
{
qse_fma_free (fma, ptr[i]);
ptr[i] = QSE_NULL;
}
}
for (i = 0; i < 100; i++)
{
ptr[i] = qse_fma_alloc (fma, sizeof(int));
if (i < 50) QSE_ASSERT (ptr[i] != QSE_NULL);
else QSE_ASSERT (ptr[i] == QSE_NULL);
if (ptr[i])
{
qse_printf (QSE_T("%d %p\n"), i, ptr[i]);
*(ptr[i]) = i;
}
else qse_printf (QSE_T("%d FAIL\n"), i);
}
qse_fma_close (fma);
return 0;
}
static void* fma_alloc (qse_mmgr_t* mmgr, qse_size_t size)
{
return qse_fma_alloc (mmgr->ctx, size);
}
static void* fma_realloc (qse_mmgr_t* mmgr, void* ptr, qse_size_t size)
{
return qse_fma_realloc (mmgr->ctx, ptr, size);
}
static void fma_free (qse_mmgr_t* mmgr, void* ptr)
{
qse_fma_free (mmgr->ctx, ptr);
}
static qse_rbt_walk_t walk (qse_rbt_t* rbt, qse_rbt_pair_t* pair, void* ctx)
{
qse_printf (QSE_T("key = %lld, value = %lld\n"),
*(long*)QSE_RBT_KPTR(pair), *(long*)QSE_RBT_VPTR(pair));
return QSE_RBT_WALK_FORWARD;
}
static int test2 ()
{
qse_mmgr_t mmgr =
{
fma_alloc,
fma_realloc,
fma_free,
QSE_NULL
};
qse_fma_t* fma;
qse_rbt_t rbt;
qse_size_t blksize;
long x;
/* key */ /* value */ /* internal node */
blksize = sizeof(long) + sizeof(long) + sizeof(qse_rbt_pair_t);
fma = qse_fma_open (QSE_MMGR_GETDFL(), 0, blksize, 10, 0);
if (fma == QSE_NULL)
{
qse_printf (QSE_T("cannot open a memory allocator\n"));
return -1;
}
mmgr.ctx = fma;
if (qse_rbt_init (&rbt, &mmgr, QSE_SIZEOF(long int), QSE_SIZEOF(long int)) <= -1)
{
qse_printf (QSE_T("cannot initialize a tree\n"));
qse_fma_close (fma);
return -1;
}
qse_rbt_setstyle (&rbt, &style);
for (x = 10; x < 100; x++)
{
long int y = x * x;
if (qse_rbt_insert (&rbt, &x, 1, &y, 1) == QSE_NULL)
{
qse_printf (QSE_T("failed to insert. out of memory\n"));
break;
}
}
for (x = 10; x < 105; x++)
{
long int y = x * x, yy;
qse_rbt_pair_t* pair;
pair = qse_rbt_search (&rbt, &x, 1);
if (x < 100)
{
QSE_ASSERT (pair != QSE_NULL);
yy = *(long int*)QSE_RBT_VPTR(pair);
QSE_ASSERT (yy = y);
qse_printf (QSE_T("%ld => %ld\n"), (long int)x, (long int)yy);
}
else
{
QSE_ASSERT (pair == QSE_NULL);
}
}
qse_rbt_walk (&rbt, walk, QSE_NULL);
qse_rbt_fini (&rbt);
qse_fma_close (fma);
return 0;
}
int main ()
{
qse_open_stdsios ();
R (test1);
R (test2);
qse_close_stdsios ();
return 0;
}

84
samples/cmn/fmt01.c Normal file
View File

@ -0,0 +1,84 @@
#include <qse/cmn/fmt.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_char_t buf[19];
int bases[] = { 2, 8, 10, 16 };
qse_char_t* prefix[] = { QSE_T("0b"), QSE_T("0"), QSE_NULL, QSE_T("0x") };
int flags[] =
{
QSE_FMTINTMAX_NOTRUNC | 0,
QSE_FMTINTMAX_NOTRUNC | QSE_FMTINTMAX_UPPERCASE,
QSE_FMTINTMAX_NOTRUNC | QSE_FMTINTMAX_FILLRIGHT,
QSE_FMTINTMAX_NOTRUNC | QSE_FMTINTMAX_UPPERCASE | QSE_FMTINTMAX_PLUSSIGN,
QSE_FMTINTMAX_NOTRUNC | QSE_FMTINTMAX_UPPERCASE | QSE_FMTINTMAX_FILLRIGHT,
QSE_FMTINTMAX_NOTRUNC | QSE_FMTINTMAX_UPPERCASE | QSE_FMTINTMAX_FILLCENTER,
QSE_FMTINTMAX_NOTRUNC | QSE_FMTINTMAX_UPPERCASE | QSE_FMTINTMAX_FILLRIGHT | QSE_FMTINTMAX_PLUSSIGN,
QSE_FMTINTMAX_NOTRUNC | QSE_FMTINTMAX_UPPERCASE | QSE_FMTINTMAX_FILLCENTER | QSE_FMTINTMAX_PLUSSIGN,
QSE_FMTINTMAX_NOTRUNC | QSE_FMTINTMAX_NONULL | 0,
QSE_FMTINTMAX_NOTRUNC | QSE_FMTINTMAX_NONULL | QSE_FMTINTMAX_UPPERCASE,
QSE_FMTINTMAX_NOTRUNC | QSE_FMTINTMAX_NONULL | QSE_FMTINTMAX_FILLRIGHT,
QSE_FMTINTMAX_NOTRUNC | QSE_FMTINTMAX_NONULL | QSE_FMTINTMAX_UPPERCASE | QSE_FMTINTMAX_PLUSSIGN,
QSE_FMTINTMAX_NOTRUNC | QSE_FMTINTMAX_NONULL | QSE_FMTINTMAX_UPPERCASE | QSE_FMTINTMAX_FILLRIGHT,
QSE_FMTINTMAX_NOTRUNC | QSE_FMTINTMAX_NONULL | QSE_FMTINTMAX_UPPERCASE | QSE_FMTINTMAX_FILLCENTER,
QSE_FMTINTMAX_NOTRUNC | QSE_FMTINTMAX_NONULL | QSE_FMTINTMAX_UPPERCASE | QSE_FMTINTMAX_FILLRIGHT | QSE_FMTINTMAX_PLUSSIGN,
QSE_FMTINTMAX_NOTRUNC | QSE_FMTINTMAX_NONULL | QSE_FMTINTMAX_UPPERCASE | QSE_FMTINTMAX_FILLCENTER | QSE_FMTINTMAX_PLUSSIGN
};
int i, j;
int num = 0xF0F3;
for (i = 0; i < QSE_COUNTOF(bases); i++)
{
for (j = 0; j < QSE_COUNTOF(flags); j++)
{
int n = qse_fmtuintmax (buf, QSE_COUNTOF(buf), num, bases[i] | flags[j], -1, QSE_T('.'), prefix[i]);
if (n <= -1)
{
qse_printf (QSE_T("%8X => [%4d:%05X] ERROR[%d]\n"), num, bases[i], flags[j], n);
}
else
{
qse_printf (QSE_T("%8X => [%4d:%05X] [%.*s][%d]\n"), num, bases[i], flags[j], n, buf, 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;
}

104
samples/cmn/fmt02.c Normal file
View File

@ -0,0 +1,104 @@
#include <qse/cmn/fmt.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_char_t buf[129];
int bases[] = { 2, 8, 10, 16 };
qse_char_t* prefix[] = { QSE_T("0b"), QSE_T("0"), QSE_NULL, QSE_T("0x") };
int flags[] =
{
QSE_FMTINTMAX_PLUSSIGN,
QSE_FMTINTMAX_FILLRIGHT | QSE_FMTINTMAX_PLUSSIGN,
QSE_FMTINTMAX_FILLCENTER | QSE_FMTINTMAX_PLUSSIGN
};
int i, j, k;
qse_intmax_t nums[] = { -12345, 12345, 0, QSE_TYPE_MAX(qse_intmax_t) };
/* note that nums[3] may not be printed properly with %d in printf().
* however, the formatting result with qse_fmtintmax() should be printed
* properly.
*/
qse_printf (QSE_T("[PRECISION -1]\n"));
for (k = 0; k < QSE_COUNTOF(nums) ; k++)
{
for (i = 0; i < QSE_COUNTOF(bases); i++)
{
for (j = 0; j < QSE_COUNTOF(flags); j++)
{
int n = qse_fmtintmax (buf, QSE_COUNTOF(buf), nums[k], bases[i] | flags[j] | QSE_FMTINTMAX_NOTRUNC, -1, QSE_T('.'), prefix[i]);
if (n <= -1)
{
qse_printf (QSE_T("%8d => [%4d:%05X] ERROR[%d]\n"), (int)nums[k], bases[i], flags[j], n);
}
else
{
qse_printf (QSE_T("%8d => [%4d:%05X] [%s]\n"), (int)nums[k], bases[i], flags[j], buf);
}
}
}
qse_printf (QSE_T("------------------------------\n"));
}
qse_printf (QSE_T("[PRECISION 0]\n"));
for (k = 0; k < QSE_COUNTOF(nums) ; k++)
{
for (i = 0; i < QSE_COUNTOF(bases); i++)
{
for (j = 0; j < QSE_COUNTOF(flags); j++)
{
int n = qse_fmtintmax (buf, QSE_COUNTOF(buf), nums[k], bases[i] | flags[j] | QSE_FMTINTMAX_NOTRUNC | QSE_FMTINTMAX_NOZERO, 0, QSE_T('*'), prefix[i]);
if (n <= -1)
{
qse_printf (QSE_T("%8d => [%4d:%05X] ERROR[%d]\n"), (int)nums[k], bases[i], flags[j], n);
}
else
{
qse_printf (QSE_T("%8d => [%4d:%05X] [%s]\n"), (int)nums[k], bases[i], flags[j], buf);
}
}
}
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;
}

148
samples/cmn/hl01.cpp Normal file
View File

@ -0,0 +1,148 @@
#include <stdio.h>
#include <qse/cmn/StdMmgr.hpp>
#include <qse/cmn/HeapMmgr.hpp>
#include <qse/cmn/LinkedList.hpp>
#include <qse/cmn/HashList.hpp>
#include <qse/si/sio.h>
class T
{
public:
T(int x = 0): x(x) {
// printf ("constructor\n");
y = x + 99;
}
~T() {
// printf ("destructor\n");
}
bool operator== (const T& x) const { return this->x == x.x; }
qse_size_t getHashCode() const { return x; }
int getValue() const { return this->x; }
int getY() const { return this->y; }
protected:
int x;
int y;
};
void* operator new (size_t x, int q)
{
return ::operator new (x);
}
struct IntHasher
{
qse_size_t operator() (int v) const { return v; }
};
struct IntIsEqual
{
qse_size_t operator() (int v1, const T& v2) const { return v1 == v2.getValue(); }
};
typedef QSE::HashList<int,IntHasher> IntList;
int main ()
{
qse_open_stdsios ();
T* x;
//QSE::StdMmgr* mmgr = QSE::StdMmgr::getDFL();
//QSE::HeapMmgr heap_mmgr (QSE::Mmgr::getDFL(), 1000000);
//QSE::Mmgr* mmgr = &heap_mmgr;
QSE::Mmgr* mmgr = QSE_NULL;
/*
x = new(mmgr) T; //[10];
printf ("x====> %p\n", x);
x->~T();
//for (int i = 0; i < 10; i++) x[i].~T();
//::operator delete[] (x, mmgr);
::operator delete (x, mmgr);
//delete[] x;
printf ("----------------------\n");
T* y = new(10) T;
y->~T();
::operator delete(y);
printf ("----------------------\n");
*/
try
{
T t1,t2,t3;
#if 0
printf ("----------------------\n");
{
QSE::LinkedList<T> l (mmgr, 100);
printf ("----------------------\n");
l.append (t1);
printf ("----------------------\n");
l.append (t2);
l.append (t3);
printf ("================\n");
QSE::LinkedList<T> l2 (mmgr, 100);
l2 = l;
}
printf ("----------------------\n");
#endif
//QSE::HashList<T> h (1000, 75, 1000, mmgr);
QSE::HashList<T> h (1000, 75, 500, mmgr);
for (int i = 0; i < 1000; i++)
{
T x(i);
h.insert (x);
}
printf ("h.getSize() => %d\n", (int)h.getSize());
QSE::HashList<T> h2 (1000, 75, 700, mmgr);
h2 = h;
for (QSE::HashList<T>::Iterator it = h2.getIterator(); it.isLegit(); it++)
{
printf ("%d\n", (*it).getValue());
}
printf ("----------------------\n");
printf ("%p\n", h2.getHeadNode());
printf ("----------------------\n");
IntList hl;
IntList::Iterator it;
hl.insert (10);
hl.insert (150);
hl.insert (200);
for (it = hl.getIterator(); it.isLegit(); it++)
{
printf ("%d\n", *it);
}
printf ("----------------------\n");
{
const QSE::HashList<T>& h3 = h2;
const T* tt = h3.heterofindValue<int,IntHasher,IntIsEqual> (100);
if (tt) printf ("%d:%d\n", tt->getValue(), tt->getY());
else printf ("not found...\n");
}
printf ("----------------------\n");
}
catch (QSE::Exception& e)
{
qse_printf (QSE_T("Exception: %s\n"), QSE_EXCEPTION_NAME(e));
}
qse_close_stdsios ();
return 0;
}

428
samples/cmn/htb01.c Normal file
View File

@ -0,0 +1,428 @@
#include <qse/cmn/mem.h>
#include <qse/cmn/chr.h>
#include <qse/cmn/str.h>
#include <qse/cmn/htb.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_build (qse_htb_t* s1)
{
int i;
for (i = 1; i < 50; i++)
{
int j = i * 2;
qse_printf (QSE_T("inserting a key %d and a value %d: "), i, j);
if (qse_htb_insert (s1, &i, sizeof(i), &j, sizeof(j)) == QSE_NULL)
{
qse_printf (QSE_T("[FAILED]\n"));
return -1;
}
qse_printf (QSE_T("[OK]\n"));
}
return 0;
}
static void test1_delete (qse_htb_t* s1)
{
int i;
int t[] = { 20, 11, 13, 40 };
int t2[] = { 22, 21, 31, 14, 48, 32, 29 };
for (i = 1; i < 53; i+=2)
{
qse_printf (QSE_T("deleting a key %d: "), i);
if (qse_htb_delete (s1, &i, sizeof(i)) == -1)
qse_printf (QSE_T("[FAILED]\n"));
else
qse_printf (QSE_T("[OK]\n"));
}
for (i = 0; i < QSE_COUNTOF(t); i++)
{
int k = t[i];
int v = k * 1000;
qse_printf (QSE_T("updating a key %d value %d: "), k, v);
if (qse_htb_update (s1, &k, sizeof(k), &v, sizeof(v)) == QSE_NULL)
qse_printf (QSE_T("[FAILED]\n"));
else
qse_printf (QSE_T("[OK]\n"));
}
for (i = 0; i < QSE_COUNTOF(t); i++)
{
int k = t[i];
int v = k * 1000;
qse_printf (QSE_T("inserting a key %d value %d: "), k, v);
if (qse_htb_insert (s1, &k, sizeof(k), &v, sizeof(v)) == QSE_NULL)
qse_printf (QSE_T("[FAILED]\n"));
else
qse_printf (QSE_T("[OK]\n"));
}
for (i = 0; i < QSE_COUNTOF(t2); i++)
{
int k = t2[i];
int v = k * 2000;
qse_printf (QSE_T("upserting a key %d value %d: "), k, v);
if (qse_htb_upsert (s1, &k, sizeof(k), &v, sizeof(v)) == QSE_NULL)
qse_printf (QSE_T("[FAILED]\n"));
else
qse_printf (QSE_T("[OK]\n"));
}
}
static void test1_dump (qse_htb_t* s1)
{
int i;
qse_printf (QSE_T("map contains %u pairs\n"), (unsigned)QSE_HTB_SIZE(s1));
for (i = 1; i < 50; i++)
{
qse_htb_pair_t* p = qse_htb_search (s1, &i, sizeof(i));
if (p == QSE_NULL)
{
qse_printf (QSE_T("%d => unknown\n"), i);
}
else
{
qse_printf (QSE_T("%d => %d(%d)\n"),
i, *(int*)QSE_HTB_VPTR(p), (int)QSE_HTB_VLEN(p));
}
}
}
static int test1 ()
{
qse_htb_t* s1;
s1 = qse_htb_open (QSE_MMGR_GETDFL(), 0, 5, 70, 1, 1);
if (s1 == QSE_NULL)
{
qse_printf (QSE_T("cannot open a hash table\n"));
return -1;
}
qse_htb_setstyle (s1, qse_gethtbstyle(QSE_HTB_STYLE_INLINE_COPIERS));
if (test1_build(s1) == -1)
{
qse_htb_close (s1);
return -1;
}
test1_dump (s1);
test1_delete (s1);
test1_dump (s1);
qse_htb_close (s1);
return 0;
}
qse_htb_walk_t print_map_pair (qse_htb_t* map, qse_htb_pair_t* pair, void* arg)
{
qse_printf (QSE_T("%.*s[%d] => %.*s[%d]\n"),
(int)QSE_HTB_KLEN(pair), QSE_HTB_KPTR(pair), (int)QSE_HTB_KLEN(pair),
(int)QSE_HTB_VLEN(pair), QSE_HTB_VPTR(pair), (int)QSE_HTB_VLEN(pair));
return QSE_HTB_WALK_FORWARD;
}
static int test2 ()
{
qse_htb_t* s1;
int i;
qse_char_t* keys[] =
{
QSE_T("if you ever happen"),
QSE_T("to want to link againt"),
QSE_T("installed libraries"),
QSE_T("in a given directory")
};
qse_char_t* vals[] =
{
QSE_T("what the hell is this"),
QSE_T("oh my goddess"),
QSE_T("hello mr monkey"),
QSE_T("this is good")
};
s1 = qse_htb_open (QSE_MMGR_GETDFL(), 0, 1, 70,
QSE_SIZEOF(qse_char_t), QSE_SIZEOF(qse_char_t));
if (s1 == QSE_NULL)
{
qse_printf (QSE_T("cannot open a hash table\n"));
return -1;
}
qse_htb_setstyle (s1, qse_gethtbstyle(QSE_HTB_STYLE_INLINE_COPIERS));
for (i = 0; i < QSE_COUNTOF(keys); i++)
{
int vi = QSE_COUNTOF(keys)-i-1;
qse_printf (QSE_T("inserting a key [%s] and a value [%s]: "), keys[i], keys[vi]);
if (qse_htb_insert (s1,
keys[i], qse_strlen(keys[i]),
keys[vi], qse_strlen(keys[vi])) == QSE_NULL)
{
qse_printf (QSE_T("[FAILED]\n"));
}
else
{
qse_printf (QSE_T("[OK]\n"));
}
}
qse_htb_walk (s1, print_map_pair, QSE_NULL);
for (i = 0; i < QSE_COUNTOF(keys); i++)
{
qse_printf (QSE_T("updating a key [%s] and a value [%s]: "), keys[i], vals[i]);
if (qse_htb_update (s1,
keys[i], qse_strlen(keys[i]),
vals[i], qse_strlen(vals[i])) == QSE_NULL)
{
qse_printf (QSE_T("[FAILED]\n"));
}
else
{
qse_printf (QSE_T("[OK]\n"));
}
}
qse_htb_walk (s1, print_map_pair, QSE_NULL);
qse_htb_close (s1);
return 0;
}
#if 0
static int comp_key (qse_htb_t* map,
const void* kptr1, qse_size_t klen1,
const void* kptr2, qse_size_t klen2)
{
return qse_strxncasecmp (kptr1, klen1, kptr2, klen2);
}
qse_htb_walk_t print_map_pair_3 (qse_htb_t* map, qse_htb_pair_t* pair, void* arg)
{
qse_printf (QSE_T("%.*s[%d] => %d\n"),
(int)QSE_HTB_KLEN(pair), QSE_HTB_KPTR(pair), (int)QSE_HTB_KLEN(pair),
*(int*)QSE_HTB_VPTR(pair));
return QSE_HTB_WALK_FORWARD;
}
static int test3 ()
{
qse_htb_t* s1;
int i;
qse_char_t* keys[] =
{
QSE_T("one"),
QSE_T("two"),
QSE_T("three"),
QSE_T("four"),
QSE_T("ONE"),
QSE_T("Two"),
QSE_T("thRee")
};
s1 = qse_htb_open (QSE_MMGR_GETDFL(), 0, 1, 70);
if (s1 == QSE_NULL)
{
qse_printf (QSE_T("cannot open a hash table\n"));
return -1;
}
qse_htb_setscale (s1, QSE_HTB_KEY, QSE_SIZEOF(qse_char_t));
qse_htb_setcopier (s1, QSE_HTB_KEY, QSE_HTB_COPIER_INLINE);
qse_htb_setcopier (s1, QSE_HTB_VAL, QSE_HTB_COPIER_INLINE);
qse_htb_setcomper (s1, comp_key);
for (i = 0; i < QSE_COUNTOF(keys); i++)
{
qse_printf (QSE_T("inserting a key [%s] and a value %d: "), keys[i], i);
if (qse_htb_insert (s1, keys[i], qse_strlen(keys[i]), &i, sizeof(i)) == QSE_NULL)
{
qse_printf (QSE_T("[FAILED]\n"));
}
else
{
qse_printf (QSE_T("[OK]\n"));
}
}
qse_htb_walk (s1, print_map_pair_3, QSE_NULL);
qse_htb_close (s1);
return 0;
}
static int test4 ()
{
qse_htb_t* s1;
int i;
qse_char_t* keys[] =
{
QSE_T("one"),
QSE_T("two"),
QSE_T("three"),
QSE_T("four"),
QSE_T("ONE"),
QSE_T("Two"),
QSE_T("thRee")
};
s1 = qse_htb_open (QSE_MMGR_GETDFL(), 0, 1, 70);
if (s1 == QSE_NULL)
{
qse_printf (QSE_T("cannot open a hash table\n"));
return -1;
}
qse_htb_setscale (s1, QSE_HTB_KEY, QSE_SIZEOF(qse_char_t));
qse_htb_setcopier (s1, QSE_HTB_KEY, QSE_HTB_COPIER_INLINE);
qse_htb_setcopier (s1, QSE_HTB_VAL, QSE_HTB_COPIER_INLINE);
qse_htb_setcomper (s1, comp_key);
for (i = 0; i < QSE_COUNTOF(keys); i++)
{
qse_printf (QSE_T("upserting a key [%s] and a value %d: "), keys[i], i);
if (qse_htb_upsert (s1, keys[i], qse_strlen(keys[i]), &i, sizeof(i)) == QSE_NULL)
{
qse_printf (QSE_T("[FAILED]\n"));
}
else
{
qse_printf (QSE_T("[OK]\n"));
}
}
qse_htb_walk (s1, print_map_pair_3, QSE_NULL);
qse_htb_close (s1);
return 0;
}
#endif
static qse_htb_pair_t* test5_cbserter (
qse_htb_t* htb, qse_htb_pair_t* pair,
void* kptr, qse_size_t klen, void* ctx)
{
qse_cstr_t* v = (qse_cstr_t*)ctx;
if (pair == QSE_NULL)
{
/* no existing key for the key */
return qse_htb_allocpair (htb, kptr, klen, v->ptr, v->len);
}
else
{
/* a pair with the key exists.
* in this sample, i will append the new value to the old value
* separated by a comma */
qse_htb_pair_t* new_pair;
qse_char_t comma = QSE_T(',');
qse_byte_t* vptr;
/* allocate a new pair, but without filling the actual value.
* note vptr is given QSE_NULL for that purpose */
new_pair = qse_htb_allocpair (
htb, kptr, klen, QSE_NULL,
QSE_HTB_VLEN(pair) + 1 + v->len);
if (new_pair == QSE_NULL) return QSE_NULL;
/* fill in the value space */
vptr = QSE_HTB_VPTR(new_pair);
qse_memcpy (vptr, QSE_HTB_VPTR(pair),
QSE_HTB_VLEN(pair)*QSE_SIZEOF(qse_char_t));
vptr += QSE_HTB_VLEN(pair) * QSE_SIZEOF(qse_char_t);
qse_memcpy (vptr, &comma, QSE_SIZEOF(qse_char_t));
vptr += QSE_SIZEOF(qse_char_t);
qse_memcpy (vptr, v->ptr, v->len*QSE_SIZEOF(qse_char_t));
/* this callback requires the old pair to be destroyed */
qse_htb_freepair (htb, pair);
/* return the new pair */
return new_pair;
}
}
static int test5 ()
{
qse_htb_t* s1;
int i;
qse_char_t* keys[] =
{
QSE_T("one"), QSE_T("two"), QSE_T("three")
};
qse_char_t* vals[] =
{
QSE_T("1"), QSE_T("2"), QSE_T("3"), QSE_T("4"), QSE_T("5"),
};
s1 = qse_htb_open (QSE_MMGR_GETDFL(), 0, 10, 70,
QSE_SIZEOF(qse_char_t), QSE_SIZEOF(qse_char_t));
if (s1 == QSE_NULL)
{
qse_printf (QSE_T("cannot open a hash table\n"));
return -1;
}
qse_htb_setstyle (s1, qse_gethtbstyle(QSE_HTB_STYLE_INLINE_COPIERS));
for (i = 0; i < QSE_COUNTOF(vals); i++)
{
qse_cstr_t ctx;
qse_printf (QSE_T("setting a key [%s] and a value [%s]: "), keys[i%QSE_COUNTOF(keys)], vals[i]);
ctx.ptr = vals[i];
ctx.len = qse_strlen(vals[i]);
if (qse_htb_cbsert (s1,
keys[i%QSE_COUNTOF(keys)],
qse_strlen(keys[i%QSE_COUNTOF(keys)]),
test5_cbserter, &ctx) == QSE_NULL)
{
qse_printf (QSE_T("[FAILED]\n"));
}
else
{
qse_printf (QSE_T("[OK]\n"));
}
}
qse_htb_walk (s1, print_map_pair, QSE_NULL);
qse_htb_close (s1);
return 0;
}
int main ()
{
qse_open_stdsios ();
R (test1);
R (test2);
#if 0
R (test3);
R (test4);
#endif
R (test5);
qse_close_stdsios ();
return 0;
}

221
samples/cmn/htb02.cpp Normal file
View File

@ -0,0 +1,221 @@
#include <stdio.h>
#include <qse/cmn/StdMmgr.hpp>
#include <qse/cmn/HeapMmgr.hpp>
#include <qse/cmn/HashTable.hpp>
#include <qse/si/sio.h>
#include <string>
#include <string.h>
struct IntHasher
{
qse_size_t operator() (int v) const
{
return v;
}
};
typedef QSE::HashTable<int,int,IntHasher> IntTable;
struct IntClass
{
IntClass (int x = 0): x (x) {}
int x;
};
struct IntClassHasher
{
qse_size_t operator() (const IntClass& v) const
{
return v.x;
}
};
struct IntClassComparator
{
bool operator() (const IntClass& v, int y) const
{
return v.x == y;
}
};
struct StrHasher
{
qse_size_t operator() (const std::string& v) const
{
return QSE::Hashable::getHashCode (v.c_str(), v.length());
}
};
typedef QSE::HashTable<std::string,int,StrHasher> Str1Table;
struct cstr_hasher
{
qse_size_t operator() (const char* v) const
{
return QSE::Hashable::getHashCode (v, strlen(v));
}
};
struct cstr_is_equal
{
int operator() (const char* v1, const std::string& v2) const
{
return strcmp (v1, v2.c_str()) == 0;
}
};
int main ()
{
// qse_open_stdsios ();
QSE::HeapMmgr heap_mmgr (3000000, QSE::Mmgr::getDFL());
//QSE::HashTable<int,int,IntHasher> int_table (&heap_mmgr, 1000);
IntTable int_table (IntTable::DEFAULT_CAPACITY, IntTable::DEFAULT_LOAD_FACTOR, 1000);
//IntTable int_table (IntTable::DEFAULT_CAPACITY, IntTable::DEFAULT_LOAD_FACTOR, 0);
printf ("----------\n");
for (int i = 0; i < 100; i++)
{
int_table.insert (i, i * 20);
}
for (int i = 50; i < 150; i++)
{
int_table.upsert (i, i * 20);
}
printf ("----------\n");
/*
qse_size_t bucket_size = int_table.getBucketSize();
for (qse_size_t i = 0; i < bucket_size; i++)
{
IntTable::Bucket* b = int_table.getBucket (i);
}
*/
printf ("----------\n");
/*
IntTable::Pair* pair = int_table.findPairWithCustomKey<IntClass,IntClassHasher,IntClassComparator> (IntClass(50));
if (pair)
{
printf ("pair found.... [%d]\n", pair->value);
}
*/
printf ("%p %p\n", int_table.search (60), int_table.search (90));
printf ("%d %d\n", int_table.remove (60), int_table.remove (60));
printf ("%d %d\n", int_table.remove (70), int_table.remove (70));
/*
printf ("%d\n", int_table[90]);
printf ("%d\n", int_table[9990]);
*/
//Str1Table s1 (IntTable::DEFAULT_CAPACITY, IntTable::DEFAULT_LOAD_FACTOR, 1000);
Str1Table s1 (IntTable::DEFAULT_CAPACITY, IntTable::DEFAULT_LOAD_FACTOR, 0);
//Str1Table s1;
s1.insert ("hello", 20);
s1.insert ("hello", 20);
s1.insert ("hello kara", 20);
s1.insert ("this is good", 3896);
printf ("remove = %d\n", s1.remove ("hello"));
printf ("remove = %d\n", s1.remove ("hello"));
for (int i = 0; i < 100; i++)
{
char buf[128];
sprintf (buf, "surukaaaa %d", i);
s1.insert (buf, i * 2);
}
printf ("%d\n", (int)s1.getSize());
for (Str1Table::Iterator it = s1.getIterator(); it.isLegit(); it++)
{
Str1Table::Pair& pair = *it;
printf ("[%s] [%d]\n", pair.key.c_str(), pair.value);
}
printf ("------------------\n");
{
Str1Table s11 (s1);
for (Str1Table::Iterator it = s11.getIterator(); it.isLegit(); it++)
{
Str1Table::Pair& pair = *it;
printf ("[%s] [%d]\n", pair.key.c_str(), pair.value);
}
}
printf ("------------------\n");
{
Str1Table s11 (IntTable::DEFAULT_CAPACITY, IntTable::DEFAULT_LOAD_FACTOR, 0, &heap_mmgr);
//Str1Table s11 (IntTable::DEFAULT_CAPACITY, IntTable::DEFAULT_LOAD_FACTOR, 200);
//Str1Table s11;
for (int i = 0; i < 100; i++)
{
char buf[128];
sprintf (buf, "abiyo %d", i);
s11.insert (buf, i * 2);
}
printf ("%d\n", s11.heteroremove<const char*, cstr_hasher, cstr_is_equal> ("abiyo 12"));
printf ("%d\n", s11.heteroremove<const char*, cstr_hasher, cstr_is_equal> ("abiyo 12"));
printf ("SIZE => %d\n", (int)s11.getSize());
for (Str1Table::Iterator it = s11.getIterator(); it.isLegit(); it++)
{
Str1Table::Pair& pair = *it;
printf ("[%s] [%d]\n", pair.key.c_str(), pair.value);
}
printf ("------------------\n");
//s11.clear (true);
s11 = s1;
printf ("SIZE => %d\n", (int)s11.getSize());
for (Str1Table::Iterator it = s11.getIterator(); it.isLegit(); it++)
{
Str1Table::Pair& pair = *it;
printf ("[%s] [%d]\n", pair.key.c_str(), pair.value);
}
printf ("-------------------\n");
Str1Table::Pair* pair = s11.heterosearch<const char*, cstr_hasher, cstr_is_equal> ("surukaaaa 13");
if (pair) printf ("%d\n", pair->value);
else printf ("not found\n");
s11.update ("surukaaaa 13", 999);
s11.update ("surukaaaa 16", 99999);
s11.inject ("surukaaaa 18", 999999, 1);
pair = s11.heterosearch<const char*, cstr_hasher, cstr_is_equal> ("surukaaaa 13");
if (pair) printf ("%d\n", pair->value);
else printf ("not found\n");
s1 = s11;
}
printf ("-------------------\n");
printf ("%d %d\n", (int)s1.getSize(), (int)s1.getCapacity());
for (Str1Table::ConstIterator it = s1.getConstIterator(); it.isLegit(); it++)
{
const Str1Table::Pair& pair = *it;
printf ("[%s] [%d]\n", pair.key.c_str(), pair.value);
}
printf ("-------------------\n");
for (Str1Table::PairNode* np = s1.getTailNode(); np; np = np->getPrevNode())
{
Str1Table::Pair& pair = np->value;
printf ("[%s] [%d]\n", pair.key.c_str(), pair.value);
}
// qse_close_stdsios ();
return 0;
}

220
samples/cmn/htl01.c Normal file
View File

@ -0,0 +1,220 @@
#include <qse/cmn/mem.h>
#include <qse/cmn/str.h>
#include <qse/cmn/htl.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)
typedef struct item_t item_t;
struct item_t
{
long a;
long x;
long y;
};
static qse_htl_walk_t walk1 (qse_htl_t* htl, void* data, void* ctx)
{
qse_printf (QSE_T("[%ld]\n"), *(long*)data);
return QSE_HTL_WALK_FORWARD;
}
static qse_htl_walk_t walk2 (qse_htl_t* htl, void* data, void* ctx)
{
item_t* item = (item_t*)data;
qse_printf (QSE_T("a [%ld] x [%ld] y [%ld]\n"), item->a, item->x, item->y);
return QSE_HTL_WALK_FORWARD;
}
static qse_size_t hash_item (qse_htl_t* htl, const void* data)
{
item_t* item = (item_t*)data;
//return item->a + 123445;
return qse_genhash32 (&item->a, QSE_SIZEOF(item->a));
}
static int comp_item (qse_htl_t* htl, const void* data1, const void* data2)
{
return ((item_t*)data1)->a != ((item_t*)data2)->a;
}
static void* copy_item (qse_htl_t* htl, void* data)
{
item_t* x = (item_t*)QSE_MMGR_ALLOC (htl->mmgr, QSE_SIZEOF(item_t));
if (x) *x = *(item_t*)data;
return x;
}
static void free_item (qse_htl_t* htl, void* data)
{
QSE_MMGR_FREE (htl->mmgr, data);
}
static void* copy_long (qse_htl_t* htl, void* data)
{
long* x = (long*)QSE_MMGR_ALLOC (htl->mmgr, QSE_SIZEOF(long));
if (x) *x = *(long*)data;
return x;
}
static void free_long (qse_htl_t* htl, void* data)
{
QSE_MMGR_FREE (htl->mmgr, data);
}
static int test1 ()
{
long x;
qse_htl_t* htl;
qse_htl_node_t* np;
htl = qse_htl_open (QSE_MMGR_GETDFL(), 0, QSE_SIZEOF(x));
if (htl == QSE_NULL)
{
qse_printf (QSE_T("failed to open a table\n"));
return -1;
}
qse_htl_setcopier (htl, copy_long);
qse_htl_setfreeer (htl, free_long);
for (x = 9; x < 20; x++)
{
if (qse_htl_insert(htl, &x))
{
qse_printf (QSE_T("SUCCESS: inserted %ld\n"), x);
}
else
{
qse_printf (QSE_T("FAILURE: failed to insert %ld\n"), x);
}
}
x = 10;
if ((np = qse_htl_search(htl, &x)))
{
qse_printf (QSE_T("SUCCESS: found %ld\n"), *(long*)np->data);
QSE_ASSERT (*(long*)np->data == x);
}
else
{
qse_printf (QSE_T("FAILURE: failed to found %ld\n"), x);
}
x = 10;
if (qse_htl_delete(htl, &x) == 0)
{
qse_printf (QSE_T("SUCCESS: deleted %ld\n"), x);
}
else
{
qse_printf (QSE_T("FAILURE: failed to delete %ld\n"), x);
}
x = 10;
qse_printf (QSE_T("searching for %ld\n"), x);
np = qse_htl_search(htl, &x);
QSE_ASSERT (np == QSE_NULL);
if (np)
{
qse_printf (QSE_T("FAILURE: found something that must not be found - %ld\n"), x);
}
qse_printf (QSE_T("total %lu items\n"), (unsigned long)qse_htl_getsize(htl));
qse_htl_walk (htl, walk1, QSE_NULL);
qse_htl_close (htl);
return 0;
}
static int test2 ()
{
item_t x;
qse_htl_t* htl;
qse_htl_node_t* np;
int count = 0;
htl = qse_htl_open (QSE_MMGR_GETDFL(), 0, QSE_SIZEOF(x));
if (htl == QSE_NULL)
{
qse_printf (QSE_T("failed to open a table\n"));
return -1;
}
qse_htl_sethasher (htl, hash_item);
qse_htl_setcomper (htl, comp_item);
qse_htl_setcopier (htl, copy_item);
qse_htl_setfreeer (htl, free_item);
again:
for (x.a = 9; x.a < 20; x.a++)
{
x.x = x.a * 10;
x.y = x.a * 100;
if (qse_htl_insert(htl, &x))
{
qse_printf (QSE_T("SUCCESS: inserted %ld\n"), x.a);
}
else
{
qse_printf (QSE_T("FAILURE: failed to insert %ld\n"), x.a);
}
}
x.a = 10;
if ((np = qse_htl_search(htl, &x)))
{
qse_printf (QSE_T("SUCCESS: found %ld\n"), *(long*)np->data);
QSE_ASSERT (*(long*)np->data == x.a);
}
else
{
qse_printf (QSE_T("FAILURE: failed to found %ld\n"), x.a);
}
x.a = 10;
if (qse_htl_delete(htl, &x) == 0)
{
qse_printf (QSE_T("SUCCESS: deleted %ld\n"), x.a);
}
else
{
qse_printf (QSE_T("FAILURE: failed to delete %ld\n"), x.a);
}
x.a = 10;
qse_printf (QSE_T("searching for %ld\n"), x.a);
np = qse_htl_search(htl, &x);
QSE_ASSERT (np == QSE_NULL);
if (np)
{
qse_printf (QSE_T("FAILURE: found something that must not be found - %ld\n"), x.a);
}
qse_printf (QSE_T("total %lu items\n"), (unsigned long)qse_htl_getsize(htl));
qse_htl_walk (htl, walk2, QSE_NULL);
qse_htl_clear (htl);
if (count == 0)
{
count++;
qse_printf (QSE_T("<<<TRAYING AGAIN ....>>>\n"));
goto again;
}
qse_htl_close (htl);
return 0;
}
int main ()
{
qse_open_stdsios ();
R (test1);
R (test2);
qse_close_stdsios ();
return 0;
}

215
samples/cmn/ipad01.c Normal file
View File

@ -0,0 +1,215 @@
#include <qse/cmn/ipad.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_ip4ad (void)
{
qse_ip4ad_t ip4ad;
qse_char_t buf[32];
qse_mchar_t mbsbuf[32];
qse_wchar_t wcsbuf[32];
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")
};
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")
};
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")
};
for (i = 0; i < QSE_COUNTOF(ipstr); i++)
{
if (qse_strtoip4ad (ipstr[i], &ip4ad) <= -1)
{
qse_printf (QSE_T("Failed to convert %s\n"), ipstr[i]);
}
else
{
qse_ip4adtostr (&ip4ad, buf, QSE_COUNTOF(buf));
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_mbstoip4ad (ipstr_mbs[i], &ip4ad) <= -1)
{
qse_printf (QSE_T("Failed to convert %hs\n"), ipstr_mbs[i]);
}
else
{
qse_ip4adtombs (&ip4ad, mbsbuf, QSE_COUNTOF(mbsbuf));
qse_printf (QSE_T("Converted [%hs] to [%hs]\n"), ipstr[i], mbsbuf);
}
}
qse_printf (QSE_T("-------------------\n"));
for (i = 0; i < QSE_COUNTOF(ipstr_wcs); i++)
{
if (qse_wcstoip4ad (ipstr_wcs[i], &ip4ad) <= -1)
{
qse_printf (QSE_T("Failed to convert %ls\n"), ipstr_wcs[i]);
}
else
{
qse_ip4adtowcs (&ip4ad, wcsbuf, QSE_COUNTOF(wcsbuf));
qse_printf (QSE_T("Converted [%ls] to [%ls]\n"), ipstr[i], wcsbuf);
}
}
return 0;
}
static int test_ip6ad (void)
{
qse_ip6ad_t ip6ad;
qse_char_t buf[32];
qse_mchar_t mbsbuf[32];
qse_wchar_t wcsbuf[32];
qse_size_t i;
static qse_char_t* ipstr[] =
{
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")
};
static qse_mchar_t* ipstr_mbs[] =
{
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")
};
static qse_wchar_t* ipstr_wcs[] =
{
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")
};
for (i = 0; i < QSE_COUNTOF(ipstr); i++)
{
if (qse_strtoip6ad (ipstr[i], &ip6ad) <= -1)
{
qse_printf (QSE_T("Failed to convert %s\n"), ipstr[i]);
}
else
{
qse_ip6adtostr (&ip6ad, buf, QSE_COUNTOF(buf));
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_mbstoip6ad (ipstr_mbs[i], &ip6ad) <= -1)
{
qse_printf (QSE_T("Failed to convert %hs\n"), ipstr_mbs[i]);
}
else
{
qse_ip6adtombs (&ip6ad, mbsbuf, QSE_COUNTOF(mbsbuf));
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_wcstoip6ad (ipstr_wcs[i], &ip6ad) <= -1)
{
qse_printf (QSE_T("Failed to convert %ls\n"), ipstr_wcs[i]);
}
else
{
qse_ip6adtowcs (&ip6ad, wcsbuf, QSE_COUNTOF(wcsbuf));
qse_printf (QSE_T("Converted [%ls] to [%ls]\n"), ipstr_wcs[i], wcsbuf);
}
}
return 0;
}
static int test_main (int argc, qse_char_t* argv[])
{
test_ip4ad ();
qse_printf (QSE_T("==============\n"));
test_ip6ad ();
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;
}

51
samples/cmn/main01.c Normal file
View File

@ -0,0 +1,51 @@
#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[])
{
int i;
for (i = 0; i < argc; i++)
{
qse_printf (QSE_T("%d => [%s]\n"), i, argv[i]);
}
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;
}

56
samples/cmn/main02.c Normal file
View File

@ -0,0 +1,56 @@
#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_char_t* envp[])
{
int i;
for (i = 0; i < argc; i++)
{
qse_printf (QSE_T("ARG %d => [%s]\n"), i, argv[i]);
}
for (i = 0; envp[i]; i++)
{
qse_printf (QSE_T("ENV %d => [%s]\n"), i, envp[i]);
}
return 0;
}
int qse_main (int argc, qse_achar_t* argv[], qse_achar_t* envp[])
{
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_with_env (argc, argv, envp, test_main);
qse_close_stdsios ();
return x;
}

247
samples/cmn/mbwc01.c Normal file
View File

@ -0,0 +1,247 @@
#include <qse/cmn/mbwc.h>
#include <qse/cmn/mem.h>
#include <qse/cmn/str.h>
#include <qse/si/sio.h>
#include <locale.h>
#if defined(_WIN32)
# include <windows.h>
#endif
#define R(f) \
do { \
qse_printf (QSE_T("== %s ==\n"), QSE_T(#f)); \
if (f() == -1) return -1; \
} while (0)
/* the texts here are all in utf-8. you will see many errors
* on a non-utf8 locale */
static int test1 (void)
{
int i;
const qse_mchar_t* x[] =
{
QSE_MT(""),
QSE_MT("This is good."),
QSE_MT("이거슨"),
QSE_MT("뭐냐이거"),
QSE_MT("過去一個月"),
QSE_MT("是成功的建商"),
QSE_MT("뛰어 올라봐. 멀리멀리 잘난척하기는"),
QSE_MT("Fly to the universe. eat my shorts.")
};
qse_wchar_t buf[5];
qse_wchar_t buf2[50];
for (i = 0; i < QSE_COUNTOF(x); i++)
{
int n;
qse_size_t wlen = QSE_COUNTOF(buf), wlen1;
qse_size_t mlen, mlen1;
n = qse_mbstowcs (x[i], &mlen, buf, &wlen);
qse_printf (QSE_T("[%hs]=>"), x[i]);
qse_mbstowcs (x[i], &mlen1, QSE_NULL, &wlen1);
/* the string should not be properly null terminated
* if buf is shorter than the necesary buffer size needed for
* full conversion */
qse_wcsxncpy (buf2, QSE_COUNTOF(buf2), buf, wlen);
qse_printf (QSE_T("%s %d chars %d bytes (IF FULL %d chars %d bytes) [%ls]\n"),
((n == -3)? QSE_T("INCOMPLETE-SEQ"): (n == -2)? QSE_T("BUFFER-SMALL"): (n == -1)? QSE_T("ILLEGAL-CHAR"): QSE_T("FULL")),
(int)wlen, (int)mlen, (int)wlen1, (int)mlen1, buf2);
qse_fflush (QSE_STDOUT);
}
qse_printf (QSE_T("-----------------\n"));
for (i = 0; i < QSE_COUNTOF(x); i++)
{
int n;
qse_size_t wlen = QSE_COUNTOF(buf2), wlen1;
qse_size_t mlen, mlen1;
n = qse_mbstowcs (x[i], &mlen, buf2, &wlen);
qse_mbstowcs (x[i], &mlen1, QSE_NULL, &wlen1);
qse_printf (QSE_T("%s %d chars %d bytes (IF FULL %d chars %d bytes) [%.*ls]\n"),
((n == -3)? QSE_T("INCOMPLETE-SEQ"): (n == -2)? QSE_T("BUFFER-SMALL"): (n == -1)? QSE_T("ILLEGAL-CHAR"): QSE_T("FULL")),
(int)wlen, (int)mlen, (int)wlen, (int)wlen1, (int)mlen1, buf2);
qse_fflush (QSE_STDOUT);
}
return 0;
}
static int test2 (void)
{
int i;
const qse_mchar_t* x[] =
{
QSE_MT("\0\0\0"),
QSE_MT("This is good."),
QSE_MT("이거슨"),
QSE_MT("뭐냐이거"),
QSE_MT("過去一個月"),
QSE_MT("是成功的建商"),
QSE_MT("뛰어 올라봐. 멀리멀리 잘난척하기는"),
QSE_MT("Fly to the universe")
};
for (i = 0; i < QSE_COUNTOF(x); i++)
{
qse_size_t k = qse_mbslen(x[i]);
qse_size_t j = 0;
qse_wchar_t buf[2];
qse_size_t wlen, mlen;
int iter = 0;
if (k == 0) k = 3; /* for x[0] */
mlen = k;
qse_mbsntowcsn (&x[i][j], &mlen, QSE_NULL, &wlen);
qse_printf (QSE_T("expecting %d chars, %d bytes ["), wlen, mlen);
while (j < k)
{
int y;
wlen = QSE_COUNTOF(buf);
mlen = k - j;
y = qse_mbsntowcsn (&x[i][j], &mlen, buf, &wlen);
if (y <= -1 && y != -2)
{
qse_printf (QSE_T("***illegal or incomplete sequence***"));
break;
}
if (wlen > 0 && buf[0] == QSE_T('\0'))
{
while (wlen > 0 && buf[--wlen] == QSE_T('\0'))
{
qse_printf (QSE_T("\\0"));
}
}
else
{
qse_printf (QSE_T("%.*s"), (int)wlen, buf);
}
j += mlen;
iter++;
}
qse_printf (QSE_T("] => %d bytes, %d iterations\n"), (int)k, (int)iter);
}
return 0;
}
static int test3 (void)
{
int i;
const qse_mchar_t* x[] =
{
QSE_MT(""),
QSE_MT("This is good."),
QSE_MT("이거슨"),
QSE_MT("뭐냐이거"),
QSE_MT("過去一個月"),
QSE_MT("是成功的建商"),
QSE_MT("뛰어 올라봐. 멀리멀리 잘난척하기는"),
QSE_MT("Fly to the universe")
};
for (i = 0; i < QSE_COUNTOF(x); i++)
{
qse_wchar_t* wcs;
wcs = qse_mbstowcsdup (x[i], QSE_NULL, QSE_MMGR_GETDFL());
if (wcs == QSE_NULL)
{
qse_printf (QSE_T("[ERROR]"));
}
else
{
qse_printf (QSE_T("[%ls]\n"), wcs);
QSE_MMGR_FREE (QSE_MMGR_GETDFL(), wcs);
}
}
return 0;
}
static int test4 (void)
{
const qse_mchar_t* x[] =
{
QSE_MT("\0\0\0"),
QSE_MT("This is good."),
QSE_MT("이거슨"),
QSE_MT("뭐냐이거"),
QSE_MT("過去一個月"),
QSE_MT("是成功的建商"),
QSE_MT("뛰어 올라봐. 멀리멀리 잘난척하기는"),
QSE_MT("Fly to the universe"),
QSE_NULL
};
qse_wchar_t* wcs;
wcs = qse_mbsatowcsdup (x, QSE_NULL, QSE_MMGR_GETDFL());
if (wcs == QSE_NULL)
{
qse_printf (QSE_T("[ERROR]\n"));
}
else
{
qse_printf (QSE_T("[%ls]\n"), wcs);
QSE_MMGR_FREE (QSE_MMGR_GETDFL(), wcs);
}
return 0;
}
int main ()
{
#if defined(_WIN32)
char 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 ();
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);
qse_close_stdsios ();
return 0;
}

326
samples/cmn/mbwc02.c Normal file
View File

@ -0,0 +1,326 @@
#include <qse/cmn/mbwc.h>
#include <qse/cmn/mem.h>
#include <qse/cmn/str.h>
#include <qse/si/sio.h>
#include <locale.h>
#if defined(_WIN32)
# include <windows.h>
#endif
#define R(f) \
do { \
qse_printf (QSE_T("== %s ==\n"), QSE_T(#f)); \
if (f() == -1) return -1; \
} while (0)
static int test1 (void)
{
const qse_wchar_t unistr[] =
{
/*L"\uB108 \uBB50\uAC00 \uC798\uB0AC\uC5B4?",*/
0xB108,
L' ',
0xBB50,
0xAC00,
L' ',
0xC798,
0xB0AC,
0xC5B4,
L'?',
L'\0'
};
const qse_wchar_t* x[] =
{
L"",
L"",
L"Fly to the universe, kick your ass"
};
qse_mchar_t buf[10];
qse_mchar_t buf2[100];
int i;
x[1] = unistr;
for (i = 0; i < QSE_COUNTOF(x); i++)
{
int n;
qse_size_t wlen, mlen;
n = qse_wcstombs (x[i], &wlen, QSE_NULL, &mlen);
qse_printf (QSE_T("[%ls] n=%d, wcslen()=%d, wlen=%d, mlen=%d\n"),
x[i], (int)n, (int)qse_wcslen(x[i]), (int)wlen, (int)mlen);
}
qse_printf (QSE_T("-----------------\n"));
for (i = 0; i < QSE_COUNTOF(x); i++)
{
int n;
qse_size_t wlen, mlen;
mlen = QSE_COUNTOF(buf) - 1;
qse_mbsset (buf, QSE_MT('A'), QSE_COUNTOF(buf));
n = qse_wcstombs (x[i], &wlen, buf, &mlen);
QSE_ASSERT (buf[QSE_COUNTOF(buf)-1] == QSE_MT('A'));
buf[QSE_COUNTOF(buf)-1] = QSE_MT('\0');
qse_printf (QSE_T("%s chars=%d bytes=%d [%hs]\n"),
((n == -2)? QSE_T("BUFFER-SMALL"): (n == -1)? QSE_T("ILLEGAL-CHAR"): QSE_T("FULL")),
(int)wlen, (int)mlen, buf);
}
qse_printf (QSE_T("-----------------\n"));
for (i = 0; i < QSE_COUNTOF(x); i++)
{
int n;
qse_size_t wlen, mlen;
mlen = QSE_COUNTOF(buf2);
n = qse_wcstombs (x[i], &wlen, buf2, &mlen);
qse_printf (QSE_T("%s chars=%d bytes=%d [%hs]\n"),
((n == -2)? QSE_T("BUFFER-SMALL"): (n == -1)? QSE_T("ILLEGAL-CHAR"): QSE_T("FULL")),
(int)wlen, (int)mlen, buf2);
}
qse_printf (QSE_T("-----------------\n"));
for (i = 0; i < QSE_COUNTOF(x); i++)
{
int n;
const qse_wchar_t* p = x[i];
while (*p)
{
qse_size_t wlen, mlen;
mlen = QSE_COUNTOF(buf) - 1;
n = qse_wcstombs (p, &wlen, buf, &mlen);
qse_printf (QSE_T("%s chars=%d bytes=%d [%hs]\n"),
((n == -2)? QSE_T("BUFFER-SMALL"): (n == -1)? QSE_T("ILLEGAL-CHAR"): QSE_T("FULL")),
(int)wlen, (int)mlen, buf);
p += wlen;
}
}
return 0;
}
static int test2 (void)
{
const qse_wchar_t unistr[] =
{
/*L"\uB108 \uBB50\uAC00 \uC798\uB0AC\uC5B4?",*/
0xB108,
L' ',
0xBB50,
0xAC00,
L' ',
0xC798,
0xB0AC,
0xC5B4,
L'?',
L'\0'
};
const qse_wchar_t* x[] =
{
L"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0",
L"",
L"Fly to the universe, kick your ass"
};
qse_mchar_t buf[9];
int i, j;
x[1] = unistr;
for (i = 0; i < QSE_COUNTOF(x); i++)
{
qse_size_t len, wlen, mlen, iter;
int n;
len = qse_wcslen(x[i]);
if (len == 0) len = 20; /* for x[0] */
qse_printf (QSE_T("Converting %d wide-character - "), (int)len);
wlen = len;
n = qse_wcsntombsn (x[i], &wlen, QSE_NULL, &mlen);
if (n == -1)
{
qse_printf (QSE_T("***illegal character[mlen=%d/wlen=%d]*** ["), (int)mlen, (int)wlen);
}
else
{
/* -2 is never returned if i don't provide the multibyte buffer */
qse_printf (QSE_T("%d multibyte characters to be produced from %d wide characters ["), (int)mlen, (int)wlen);
}
for (j = 0, iter = 0; j < len; j += wlen)
{
iter = iter + 1;
wlen = len - j;
mlen = QSE_COUNTOF(buf);
n = qse_wcsntombsn (&x[i][j], &wlen, buf, &mlen);
if (n == -1)
{
qse_printf (QSE_T("***illegal character*** wlen=%d/mlen=%d/n=%d"), (int)(len-j), (int)mlen, (int)n);
break;
}
#if 0
else if (n == -2)
{
qse_printf (QSE_T("***buffer not large*** wlen=%d/mlen=%d/n=%d"), (int)(len-j), (int)mlen, (int)n);
break;
}
#endif
if (mlen > 0 && buf[0] == QSE_T('\0'))
{
while (mlen > 0 && buf[--mlen] == QSE_T('\0'))
{
qse_printf (QSE_T("\\0"));
}
}
else
{
#if defined(QSE_CHAR_IS_MCHAR) || defined(_WIN32)
qse_printf (QSE_T("%.*hs"), (int)mlen, buf);
#else
/* at least on linux and macosx, wprintf seems
* to taks preceision as the number of wide
* characters with %s. */
/* THIS HACK SHOULD BE REMOVED ONCE I IMPLEMENTE
* MY OWN qse_printf */
qse_printf (QSE_T("%.*hs"), (int)wlen, buf);
#endif
}
}
qse_printf (QSE_T("] => %d iterations\n"), (int)iter);
}
return 0;
}
static int test3 (void)
{
const qse_wchar_t unistr[] =
{
/*L"\uB108 \uBB50\uAC00 \uC798\uB0AC\uC5B4?",*/
0xB108,
L' ',
0xBB50,
0xAC00,
L' ',
0xC798,
0xB0AC,
0xC5B4,
L'?',
L'\0'
};
const qse_wchar_t* x[] =
{
L"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0",
L"",
L"Fly to the universe, kick your ass"
};
int i;
x[1] = unistr;
for (i = 0; i < QSE_COUNTOF(x); i++)
{
qse_mchar_t* m = qse_wcstombsdup (x[i], QSE_NULL, QSE_MMGR_GETDFL());
if (m == QSE_NULL)
{
qse_printf (QSE_T("[ERROR]\n"), m);
}
else
{
qse_printf (QSE_T("[%hs]\n"), m);
}
QSE_MMGR_FREE (QSE_MMGR_GETDFL(), m);
}
return 0;
}
static int test4 (void)
{
const qse_wchar_t unistr[] =
{
/*L"\uB108 \uBB50\uAC00 \uC798\uB0AC\uC5B4?",*/
0xB108,
L' ',
0xBB50,
0xAC00,
L' ',
0xC798,
0xB0AC,
0xC5B4,
L'?',
L'\0'
};
const qse_wchar_t* x[] =
{
L"Good morning angels",
L"",
L"Fly to the universe, kick your ass",
QSE_NULL
};
qse_mchar_t* m;
x[1] = unistr;
m = qse_wcsatombsdup (x, QSE_NULL, QSE_MMGR_GETDFL());
if (m == QSE_NULL)
{
qse_printf (QSE_T("[ERROR]\n"), m);
}
else
{
qse_printf (QSE_T("[%hs]\n"), m);
}
QSE_MMGR_FREE (QSE_MMGR_GETDFL(), m);
return 0;
}
int main ()
{
#if defined(_WIN32)
char 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 ();
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);
qse_close_stdsios ();
return 0;
}

131
samples/cmn/oht.c Normal file
View File

@ -0,0 +1,131 @@
#include <qse/cmn/mem.h>
#include <qse/cmn/str.h>
#include <qse/cmn/oht.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)
typedef struct item_t item_t;
struct item_t
{
long a;
long x;
long y;
};
static qse_oht_walk_t walk1 (qse_oht_t* oht, void* data, void* ctx)
{
qse_printf (QSE_T("[%ld]\n"), *(long*)data);
return QSE_OHT_WALK_FORWARD;
}
static qse_oht_walk_t walk2 (qse_oht_t* oht, void* data, void* ctx)
{
item_t* item = (item_t*)data;
qse_printf (QSE_T("a [%ld] x [%ld] y [%ld]\n"), item->a, item->x, item->y);
return QSE_OHT_WALK_FORWARD;
}
static qse_size_t hash (qse_oht_t* oht, const void* data)
{
item_t* item = (item_t*)data;
return item->a + 123445;
}
static int comp (qse_oht_t* oht, const void* data1, const void* data2)
{
return ((item_t*)data1)->a != ((item_t*)data2)->a;
}
static int test1 ()
{
long x;
qse_oht_t* oht;
oht = qse_oht_open (QSE_MMGR_GETDFL(), 0, QSE_SIZEOF(x), 10, 5);
if (oht == QSE_NULL)
{
qse_printf (QSE_T("failed to open a table\n"));
return -1;
}
for (x = 9; x < 20; x++)
{
qse_printf (QSE_T("inserting %ld => %lu\n"),
x, (unsigned long)qse_oht_insert (oht, &x));
}
x = 10;
qse_printf (QSE_T("searching for %ld => %lu\n"),
x, (unsigned long)qse_oht_search (oht, &x));
x = 10;
qse_printf (QSE_T("deleting %ld => %lu\n"),
x, (unsigned long)qse_oht_delete (oht, &x));
x = 10;
qse_printf (QSE_T("searching for %ld => %lu\n"),
x, (unsigned long)qse_oht_search (oht, &x));
qse_printf (QSE_T("total %lu items\n"), (unsigned long)QSE_OHT_SIZE(oht));
qse_oht_walk (oht, walk1, QSE_NULL);
qse_oht_close (oht);
return 0;
}
static int test2 ()
{
item_t x;
qse_oht_t* oht;
oht = qse_oht_open (QSE_MMGR_GETDFL(), 0, QSE_SIZEOF(x), 10, 10);
if (oht == QSE_NULL)
{
qse_printf (QSE_T("failed to open a table\n"));
return -1;
}
qse_oht_sethasher (oht, hash);
qse_oht_setcomper (oht, comp);
for (x.a = 9; x.a < 20; x.a++)
{
x.x = x.a * 10;
x.y = x.a * 100;
qse_printf (QSE_T("inserting %ld => %lu\n"),
x.a, (unsigned long)qse_oht_insert (oht, &x));
}
x.a = 10;
qse_printf (QSE_T("searching for %ld => %lu\n"),
x.a, (unsigned long)qse_oht_search (oht, &x));
x.a = 10;
qse_printf (QSE_T("deleting %ld => %lu\n"),
x.a, (unsigned long)qse_oht_delete (oht, &x));
x.a = 10;
qse_printf (QSE_T("searching for %ld => %lu\n"),
x.a, (unsigned long)qse_oht_search (oht, &x));
qse_printf (QSE_T("total %lu items\n"), (unsigned long)QSE_OHT_SIZE(oht));
qse_oht_walk (oht, walk2, QSE_NULL);
qse_oht_close (oht);
return 0;
}
int main ()
{
qse_open_stdsios ();
R (test1);
R (test2);
qse_close_stdsios ();
return 0;
}

94
samples/cmn/path01.c Normal file
View File

@ -0,0 +1,94 @@
#include <qse/cmn/path.h>
#include <qse/cmn/main.h>
#include <qse/cmn/mbwc.h>
#include <qse/cmn/str.h>
#include <qse/cmn/mem.h>
#include <qse/si/sio.h>
#include <locale.h>
#if defined(_WIN32)
# include <windows.h>
#endif
static int path_main (int argc, qse_char_t* argv[])
{
qse_char_t* canon;
qse_size_t len;
int i;
int options[] =
{
0,
QSE_CANONPATH_EMPTYSINGLEDOT,
QSE_CANONPATH_KEEPDOUBLEDOTS,
QSE_CANONPATH_DROPTRAILINGSEP,
QSE_CANONPATH_EMPTYSINGLEDOT | QSE_CANONPATH_KEEPDOUBLEDOTS,
QSE_CANONPATH_EMPTYSINGLEDOT | QSE_CANONPATH_DROPTRAILINGSEP,
QSE_CANONPATH_KEEPDOUBLEDOTS | QSE_CANONPATH_DROPTRAILINGSEP,
QSE_CANONPATH_EMPTYSINGLEDOT | QSE_CANONPATH_KEEPDOUBLEDOTS | QSE_CANONPATH_DROPTRAILINGSEP
};
if (argc != 2)
{
qse_fprintf (QSE_STDERR, QSE_T("Usage: %s <directory>\n"), argv[0]);
return -1;
}
canon = QSE_MMGR_ALLOC (
QSE_MMGR_GETDFL(), (qse_strlen(argv[1]) + 1) * QSE_SIZEOF(*canon));
if (!canon)
{
QSE_MMGR_FREE (QSE_MMGR_GETDFL(), canon);
qse_fprintf (QSE_STDERR, QSE_T("Error: out of memory\n"));
return -1;
}
for (i = 0; i < QSE_COUNTOF(options); i++)
{
len = qse_canonpath (argv[1], canon, options[i]);
qse_printf (QSE_T("OPT[%c%c%c] [%s]: [%5d] [%s]\n"),
((options[i] & QSE_CANONPATH_EMPTYSINGLEDOT)? QSE_T('E'): QSE_T(' ')),
((options[i] & QSE_CANONPATH_KEEPDOUBLEDOTS)? QSE_T('K'): QSE_T(' ')),
((options[i] & QSE_CANONPATH_DROPTRAILINGSEP)? QSE_T('D'): QSE_T(' ')),
argv[1], (int)len, canon);
}
QSE_MMGR_FREE (QSE_MMGR_GETDFL(), canon);
#if 0
qse_printf (QSE_T("[%s] => "), argv[1]);
len = qse_canonpath (argv[1], argv[1], 0);
qse_printf (QSE_T("[%s] %d chars\n"), argv[1], (int)len);
#endif
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, path_main);
qse_close_stdsios ();
return x;
}

96
samples/cmn/pma.c Normal file
View File

@ -0,0 +1,96 @@
#include <qse/cmn/pma.h>
#include <qse/cmn/mem.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 ()
{
int i;
int* ptr[100];
qse_pma_t* pma = qse_pma_open (QSE_MMGR_GETDFL(), 0);
if (pma == QSE_NULL)
{
qse_printf (QSE_T("cannot open pma\n"));
return -1;
}
for (i = 0; i < 100; i++)
{
ptr[i] = qse_pma_alloc (pma, sizeof(int));
if (ptr[i])
{
qse_printf (QSE_T("%d %p\n"), i, ptr[i]);
*(ptr[i]) = i;
}
else qse_printf (QSE_T("%d FAIL\n"), i);
}
for (i = 0; i < 100; i++)
{
ptr[i] = qse_pma_alloc (pma, sizeof(int));
if (ptr[i])
{
qse_printf (QSE_T("%d %p\n"), i, ptr[i]);
*(ptr[i]) = i;
}
else qse_printf (QSE_T("%d FAIL\n"), i);
}
qse_pma_close (pma);
return 0;
}
static int test2 ()
{
int i;
int* ptr[100];
qse_pma_t* pma = qse_pma_open (QSE_MMGR_GETDFL(), 0);
if (pma == QSE_NULL)
{
qse_printf (QSE_T("cannot open pma\n"));
return -1;
}
for (i = 0; i < 100; i++)
{
ptr[i] = qse_pma_alloc (pma, sizeof(int));
if (ptr[i])
{
qse_printf (QSE_T("%d %p\n"), i, ptr[i]);
*(ptr[i]) = i;
}
else qse_printf (QSE_T("%d FAIL\n"), i);
}
qse_pma_clear (pma);
for (i = 0; i < 100; i++)
{
ptr[i] = qse_pma_alloc (pma, sizeof(int));
if (ptr[i])
{
qse_printf (QSE_T("%d %p\n"), i, ptr[i]);
*(ptr[i]) = i;
}
else qse_printf (QSE_T("%d FAIL\n"), i);
}
qse_pma_close (pma);
return 0;
}
int main ()
{
qse_open_stdsios ();
R (test1);
R (test2);
qse_close_stdsios ();
return 0;
}

232
samples/cmn/rbt01.c Normal file
View File

@ -0,0 +1,232 @@
#include <qse/cmn/mem.h>
#include <qse/cmn/str.h>
#include <qse/cmn/rbt.h>
#include <qse/si/sio.h>
#include <stdlib.h>
#define R(f) \
do { \
qse_printf (QSE_T("== %s ==\n"), QSE_T(#f)); \
if (f() == -1) return -1; \
} while (0)
static qse_rbt_walk_t walk (qse_rbt_t* rbt, qse_rbt_pair_t* pair, void* ctx)
{
qse_printf (QSE_T("key = %d, value = %d\n"),
*(int*)QSE_RBT_KPTR(pair), *(int*)QSE_RBT_VPTR(pair));
return QSE_RBT_WALK_FORWARD;
}
static qse_rbt_walk_t walk2 (qse_rbt_t* rbt, qse_rbt_pair_t* pair, void* ctx)
{
qse_printf (QSE_T("key = %.*s, value = %.*s\n"),
(int)QSE_RBT_KLEN(pair),
QSE_RBT_KPTR(pair),
(int)QSE_RBT_VLEN(pair),
QSE_RBT_VPTR(pair));
return QSE_RBT_WALK_FORWARD;
}
static int test1 ()
{
qse_rbt_t* s1;
int i;
s1 = qse_rbt_open (QSE_MMGR_GETDFL(), 0, 1, 1);
if (s1 == QSE_NULL)
{
qse_printf (QSE_T("cannot open a table\n"));
return -1;
}
qse_rbt_setstyle (s1, qse_getrbtstyle(QSE_RBT_STYLE_INLINE_COPIERS));
for (i = 0; i < 20; i++)
{
int x = i * 20;
qse_printf (QSE_T("inserting at %d\n"), i);
qse_rbt_insert (s1, &i, QSE_SIZEOF(i), &x, QSE_SIZEOF(x));
}
qse_rbt_rwalk (s1, walk, QSE_NULL);
for (i = 0; i < 20; i += 2)
{
qse_printf (QSE_T("deleting %d\n"), i);
qse_rbt_delete (s1, &i, QSE_SIZEOF(i));
}
qse_rbt_rwalk (s1, walk, QSE_NULL);
for (i = 0; i < 20; i++)
{
int x = i * 20;
qse_printf (QSE_T("inserting at %d\n"), i);
qse_rbt_insert (s1, &i, QSE_SIZEOF(i), &x, QSE_SIZEOF(x));
}
qse_rbt_rwalk (s1, walk, QSE_NULL);
qse_rbt_clear (s1);
for (i = 20; i > 0; i--)
{
int x = i * 20;
qse_printf (QSE_T("inserting at %d\n"), i);
qse_rbt_insert (s1, &i, QSE_SIZEOF(i), &x, QSE_SIZEOF(x));
}
qse_rbt_rwalk (s1, walk, QSE_NULL);
for (i = 0; i < 20; i += 3)
{
qse_printf (QSE_T("deleting %d\n"), i);
qse_rbt_delete (s1, &i, QSE_SIZEOF(i));
}
qse_rbt_rwalk (s1, walk, QSE_NULL);
qse_rbt_close (s1);
return 0;
}
static int test2 ()
{
qse_rbt_t* s1;
s1 = qse_rbt_open (QSE_MMGR_GETDFL(), 0, QSE_SIZEOF(qse_char_t), QSE_SIZEOF(qse_char_t));
if (s1 == QSE_NULL)
{
qse_printf (QSE_T("cannot open a table\n"));
return -1;
}
qse_rbt_setstyle (s1, qse_getrbtstyle(QSE_RBT_STYLE_INLINE_COPIERS));
qse_rbt_insert (s1, QSE_T("hello"), 5, QSE_T("mr. monkey"), 10);
qse_rbt_insert (s1, QSE_T("world"), 5, QSE_T("ms. panda"), 9);
qse_rbt_insert (s1, QSE_T("thinkpad"), 8, QSE_T("x61"), 3);
qse_rbt_rwalk (s1, walk2, QSE_NULL);
qse_printf (QSE_T("-------------------\n"));
qse_rbt_upsert (s1, QSE_T("hello"), 5, QSE_T("dr. gorilla"), 11);
qse_rbt_upsert (s1, QSE_T("thinkpad"), 8, QSE_T("x61 rocks on"), 13);
qse_rbt_ensert (s1, QSE_T("vista"), 5, QSE_T("microsoft"), 9);
qse_rbt_update (s1, QSE_T("world"), 5, QSE_T("baseball classic"), 16);
qse_rbt_rwalk (s1, walk2, QSE_NULL);
qse_rbt_close (s1);
return 0;
}
qse_rbt_walk_t print_map_pair (qse_rbt_t* map, qse_rbt_pair_t* pair, void* arg)
{
qse_printf (QSE_T("%.*s[%d] => %.*s[%d]\n"),
(int)QSE_RBT_KLEN(pair), QSE_RBT_KPTR(pair), (int)QSE_RBT_KLEN(pair),
(int)QSE_RBT_VLEN(pair), QSE_RBT_VPTR(pair), (int)QSE_RBT_VLEN(pair));
return QSE_RBT_WALK_FORWARD;
}
static qse_rbt_pair_t* test5_cbserter (
qse_rbt_t* rbt, qse_rbt_pair_t* pair,
void* kptr, qse_size_t klen, void* ctx)
{
qse_cstr_t* v = (qse_cstr_t*)ctx;
if (pair == QSE_NULL)
{
/* no existing key for the key */
return qse_rbt_allocpair (rbt, kptr, klen, v->ptr, v->len);
}
else
{
/* a pair with the key exists.
* in this sample, i will append the new value to the old value
* separated by a comma */
qse_rbt_pair_t* new_pair;
qse_char_t comma = QSE_T(',');
qse_byte_t* vptr;
/* allocate a new pair, but without filling the actual value.
* note vptr is given QSE_NULL for that purpose */
new_pair = qse_rbt_allocpair (
rbt, kptr, klen, QSE_NULL,
QSE_RBT_VLEN(pair) + 1 + v->len);
if (new_pair == QSE_NULL) return QSE_NULL;
/* fill in the value space */
vptr = QSE_RBT_VPTR(new_pair);
qse_memcpy (vptr, QSE_RBT_VPTR(pair),
QSE_RBT_VLEN(pair) * QSE_SIZEOF(qse_char_t));
vptr += QSE_RBT_VLEN(pair) * QSE_SIZEOF(qse_char_t);
qse_memcpy (vptr, &comma, QSE_SIZEOF(qse_char_t));
vptr += QSE_SIZEOF(qse_char_t);
qse_memcpy (vptr, v->ptr, v->len*QSE_SIZEOF(qse_char_t));
/* this callback requires the old pair to be destroyed */
qse_rbt_freepair (rbt, pair);
/* return the new pair */
return new_pair;
}
}
static int test5 ()
{
qse_rbt_t* s1;
int i;
qse_char_t* keys[] =
{
QSE_T("one"), QSE_T("two"), QSE_T("three")
};
qse_char_t* vals[] =
{
QSE_T("1"), QSE_T("2"), QSE_T("3"), QSE_T("4"), QSE_T("5"),
};
s1 = qse_rbt_open (QSE_MMGR_GETDFL(), 0,
QSE_SIZEOF(qse_char_t), QSE_SIZEOF(qse_char_t));
if (s1 == QSE_NULL)
{
qse_printf (QSE_T("cannot open a hash table\n"));
return -1;
}
qse_rbt_setstyle (s1, qse_getrbtstyle(QSE_RBT_STYLE_INLINE_COPIERS));
for (i = 0; i < QSE_COUNTOF(vals); i++)
{
qse_cstr_t ctx;
qse_printf (QSE_T("setting a key [%s] and a value [%s]: "), keys[i%QSE_COUNTOF(keys)], vals[i]);
ctx.ptr = vals[i];
ctx.len = qse_strlen(vals[i]);
if (qse_rbt_cbsert (s1,
keys[i%QSE_COUNTOF(keys)],
qse_strlen(keys[i%QSE_COUNTOF(keys)]),
test5_cbserter, &ctx) == QSE_NULL)
{
qse_printf (QSE_T("[FAILED]\n"));
}
else
{
qse_printf (QSE_T("[OK]\n"));
}
}
qse_rbt_walk (s1, print_map_pair, QSE_NULL);
qse_rbt_close (s1);
return 0;
}
int main ()
{
qse_open_stdsios ();
R (test1);
R (test2);
R (test5);
qse_close_stdsios ();
return 0;
}

64
samples/cmn/rbt02.cpp Normal file
View File

@ -0,0 +1,64 @@
#include <stdio.h>
#include <qse/cmn/RedBlackTree.hpp>
class IntPair
{
public:
IntPair (int x = 0, int y = 0): x (x), y (y) {}
int getX () const { return this->x; }
int getY () const { return this->y; }
bool operator== (const IntPair& ip) const { return this->x == ip.x; }
bool operator< (const IntPair& ip) const { return this->x < ip.x; }
bool operator> (const IntPair& ip) const { return this->x > ip.x; }
protected:
int x, y;
};
typedef QSE::RedBlackTree<IntPair> IntTree;
int main ()
{
IntTree t (10000, QSE_NULL);
//IntTree t (0);
for (int i = 0; i < 20 ; i++)
{
t.insert (IntPair (i , i * 2));
}
t.clear (true);
for (int i = 0; i < 20 ; i++)
{
t.insert (IntPair (i , i * 2));
}
IntTree::ConstIterator it;
for (it = t.getConstIterator(); it.isLegit(); ++it)
{
printf ("%d %d\n", it.getValue().getX(), (*it).getY());
}
printf ("------------------\n");
IntTree x(t);
for (it = t.getConstIterator(); it.isLegit(); ++it)
{
printf ("%d %d\n", it.getValue().getX(), (*it).getY());
}
printf ("------------------\n");
t.insert (IntPair(99, 999));
t.insert (IntPair(88, 888));
//IntTree y (5, QSE_NULL);
x = t;
for (it = x.getConstIterator(); it.isLegit(); ++it)
{
printf ("%d %d\n", it.getValue().getX(), (*it).getY());
}
return 0;
}

191
samples/cmn/rbt03.cpp Normal file
View File

@ -0,0 +1,191 @@
#include <stdio.h>
#include <qse/cmn/StdMmgr.hpp>
#include <qse/cmn/HeapMmgr.hpp>
#include <qse/cmn/RedBlackTable.hpp>
#include <qse/si/sio.h>
#include <string>
#include <string.h>
typedef QSE::RedBlackTable<int,int> IntTable;
struct IntClass
{
IntClass (int x = 0): x (x) {}
int x;
};
struct IntClassComparator
{
int operator() (const IntClass& v, int y) const
{
IntTable::DefaultComparator comp;
return comp (v.x, y);
}
};
typedef QSE::RedBlackTable<std::string,int> Str1Table;
struct cstr_comparator
{
int operator() (const char* v1, const std::string& v2) const
{
return strcmp (v1, v2.c_str());
}
};
int main ()
{
// qse_open_stdsios ();
QSE::HeapMmgr heap_mmgr (3000000, QSE::Mmgr::getDFL());
//QSE::RedBlackTable<int,int,IntHasher> int_table (1000, &heap_mmgr);
IntTable int_table (1000, QSE_NULL);
//IntTable int_table (0, QSE_NULL);
printf ("----------\n");
for (int i = 0; i < 100; i++)
{
int_table.insert (i, i * 20);
}
for (int i = 50; i < 150; i++)
{
int_table.upsert (i, i * 20);
}
printf ("----------\n");
/*
qse_size_t bucket_size = int_table.getBucketSize();
for (qse_size_t i = 0; i < bucket_size; i++)
{
IntTable::Bucket* b = int_table.getBucket (i);
}
*/
printf ("----------\n");
/*
IntTable::Pair* pair = int_table.findPairWithCustomKey<IntClass,IntClassHasher,IntClassComparator> (IntClass(50));
if (pair)
{
printf ("pair found.... [%d]\n", pair->value);
}
*/
printf ("%p %p\n", int_table.search (60), int_table.search (90));
printf ("%d %d\n", int_table.remove (60), int_table.remove (60));
printf ("%d %d\n", int_table.remove (70), int_table.remove (70));
/*
printf ("%d\n", int_table[90]);
printf ("%d\n", int_table[9990]);
*/
//Str1Table s1 (1000, QSE_NULL);
Str1Table s1 (0, QSE_NULL);
//Str1Table s1;
s1.insert ("hello", 20);
s1.insert ("hello", 20);
s1.insert ("hello kara", 20);
s1.insert ("this is good", 3896);
printf ("remove = %d\n", s1.remove ("hello"));
printf ("remove = %d\n", s1.remove ("hello"));
for (int i = 0; i < 100; i++)
{
char buf[128];
sprintf (buf, "surukaaaa %d", i);
s1.insert (buf, i * 2);
}
printf ("%d\n", (int)s1.getSize());
for (Str1Table::Iterator it = s1.getIterator(); it.isLegit(); it++)
{
Str1Table::Pair& pair = *it;
printf ("[%s] [%d]\n", pair.key.c_str(), pair.value);
}
printf ("------------------\n");
{
Str1Table s11 (s1);
for (Str1Table::Iterator it = s11.getIterator(); it.isLegit(); it++)
{
Str1Table::Pair& pair = *it;
printf ("[%s] [%d]\n", pair.key.c_str(), pair.value);
}
}
printf ("------------------\n");
{
Str1Table s11 (0, &heap_mmgr);
//Str1Table s11 (200, QSE_NULL);
//Str1Table s11;
for (int i = 0; i < 100; i++)
{
char buf[128];
sprintf (buf, "abiyo %d", i);
s11.insert (buf, i * 2);
}
printf ("%d\n", s11.heteroremove<const char*, cstr_comparator> ("abiyo 12"));
printf ("%d\n", s11.heteroremove<const char*, cstr_comparator> ("abiyo 12"));
printf ("SIZE => %d\n", (int)s11.getSize());
for (Str1Table::Iterator it = s11.getIterator(); it.isLegit(); it++)
{
Str1Table::Pair& pair = *it;
printf ("[%s] [%d]\n", pair.key.c_str(), pair.value);
}
printf ("------------------\n");
//s11.clear (true);
s11 = s1;
printf ("SIZE => %d\n", (int)s11.getSize());
for (Str1Table::Iterator it = s11.getIterator(); it.isLegit(); it++)
{
Str1Table::Pair& pair = *it;
printf ("[%s] [%d]\n", pair.key.c_str(), pair.value);
}
printf ("-------------------\n");
Str1Table::Pair* pair = s11.heterosearch<const char*, cstr_comparator> ("surukaaaa 13");
if (pair) printf ("%d\n", pair->value);
else printf ("not found\n");
s11.update ("surukaaaa 13", 999);
s11.update ("surukaaaa 16", 99999);
s11.inject ("surukaaaa 18", 999999, 1);
pair = s11.heterosearch<const char*, cstr_comparator> ("surukaaaa 13");
if (pair) printf ("%d\n", pair->value);
else printf ("not found\n");
s1 = s11;
}
printf ("-------------------\n");
printf ("%d\n", (int)s1.getSize());
for (Str1Table::ConstIterator it = s1.getConstIterator(); it.isLegit(); it++)
{
const Str1Table::Pair& pair = *it;
printf ("[%s] [%d]\n", pair.key.c_str(), pair.value);
}
printf ("-------------------\n");
#if 0
for (Str1Table::PairNode* np = s1.getTailNode(); np; np = np->getPrevNode())
{
Str1Table::Pair& pair = np->value;
printf ("[%s] [%d]\n", pair.key.c_str(), pair.value);
}
#endif
// qse_close_stdsios ();
return 0;
}

98
samples/cmn/rex01.c Normal file
View File

@ -0,0 +1,98 @@
#include <qse/cmn/rex.h>
#include <qse/cmn/mem.h>
#include <qse/cmn/str.h>
#include <qse/cmn/main.h>
#include <qse/cmn/mbwc.h>
#include <qse/cmn/path.h>
#include <qse/si/sio.h>
#include <locale.h>
#if defined(_WIN32)
# include <windows.h>
#endif
static int rex_main (int argc, qse_char_t* argv[])
{
qse_rex_t* rex;
qse_rex_node_t* start;
qse_cstr_t str;
qse_cstr_t matstr;
int n;
if (argc != 3)
{
qse_printf (QSE_T("USAGE: %s pattern string\n"),
qse_basename(argv[0]));
return -1;
}
rex = qse_rex_open (QSE_MMGR_GETDFL(), 0, QSE_NULL);
if (rex == QSE_NULL)
{
qse_printf (QSE_T("ERROR: cannot open rex\n"));
return -1;
}
qse_rex_setopt (rex, QSE_REX_STRICT);
start = qse_rex_comp (rex, argv[1], qse_strlen(argv[1]));
if (start == QSE_NULL)
{
qse_printf (QSE_T("ERROR: cannot compile - %s\n"),
qse_rex_geterrmsg(rex));
qse_rex_close (rex);
return -1;
}
str.ptr = argv[2];
str.len = qse_strlen(argv[2]);
qse_printf (QSE_T("compile ok\n"));
n = qse_rex_exec (rex, &str, &str, &matstr);
if (n <= -1)
{
qse_printf (QSE_T("ERROR: cannot execute - %s\n"),
qse_rex_geterrmsg(rex));
qse_rex_close (rex);
return -1;
}
if (n >= 1)
{
qse_printf (QSE_T("MATCH: [%.*s] beginning from char #%d\n"),
(int)matstr.len, matstr.ptr,
(int)(matstr.ptr - str.ptr + 1));
}
qse_rex_close (rex);
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, rex_main);
qse_close_stdsios ();
return x;
}

360
samples/cmn/rex02.cpp Normal file
View File

@ -0,0 +1,360 @@
#include <qse/cmn/rex.h>
#include <qse/cmn/mem.h>
#include <qse/cmn/str.h>
#include <qse/cmn/stdio.h>
#include <wx/wx.h>
#include <wx/cmdline.h>
#include <string.h>
#include <locale.h>
class MyApp: public wxApp
{
public:
virtual bool OnInit();
virtual int OnExit();
virtual int OnRun();
virtual void OnInitCmdLine(wxCmdLineParser& parser);
virtual bool OnCmdLineParsed(wxCmdLineParser& parser);
wxString matPattern;
wxString matString;
};
DECLARE_APP(MyApp)
class MyFrame: public wxFrame
{
public:
MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size);
~MyFrame ()
{
if (rex) qse_rex_close (rex);
}
void OnQuit(wxCommandEvent& event);
void OnAbout(wxCommandEvent& event);
void OnComp(wxCommandEvent& event);
void OnPaint (wxPaintEvent& event);
DECLARE_EVENT_TABLE()
protected:
qse_rex_t* rex;
qse_rex_node_t* start;
void drawArrow (wxDC& dc, qse_rex_node_t* f, qse_rex_node_t* t);
void drawNode (wxDC& dc, qse_rex_node_t* n);
void drawChain (wxDC& dc, qse_rex_node_t* n);
int nodex, nodey;
};
enum
{
ID_Quit = 1,
ID_Comp,
ID_About,
};
BEGIN_EVENT_TABLE(MyFrame, wxFrame)
EVT_MENU(ID_Quit, MyFrame::OnQuit)
EVT_MENU(ID_Comp, MyFrame::OnComp)
EVT_MENU(ID_About, MyFrame::OnAbout)
EVT_PAINT(MyFrame::OnPaint)
END_EVENT_TABLE()
IMPLEMENT_APP(MyApp)
bool MyApp::OnInit()
{
if (!wxApp::OnInit()) return false;
MyFrame *frame = new MyFrame(
_T("\uB108 \uBB50\uAC00 \uC798\uB0AC\uC5B4?"),
wxPoint(50,50), wxSize(450,340)
);
frame->Show(TRUE);
SetTopWindow(frame);
return TRUE;
}
int MyApp::OnExit()
{
// clean up
return 0;
}
int MyApp::OnRun()
{
int exitcode = wxApp::OnRun();
//wxTheClipboard->Flush();
return exitcode;
}
void MyApp::OnInitCmdLine(wxCmdLineParser& parser)
{
static const wxCmdLineEntryDesc g_cmdLineDesc [] =
{
{ wxCMD_LINE_SWITCH, wxT("h"), wxT("help"),
wxT("displays help on the command line parameters"),
wxCMD_LINE_VAL_NONE, wxCMD_LINE_OPTION_HELP },
//{ wxCMD_LINE_SWITCH, wxT("t"), wxT("test"),
// wxT("test switch"),
// wxCMD_LINE_VAL_NONE, wxCMD_LINE_OPTION_MANDATORY },
{ wxCMD_LINE_PARAM, NULL, NULL, wxT("pattern"),
wxCMD_LINE_VAL_STRING, wxCMD_LINE_OPTION_MANDATORY },
{ wxCMD_LINE_PARAM, NULL, NULL, wxT("string"),
wxCMD_LINE_VAL_STRING, wxCMD_LINE_OPTION_MANDATORY },
{ wxCMD_LINE_NONE }
};
parser.SetDesc (g_cmdLineDesc);
// must refuse '/' as parameter starter or cannot use "/path" style paths
parser.SetSwitchChars (wxT("-"));
}
bool MyApp::OnCmdLineParsed(wxCmdLineParser& parser)
{
//silent_mode = parser.Found(wxT("s"));
// to get at your unnamed parameters use
/*
wxArrayString files;
for (int i = 0; i < parser.GetParamCount(); i++)
{
files.Add(parser.GetParam(i));
}
*/
if (parser.GetParamCount() != 2)
{
wxMessageBox(_T("Usage: XXXXXXXXXXXXXXXXXXXXXXXXXXXXx"),
_T("Error"), wxOK | wxICON_INFORMATION, NULL);
return false;
}
matPattern = parser.GetParam(0);
matString = parser.GetParam(1);
// and other command line parameters
// then do what you need with them.
return true;
}
MyFrame::MyFrame (
const wxString& title, const wxPoint& pos, const wxSize& size)
: wxFrame((wxFrame *)NULL, -1, title, pos, size), rex (NULL)
{
wxMenu *menuFile = new wxMenu;
menuFile->Append( ID_About, _T("&About...") );
menuFile->Append( ID_Comp, _T("&Compile...") );
menuFile->AppendSeparator();
menuFile->Append( ID_Quit, _T("E&xit") );
wxMenuBar *menuBar = new wxMenuBar;
menuBar->Append( menuFile, _T("&File") );
SetMenuBar( menuBar );
/*
wxPanel *panel = new wxPanel(this, -1);
wxFlexGridSizer* fgs = new wxFlexGridSizer (1, 2);
wxButton* but = new wxButton (panel, wxID_ANY, _T("XXXX"));
wxTextCtrl* textctrl = new wxTextCtrl(panel, -1, wxT(""), wxPoint(-1, -1),
wxSize(250, 150));
fgs->Add (but);
fgs->Add (textctrl, 1, wxEXPAND);
panel->SetSizer (fgs);
*/
CreateStatusBar();
SetStatusText( _T("Welcome to wxWidgets!") );
SetSize (wxSize (700,500));
}
void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
{
Close(TRUE);
}
void MyFrame::OnComp(wxCommandEvent& WXUNUSED(event))
{
if (rex == NULL)
{
rex = qse_rex_open (QSE_MMGR_GETDFL(), 0, QSE_NULL);
if (rex == NULL)
{
wxMessageBox(_T("Cannot open rex"),
_T("Error"), wxOK | wxICON_INFORMATION, this);
return;
}
}
//const qse_char_t* x = QSE_T("y(abc|def|xyz|1234)x");
//const qse_char_t* x = QSE_T("(abc|def|xyz|1234)x");
//const qse_char_t* x = QSE_T("y(abc|def|xyz|1234");
//const qse_char_t* x = QSE_T("(abc|abcdefg)");
const qse_char_t* x = QSE_T("a*b?c*defg");
//((MyApp*)wxTheApp)->matPattern;
MyApp& app = wxGetApp();
start = qse_rex_comp (rex, app.matPattern.wx_str(), app.matPattern.Len());
if (start == QSE_NULL)
{
wxMessageBox(_T("Cannot compile rex"),
_T("Error"), wxOK | wxICON_INFORMATION, this);
return;
}
//const qse_char_t* text = QSE_T("abcyabcxxx");
const qse_char_t* text = QSE_T("abcdefg");
qse_rex_exec (rex,
app.matString.wx_str(),app.matString.Len(),
app.matString.wx_str(),app.matString.Len());
Refresh ();
}
void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
{
wxMessageBox(_T("This is a wxWidgets Hello world sample"),
_T("\uB108 \uBB50\uAC00 \uC798\uB0AC\uC5B4?"), wxOK | wxICON_INFORMATION, this);
}
void MyFrame::drawArrow (wxDC& dc, qse_rex_node_t* f, qse_rex_node_t* t)
{
}
void MyFrame::drawNode (wxDC& dc, qse_rex_node_t* n)
{
if (n->id == QSE_REX_NODE_BRANCH)
{
dc.DrawText (_T("<BR>"), nodex, nodey);
}
else if (n->id == QSE_REX_NODE_BOL)
{
dc.DrawText (_T("<^>"), nodex, nodey);
}
else if (n->id == QSE_REX_NODE_EOL)
{
dc.DrawText (_T("<$>"), nodex, nodey);
}
else if (n->id == QSE_REX_NODE_ANYCHAR)
{
dc.DrawText (_T("<AY>"), nodex, nodey);
}
else if (n->id == QSE_REX_NODE_CHAR)
{
qse_char_t x[2];
x[0] = n->u.c;
x[1] = QSE_T('\0');
dc.DrawText (x, nodex, nodey);
}
else if (n->id == QSE_REX_NODE_START)
{
dc.DrawText (_T("<ST>"), nodex, nodey);
}
else if (n->id == QSE_REX_NODE_END)
{
dc.DrawText (_T("<E>"), nodex, nodey);
}
else if (n->id == QSE_REX_NODE_GROUP)
{
dc.DrawText (_T("<G>"), nodex, nodey);
}
else if (n->id == QSE_REX_NODE_GROUPEND)
{
dc.DrawText (_T("<GE>"), nodex, nodey);
}
else if (n->id == QSE_REX_NODE_NOP)
{
dc.DrawText (_T("<NP>"), nodex, nodey);
}
}
void MyFrame::drawChain (wxDC& dc, qse_rex_node_t* n)
{
qse_rex_node_t* t = n;
while (t != QSE_NULL)
{
if (t->id == QSE_REX_NODE_BRANCH)
{
drawNode (dc, t);
nodex += 40;
int oldx = nodex;
drawChain (dc, t->u.b.left);
nodex = oldx;
nodey += 40;
drawChain (dc, t->u.b.right);
}
else
{
drawNode (dc, t);
nodex += 40;
}
if (t->id == QSE_REX_NODE_GROUP)
t = t->u.g.head;
else if (t->id == QSE_REX_NODE_GROUPEND)
t = t->u.ge.group->next;
else t = t->next;
}
}
void MyFrame::OnPaint(wxPaintEvent& event)
{
wxPaintDC dc(this);
//wxClientDC dc (this);
dc.SetPen(*wxBLACK_PEN);
dc.SetBrush(*wxGREY_BRUSH);
// Get window dimensions
wxSize sz = GetClientSize();
#if 0
// Our rectangle dimensions
wxCoord w = 100, h = 50;
// Center the rectangle on the window, but never
// draw at a negative position.
int x = wxMax (0, (sz.x - w) / 2);
int y = wxMax (0, (sz.y - h) / 2);
wxRect rectToDraw(x, y, w, h);
// For efficiency, do not draw if not exposed
if (IsExposed(rectToDraw)) dc.DrawRectangle(rectToDraw);
#endif
if (start)
{
nodex = 5; nodey = 5;
drawChain (dc, start);
}
}

67
samples/cmn/sll.c Normal file
View File

@ -0,0 +1,67 @@
#include <qse/cmn/mem.h>
#include <qse/cmn/str.h>
#include <qse/cmn/sll.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 qse_sll_walk_t walk_sll (qse_sll_t* sll, qse_sll_node_t* n, void* arg)
{
qse_printf (QSE_T("[%.*s]\n"), (int)QSE_SLL_DLEN(n), QSE_SLL_DPTR(n));
return QSE_SLL_WALK_FORWARD;
}
static int test1 ()
{
qse_sll_t* s1;
qse_sll_node_t* p;
qse_char_t* x[] =
{
QSE_T("this is so good"),
QSE_T("what the hack"),
QSE_T("do you like it?")
};
int i;
s1 = qse_sll_open (QSE_MMGR_GETDFL(), 0);
if (s1 == QSE_NULL)
{
qse_printf (QSE_T("cannot open a string\n"));
return -1;
}
qse_sll_setcopier (s1, QSE_SLL_COPIER_INLINE);
qse_sll_setscale (s1, QSE_SIZEOF(qse_char_t));
for (i = 0; i < QSE_COUNTOF(x); i++)
{
qse_sll_pushtail (s1, x[i], qse_strlen(x[i]));
}
qse_printf (QSE_T("s1 holding [%d] nodes\n"), QSE_SLL_SIZE(s1));
qse_sll_walk (s1, walk_sll, QSE_NULL);
p = qse_sll_search (s1, QSE_NULL, x[0], qse_strlen(x[0]));
if (p != QSE_NULL)
{
qse_sll_delete (s1, p);
}
qse_printf (QSE_T("s1 holding [%d] nodes\n"), QSE_SLL_SIZE(s1));
qse_sll_walk (s1, walk_sll, QSE_NULL);
qse_sll_close (s1);
return 0;
}
int main ()
{
qse_open_stdsios ();
R (test1);
qse_close_stdsios ();
return 0;
}

234
samples/cmn/slmb01.c Normal file
View File

@ -0,0 +1,234 @@
#include <qse/cmn/slmb.h>
#include <qse/cmn/mbwc.h>
#include <qse/cmn/mem.h>
#include <qse/cmn/str.h>
#include <qse/si/sio.h>
#include <locale.h>
#include <wchar.h>
#if defined(_WIN32)
# include <windows.h>
#endif
#define R(f) \
do { \
qse_printf (QSE_T("== %s ==\n"), QSE_T(#f)); \
if (f() == -1) return -1; \
} while (0)
static int test1 (void)
{
qse_printf (QSE_T("slmblenmax=%d\n"), (int)qse_slmblenmax());
return 0;
}
static int test2 (void)
{
int i;
const qse_mchar_t* x[] =
{
"\0",
"뛰어 올라봐", /* this text is in utf8. so some conversions fail on a non-utf8 locale */
#if defined(QSE_ENDIAN_BIG)
/* this text is in cp949. 뛰어올라봐 */
"\xD9\xB6\xEE\xBE \xC3\xBF\xF3\xB6\xC1\xBA",
#elif defined(QSE_ENDIAN_LITTLE)
/* this text is in cp949. 뛰어올라봐 */
"\xB6\xD9\xBE\xEE \xBF\xC3\xB6\xF3\xBA\xC1",
#else
# error ENDIAN UNKNOWN
#endif
"Fly to the universe"
};
for (i = 0; i < QSE_COUNTOF(x); i++)
{
qse_size_t k = qse_mbslen(x[i]);
qse_size_t j = 0;
qse_wchar_t wc;
if (k == 0) k++; /* for x[0] */
qse_printf (QSE_T("["));
while (j < k)
{
mbstate_t state;
qse_size_t y, ym, ymr;
y = qse_slmblen (&x[i][j], k-j);
ym = mblen (&x[i][j], k-j);
memset (&state, 0, sizeof(state));
ymr = mbrlen (&x[i][j], k-j, &state);
if (ym != ymr)
{
/* if this line is shown, the c runtime library is buggy.
* note that i assume we handle stateless encoding only
* since the state is initlized to 0 above all the time */
qse_printf (QSE_T("***buggy clib [mblen=%d],[mbrlen=%d]***"), (int)ym, (int)ymr);
}
if (y == 0)
{
qse_printf (QSE_T("***illegal sequence[y=%d][ym=%d][ymr=%d]***"), (int)y, (int)ym, (int)ymr);
break;
}
else if (y > k-j)
{
qse_printf (QSE_T("***incomplete sequence***"));
break;
}
else
{
qse_size_t y2 = qse_slmbtoslwc (&x[i][j], y, &wc);
if (y2 != y)
{
qse_printf (QSE_T("***y(%d) != y2(%d). something is wrong*** "), (int)y, (int)y2);
break;
}
if (wc == L'\0')
{
qse_printf (QSE_T("'\\0'"));
}
else
{
#ifdef QSE_CHAR_IS_MCHAR
qse_printf (QSE_T("%C"), wc);
#else
qse_printf (QSE_T("%c"), wc);
#endif
}
j += y;
}
}
qse_printf (QSE_T("] => %d bytes\n"), (int)k);
}
return 0;
}
static int test3 (void)
{
const qse_wchar_t unistr_kr[] =
{
/*L"\uB108 \uBB50\uAC00 \uC798\uB0AC\uC5B4!",*/
0xB108,
L' ',
0xBB50,
0xAC00,
L' ',
0xC798,
0xB0AC,
0xC5B4,
L'!',
L'\0'
};
const qse_wchar_t unistr_cn[] =
{
/* 智慧手機帶頭 */
/* \u667A\u6167\u624B\u6A5F\u5E36\u982D */
0x667A,
0x6167,
0x624B,
0x6A5F,
0x5E36,
0x982D,
L'\0'
};
const qse_wchar_t* x[] =
{
L"\0",
L"",
L"",
L"Fly to the universe"
};
char buf[100];
int i, j;
x[1] = unistr_kr;
x[2] = unistr_cn;
for (i = 0; i < QSE_COUNTOF(x); i++)
{
int nbytes = 0;
int len = qse_wcslen (x[i]);
if (len == 0) len++; /* for x[0] */
qse_printf (QSE_T("["));
for (j = 0; j < len; j++)
{
qse_size_t n = qse_slwctoslmb (x[i][j], buf, sizeof(buf) - 1);
if (n == 0)
{
qse_printf (QSE_T("***illegal character***"));
}
else if (n > sizeof(buf))
{
qse_printf (QSE_T("***buffer too small***"));
}
else
{
if (n == 1 && buf[0] == '\0')
{
qse_printf (QSE_T("'\\0'"));
}
else
{
buf[n] = QSE_MT('\0');
qse_printf (QSE_T("%hs"), buf);
}
nbytes += n;
}
}
qse_printf (QSE_T("] => %d chars, %d bytes\n"), (int)len, (int)nbytes);
}
return 0;
}
int main ()
{
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);*/
}
#if 0
{
WORD LangID = MAKELANGID(LANG_CHINESE, SUBLANG_DEFAULT);
SetThreadLocale(MAKELCID(LangID, SORT_DEFAULT));
/* SetThreadUILanguage(), SetThreadPreferredUILanguage(). */
}
#endif
#else
setlocale (LC_ALL, "");
/*qse_setdflcmgrbyid (QSE_CMGR_SLMB);*/
#endif
qse_open_stdsios ();
R (test1);
R (test2);
R (test3);
qse_close_stdsios ();
return 0;
}

54
samples/cmn/sp01.cpp Normal file
View File

@ -0,0 +1,54 @@
#include <stdio.h>
#include <qse/cmn/ScopedPtr.hpp>
#include <qse/cmn/HeapMmgr.hpp>
class X
{
public:
X()
{
printf ("X constructured\n");
}
~X()
{
printf ("X destructed\n");
}
};
struct destroy_x_in_mmgr
{
void operator() (X* x, void* arg)
{
x->~X();
::operator delete (x, (QSE::Mmgr*)arg);
}
};
int main ()
{
QSE::HeapMmgr heap_mmgr (30000, QSE::Mmgr::getDFL());
{
QSE::ScopedPtr<X> x1 (new X);
}
printf ("----------------------------\n");
{
QSE::ScopedPtr<X,QSE::ScopedPtrArrayDeleter<X> > x3 (new X[10]);
}
printf ("----------------------------\n");
{
//QSE::ScopedPtr<X> x2 (new(&heap_mmgr) X, destroy_x);
QSE::ScopedPtr<X,destroy_x_in_mmgr> x2 (new(&heap_mmgr) X, &heap_mmgr);
}
printf ("----------------------------\n");
{
QSE::ScopedPtr<X,QSE::ScopedPtrMmgrDeleter<X> > x4 (new(&heap_mmgr) X, &heap_mmgr);
}
printf ("----------------------------\n");
return 0;
}

105
samples/cmn/sp02.cpp Normal file
View File

@ -0,0 +1,105 @@
#include <stdio.h>
#include <qse/cmn/SharedPtr.hpp>
#include <qse/cmn/HeapMmgr.hpp>
#include <qse/cmn/test.h>
static int test_marker[1000] = { 0, };
static int array_deleted = 0;
class X
{
public:
X(int y = 0): y(y)
{
qse_printf (QSE_T("X(%d) constructured\n"), this->y);
test_marker[y]++;
}
~X()
{
qse_printf (QSE_T("X(%d) destructed\n"), this->y);
test_marker[y]--;
}
int y;
};
struct array_deleter: QSE::SharedPtrArrayDeleter<X>
{
void operator() (X* ptr, void* arg)
{
array_deleted++;
QSE::SharedPtrArrayDeleter<X>::operator() (ptr, arg);
}
};
struct destroy_x_in_mmgr
{
void operator() (X* x, void* arg)
{
array_deleted++;
x->~X();
::operator delete (x, (QSE::Mmgr*)arg);
}
};
int f2 ()
{
QSE::HeapMmgr heap_mmgr (30000, QSE::Mmgr::getDFL());
QSE::HeapMmgr heap_mmgr_2 (30000, QSE::Mmgr::getDFL());
QSE::SharedPtr<X> y (new X(1));
QSE::SharedPtr<X,QSE::SharedPtrMmgrDeleter<X> > k (&heap_mmgr);
QSE_TESASSERT1 (y->y == 1, QSE_T("unexpected value"));
QSE_TESASSERT1 (test_marker[y->y] == 1, QSE_T("allocation tally wrong"));
{
QSE::SharedPtr<X> x1 (y);
QSE_TESASSERT1 (x1->y == 1, QSE_T("unexpected value"));
}
QSE_TESASSERT1 (test_marker[y->y] == 1, QSE_T("allocation tally wrong"));
qse_printf (QSE_T("----------------------------\n"));
{
//QSE::SharedPtr<X,QSE::SharedPtrArrayDeleter<X> > x3 (new X[10]);
QSE::SharedPtr<X,array_deleter > x3 (new X[10]);
}
QSE_TESASSERT1 (array_deleted == 1, QSE_T("array not deleted"));
qse_printf (QSE_T("----------------------------\n"));
{
//QSE::SharedPtr<X> x2 (new(&heap_mmgr) X, destroy_x);
QSE_TESASSERT1 (test_marker[2] == 0, QSE_T("allocation tally wrong"));
QSE::SharedPtr<X,destroy_x_in_mmgr> x2 (&heap_mmgr, new(&heap_mmgr) X(2), &heap_mmgr);
QSE_TESASSERT1 (x2->y == 2, QSE_T("unexpected value"));
QSE_TESASSERT1 (test_marker[2] == 1, QSE_T("allocation tally wrong"));
}
QSE_TESASSERT1 (test_marker[2] == 0, QSE_T("allocation tally wrong"));
qse_printf (QSE_T("----------------------------\n"));
{
QSE_TESASSERT1 (test_marker[3] == 0, QSE_T("allocation tally wrong"));
QSE::SharedPtr<X,QSE::SharedPtrMmgrDeleter<X> > x4 (new(&heap_mmgr_2) X(3), &heap_mmgr_2);
QSE_TESASSERT1 (test_marker[3] == 1, QSE_T("allocation tally wrong"));
k = x4;
QSE::SharedPtr<X,QSE::SharedPtrMmgrDeleter<X> > x5 (k);
}
QSE_TESASSERT1 (test_marker[3] == 1, QSE_T("allocation tally wrong"));
qse_printf (QSE_T("----------------------------\n"));
QSE_TESASSERT1 (k->y == 3, QSE_T("unexpected value"));
return 0;
oops:
return -1;
}
int main ()
{
qse_open_stdsios();
f2 ();
qse_close_stdsios();
return 0;
}

490
samples/cmn/str01.c Normal file
View File

@ -0,0 +1,490 @@
#include <qse/cmn/test.h>
#include <qse/cmn/mem.h>
#include <qse/cmn/str.h>
#include <qse/cmn/mbwc.h>
#include <qse/si/sio.h>
#include <locale.h>
#if defined(_WIN32)
# include <windows.h>
#endif
#define R(f) \
do { \
qse_printf (QSE_T("== %s ==\n"), QSE_T(#f)); \
if (f() == -1) return -1; \
} while (0)
static int test1 ()
{
qse_str_t* s1;
s1 = qse_str_open (QSE_MMGR_GETDFL(), 0, 5);
if (s1 == QSE_NULL)
{
qse_printf (QSE_T("cannot open a string\n"));
return -1;
}
qse_printf (QSE_T("LEN=%u\n"),
(unsigned)qse_str_ncat (s1, QSE_T("i love you this is great"), 24));
qse_printf (QSE_T("LEN=%u CAPA=%u [%.*s]\n"),
(unsigned)QSE_STR_LEN(s1), (unsigned)QSE_STR_CAPA(s1),
(int)QSE_STR_LEN(s1), QSE_STR_PTR(s1));
qse_printf (QSE_T("LEN=%u\n"),
(unsigned int)qse_str_setlen (s1, QSE_STR_LEN(s1)-1));
qse_printf (QSE_T("LEN=%u CAPA=%u [%.*s]\n"),
(unsigned)QSE_STR_LEN(s1), (unsigned)QSE_STR_CAPA(s1),
(int)QSE_STR_LEN(s1), QSE_STR_PTR(s1));
qse_printf (QSE_T("LEN=%u\n"),
(unsigned int)qse_str_setlen (s1, QSE_STR_CAPA(s1) + 5));
qse_printf (QSE_T("LEN=%u CAPA=%u [%.*s]\n"),
(unsigned)QSE_STR_LEN(s1), (unsigned)QSE_STR_CAPA(s1),
(int)QSE_STR_LEN(s1), QSE_STR_PTR(s1));
qse_str_close (s1);
return 0;
}
static int test2 ()
{
qse_str_t s1;
qse_str_init (&s1, QSE_MMGR_GETDFL(), 5);
qse_printf (QSE_T("LEN=%u\n"),
(unsigned)qse_str_ncat (&s1, QSE_T("i love you this is great"), 24));
qse_printf (QSE_T("LEN=%u CAPA=%u [%.*s]\n"),
(unsigned)QSE_STR_LEN(&s1), (unsigned)QSE_STR_CAPA(&s1),
(int)QSE_STR_LEN(&s1), QSE_STR_PTR(&s1));
qse_str_fini (&s1);
return 0;
}
static qse_size_t resize_str_1 (qse_str_t* str, qse_size_t hint)
{
return QSE_STR_CAPA(str) + 1;
}
static qse_size_t resize_str_2 (qse_str_t* str, qse_size_t hint)
{
return hint;
}
static qse_size_t resize_str_3 (qse_str_t* str, qse_size_t hint)
{
return hint * 2 + hint / 2;
}
static qse_size_t resize_str_4 (qse_str_t* str, qse_size_t hint)
{
return 0;
}
static int test3 ()
{
qse_str_t s1;
int i;
qse_str_init (&s1, QSE_MMGR_GETDFL(), 5);
for (i = 0; i < 9; i++)
{
if (i == 0) qse_str_setsizer (&s1, resize_str_1);
if (i == 1)
{
qse_str_setsizer (&s1, resize_str_2);
qse_str_clear (&s1);
}
if (i == 2) qse_str_setsizer (&s1, resize_str_3);
if (i == 3) qse_str_setsizer (&s1, resize_str_4);
if (i == 8) qse_str_setsizer (&s1, resize_str_2);
if (qse_str_ncat (&s1, QSE_T("i love you this is great"), 24) == (qse_size_t)-1)
{
qse_printf (QSE_T("cannot add string\n"));
qse_str_fini (&s1);
return -1;
}
qse_printf (QSE_T("LEN=%u CAPA=%u [%.*s]\n"),
(unsigned)QSE_STR_LEN(&s1), (unsigned)QSE_STR_CAPA(&s1),
(int)QSE_STR_LEN(&s1), QSE_STR_PTR(&s1));
}
qse_str_fini (&s1);
return 0;
}
static int test4 ()
{
qse_str_t s1;
qse_cstr_t out;
qse_str_init (&s1, QSE_MMGR_GETDFL(), 0);
if (qse_str_yield (&s1, &out, 0) == -1)
{
qse_printf (QSE_T("cannot yield string\n"));
qse_str_fini (&s1);
return -1;
}
qse_printf (QSE_T("out.ptr=%p LEN=%u [.*s]\n"),
out.ptr, (unsigned)out.len, (int)out.len, out.ptr);
if (qse_str_ncat (&s1, QSE_T("i love you this is great"), 24) == (qse_size_t)-1)
{
qse_printf (QSE_T("cannot add string\n"));
qse_str_fini (&s1);
return -1;
}
if (qse_str_yield (&s1, &out, 0) == -1)
{
qse_printf (QSE_T("cannot yield string\n"));
qse_str_fini (&s1);
return -1;
}
qse_printf (QSE_T("out.ptr=%p LEN=%u, [%.*s]\n"),
out.ptr, (unsigned)out.len, (int)out.len, out.ptr);
qse_str_fini (&s1);
QSE_MMGR_FREE (QSE_MMGR_GETDFL(), out.ptr);
return 0;
}
static int test11 (void)
{
qse_char_t buf[1000];
const qse_char_t* arg3[] =
{
QSE_T("00000"),
QSE_T("11111"),
QSE_T("22222")
};
const qse_char_t* arg12[] =
{
QSE_T("00000"), QSE_T("11111"), QSE_T("22222"),
QSE_T("33333"), QSE_T("44444"), QSE_T("55555"),
QSE_T("66666"), QSE_T("77777"), QSE_T("88888"),
QSE_T("99999"), QSE_T("aaaaa"), QSE_T("bbbbb")
};
qse_strfcpy (buf, QSE_T("${2}${1}${0}"), arg3);
qse_printf (QSE_T("buf=[%s]\n"), buf);
qse_strfcpy (buf, QSE_T("${2}/${1}/${0}"), arg3);
qse_printf (QSE_T("buf=[%s]\n"), buf);
qse_strfcpy (buf, QSE_T("/${2}/${1}/${0}/"), arg3);
qse_printf (QSE_T("buf=[%s]\n"), buf);
qse_strfcpy (buf, QSE_T("/$${2}/$${1}/$${0}/"), arg3);
qse_printf (QSE_T("buf=[%s]\n"), buf);
qse_strfcpy (buf, QSE_T("/${2/${1}/${0}/"), arg3);
qse_printf (QSE_T("buf=[%s]\n"), buf);
qse_strfcpy (buf, QSE_T("/$2}/${1}/${0}/"), arg3);
qse_printf (QSE_T("buf=[%s]\n"), buf);
qse_strfcpy (buf, QSE_T("/${2}/${1}/${0}/${3}/${4}/${5}/${6}/${7}/${8}/${9}/${10}/${11}/"), arg12);
qse_printf (QSE_T("buf=[%s]\n"), buf);
qse_strfcpy (buf, QSE_T("/${2}/${1}/${0}/${0}/${1}/${2}/"), arg3);
qse_printf (QSE_T("buf=[%s]\n"), buf);
qse_strfcpy (buf, QSE_T("/${002}/${001}/${000}/"), arg3);
qse_printf (QSE_T("buf=[%s]\n"), buf);
return 0;
}
static int test12 (void)
{
qse_char_t buf[20];
int i, j;
const qse_char_t* arg3[] =
{
QSE_T("00000"),
QSE_T("11111"),
QSE_T("22222")
};
for (i = 0; i <= QSE_COUNTOF(buf); i++)
{
qse_strcpy (buf, QSE_T("AAAAAAAAAAAAAAAAAAA"));
qse_strxfcpy (buf, i, QSE_T("${2}${1}${0}"), arg3);
qse_printf (QSE_T("bufsize=%02d, buf=[%-20s] "), i, buf);
qse_printf (QSE_T("["));
for (j = 0; j < QSE_COUNTOF(buf); j++)
{
if (buf[j] == QSE_T('\0')) qse_printf (QSE_T("*"));
else qse_printf (QSE_T("%c"), buf[j]);
}
qse_printf (QSE_T("]\n"));
}
return 0;
}
qse_char_t* subst (qse_char_t* buf, qse_size_t bsz, const qse_cstr_t* ident, void* ctx)
{
if (qse_strxcmp (ident->ptr, ident->len, QSE_T("USER")) == 0)
{
return buf + qse_strxput (buf, bsz, QSE_T("sam"));
}
else if (qse_strxcmp (ident->ptr, ident->len, QSE_T("GROUP")) == 0)
{
return buf + qse_strxput (buf, bsz, QSE_T("coders"));
}
/*return buf; returning the buffer pointer will result in empty substitution and the default value won't be used */
return QSE_NULL; /* return NULL to take the default value if specified */
}
static int test13 (void)
{
qse_char_t buf[24];
qse_char_t buf2[48];
qse_size_t i, j;
for (i = 0; i <= QSE_COUNTOF(buf); i++)
{
qse_strcpy (buf, QSE_T("AAAAAAAAAAAAAAAAAAAAAAA"));
qse_strxsubst (buf, i, QSE_T("user=${USER},group=${GROUP}"), subst, QSE_NULL);
qse_printf (QSE_T("bufsize=%02d, buf=[%-25s] "), i, buf);
qse_printf (QSE_T("["));
for (j = 0; j < QSE_COUNTOF(buf); j++)
{
if (buf[j] == QSE_T('\0')) qse_printf (QSE_T("*"));
else qse_printf (QSE_T("%c"), buf[j]);
}
qse_printf (QSE_T("]\n"));
}
for (i = 0; i <= QSE_COUNTOF(buf2); i++)
{
qse_strcpy (buf2, QSE_T("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"));
qse_strxsubst (buf2, i, QSE_T("user=${USER},group=${GROUP},xxx=${USERX:=${GROUP:=default value}}"), subst, QSE_NULL);
qse_printf (QSE_T("bufsize=%02d, buf2=[%-49s] "), i, buf2);
qse_printf (QSE_T("["));
for (j = 0; j < QSE_COUNTOF(buf2); j++)
{
if (buf2[j] == QSE_T('\0')) qse_printf (QSE_T("*"));
else qse_printf (QSE_T("%c"), buf2[j]);
}
qse_printf (QSE_T("]\n"));
}
for (i = 0; i <= QSE_COUNTOF(buf2); i++)
{
qse_strcpy (buf2, QSE_T("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"));
qse_strxsubst (buf2, i, QSE_T("user=${USER},group=${GROUP},xxx=${USERX:=${GROUPX:=default value}}"), subst, QSE_NULL);
qse_printf (QSE_T("bufsize=%02d, buf2=[%-49s] "), i, buf2);
qse_printf (QSE_T("["));
for (j = 0; j < QSE_COUNTOF(buf2); j++)
{
if (buf2[j] == QSE_T('\0')) qse_printf (QSE_T("*"));
else qse_printf (QSE_T("%c"), buf2[j]);
}
qse_printf (QSE_T("]\n"));
}
return 0;
}
static int test14 (void)
{
qse_char_t a1[] = QSE_T(" this is a test string ");
qse_char_t a2[] = QSE_T(" this is a test string ");
qse_char_t a3[] = QSE_T(" this is a test string ");
qse_printf (QSE_T("[%s] =>"), a1);
qse_printf (QSE_T("[%s]\n"), qse_strtrmx (a1, QSE_STRTRMX_LEFT));
qse_printf (QSE_T("[%s] =>"), a2);
qse_printf (QSE_T("[%s]\n"), qse_strtrmx (a2, QSE_STRTRMX_RIGHT));
qse_printf (QSE_T("[%s] =>"), a3);
qse_printf (QSE_T("[%s]\n"),
qse_strtrmx (a3, QSE_STRTRMX_LEFT|QSE_STRTRMX_RIGHT));
return 0;
}
static int test15 (void)
{
qse_char_t a1[] = QSE_T("abcdefghijklmnopqrstuvwxyz");
qse_str_t x;
int i;
qse_str_init (&x, QSE_MMGR_GETDFL(), 5);
for (i = 1; i < 20; i++)
{
qse_str_cpy (&x, a1);
qse_str_del (&x, 10, i);
qse_printf (QSE_T("deleleted %d from 10 => %lu [%s]\n"),
i,
(unsigned long)QSE_STR_LEN(&x),
QSE_STR_PTR(&x));
}
qse_str_fini (&x);
return 0;
}
static int test16 (void)
{
const qse_char_t* x = QSE_T("this is good");
qse_printf (QSE_T("[%s]\n"), qse_strpbrk (x, QSE_T("si")));
qse_printf (QSE_T("[%s]\n"), qse_strrpbrk (x, QSE_T("si")));
qse_printf (QSE_T("[%s]\n"), qse_strpbrk (x, QSE_T("d")));
qse_printf (QSE_T("[%s]\n"), qse_strrpbrk (x, QSE_T("d")));
qse_printf (QSE_T("[%s]\n"), qse_strpbrk (x, QSE_T("t")));
qse_printf (QSE_T("[%s]\n"), qse_strrpbrk (x, QSE_T("t")));
return 0;
}
static int test17 (void)
{
const qse_char_t* ptn[] =
{
QSE_T("*.c"),
QSE_T("h??lo.???"),
QSE_T("[a-z]*.cpp")
};
const qse_char_t* name[] =
{
QSE_T("hello.c"),
QSE_T("hello.cpp"),
QSE_T("heLLo.Cpp"),
QSE_T("/tmp/hello.c"),
QSE_T("/tmp/Hello.c")
};
int i, j;
qse_printf (QSE_T("flags => 0\n"));
for (i = 0; i < QSE_COUNTOF(ptn); i++)
for (j = 0; j < QSE_COUNTOF(name); j++)
qse_printf (QSE_T("[%s] [%s] %d\n"), ptn[i], name[j], qse_strfnmat (name[j], ptn[i], 0));
qse_printf (QSE_T("flags => QSE_STRFNMAT_PATHNAME\n"));
for (i = 0; i < QSE_COUNTOF(ptn); i++)
for (j = 0; j < QSE_COUNTOF(name); j++)
qse_printf (QSE_T("[%s] [%s] %d\n"), ptn[i], name[j], qse_strfnmat (name[j], ptn[i], QSE_STRFNMAT_PATHNAME));
qse_printf (QSE_T("flags => QSE_STRFNMAT_PATHNAME | QSE_STRFNMAT_IGNORECASE\n"));
for (i = 0; i < QSE_COUNTOF(ptn); i++)
for (j = 0; j < QSE_COUNTOF(name); j++)
qse_printf (QSE_T("[%s] [%s] %d\n"), ptn[i], name[j], qse_strfnmat (name[j], ptn[i], QSE_STRFNMAT_PATHNAME | QSE_STRFNMAT_IGNORECASE));
return 0;
}
static int test18 (void)
{
qse_str_t* s1 = QSE_NULL;
s1 = qse_str_open (QSE_MMGR_GETDFL(), 0, 5);
if (s1 == QSE_NULL)
{
qse_printf (QSE_T("cannot open a string\n"));
return -1;
}
qse_str_ncat (s1, QSE_T("["), 1);
qse_str_ncatwcs (s1, QSE_WT("hello"), 5, qse_findcmgrbyid(QSE_CMGR_UTF8));
qse_str_ncatmbs (s1, QSE_MT("world"), 5, qse_findcmgrbyid(QSE_CMGR_UTF8));
qse_str_ncat (s1, QSE_T("]"), 1);
QSE_TESASSERT1 (QSE_STR_LEN(s1) == 12, QSE_T("wrong length of dynamic string"));
qse_str_ncatmbs (s1, QSE_MT("\xEB\xAC\xB4\xEB\xAC\xB4\xEB\xAC\xB4"), 9, qse_findcmgrbyid(QSE_CMGR_UTF8));
#if defined(QSE_CHAR_IS_MCHAR)
QSE_TESASSERT1 (QSE_STR_LEN(s1) == 21, QSE_T("wrong length of dynamic string"));
#else
QSE_TESASSERT1 (QSE_STR_LEN(s1) == 15, QSE_T("wrong length of dynamic string"));
#endif
qse_str_ncatwcs (s1, QSE_WT("날아올라라"), 5, qse_findcmgrbyid(QSE_CMGR_UTF8));
#if defined(QSE_CHAR_IS_MCHAR)
QSE_TESASSERT1 (QSE_STR_LEN(s1) == 36, QSE_T("wrong length of dynamic string"));
#else
QSE_TESASSERT1 (QSE_STR_LEN(s1) == 20, QSE_T("wrong length of dynamic string"));
#endif
qse_printf (QSE_T("\t%js\n"), QSE_STR_PTR(s1));
qse_str_close (s1);
qse_printf (QSE_T("\tOK\n"));
return 0;
oops:
if (s1) qse_str_close (s1);
return -1;
}
int main ()
{
#if defined(_WIN32)
char codepage[100];
UINT old_cp = GetConsoleOutputCP();
SetConsoleOutputCP (CP_UTF8);
/* TODO: on windows this set locale only affects those mbcs fucntions in clib.
* it doesn't support utf8 i guess find a working way. the following won't work
sprintf (codepage, ".%d", GetACP());
setlocale (LC_ALL, codepage);
*/
#else
setlocale (LC_ALL, "");
#endif
qse_open_stdsios ();
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 (test11);
R (test12);
R (test13);
R (test14);
R (test15);
R (test16);
R (test17);
R (test18);
qse_close_stdsios ();
#if defined(_WIN32)
SetConsoleOutputCP (old_cp);
#endif
return 0;
}

139
samples/cmn/str02.cpp Normal file
View File

@ -0,0 +1,139 @@
#include <qse/si/sio.h>
#include <qse/cmn/String.hpp>
#include <qse/cmn/HeapMmgr.hpp>
#include <locale.h>
void t1 ()
{
QSE::HeapMmgr heap_mmgr (30000, QSE::Mmgr::getDFL());
QSE::String* z = new QSE::String();
{
QSE::String x (QSE_T("this is a sample string"), &heap_mmgr);
QSE::String y (x);
*z = y;
//z->setCharAt (0, QSE_T('Q'));
//z->prepend (QSE_T("ok."));
z->append (*z);
for (int i = 0; i < 80; i++)
{
z->prepend (QSE_T("ok."));
z->insert (10, QSE_T("XXX"));
}
z->update (10, 2, QSE_T("ZZZZ"));
//z->update (QSE_T("QQQ"));
z->replace (QSE_T("XX"), QSE_T("^"));
//z->invert();
qse_printf (QSE_T("x: [%s] [%c] capa=%d len=%d\n"), x.getBuffer(), x[0u], (int)x.getCapacity(), (int)x.getLength());
qse_printf (QSE_T("z: [%s] [%c] capa=%d len=%d\n"), z->getBuffer(), (*z)[0u], (int)z->getCapacity(), (int)z->getLength());
qse_printf (QSE_T("%d %d\n"), (int)z->findIndex (0, QSE_T("K")), (int)z->findLastIndex (0, QSE_T("K")));
qse_printf (QSE_T("%d %d %d\n"), z->beginsWith (QSE_T("ok.ok")), z->beginsWith (QSE_T("ok.okX")), z->endsWith (QSE_T("string")));
////////////////////////////////////////////////////
QSE::String t(QSE_T(" hello world good "));
t.trim ();
QSE_ASSERT (t.getLength() == 20);
qse_printf (QSE_T("t: [%s] %d\n"), t.getBuffer(), t.getLength());
t = QSE_T(" come on and join me ");
QSE_ASSERT (t.getLength() == 24);
t.trimLeft ();
QSE_ASSERT (t.getLength() == 22);
qse_printf (QSE_T("t: [%s] %d\n"), t.getBuffer(), t.getLength());
t = QSE_T(" come on and join me ");
t.trimRight ();
QSE_ASSERT (t.getLength() == 21);
qse_printf (QSE_T("t: [%s] %d\n"), t.getBuffer(), t.getLength());
////////////////////////////////////////////////////
QSE::String q (z->getSubstring (4, 10));
QSE_ASSERT (q.getLength() == 10);
QSE_ASSERT (q.getCharAt(0) == z->getCharAt(4));
qse_printf (QSE_T("q: [%s] %d\n"), q.getBuffer(), q.getLength());
q = z->getSubstring (z->getLength() - 5);
QSE_ASSERT (q.getLength() == 5);
qse_printf (QSE_T("q: [%s] %d\n"), q.getBuffer(), q.getLength());
QSE::PercentageGrowthPolicy gp(1);
QSE::String g1(128), g2(128);
QSE_ASSERT (g1.getCapacity() == 128);
QSE_ASSERT (g2.getCapacity() == 128);
QSE_ASSERT (g1.getLength() == 0);
QSE_ASSERT (g2.getLength() == 0);
g2.setGrowthPolicy (&gp);
for (int i = 0; i < 1500; i++)
{
g1.append (i);
g2.append (i);
}
qse_printf (QSE_T("g1: %d %d g2: %d %d\n"), (int)g1.getCapacity(), (int)g1.getLength(), (int)g2.getCapacity(), (int)g2.getLength());
g1.compact();
g2.compact();
qse_printf (QSE_T("g1: %d %d g2: %d %d\n"), (int)g1.getCapacity(), (int)g1.getLength(), (int)g2.getCapacity(), (int)g2.getLength());
}
qse_printf (QSE_T("-----------------\n"));
delete z;
}
#include <stdio.h>
void t2()
{
QSE::MbString x(QSE_MT("this is a string"));
QSE::MbString z(QSE_MT("this is a string"));
qse_printf (QSE_T("x: [%hs] capa=%d len=%d\n"), x.getBuffer(), (int)x.getCapacity(), (int)x.getLength());
QSE::MbString y(x);
y.format (QSE_MT("what is this %u %u fuck i don't like [%hs] 01234567890123456789 [%lu] [%ld]"), (int)10, (int)20, QSE_MT("what is what"), (unsigned long)QSE_TYPE_MAX(unsigned long), (long)QSE_TYPE_MAX(long));
qse_printf (QSE_T("y: [%hs] capa=%d len=%d\n"), y.getBuffer(), (int)y.getCapacity(), (int)y.getLength());
qse_size_t zl = z.getLength();
z.append (QSE_MT("is this good how do you do 0123456789 abcdefghijklmnopqrstuvwxyz AAAAAAAAAAAAAAAAAAABBBBBBBBBBBBBBBBBVVVVVVVVVVVVVVVVVVVVVVDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD?"));
qse_printf (QSE_T("z: [%hs] capa=%d len=%d\n"), z.getBuffer(), (int)z.getCapacity(), (int)z.getLength());
QSE_ASSERT (z.getCapacity() > x.getCapacity());
z.remove (zl, z.getLength() - zl);
QSE_ASSERT (z.getCapacity() > x.getCapacity());
QSE_ASSERT (z.getLength() == x.getLength());
QSE_ASSERT (x == x.getBuffer());
QSE_ASSERT (x != y.getBuffer());
QSE_ASSERT (y == y.getBuffer());
QSE_ASSERT (y != x.getBuffer());
QSE_ASSERT (x != y);
QSE_ASSERT (y == y);
QSE_ASSERT (x == z);
QSE_ASSERT (x.getBuffer() != z.getBuffer());
z.compact ();
QSE_ASSERT (z.getCapacity() == z.getLength());
QSE_ASSERT (z.getLength() == x.getLength());
qse_printf (QSE_T("z: [%hs] capa=%d len=%d\n"), z.getBuffer(), (int)z.getCapacity(), (int)z.getLength());
z.format (QSE_MT("hello %p world"), z.getBuffer());
qse_printf (QSE_T("z: [%hs] capa=%d len=%d\n"), z.getBuffer(), (int)z.getCapacity(), (int)z.getLength());
}
int main ()
{
setlocale (LC_ALL, "");
qse_open_stdsios ();
t1 ();
qse_printf (QSE_T("=================\n"));
t2 ();
qse_printf (QSE_T("=================\n"));
qse_close_stdsios ();
return 0;
}

187
samples/cmn/time.c Normal file
View File

@ -0,0 +1,187 @@
/*
* NOTE Targets without a 64-bit or bigger integer will suffer
* since milliseconds could be too large for a 32-bit integer.
*/
#include <qse/cmn/time.h>
#include <qse/si/sio.h>
#include <locale.h>
#include <sys/time.h>
#include <time.h>
#include <stdlib.h>
#define R(f) \
do { \
qse_printf (QSE_T("== %s ==\n"), QSE_T(#f)); \
if (f() == -1) return -1; \
} while (0)
void print_time (const qse_ntime_t* nt, const qse_btime_t* bt)
{
#if defined(_WIN32)
qse_printf (QSE_T("TIME: %I64d.%I64d\n"), (__int64)nt->sec, (__int64)nt->nsec);
#elif (QSE_SIZEOF_LONG_LONG > 0)
qse_printf (QSE_T("TIME: %lld.%lld\n"), (long long)nt->sec, (long long)nt->nsec);
#else
qse_printf (QSE_T("TIME: %ld.%ld\n"), (long)nt->sec, (long)nt->nsec);
#endif
qse_printf (QSE_T("year: %d\n"), bt->year + QSE_BTIME_YEAR_BASE);
qse_printf (QSE_T("mon: %d\n"), bt->mon + 1);
qse_printf (QSE_T("mday: %d\n"), bt->mday);
qse_printf (QSE_T("wday: %d\n"), bt->wday);
qse_printf (QSE_T("hour: %d\n"), bt->hour);
qse_printf (QSE_T("min: %d\n"), bt->min);
qse_printf (QSE_T("sec: %d\n"), bt->sec);
/*qse_printf (QSE_T("msec: %d\n"), bt->msec);*/
}
static int test1 (void)
{
qse_ntime_t nt;
qse_btime_t bt;
int count = 0;
if (qse_gettime (&nt) == -1)
{
qse_printf (QSE_T("cannot get the current time\n"));
return -1;
}
do
{
qse_gmtime (&nt, &bt);
print_time (&nt, &bt);
qse_printf (QSE_T("-------------------------------\n"));
if (qse_timegm (&bt, &nt) == -1)
{
qse_printf (QSE_T("cannot covert back to ntime\n"));
qse_printf (QSE_T("===================================\n"));
}
else
{
/* note that nt.nsec is set to 0 after qse_timegm()
* since qse_btime_t doesn't have the nsec field. */
#ifdef _WIN32
qse_printf (QSE_T("back to ntime: %I64d.%I64d\n"), (__int64)nt.sec, (__int64)nt.nsec);
#elif (QSE_SIZEOF_LONG_LONG > 0)
qse_printf (QSE_T("back to ntime: %lld.%lld\n"), (long long)nt.sec, (long long)nt.nsec);
#else
qse_printf (QSE_T("back to ntime: %ld\.%ldn"), (long)nt.sec, (long)nt.nsec);
#endif
qse_gmtime (&nt, &bt);
print_time (&nt, &bt);
qse_printf (QSE_T("===================================\n"));
}
if (count > 0) break;
nt.sec *= -1;
count++;
}
while (1);
nt.nsec = 0;
#if (QSE_SIZEOF_LONG_LONG > 0)
for (nt.sec = (qse_long_t)QSE_TYPE_MIN(int); nt.sec <= (qse_long_t)QSE_TYPE_MAX(int); nt.sec += QSE_SECS_PER_DAY)
#else
for (nt.sec = QSE_TYPE_MIN(int); nt.sec < (QSE_TYPE_MAX(int) - QSE_SECS_PER_DAY * 2); nt.sec += QSE_SECS_PER_DAY)
#endif
{
#ifdef _WIN32
__time64_t t = (__time64_t)nt.sec;
#else
time_t t = (time_t)nt.sec;
#endif
qse_ntime_t qnt;
struct tm* tm;
qse_ntime_t xx;
qnt = nt;
#if 0
if (qnt.sec >= 0) qnt.sec += rand() % 1000;
else qnt.sec -= rand() % 1000;
#endif
#ifdef _WIN32
tm = _gmtime64 (&t);
#else
tm = gmtime (&t);
#endif
qse_gmtime (&qnt, &bt);
#ifdef _WIN32
qse_printf (QSE_T(">>> time %I64d.%I64d: "), (__int64)qnt.sec, (__int64)qnt.nsec);
#elif (QSE_SIZEOF_LONG_LONG > 0)
qse_printf (QSE_T(">>> time %lld.%lld: "), (long long)qnt.sec, (long long)qnt.nsec);
#else
qse_printf (QSE_T(">>> time %ld.%ld: "), (long)qnt.sec, (long)qnt.nsec);
#endif
if (tm == QSE_NULL ||
tm->tm_year != bt.year ||
tm->tm_mon != bt.mon ||
tm->tm_mday != bt.mday ||
tm->tm_wday != bt.wday ||
tm->tm_hour != bt.hour ||
tm->tm_min != bt.min ||
tm->tm_sec != bt.sec)
{
#ifdef _WIN32
qse_printf (QSE_T("[GMTIME ERROR %I64d]\n"), (__int64)t);
#elif (QSE_SIZEOF_LONG_LONG > 0)
qse_printf (QSE_T("[GMTIME ERROR %lld]\n"), (long long)t);
#else
qse_printf (QSE_T("[GMTIME ERROR %ld]\n"), (long)t);
#endif
if (tm == QSE_NULL) qse_printf (QSE_T(">> GMTIME RETURNED NULL\n"));
print_time (&qnt, &bt);
qse_printf (QSE_T("-------------------------------\n"));
}
else
{
qse_printf (QSE_T("[GMTIME OK]"));
}
if (qse_timegm (&bt, &xx) == -1)
{
qse_printf (QSE_T("[TIMEGM FAIL]\n"));
}
else
{
if (xx.sec == qnt.sec && xx.nsec == qnt.nsec)
{
qse_printf (QSE_T("[TIMEGM OK %d/%d/%d %d:%d:%d]\n"), bt.year + QSE_BTIME_YEAR_BASE, bt.mon + 1, bt.mday, bt.hour, bt.min, bt.sec);
}
else
{
#ifdef _WIN32
qse_printf (QSE_T("[TIMEGM ERROR %I64d.%I64d, %d/%d/%d %d:%d:%d]\n"), (__int64)xx.sec, (__int64)xx.nsec, bt.year + QSE_BTIME_YEAR_BASE, bt.mon + 1, bt.mday, bt.hour, bt.min, bt.sec);
#elif (QSE_SIZEOF_LONG_LONG > 0)
qse_printf (QSE_T("[TIMEGM ERROR %lld.%lld, %d/%d/%d %d:%d:%d]\n"), (long long)xx.sec, (long long)xx.nsec, bt.year + QSE_BTIME_YEAR_BASE, bt.mon + 1, bt.mday, bt.hour, bt.min, bt.sec);
#else
qse_printf (QSE_T("[TIMEGM ERROR %ld.%ld, %d/%d/%d %d:%d:%d]\n"), (long)xx.sec, (long)xx.nsec, bt.year + QSE_BTIME_YEAR_BASE, bt.mon + 1, bt.mday, bt.hour, bt.min, bt.sec);
#endif
}
}
}
return 0;
}
int main ()
{
setlocale (LC_ALL, "");
qse_open_stdsios ();
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);
qse_close_stdsios ();
return 0;
}

104
samples/cmn/tre01.c Normal file
View File

@ -0,0 +1,104 @@
#include <qse/cmn/tre.h>
#include <qse/cmn/mem.h>
#include <qse/cmn/path.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_tre_t tre;
unsigned int nsubmat;
qse_tre_match_t* mat = QSE_NULL;
if (argc != 3)
{
qse_printf (QSE_T("USAGE: %s pattern string\n"),
qse_basename(argv[0]));
return -1;
}
qse_tre_init (&tre, QSE_MMGR_GETDFL());
if (qse_tre_comp (&tre, argv[1], &nsubmat, QSE_TRE_EXTENDED) <= -1)
{
qse_printf (QSE_T("ERROR: Cannot compile pattern [%s] - %s\n"), argv[1], qse_tre_geterrmsg(&tre));
goto oops;
}
if (nsubmat > 0)
{
mat = QSE_MMGR_ALLOC (qse_tre_getmmgr(&tre), QSE_SIZEOF(*mat) * nsubmat);
if (mat == QSE_NULL)
{
qse_printf (QSE_T("ERROR: Cannot allocate submatch array\n"));
goto oops;
}
}
if (qse_tre_exec(&tre, argv[2], mat, nsubmat, 0) <= -1)
{
if (QSE_TRE_ERRNUM(&tre) == QSE_TRE_ENOMATCH) qse_printf (QSE_T("Match: NO\n"));
else
{
qse_printf (QSE_T("ERROR: Cannot not match pattern - %s\n"), qse_tre_geterrmsg(&tre));
goto oops;
}
}
else
{
unsigned int i;
qse_printf (QSE_T("Match: YES\n"));
for (i = 0; i < nsubmat; i++)
{
if (mat[i].rm_so == -1) break;
qse_printf (QSE_T("SUBMATCH[%u] = [%.*s]\n"), i,
(int)(mat[i].rm_eo - mat[i].rm_so), &argv[2][mat[i].rm_so]);
}
}
if (mat) QSE_MMGR_FREE (qse_tre_getmmgr(&tre), mat);
qse_tre_fini (&tre);
return 0;
oops:
if (mat) QSE_MMGR_FREE (qse_tre_getmmgr(&tre), mat);
qse_tre_fini (&tre);
return -1;
}
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;
}

143
samples/cmn/uri01.c Normal file
View File

@ -0,0 +1,143 @@
#include <qse/cmn/uri.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 void xxx (const qse_wchar_t* ptr, qse_size_t len)
{
if (ptr)
{
qse_size_t i;
qse_printf (QSE_T(" ["));
for (i = 0; i < len; i++)
qse_printf (QSE_T("%hc"), ptr[i]);
qse_printf (QSE_T("] "));
}
else
{
qse_printf (QSE_T(" (null) "));
}
}
static int test_main (int argc, qse_char_t* argv[])
{
static const qse_wchar_t* wcs[] =
{
QSE_WT("http://www.google.com"),
QSE_WT("http://www.google.com:80"),
QSE_WT("http://abc@www.google.com:80"),
QSE_WT("http://abc:def@www.google.com:80"),
QSE_WT("http://abc:def@www.google.com:80/"),
QSE_WT("http://abc:def@www.google.com:80/?#"),
QSE_WT("http://abc:def@www.google.com:80/abcdef/ghi?kkk=23#fragment"),
QSE_WT("http://abc:def@www.google.com:80/abcdef/ghi#fragment#fragment"),
QSE_WT("http://abc:def@www@.google.com:80/abcdef/ghi#fragment#fragment"),
QSE_WT("http://@@@@@abc:def@www@.google.com:80/abcdef/ghi#fragment#fragment")
};
qse_size_t i;
qse_uri_t uri;
for (i = 0; i < QSE_COUNTOF(wcs); i++)
{
qse_printf (QSE_T("[%ls] => "), wcs[i]);
if (qse_wcstouri (wcs[i], &uri, 0) <= -1)
{
qse_printf (QSE_T("[ERROR]"));
}
else
{
xxx (uri.scheme.ptr, uri.scheme.len);
xxx (uri.auth.user.ptr, uri.auth.user.len);
xxx (uri.auth.pass.ptr, uri.auth.pass.len);
xxx (uri.host.ptr, uri.host.len);
xxx (uri.port.ptr, uri.port.len);
xxx (uri.path.ptr, uri.path.len);
xxx (uri.query.ptr, uri.query.len);
xxx (uri.frag.ptr, uri.frag.len);
}
qse_printf (QSE_T("\n"));
}
qse_printf (QSE_T("================================================\n"));
for (i = 0; i < QSE_COUNTOF(wcs); i++)
{
qse_printf (QSE_T("[%ls] => "), wcs[i]);
if (qse_wcstouri (wcs[i], &uri, QSE_WCSTOURI_NOQUERY | QSE_WCSTOURI_NOAUTH) <= -1)
{
qse_printf (QSE_T("[ERROR]"));
}
else
{
xxx (uri.scheme.ptr, uri.scheme.len);
xxx (uri.auth.user.ptr, uri.auth.user.len);
xxx (uri.auth.pass.ptr, uri.auth.pass.len);
xxx (uri.host.ptr, uri.host.len);
xxx (uri.port.ptr, uri.port.len);
xxx (uri.path.ptr, uri.path.len);
xxx (uri.query.ptr, uri.query.len);
xxx (uri.frag.ptr, uri.frag.len);
}
qse_printf (QSE_T("\n"));
}
qse_printf (QSE_T("================================================\n"));
for (i = 0; i < QSE_COUNTOF(wcs); i++)
{
qse_printf (QSE_T("[%ls] => "), wcs[i]);
if (qse_wcstouri (wcs[i], &uri, QSE_WCSTOURI_NOQUERY | QSE_WCSTOURI_NOAUTH | QSE_WCSTOURI_NOFRAG) <= -1)
{
qse_printf (QSE_T("[ERROR]"));
}
else
{
xxx (uri.scheme.ptr, uri.scheme.len);
xxx (uri.auth.user.ptr, uri.auth.user.len);
xxx (uri.auth.pass.ptr, uri.auth.pass.len);
xxx (uri.host.ptr, uri.host.len);
xxx (uri.port.ptr, uri.port.len);
xxx (uri.path.ptr, uri.path.len);
xxx (uri.query.ptr, uri.query.len);
xxx (uri.frag.ptr, uri.frag.len);
}
qse_printf (QSE_T("\n"));
}
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;
}

229
samples/cmn/xma.c Normal file
View File

@ -0,0 +1,229 @@
#include <qse/cmn/xma.h>
#include <qse/cmn/mem.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* ptr[100];
qse_xma_t* xma = qse_xma_open (QSE_MMGR_GETDFL(), 0, 100000L);
if (xma == QSE_NULL)
{
qse_printf (QSE_T("cannot open xma\n"));
return -1;
}
ptr[0] = qse_xma_alloc (xma, 5000);
ptr[1] = qse_xma_alloc (xma, 1000);
ptr[2] = qse_xma_alloc (xma, 3000);
ptr[3] = qse_xma_alloc (xma, 1000);
/*qse_xma_dump (xma, qse_printf);
qse_xma_free (xma, ptr[0]);
qse_xma_free (xma, ptr[2]);
qse_xma_free (xma, ptr[3]); */
qse_xma_dump (xma, (qse_xma_dumper_t)qse_fprintf, QSE_STDOUT);
qse_xma_realloc (xma, ptr[0], 500);
qse_xma_realloc (xma, ptr[3], 500);
qse_xma_dump (xma, (qse_xma_dumper_t)qse_fprintf, QSE_STDOUT);
qse_xma_close (xma);
return 0;
}
static int test2 ()
{
void* ptr[100];
qse_xma_t* xma = qse_xma_open (QSE_MMGR_GETDFL(), 0, 100000L);
if (xma == QSE_NULL)
{
qse_printf (QSE_T("cannot open xma\n"));
return -1;
}
ptr[0] = qse_xma_alloc (xma, 5000);
ptr[1] = qse_xma_alloc (xma, 1000);
ptr[2] = qse_xma_alloc (xma, 3000);
ptr[3] = qse_xma_alloc (xma, 1000);
qse_xma_dump (xma, (qse_xma_dumper_t)qse_fprintf, QSE_STDOUT);
qse_xma_free (xma, ptr[0]);
qse_xma_free (xma, ptr[2]);
qse_xma_dump (xma, (qse_xma_dumper_t)qse_fprintf, QSE_STDOUT);
qse_xma_realloc (xma, ptr[1], 500);
qse_xma_dump (xma, (qse_xma_dumper_t)qse_fprintf, QSE_STDOUT);
qse_xma_close (xma);
return 0;
}
static int test3 ()
{
void* ptr[100];
qse_xma_t* xma = qse_xma_open (QSE_MMGR_GETDFL(), 0, 100000L);
if (xma == QSE_NULL)
{
qse_printf (QSE_T("cannot open xma\n"));
return -1;
}
ptr[0] = qse_xma_alloc (xma, 5000);
ptr[1] = qse_xma_alloc (xma, 1000);
ptr[2] = qse_xma_alloc (xma, 3000);
ptr[3] = qse_xma_alloc (xma, 1000);
qse_xma_dump (xma, (qse_xma_dumper_t)qse_fprintf, QSE_STDOUT);
qse_xma_free (xma, ptr[0]);
qse_xma_free (xma, ptr[2]);
qse_xma_dump (xma, (qse_xma_dumper_t)qse_fprintf, QSE_STDOUT);
ptr[1] = qse_xma_realloc (xma, ptr[1], 3000);
qse_xma_dump (xma, (qse_xma_dumper_t)qse_fprintf, QSE_STDOUT);
qse_xma_free (xma, ptr[1]);
qse_xma_dump (xma, (qse_xma_dumper_t)qse_fprintf, QSE_STDOUT);
qse_xma_close (xma);
return 0;
}
static int test4 ()
{
int i;
void* ptr[100];
qse_xma_t* xma = qse_xma_open (QSE_MMGR_GETDFL(), 0, 2000000L);
if (xma == QSE_NULL)
{
qse_printf (QSE_T("cannot open xma\n"));
return -1;
}
for (i = 0; i < 100; i++)
{
int sz = (i + 1) * 10;
/*int sz = 10240;*/
ptr[i] = qse_xma_alloc (xma, sz);
if (ptr[i] == QSE_NULL)
{
qse_printf (QSE_T("failed to alloc %d\n"), sz);
break;
}
}
for (--i; i > 0; i-= 3)
{
if (i >= 0) qse_xma_free (xma, ptr[i]);
}
/*
qse_xma_free (xma, ptr[0]);
qse_xma_free (xma, ptr[1]);
qse_xma_free (xma, ptr[2]);
*/
{
void* x, * y;
qse_xma_alloc (xma, 5000);
qse_xma_alloc (xma, 1000);
x = qse_xma_alloc (xma, 10);
y = qse_xma_alloc (xma, 40);
if (x) qse_xma_free (xma, x);
if (y) qse_xma_free (xma, y);
x = qse_xma_alloc (xma, 10);
y = qse_xma_alloc (xma, 40);
}
qse_xma_dump (xma, (qse_xma_dumper_t)qse_fprintf, QSE_STDOUT);
qse_xma_close (xma);
return 0;
}
static void* xma_alloc (qse_mmgr_t* mmgr, qse_size_t size)
{
return qse_xma_alloc (mmgr->ctx, size);
}
static void* xma_realloc (qse_mmgr_t* mmgr, void* ptr, qse_size_t size)
{
return qse_xma_realloc (mmgr->ctx, ptr, size);
}
static void xma_free (qse_mmgr_t* mmgr, void* ptr)
{
qse_xma_free (mmgr->ctx, ptr);
}
static int test5 ()
{
void* ptr[100];
qse_mmgr_t xmammgr =
{
xma_alloc,
xma_realloc,
xma_free,
QSE_NULL
};
qse_xma_t* xma1, * xma2, * xma3;
xma1 = qse_xma_open (QSE_MMGR_GETDFL(), 0, 2000000L);
if (xma1 == QSE_NULL)
{
qse_printf (QSE_T("cannot open outer xma\n"));
return -1;
}
xmammgr.ctx = xma1;
xma2 = qse_xma_open (&xmammgr, 0, 500000L);
if (xma1 == QSE_NULL)
{
qse_printf (QSE_T("cannot open inner xma\n"));
return -1;
}
xma3 = qse_xma_open (&xmammgr, 0, 500000L);
if (xma1 == QSE_NULL)
{
qse_printf (QSE_T("cannot open inner xma\n"));
return -1;
}
qse_xma_alloc (xma2, 10345);
qse_xma_alloc (xma3, 200301);
qse_xma_alloc (xma2, 20000);
ptr[0] = qse_xma_alloc (xma3, 40031);
qse_xma_alloc (xma3, 8);
qse_xma_realloc (xma3, ptr[0], 40000);
qse_xma_dump (xma3, (qse_xma_dumper_t)qse_fprintf, QSE_STDOUT);
qse_xma_dump (xma2, (qse_xma_dumper_t)qse_fprintf, QSE_STDOUT);
qse_xma_dump (xma1, (qse_xma_dumper_t)qse_fprintf, QSE_STDOUT);
qse_xma_close (xma3);
qse_xma_close (xma2);
qse_xma_close (xma1);
return 0;
}
int main ()
{
qse_open_stdsios ();
R (test1);
R (test2);
R (test3);
R (test4);
R (test5);
qse_close_stdsios ();
return 0;
}