added hawk_subst_for_uchars_to_ucstr(), hawk_subst_for_ucstr_to_ucstr() hawk_subst_for_bchars_to_bcstr(), hawk_subst_for_bcstr_to_bcstr()

This commit is contained in:
hyung-hwan 2020-04-17 11:10:47 +00:00
parent 0b9d29ba72
commit 7e9d7d8015
7 changed files with 490 additions and 33 deletions

View File

@ -295,6 +295,41 @@ typedef int (*hawk_sort_comperx_t) (
int* cv
);
/* =========================================================================
* SUBSTITUTION
* ========================================================================= */
#define HAWK_SUBST_NOBUF ((void*)1)
/**
* The hawk_subst_ucs_t type defines a callback function for hawk_subst_for_uchars_to_ucstr()
* and hawk_subst_for_ucstr_to_ucstr() to substitue a new value for an identifier \a ident.
*/
typedef hawk_uch_t* (*hawk_subst_for_ucs_t) (
hawk_uch_t* buf,
hawk_oow_t bsz,
const hawk_ucs_t* ident,
void* ctx
);
/**
* The hawk_subst_bcs_t type defines a callback function for hawk_subst_for_bchars_to_bcstr()
* and hawk_subst_for_bcstr_to_bcstr() to substitue a new value for an identifier \a ident.
*/
typedef hawk_bch_t* (*hawk_subst_for_bcs_t) (
hawk_bch_t* buf,
hawk_oow_t bsz,
const hawk_bcs_t* ident,
void* ctx
);
#if defined(HAWK_OOCH_IS_UCH)
# define hawk_subst_for_oocs_t hawk_subst_for_ucs_t
#else
# define hawk_subst_for_oocs_t hawk_subst_for_bcs_t
#endif
#if defined(__cplusplus)
extern "C" {
#endif
@ -704,6 +739,40 @@ HAWK_EXPORT void hawk_unescape_bcstr (
);
HAWK_EXPORT hawk_oow_t hawk_subst_for_uchars_to_ucstr (
hawk_uch_t* buf,
hawk_oow_t bsz,
const hawk_uch_t* fmt,
hawk_oow_t fsz,
hawk_subst_for_ucs_t subst,
void* ctx
);
HAWK_EXPORT hawk_oow_t hawk_subst_for_ucstr_to_ucstr (
hawk_uch_t* buf,
hawk_oow_t bsz,
const hawk_uch_t* fmt,
hawk_subst_for_ucs_t subst,
void* ctx
);
HAWK_EXPORT hawk_oow_t hawk_subst_for_bchars_to_bcstr (
hawk_bch_t* buf,
hawk_oow_t bsz,
const hawk_bch_t* fmt,
hawk_oow_t fsz,
hawk_subst_for_bcs_t subst,
void* ctx
);
HAWK_EXPORT hawk_oow_t hawk_subst_for_bcstr_to_bcstr (
hawk_bch_t* buf,
hawk_oow_t bsz,
const hawk_bch_t* fmt,
hawk_subst_for_bcs_t subst,
void* ctx
);
#if defined(HAWK_OOCH_IS_UCH)
# define hawk_equal_oochars hawk_equal_uchars
# define hawk_comp_oochars hawk_comp_uchars
@ -744,6 +813,8 @@ HAWK_EXPORT void hawk_unescape_bcstr (
# define hawk_split_oocstr hawk_split_ucstr
# define hawk_tokenize_oochars hawk_tokenize_uchars
# define hawk_unescape_oocstr hawk_unescape_ucstr
# define hawk_subst_for_oochars_to_oocstr hawk_subst_for_uchars_to_ucstr
# define hawk_subst_for_oocstr_to_oocstr hawk_subst_for_ucstr_to_ucstr (
#else
# define hawk_equal_oochars hawk_equal_bchars
# define hawk_comp_oochars hawk_comp_bchars
@ -784,6 +855,8 @@ HAWK_EXPORT void hawk_unescape_bcstr (
# define hawk_split_oocstr hawk_split_bcstr
# define hawk_tokenize_oochars hawk_tokenize_bchars
# define hawk_unescape_oocstr hawk_unescape_bcstr
# define hawk_subst_for_oochars_to_oocstr hawk_subst_for_bchars_to_bcstr
# define hawk_subst_for_oocstr_to_oocstr hawk_subst_for_bcstr_to_bcstr (
#endif
/* ------------------------------------------------------------------------- */

View File

@ -2180,6 +2180,310 @@ void hawk_unescape_bcstr (hawk_bch_t* str)
/* ------------------------------------------------------------------------ */
static const hawk_uch_t* scan_dollar_for_subst_u (const hawk_uch_t* f, hawk_oow_t l, hawk_ucs_t* ident, hawk_ucs_t* dfl, int depth)
{
const hawk_uch_t* end = f + l;
HAWK_ASSERT (l >= 2);
f += 2; /* skip ${ */
if (ident) ident->ptr = f;
while (1)
{
if (f >= end) return HAWK_NULL;
if (*f == '}' || *f == ':') break;
f++;
}
if (*f == ':')
{
if (f >= end || *(f + 1) != '=')
{
/* not := */
return HAWK_NULL;
}
if (ident) ident->len = f - ident->ptr;
f += 2; /* skip := */
if (dfl) dfl->ptr = f;
while (1)
{
if (f >= end) return HAWK_NULL;
else if (*f == '$' && *(f + 1) == '{')
{
if (depth >= 64) return HAWK_NULL; /* depth too deep */
/* TODO: remove recursion */
f = scan_dollar_for_subst_u(f, end - f, HAWK_NULL, HAWK_NULL, depth + 1);
if (!f) return HAWK_NULL;
}
else if (*f == '}')
{
/* ending bracket */
if (dfl) dfl->len = f - dfl->ptr;
return f + 1;
}
else f++;
}
}
else if (*f == '}')
{
if (ident) ident->len = f - ident->ptr;
if (dfl)
{
dfl->ptr = HAWK_NULL;
dfl->len = 0;
}
return f + 1;
}
/* this part must not be reached */
return HAWK_NULL;
}
static hawk_uch_t* exapnd_dollar_for_subst_u (hawk_uch_t* buf, hawk_oow_t bsz, const hawk_ucs_t* ident, const hawk_ucs_t* dfl, hawk_subst_for_ucs_t subst, void* ctx)
{
hawk_uch_t* tmp;
tmp = subst(buf, bsz, ident, ctx);
if (tmp == HAWK_NULL)
{
/* substitution failed */
if (dfl->len > 0)
{
/* take the default value */
hawk_oow_t len;
/* TODO: remove recursion */
len = hawk_subst_for_uchars_to_ucstr(buf, bsz, dfl->ptr, dfl->len, subst, ctx);
tmp = buf + len;
}
else tmp = buf;
}
return tmp;
}
hawk_oow_t hawk_subst_for_uchars_to_ucstr (hawk_uch_t* buf, hawk_oow_t bsz, const hawk_uch_t* fmt, hawk_oow_t fsz, hawk_subst_for_ucs_t subst, void* ctx)
{
hawk_uch_t* b = buf;
hawk_uch_t* end = buf + bsz - 1;
const hawk_uch_t* f = fmt;
const hawk_uch_t* fend = fmt + fsz;
if (buf != HAWK_SUBST_NOBUF && bsz <= 0) return 0;
while (f < fend)
{
if (*f == '$' && f < fend - 1)
{
if (*(f + 1) == '{')
{
const hawk_uch_t* tmp;
hawk_ucs_t ident, dfl;
tmp = scan_dollar_for_subst_u(f, fend - f, &ident, &dfl, 0);
if (!tmp || ident.len <= 0) goto normal;
f = tmp;
if (buf != HAWK_SUBST_NOBUF)
{
b = exapnd_dollar_for_subst_u(b, end - b + 1, &ident, &dfl, subst, ctx);
if (b >= end) goto fini;
}
else
{
/* the buffer points to HAWK_SUBST_NOBUF. */
tmp = exapnd_dollar_for_subst_u(buf, bsz, &ident, &dfl, subst, ctx);
/* increment b by the length of the expanded string */
b += (tmp - buf);
}
continue;
}
else if (*(f + 1) == '$')
{
/* $$ -> $. */
f++;
}
}
normal:
if (buf != HAWK_SUBST_NOBUF)
{
if (b >= end) break;
*b = *f;
}
b++; f++;
}
fini:
if (buf != HAWK_SUBST_NOBUF) *b = '\0';
return b - buf;
}
hawk_oow_t hawk_subst_for_ucstr_to_ucstr (hawk_uch_t* buf, hawk_oow_t bsz, const hawk_uch_t* fmt, hawk_subst_for_ucs_t subst, void* ctx)
{
return hawk_subst_for_uchars_to_ucstr(buf, bsz, fmt, hawk_count_ucstr(fmt), subst, ctx);
}
/* ------------------------------------------------------------------------ */
static const hawk_bch_t* scan_dollar_for_subst_b (const hawk_bch_t* f, hawk_oow_t l, hawk_bcs_t* ident, hawk_bcs_t* dfl, int depth)
{
const hawk_bch_t* end = f + l;
HAWK_ASSERT (l >= 2);
f += 2; /* skip ${ */
if (ident) ident->ptr = f;
while (1)
{
if (f >= end) return HAWK_NULL;
if (*f == '}' || *f == ':') break;
f++;
}
if (*f == ':')
{
if (f >= end || *(f + 1) != '=')
{
/* not := */
return HAWK_NULL;
}
if (ident) ident->len = f - ident->ptr;
f += 2; /* skip := */
if (dfl) dfl->ptr = f;
while (1)
{
if (f >= end) return HAWK_NULL;
else if (*f == '$' && *(f + 1) == '{')
{
if (depth >= 64) return HAWK_NULL; /* depth too deep */
/* TODO: remove recursion */
f = scan_dollar_for_subst_b(f, end - f, HAWK_NULL, HAWK_NULL, depth + 1);
if (!f) return HAWK_NULL;
}
else if (*f == '}')
{
/* ending bracket */
if (dfl) dfl->len = f - dfl->ptr;
return f + 1;
}
else f++;
}
}
else if (*f == '}')
{
if (ident) ident->len = f - ident->ptr;
if (dfl)
{
dfl->ptr = HAWK_NULL;
dfl->len = 0;
}
return f + 1;
}
/* this part must not be reached */
return HAWK_NULL;
}
static hawk_bch_t* exapnd_dollar_for_subst_b (hawk_bch_t* buf, hawk_oow_t bsz, const hawk_bcs_t* ident, const hawk_bcs_t* dfl, hawk_subst_for_bcs_t subst, void* ctx)
{
hawk_bch_t* tmp;
tmp = subst(buf, bsz, ident, ctx);
if (tmp == HAWK_NULL)
{
/* substitution failed */
if (dfl->len > 0)
{
/* take the default value */
hawk_oow_t len;
/* TODO: remove recursion */
len = hawk_subst_for_bchars_to_bcstr(buf, bsz, dfl->ptr, dfl->len, subst, ctx);
tmp = buf + len;
}
else tmp = buf;
}
return tmp;
}
hawk_oow_t hawk_subst_for_bchars_to_bcstr (hawk_bch_t* buf, hawk_oow_t bsz, const hawk_bch_t* fmt, hawk_oow_t fsz, hawk_subst_for_bcs_t subst, void* ctx)
{
hawk_bch_t* b = buf;
hawk_bch_t* end = buf + bsz - 1;
const hawk_bch_t* f = fmt;
const hawk_bch_t* fend = fmt + fsz;
if (buf != HAWK_SUBST_NOBUF && bsz <= 0) return 0;
while (f < fend)
{
if (*f == '$' && f < fend - 1)
{
if (*(f + 1) == '{')
{
const hawk_bch_t* tmp;
hawk_bcs_t ident, dfl;
tmp = scan_dollar_for_subst_b(f, fend - f, &ident, &dfl, 0);
if (!tmp || ident.len <= 0) goto normal;
f = tmp;
if (buf != HAWK_SUBST_NOBUF)
{
b = exapnd_dollar_for_subst_b(b, end - b + 1, &ident, &dfl, subst, ctx);
if (b >= end) goto fini;
}
else
{
/* the buffer points to HAWK_SUBST_NOBUF. */
tmp = exapnd_dollar_for_subst_b(buf, bsz, &ident, &dfl, subst, ctx);
/* increment b by the length of the expanded string */
b += (tmp - buf);
}
continue;
}
else if (*(f + 1) == '$')
{
/* $$ -> $. */
f++;
}
}
normal:
if (buf != HAWK_SUBST_NOBUF)
{
if (b >= end) break;
*b = *f;
}
b++; f++;
}
fini:
if (buf != HAWK_SUBST_NOBUF) *b = '\0';
return b - buf;
}
hawk_oow_t hawk_subst_for_bcstr_to_bcstr (hawk_bch_t* buf, hawk_oow_t bsz, const hawk_bch_t* fmt, hawk_subst_for_bcs_t subst, void* ctx)
{
return hawk_subst_for_bchars_to_bcstr(buf, bsz, fmt, hawk_count_bcstr(fmt), subst, ctx);
}
/* ------------------------------------------------------------------------ */
hawk_oow_t hawk_int_to_oocstr (hawk_int_t value, int radix, const hawk_ooch_t* prefix, hawk_ooch_t* buf, hawk_oow_t size)
{
hawk_int_t t, rem;

View File

@ -16,8 +16,7 @@ check_SCRIPTS = h-001.hawk
##noinst_SCRIPTS = $(check_SCRIPTS)
EXTRA_DIST = $(check_SCRIPTS)
##bin_PROGRAMS = t-001 t-002 t-003 t-004
check_PROGRAMS = t-001 t-002 t-005
check_PROGRAMS = t-001 t-002 t-003 t-004 t-005
t_001_SOURCES = t-001.c t.h
@ -25,13 +24,13 @@ t_002_SOURCES = t-002.c t.h
t_002_LDADD = -lhawk $(LDADD)
t_002_DEPENDENCIES = ../lib/libhawk.la
#t_003_SOURCES = t-003.c t.h
#t_003_LDADD = -lhawk $(LDADD)
#t_003_DEPENDENCIES = ../lib/libhawk.la
#
#t_004_SOURCES = t-004.c t.h
#t_004_LDADD = -lhawk $(LDADD)
#t_004_DEPENDENCIES = ../lib/libhawk.la
t_003_SOURCES = t-003.c t.h
t_003_LDADD = -lhawk $(LDADD)
t_003_DEPENDENCIES = ../lib/libhawk.la
t_004_SOURCES = t-004.c t.h
t_004_LDADD = -lhawk $(LDADD) $(LIBM)
t_004_DEPENDENCIES = ../lib/libhawk.la
t_005_SOURCES = t-005.c t.h
t_005_LDADD = -lhawk $(LDADD)

View File

@ -87,7 +87,8 @@ PRE_UNINSTALL = :
POST_UNINSTALL = :
build_triplet = @build@
host_triplet = @host@
check_PROGRAMS = t-001$(EXEEXT) t-002$(EXEEXT) t-005$(EXEEXT)
check_PROGRAMS = t-001$(EXEEXT) t-002$(EXEEXT) t-003$(EXEEXT) \
t-004$(EXEEXT) t-005$(EXEEXT)
subdir = t
ACLOCAL_M4 = $(top_srcdir)/aclocal.m4
am__aclocal_m4_deps = $(top_srcdir)/m4/ax_check_sign.m4 \
@ -118,6 +119,10 @@ am__v_lt_1 =
am_t_002_OBJECTS = t-002.$(OBJEXT)
t_002_OBJECTS = $(am_t_002_OBJECTS)
am__DEPENDENCIES_2 = $(am__DEPENDENCIES_1)
am_t_003_OBJECTS = t-003.$(OBJEXT)
t_003_OBJECTS = $(am_t_003_OBJECTS)
am_t_004_OBJECTS = t-004.$(OBJEXT)
t_004_OBJECTS = $(am_t_004_OBJECTS)
am_t_005_OBJECTS = t-005.$(OBJEXT)
t_005_OBJECTS = $(am_t_005_OBJECTS)
AM_V_P = $(am__v_P_@AM_V@)
@ -136,7 +141,7 @@ DEFAULT_INCLUDES =
depcomp = $(SHELL) $(top_srcdir)/ac/depcomp
am__maybe_remake_depfiles = depfiles
am__depfiles_remade = ./$(DEPDIR)/t-001.Po ./$(DEPDIR)/t-002.Po \
./$(DEPDIR)/t-005.Po
./$(DEPDIR)/t-003.Po ./$(DEPDIR)/t-004.Po ./$(DEPDIR)/t-005.Po
am__mv = mv -f
COMPILE = $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) \
$(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS)
@ -156,8 +161,10 @@ AM_V_CCLD = $(am__v_CCLD_@AM_V@)
am__v_CCLD_ = $(am__v_CCLD_@AM_DEFAULT_V@)
am__v_CCLD_0 = @echo " CCLD " $@;
am__v_CCLD_1 =
SOURCES = $(t_001_SOURCES) $(t_002_SOURCES) $(t_005_SOURCES)
DIST_SOURCES = $(t_001_SOURCES) $(t_002_SOURCES) $(t_005_SOURCES)
SOURCES = $(t_001_SOURCES) $(t_002_SOURCES) $(t_003_SOURCES) \
$(t_004_SOURCES) $(t_005_SOURCES)
DIST_SOURCES = $(t_001_SOURCES) $(t_002_SOURCES) $(t_003_SOURCES) \
$(t_004_SOURCES) $(t_005_SOURCES)
am__can_run_installinfo = \
case $$AM_UPDATE_INFO_DIR in \
n|no|NO) false;; \
@ -564,14 +571,12 @@ t_001_SOURCES = t-001.c t.h
t_002_SOURCES = t-002.c t.h
t_002_LDADD = -lhawk $(LDADD)
t_002_DEPENDENCIES = ../lib/libhawk.la
#t_003_SOURCES = t-003.c t.h
#t_003_LDADD = -lhawk $(LDADD)
#t_003_DEPENDENCIES = ../lib/libhawk.la
#
#t_004_SOURCES = t-004.c t.h
#t_004_LDADD = -lhawk $(LDADD)
#t_004_DEPENDENCIES = ../lib/libhawk.la
t_003_SOURCES = t-003.c t.h
t_003_LDADD = -lhawk $(LDADD)
t_003_DEPENDENCIES = ../lib/libhawk.la
t_004_SOURCES = t-004.c t.h
t_004_LDADD = -lhawk $(LDADD) $(LIBM)
t_004_DEPENDENCIES = ../lib/libhawk.la
t_005_SOURCES = t-005.c t.h
t_005_LDADD = -lhawk $(LDADD)
t_005_DEPENDENCIES = ../lib/libhawk.la
@ -630,6 +635,14 @@ t-002$(EXEEXT): $(t_002_OBJECTS) $(t_002_DEPENDENCIES) $(EXTRA_t_002_DEPENDENCIE
@rm -f t-002$(EXEEXT)
$(AM_V_CCLD)$(LINK) $(t_002_OBJECTS) $(t_002_LDADD) $(LIBS)
t-003$(EXEEXT): $(t_003_OBJECTS) $(t_003_DEPENDENCIES) $(EXTRA_t_003_DEPENDENCIES)
@rm -f t-003$(EXEEXT)
$(AM_V_CCLD)$(LINK) $(t_003_OBJECTS) $(t_003_LDADD) $(LIBS)
t-004$(EXEEXT): $(t_004_OBJECTS) $(t_004_DEPENDENCIES) $(EXTRA_t_004_DEPENDENCIES)
@rm -f t-004$(EXEEXT)
$(AM_V_CCLD)$(LINK) $(t_004_OBJECTS) $(t_004_LDADD) $(LIBS)
t-005$(EXEEXT): $(t_005_OBJECTS) $(t_005_DEPENDENCIES) $(EXTRA_t_005_DEPENDENCIES)
@rm -f t-005$(EXEEXT)
$(AM_V_CCLD)$(LINK) $(t_005_OBJECTS) $(t_005_LDADD) $(LIBS)
@ -642,6 +655,8 @@ distclean-compile:
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/t-001.Po@am__quote@ # am--include-marker
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/t-002.Po@am__quote@ # am--include-marker
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/t-003.Po@am__quote@ # am--include-marker
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/t-004.Po@am__quote@ # am--include-marker
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/t-005.Po@am__quote@ # am--include-marker
$(am__depfiles_remade):
@ -887,6 +902,20 @@ t-002.log: t-002$(EXEEXT)
--log-file $$b.log --trs-file $$b.trs \
$(am__common_driver_flags) $(AM_LOG_DRIVER_FLAGS) $(LOG_DRIVER_FLAGS) -- $(LOG_COMPILE) \
"$$tst" $(AM_TESTS_FD_REDIRECT)
t-003.log: t-003$(EXEEXT)
@p='t-003$(EXEEXT)'; \
b='t-003'; \
$(am__check_pre) $(LOG_DRIVER) --test-name "$$f" \
--log-file $$b.log --trs-file $$b.trs \
$(am__common_driver_flags) $(AM_LOG_DRIVER_FLAGS) $(LOG_DRIVER_FLAGS) -- $(LOG_COMPILE) \
"$$tst" $(AM_TESTS_FD_REDIRECT)
t-004.log: t-004$(EXEEXT)
@p='t-004$(EXEEXT)'; \
b='t-004'; \
$(am__check_pre) $(LOG_DRIVER) --test-name "$$f" \
--log-file $$b.log --trs-file $$b.trs \
$(am__common_driver_flags) $(AM_LOG_DRIVER_FLAGS) $(LOG_DRIVER_FLAGS) -- $(LOG_COMPILE) \
"$$tst" $(AM_TESTS_FD_REDIRECT)
t-005.log: t-005$(EXEEXT)
@p='t-005$(EXEEXT)'; \
b='t-005'; \
@ -989,6 +1018,8 @@ clean-am: clean-checkPROGRAMS clean-generic clean-libtool \
distclean: distclean-am
-rm -f ./$(DEPDIR)/t-001.Po
-rm -f ./$(DEPDIR)/t-002.Po
-rm -f ./$(DEPDIR)/t-003.Po
-rm -f ./$(DEPDIR)/t-004.Po
-rm -f ./$(DEPDIR)/t-005.Po
-rm -f Makefile
distclean-am: clean-am distclean-compile distclean-generic \
@ -1037,6 +1068,8 @@ installcheck-am:
maintainer-clean: maintainer-clean-am
-rm -f ./$(DEPDIR)/t-001.Po
-rm -f ./$(DEPDIR)/t-002.Po
-rm -f ./$(DEPDIR)/t-003.Po
-rm -f ./$(DEPDIR)/t-004.Po
-rm -f ./$(DEPDIR)/t-005.Po
-rm -f Makefile
maintainer-clean-am: distclean-am maintainer-clean-generic

View File

@ -1,3 +1,4 @@
#include <hawk-fmt.h>
#include <hawk-utl.h>
#include <string.h>
#include <stdio.h>
@ -5,7 +6,7 @@
#include <locale.h>
#include "t.h"
static int put_bcs (hawk_fmtout_t* fmtout, const hawk_bch_t* c, hawk_oow_t len)
static int put_bchars (hawk_fmtout_t* fmtout, const hawk_bch_t* c, hawk_oow_t len)
{
while (len > 0)
{
@ -16,7 +17,7 @@ static int put_bcs (hawk_fmtout_t* fmtout, const hawk_bch_t* c, hawk_oow_t len)
return 1;
}
static int put_ucs (hawk_fmtout_t* fmtout, const hawk_uch_t* c, hawk_oow_t len)
static int put_uchars (hawk_fmtout_t* fmtout, const hawk_uch_t* c, hawk_oow_t len)
{
hawk_cmgr_t* cmgr = hawk_get_utf8_cmgr();
hawk_bch_t bcs[HAWK_BCSIZE_MAX];
@ -40,8 +41,8 @@ static hawk_ooi_t bfmt_out (const hawk_bch_t* fmt, ...)
int n;
memset (&fmtout, 0, HAWK_SIZEOF(fmtout));
fmtout.putbcs = put_bcs;
fmtout.putucs = put_ucs;
fmtout.putbchars = put_bchars;
fmtout.putuchars = put_uchars;
va_start (ap, fmt);
n = hawk_bfmt_outv (&fmtout, fmt, ap);

View File

@ -7,26 +7,30 @@ int main ()
{
hawk_t* hawk = HAWK_NULL;
hawk_uch_t ufmt1[] = { '%', '0', '5', 'd', ' ', '%', '-', '9', 'h', 's', '\0' };
#if 0
hawk_uch_t ufmt2[] = { '%', '0', '5', 'd', ' ', '%', '-', '9', 'h', 's', ' ', '%','O','\0' };
#endif
hawk = hawk_openstd(0, HAWK_NULL, HAWK_NULL);
hawk = hawk_openstd(0, HAWK_NULL);
if (!hawk)
{
fprintf (stderr, "Unable to open hawk\n");
return -1;
}
hawk_seterrbfmt (hawk, HAWK_EINVAL, "%d %ld %s %hs", 10, 20L, "hawk", "hawk");
hawk_seterrbfmt (hawk, HAWK_NULL, HAWK_EINVAL, "%d %ld %s %hs", 10, 20L, "hawk", "hawk");
T_ASSERT1 (hawk_comp_oocstr_bcstr(hawk_geterrmsg(hawk), "10 20 hawk hawk") == 0, "hawk seterrbfmt #1");
hawk_logbfmt (hawk, HAWK_LOG_STDERR, "[%js]\n", hawk_geterrmsg(hawk));
hawk_seterrufmt (hawk, HAWK_EINVAL, ufmt1, 9923, "hawk");
T_ASSERT1 (hawk_comp_oocstr_bcstr(hawk_geterrmsg(hawk), "09923 hawk ") == 0, "hawk seterrufmt #1");
hawk_seterrufmt (hawk, HAWK_NULL, HAWK_EINVAL, ufmt1, 9923, "hawk");
T_ASSERT1 (hawk_comp_oocstr_bcstr(hawk_geterrmsg(hawk), "09923 hawk ") == 0, "hawk seterrufmt #1");
hawk_logbfmt (hawk, HAWK_LOG_STDERR, "[%js]\n", hawk_geterrmsg(hawk));
hawk_seterrufmt (hawk, HAWK_EINVAL, ufmt2, 9923, "hawk", HAWK_SMPTR_TO_OOP(0x12345678));
#if 0
hawk_seterrufmt (hawk, HAWK_NULL, HAWK_EINVAL, ufmt2, 9923, "hawk", HAWK_SMPTR_TO_OOP(0x12345678));
T_ASSERT1 (hawk_comp_oocstr_bcstr(hawk_geterrmsg(hawk), "09923 hawk #\\p12345678") == 0, "hawk seterrufmt #1");
hawk_logbfmt (hawk, HAWK_LOG_STDERR, "[%js]\n", hawk_geterrmsg(hawk));
#endif
hawk_close (hawk);
return 0;

View File

@ -57,15 +57,57 @@ oops:
static int test2 (void)
{
hawk_uch_t src[8] = {'a','b','c','d','e','f','g','h'};
const hawk_uch_t src[8] = {'a','b','c','d','e','f','g','h'};
const hawk_uch_t sxx[6] = {'0','1','2', '\0'};
hawk_uch_t dst[6] = {'0','1','2','3','4','5'};
hawk_oow_t q, i;
hawk_oow_t q, i;
q = hawk_copy_uchars_to_ucstr(dst, HAWK_COUNTOF(dst), src, 7);
q = hawk_copy_uchars_to_ucstr(dst, HAWK_COUNTOF(dst), src, 7);
T_ASSERT0 (q == HAWK_COUNTOF(dst) - 1);
T_ASSERT0 (dst[HAWK_COUNTOF(dst) - 1] == '\0');
for (i = 0; i < q; i++) T_ASSERT0 (dst[i] == src[i]);
q = hawk_copy_ucstr_to_uchars(dst, HAWK_COUNTOF(dst), sxx);
T_ASSERT0 (q == 3);
T_ASSERT0 (dst[q] == src[q]);
for (i = 0; i < q; i++) T_ASSERT0 (dst[i] == sxx[i]);
return 0;
oops:
return -1;
}
hawk_bch_t* subst (hawk_bch_t* buf, hawk_oow_t bsz, const hawk_bcs_t* ident, void* ctx)
{
if (hawk_comp_bchars_bcstr(ident->ptr, ident->len, "USER") == 0)
{
return buf + ((buf == HAWK_SUBST_NOBUF)? 3: hawk_copy_bcstr_to_bchars(buf, bsz, "sam"));
}
else if (hawk_comp_bchars_bcstr(ident->ptr, ident->len, "GROUP") == 0)
return buf + ((buf == HAWK_SUBST_NOBUF)? 6: hawk_copy_bcstr_to_bchars(buf, bsz, "coders"));
return buf;
}
static int test3 (void)
{
hawk_bch_t buf[25], * ptr;
hawk_oow_t n;
n = hawk_subst_for_bcstr_to_bcstr (buf, HAWK_COUNTOF(buf), "user=${USER},group=${GROUP}", subst, HAWK_NULL);
T_ASSERT0 (n == 21);
T_ASSERT0 (hawk_count_bcstr(buf) == 21);
T_ASSERT0 (hawk_comp_bcstr(buf, "user=sam,group=coders", 0) == 0);
n = hawk_subst_for_bcstr_to_bcstr(HAWK_SUBST_NOBUF, 0, "USERNAME=${USER},GROUPNAME=${GROUP}", subst, HAWK_NULL);
T_ASSERT0 (n == 29);
ptr = malloc(n + 1);
n = hawk_subst_for_bcstr_to_bcstr(ptr, n + 1, "USERNAME=${USER},GROUPNAME=${GROUP}", subst, HAWK_NULL);
T_ASSERT0 (n == 29);
T_ASSERT0 (hawk_count_bcstr(ptr) == 29);
T_ASSERT0 (hawk_comp_bcstr(ptr, "USERNAME=sam,GROUPNAME=coders", 0) == 0);
free (ptr);
return 0;
oops:
@ -76,6 +118,7 @@ int main ()
{
if (test1() <= -1) goto oops;
if (test2() <= -1) goto oops;
if (test3() <= -1) goto oops;
return 0;
oops: