1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
|
/* This file is part of the YAZ toolkit.
* Copyright (C) 1995-2012 Index Data
* See the file LICENSE for details.
*/
#if HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdlib.h>
#include <stdio.h>
#include <yaz/query-charset.h>
#include <yaz/copy_types.h>
#include <yaz/pquery.h>
#include <yaz/querytowrbuf.h>
#include <yaz/test.h>
enum query_charset_status {
NO_ERROR,
PQF_FAILED,
MATCH,
NO_MATCH,
CONV_FAILED
};
enum query_charset_status t(yaz_iconv_t cd,
const char *pqf, const char *expect_pqf)
{
YAZ_PQF_Parser parser = yaz_pqf_create();
ODR odr = odr_createmem(ODR_ENCODE);
Z_RPNQuery *rpn;
enum query_charset_status status = NO_ERROR;
YAZ_CHECK(parser);
YAZ_CHECK(odr);
rpn = yaz_pqf_parse(parser, odr, pqf);
yaz_pqf_destroy(parser);
if (!rpn)
status = PQF_FAILED;
else
{
WRBUF w = wrbuf_alloc();
Z_RPNQuery *r2 = yaz_copy_z_RPNQuery(rpn, odr);
YAZ_CHECK(r2);
YAZ_CHECK(r2 != rpn);
yaz_query_charset_convert_rpnquery(rpn, odr, cd);
yaz_rpnquery_to_wrbuf(w, rpn);
if (!expect_pqf || strcmp(expect_pqf, wrbuf_cstr(w)) == 0)
status = MATCH;
else
{
status = NO_MATCH;
printf("Result: %s\n", wrbuf_cstr(w));
}
wrbuf_destroy(w);
}
odr_destroy(odr);
return status;
}
static void tst(void)
{
yaz_iconv_t cd = yaz_iconv_open("iso-8859-1", "utf-8");
YAZ_CHECK(cd);
if (!cd)
return;
YAZ_CHECK_EQ(t(cd, "@attr 1=4 bad query", 0), PQF_FAILED);
YAZ_CHECK_EQ(t(cd, "@attr 1=4 ok", "@attrset Bib-1 @attr 1=4 ok"), MATCH);
/* m followed by latin smaller letter ae */
YAZ_CHECK_EQ(t(cd, "@attr 1=4 m\xc3\xa6", "@attrset Bib-1 @attr 1=4 m\xe6"), MATCH);
yaz_iconv_close(cd);
}
int main (int argc, char **argv)
{
YAZ_CHECK_INIT(argc, argv);
tst();
YAZ_CHECK_TERM;
}
/*
* Local variables:
* c-basic-offset: 4
* c-file-style: "Stroustrup"
* indent-tabs-mode: nil
* End:
* vim: shiftwidth=4 tabstop=8 expandtab
*/
|