Commit b4e6ddcd authored by Pekka Pessi's avatar Pekka Pessi
Browse files

Added NSTAG_TYPEDEF(t) and ns_tag_class[].

darcs-hash:20060105170537-65a35-6a9f8a7a1df218a4965c0c754035ef108b733447.gz
parent dde836be
......@@ -45,7 +45,3 @@ extern tag_class_t skip_tag_class[1];
extern tag_class_t next_tag_class[1];
extern tag_class_t any_tag_class[1];
tag_typedef_t tag_null = TAG_TYPEDEF(tag_null, null);
tag_typedef_t tag_skip = TAG_TYPEDEF(tag_skip, skip);
tag_typedef_t tag_next = TAG_TYPEDEF(tag_next, next);
tag_typedef_t tag_any = TAG_TYPEDEF(tag_any, any);
......@@ -43,7 +43,8 @@ SOFIA_BEGIN_DECLS
/* Macros for defining tag classes */
#ifndef TAG_NAMESPACE
#define TAG_NAMESPACE "su"
/** Default namespace for tags */
#define TAG_NAMESPACE ""
#endif
#define TAG_TYPEDEF(t, type) \
......@@ -55,6 +56,7 @@ SOFIA_BEGIN_DECLS
#define PTRTAG_TYPEDEF(t) TAG_TYPEDEF(t, ptr)
#define CSTRTAG_TYPEDEF(t) TAG_TYPEDEF(t, cstr)
#define STRTAG_TYPEDEF(t) TAG_TYPEDEF(t, str)
#define NSTAG_TYPEDEF(t) TAG_TYPEDEF(t, ns)
struct tag_class_s {
int tc_size;
......@@ -81,6 +83,7 @@ SU_DLL extern tag_class_t bool_tag_class[];
SU_DLL extern tag_class_t ptr_tag_class[];
SU_DLL extern tag_class_t cstr_tag_class[];
SU_DLL extern tag_class_t str_tag_class[];
SU_DLL extern tag_class_t ns_tag_class[];
#define REFTAG_TYPEDEF(tag) \
{{ TAG_NAMESPACE, #tag "_ref", ref_tag_class, (tag_value_t)tag }}
......
......@@ -68,6 +68,8 @@ int tstflags = 0;
#define TAG_K(i) tag_k, tag_int_v((i))
#define TAG_K_REF(i) tag_k_ref, tag_int_vr(&(i))
#define TAG_ANY_PQ() tag_any_pq, (tag_value_t)0
#define TAG_P(i) tag_p, tag_bool_v((i))
#define TAG_P_REF(i) tag_p_ref, tag_bool_vr(&(i))
#define TAG_Q(i) tag_q, tag_bool_v((i))
......@@ -97,6 +99,7 @@ typedef struct tag_type_s tag_typedef_win_t[1];
tag_typedef_win_t tag_a, tag_a_ref, tag_b, tag_b_ref;
tag_typedef_win_t tag_i, tag_i_ref, tag_j, tag_j_ref;
tag_typedef_win_t tag_k, tag_k_ref, tag_n, tag_n_ref;
tag_typedef_win_t tag_any_pq;
tag_typedef_win_t tag_p, tag_p_ref, tag_q, tag_q_ref;
#else
......@@ -118,6 +121,11 @@ tag_typedef_t tag_k_ref = REFTAG_TYPEDEF(tag_k);
tag_typedef_t tag_n = INTTAG_TYPEDEF(n);
tag_typedef_t tag_n_ref = REFTAG_TYPEDEF(tag_n);
#undef TAG_NAMESPACE
#define TAG_NAMESPACE "pq"
tag_typedef_t tag_any_pq = NSTAG_TYPEDEF(*);
tag_typedef_t tag_p = BOOLTAG_TYPEDEF(p);
tag_typedef_t tag_p_ref = REFTAG_TYPEDEF(tag_p);
......@@ -149,7 +157,12 @@ int main(int argc, char *argv[])
tag_typedef_t _tag_n = INTTAG_TYPEDEF(n);
tag_typedef_t _tag_n_ref = REFTAG_TYPEDEF(tag_n);
#undef TAG_NAMESPACE
#define TAG_NAMESPACE "pq"
tag_typedef_t _tag_any_pq = NSTAG_TYPEDEF(*);
tag_typedef_t _tag_p = BOOLTAG_TYPEDEF(p);
tag_typedef_t _tag_p_ref = REFTAG_TYPEDEF(tag_p);
......@@ -168,6 +181,7 @@ int main(int argc, char *argv[])
*(struct tag_type_s *)tag_k_ref = *_tag_k_ref;
*(struct tag_type_s *)tag_n = *_tag_n;
*(struct tag_type_s *)tag_n_ref = *_tag_n_ref;
*(struct tag_type_s *)tag_any_pq = *_tag_any_pq;
*(struct tag_type_s *)tag_p = *_tag_p;
*(struct tag_type_s *)tag_p_ref = *_tag_p_ref;
*(struct tag_type_s *)tag_q = *_tag_q;
......@@ -349,13 +363,17 @@ static int test_filters(void)
{
tagi_t *lst, *filter1, *filter2, *filter3, *filter4, *b1, *b2, *b3, *b4;
tagi_t *nsfilter, *b5;
BEGIN();
lst = tl_list(TAG_A("Moro"),
TAG_I(2),
TAG_I(2),
TAG_Q(3),
TAG_SKIP(2),
TAG_A("Vaan"),
TAG_I(1),
TAG_P(2),
TAG_NULL());
filter1 = tl_list(TAG_A(""), TAG_NULL());
......@@ -382,11 +400,20 @@ static int test_filters(void)
b4 = tl_afilter(NULL, filter4, lst);
TEST(tl_len(b4), 5 * sizeof(tagi_t));
TEST(tl_len(b4), 7 * sizeof(tagi_t));
TEST(tl_xtra(b4, 0), strlen("Moro" "Vaan") + 2);
su_free(NULL, b1); su_free(NULL, b2); su_free(NULL, b3); su_free(NULL, b4);
nsfilter = tl_list(TAG_ANY_PQ(), TAG_END());
b5 = tl_afilter(NULL, nsfilter, lst);
TEST(tl_len(b5), 3 * sizeof(tagi_t));
TEST(tl_xtra(b5, 0), 0);
TEST(b5[0].t_tag, tag_q);
TEST(b5[1].t_tag, tag_p);
tl_vfree(filter1); tl_vfree(filter2); tl_vfree(filter3); tl_vfree(filter4);
tl_vfree(lst);
......
......@@ -423,7 +423,10 @@ int tl_tgets(tagi_t lst[], tag_type_t tag, tag_value_t value, ...)
/** Filter an element in tag list */
tagi_t *t_filter(tagi_t *dst, tagi_t const filter[], tagi_t const *src, void **bb)
tagi_t *t_filter(tagi_t *dst,
tagi_t const filter[],
tagi_t const *src,
void **bb)
{
tag_type_t tt = TAG_TYPE_OF(src);
tagi_t const *f;
......@@ -818,6 +821,8 @@ tag_class_t null_tag_class[1] =
/* tc_scan */ NULL,
}};
tag_typedef_t tag_null = TAG_TYPEDEF(tag_null, null);
/* ====================================================================== */
/* end tag */
......@@ -892,6 +897,8 @@ tag_class_t skip_tag_class[1] =
/* tc_scan */ NULL,
}};
tag_typedef_t tag_skip = TAG_TYPEDEF(tag_skip, skip);
/* ====================================================================== */
/* next tag - jump to next tag list */
......@@ -945,6 +952,8 @@ tag_class_t next_tag_class[1] =
/* tc_scan */ NULL,
}};
tag_typedef_t tag_next = TAG_TYPEDEF(tag_next, next);
/* ====================================================================== */
/* any tag - match to any tag when filtering */
......@@ -981,6 +990,67 @@ tag_class_t any_tag_class[1] =
/* tc_scan */ NULL,
}};
tag_typedef_t tag_any = TAG_TYPEDEF(tag_any, any);
/* ====================================================================== */
/* ns tag - match to any tag with same namespace when filtering */
static
tagi_t *t_ns_filter(tagi_t *dst,
tagi_t const filter[],
tagi_t const *src,
void **bb)
{
char const *match, *ns;
if (!src)
return dst;
assert(filter);
match = TAG_TYPE_OF(filter)->tt_ns;
ns = TAG_TYPE_OF(src)->tt_ns;
if (match == NULL)
/* everything matches with this */;
else if (match == ns)
/* namespaces matche */;
else if (ns == NULL)
/* no match */
return dst;
else if (strcmp(match, ns))
/* no match */
return dst;
if (dst) {
return t_dup(dst, src, bb);
}
else {
dst = (tagi_t *)((char *)dst + t_len(src));
*bb = (char *)*bb + t_xtra(src, (size_t)*bb);
return dst;
}
}
tag_class_t ns_tag_class[1] =
{{
sizeof(any_tag_class),
/* tc_next */ NULL,
/* tc_len */ NULL,
/* tc_move */ NULL,
/* tc_xtra */ NULL,
/* tc_dup */ NULL,
/* tc_free */ NULL,
/* tc_find */ NULL,
/* tc_snprintf */ NULL,
/* tc_filter */ t_ns_filter,
/* tc_ref_set */ NULL,
/* tc_scan */ NULL,
}};
/* ====================================================================== */
/* int tag - pass integer value */
int t_int_snprintf(tagi_t const *t, char b[], size_t size)
{
return snprintf(b, size, "%i", (int)t->t_value);
......@@ -993,7 +1063,6 @@ int t_int_ref_set(tag_type_t tt, void *ref, tagi_t const value[])
return 1;
}
int t_int_scan(tag_type_t tt, su_home_t *home,
char const *s,
tag_value_t *return_value)
......@@ -1013,7 +1082,6 @@ int t_int_scan(tag_type_t tt, su_home_t *home,
}
}
tag_class_t int_tag_class[1] =
{{
sizeof(int_tag_class),
......@@ -1030,6 +1098,8 @@ tag_class_t int_tag_class[1] =
/* tc_scan */ t_int_scan,
}};
/* ====================================================================== */
/* uint tag - pass unsigned integer value */
int t_uint_snprintf(tagi_t const *t, char b[], size_t size)
{
......@@ -1079,6 +1149,9 @@ tag_class_t uint_tag_class[1] =
}};
/* ====================================================================== */
/* bool tag - pass boolean value */
int t_bool_snprintf(tagi_t const *t, char b[], size_t size)
{
return snprintf(b, size, "%s", t->t_value ? "true" : "false");
......@@ -1133,6 +1206,9 @@ tag_class_t bool_tag_class[1] =
/* tc_scan */ t_bool_scan,
}};
/* ====================================================================== */
/* ptr tag - pass pointer value */
int t_ptr_snprintf(tagi_t const *t, char b[], size_t size)
{
return snprintf(b, size, "%p", (void *)t->t_value);
......@@ -1179,6 +1255,9 @@ tag_class_t ptr_tag_class[1] =
/* tc_scan */ NULL,
}};
/* ====================================================================== */
/* str tag - pass string value */
int t_str_snprintf(tagi_t const *t, char b[], size_t size)
{
if (t->t_value)
......@@ -1203,23 +1282,6 @@ int t_str_scan(tag_type_t tt, su_home_t *home,
return retval;
}
/** Tag class for constant strings */
tag_class_t cstr_tag_class[1] =
{{
sizeof(cstr_tag_class),
/* tc_next */ NULL,
/* tc_len */ NULL,
/* tc_move */ NULL,
/* tc_xtra */ NULL,
/* tc_dup */ NULL,
/* tc_free */ NULL,
/* tc_find */ NULL,
/* tc_snprintf */ t_str_snprintf,
/* tc_filter */ NULL,
/* tc_ref_set */ t_ptr_ref_set,
/* tc_scan */ t_str_scan,
}};
tagi_t *t_str_dup(tagi_t *dst, tagi_t const *src, void **bb)
{
dst->t_tag = src->t_tag;
......@@ -1256,6 +1318,29 @@ tag_class_t str_tag_class[1] =
/* tc_scan */ t_str_scan,
}};
/* ====================================================================== */
/* cstr tag - pass constant string value (no need to dup) */
/** Tag class for constant strings */
tag_class_t cstr_tag_class[1] =
{{
sizeof(cstr_tag_class),
/* tc_next */ NULL,
/* tc_len */ NULL,
/* tc_move */ NULL,
/* tc_xtra */ NULL,
/* tc_dup */ NULL,
/* tc_free */ NULL,
/* tc_find */ NULL,
/* tc_snprintf */ t_str_snprintf,
/* tc_filter */ NULL,
/* tc_ref_set */ t_ptr_ref_set,
/* tc_scan */ t_str_scan,
}};
/* ====================================================================== */
/* ref tag - pass reference */
tag_class_t ref_tag_class[1] =
{{
sizeof(ref_tag_class),
......@@ -1271,3 +1356,5 @@ tag_class_t ref_tag_class[1] =
/* tc_ref_set */ t_ptr_ref_set,
/* tc_scan */ NULL,
}};
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment