YAZ 5.35.1
odr_util.c
Go to the documentation of this file.
1/* This file is part of the YAZ toolkit.
2 * Copyright (C) Index Data
3 * See the file LICENSE for details.
4 */
9#if HAVE_CONFIG_H
10#include <config.h>
11#endif
12
13#include <stdlib.h>
14#include <string.h>
15#include "odr-priv.h"
16#include <yaz/oid_util.h>
17
18void odr_prname(ODR o, const char *name)
19{
20 if (o->op->indent < 16)
21 odr_printf(o, "%*s", o->op->indent * 2, "");
22 else
23 odr_printf(o, "level=%-7d%*s", o->op->indent,
24 2 * (o->op->indent % 8) , "");
25 if (name)
26 odr_printf(o, "%s ", name);
27}
28
29int odp_more_chunks(ODR o, const char *base, int len)
30{
31 if (!len)
32 return 0;
33 if (len < 0) /* indefinite length */
34 {
35 if (*o->op->bp == 0 && *(o->op->bp + 1) == 0)
36 {
37 o->op->bp += 2;
38 return 0;
39 }
40 else
41 return 1;
42 }
43 else
44 return o->op->bp - base < len;
45}
46
48{
49 Odr_oid *r;
50
51 if (!o)
52 return 0;
53 if (!(r = (Odr_oid *)
54 nmem_malloc(nmem, (oid_oidlen(o) + 1) * sizeof(Odr_oid))))
55 return 0;
56 oid_oidcpy(r, o);
57 return r;
58}
59
61{
62 return odr_oiddup_nmem(odr_getmem(odr), o);
63}
64
65Odr_oid *odr_getoidbystr_nmem(NMEM nmem, const char *str)
66{
67 Odr_oid oid[OID_SIZE];
68 Odr_oid *ret;
69
70 if (oid_dotstring_to_oid(str, oid))
71 return 0;
72 ret = (Odr_oid *)nmem_malloc(nmem, sizeof(*ret)*(oid_oidlen(oid) + 1));
73 oid_oidcpy(ret, oid);
74 return ret;
75}
76
77Odr_oid *odr_getoidbystr(ODR o, const char *str)
78{
79 return odr_getoidbystr_nmem(odr_getmem(o), str);
80}
81
82int odr_missing(ODR o, int opt, const char *name)
83{
84 if (o->error)
85 return 0;
86 if (!opt)
87 {
88 odr_seterror(o, OREQUIRED, 53);
90 }
91 return opt;
92}
93
94/*
95 * Reallocate the buffer `old', using the ODR memory pool `o' to be
96 * big enough to hold its existing value (if any) plus `prefix' (if
97 * any) and a separator character. Copy `prefix', a forward slash and
98 * the old value into the new area and return its address. Can be
99 * used as follows:
100 * initRequest->implementationName = odr_prepend(o,
101 * initRequest->implementationName, "ZOOM-C");
102 */
103char *odr_prepend(ODR o, const char *prefix, const char *old)
104{
105 int plen = (prefix == 0) ? 0 : strlen(prefix);
106 int olen = (old == 0) ? 0 : strlen(old);
107 char *res = (char*) odr_malloc (o, olen + plen + 2);
108
109 *res = '\0';
110 if (plen > 0)
111 strcpy(res, prefix);
112 if (plen > 0 && old != 0)
113 strcat(res, "/");
114 if (old != 0)
115 strcat(res, old);
116 return res;
117}
118/*
119 * Local variables:
120 * c-basic-offset: 4
121 * c-file-style: "Stroustrup"
122 * indent-tabs-mode: nil
123 * End:
124 * vim: shiftwidth=4 tabstop=8 expandtab
125 */
126
int opt
Definition initopt.c:19
char * name
Definition initopt.c:18
void * nmem_malloc(NMEM n, size_t size)
allocates memory block on NMEM handle
Definition nmem.c:145
Internal ODR definitions.
void odr_setelement(ODR o, const char *element)
Definition odr.c:125
void odr_seterror(ODR o, int error, int id)
Definition odr.c:118
void odr_printf(ODR o, const char *fmt,...)
Definition odr.c:290
#define odr_getmem(o)
Definition odr.h:216
#define OREQUIRED
Definition odr.h:154
void * odr_malloc(ODR o, size_t size)
Definition odr_mem.c:31
Odr_oid * odr_oiddup(ODR odr, const Odr_oid *o)
Definition odr_util.c:60
Odr_oid * odr_getoidbystr(ODR o, const char *str)
Definition odr_util.c:77
void odr_prname(ODR o, const char *name)
Definition odr_util.c:18
Odr_oid * odr_getoidbystr_nmem(NMEM nmem, const char *str)
Definition odr_util.c:65
int odr_missing(ODR o, int opt, const char *name)
Definition odr_util.c:82
int odp_more_chunks(ODR o, const char *base, int len)
Definition odr_util.c:29
char * odr_prepend(ODR o, const char *prefix, const char *old)
Definition odr_util.c:103
Odr_oid * odr_oiddup_nmem(NMEM nmem, const Odr_oid *o)
Definition odr_util.c:47
int oid_dotstring_to_oid(const char *name, Odr_oid *oid)
converts dot string to OID
Definition oid_util.c:75
void oid_oidcpy(Odr_oid *t, const Odr_oid *s)
copies OID
Definition oid_util.c:22
int oid_oidlen(const Odr_oid *o)
returns length of OIDs
Definition oid_util.c:49
Header for OID basic functions.
short Odr_oid
Definition oid_util.h:42
#define OID_SIZE
Definition oid_util.h:39
const char * bp
Definition odr-priv.h:85
Definition odr.h:125
struct Odr_private * op
Definition odr.h:132
int error
Definition odr.h:128