7930d64e71d437bccd2f93781136bdf5084f1e92
[yaz-moved-to-github.git] / src / copy_types.c
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) 1995-2013 Index Data
3  * See the file LICENSE for details.
4  */
5 /** \file copy_types.c
6     \brief Copies various Z39.50 types
7  */
8 #if HAVE_CONFIG_H
9 #include <config.h>
10 #endif
11
12 #include <yaz/copy_types.h>
13
14 /** macro clone_z_type copies a given ASN.1 type */
15 #define clone_z_type(x) \
16 Z_##x *yaz_clone_z_##x(Z_##x *q, NMEM nmem_out) \
17 { \
18     Z_##x *q1 = 0; \
19     ODR enc = odr_createmem(ODR_ENCODE); \
20     ODR dec = odr_createmem(ODR_DECODE); \
21     if (z_##x(enc, &q, 0, 0)) \
22     { \
23         int len; \
24         char *buf = odr_getbuf(enc, &len, 0); \
25         if (buf) \
26         { \
27             odr_setbuf(dec, buf, len, 0); \
28             z_##x(dec, &q1, 0, 0); \
29             nmem_transfer(nmem_out, dec->mem);  \
30         } \
31     } \
32     odr_destroy(enc); \
33     odr_destroy(dec); \
34     return q1; \
35 }
36
37 clone_z_type(NamePlusRecord)
38 clone_z_type(RPNQuery)
39 clone_z_type(Query)
40 clone_z_type(RecordComposition)
41 clone_z_type(OtherInformation)
42
43 Z_RPNQuery *yaz_copy_z_RPNQuery(Z_RPNQuery *q, ODR out)
44 {
45     return yaz_clone_z_RPNQuery(q, out->mem);
46 }
47
48 Z_Query *yaz_copy_Z_Query(Z_Query *q, ODR out)
49 {
50     return yaz_clone_z_Query(q, out->mem);
51 }
52
53 /*
54  * Local variables:
55  * c-basic-offset: 4
56  * c-file-style: "Stroustrup"
57  * indent-tabs-mode: nil
58  * End:
59  * vim: shiftwidth=4 tabstop=8 expandtab
60  */
61