CQL: accept relations "within", "encloses"
[yaz-moved-to-github.git] / src / icu_utf16.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
6 /**
7  * \file
8  * \brief UTF-16 string utilities for ICU
9  */
10
11 #if HAVE_CONFIG_H
12 #include "config.h"
13 #endif
14
15 #if YAZ_HAVE_ICU
16 #include <yaz/xmalloc.h>
17
18 #include <yaz/icu_I18N.h>
19
20 #include <yaz/log.h>
21
22 #include <string.h>
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <assert.h>
26
27 #include <unicode/ustring.h>  /* some more string fcns*/
28 #include <unicode/uchar.h>    /* char names           */
29
30 struct icu_buf_utf16 *icu_buf_utf16_create(size_t capacity)
31 {
32     struct icu_buf_utf16 *buf16
33         = (struct icu_buf_utf16 *) xmalloc(sizeof(struct icu_buf_utf16));
34
35     buf16->utf16_len = 0;
36     buf16->utf16_cap = capacity;
37     if (capacity > 0)
38     {
39         buf16->utf16 = (UChar *) xmalloc(sizeof(UChar) * capacity);
40         buf16->utf16[0] = (UChar) 0;
41     }
42     else
43         buf16->utf16 = 0;
44     return buf16;
45 }
46
47 struct icu_buf_utf16 *icu_buf_utf16_clear(struct icu_buf_utf16 *buf16)
48 {
49     if (buf16)
50     {
51         if (buf16->utf16)
52             buf16->utf16[0] = (UChar) 0;
53         buf16->utf16_len = 0;
54     }
55     return buf16;
56 }
57
58 struct icu_buf_utf16 *icu_buf_utf16_resize(struct icu_buf_utf16 *buf16,
59                                            size_t capacity)
60 {
61     if (!buf16)
62         return 0;
63
64     if (capacity > 0)
65     {
66         if (0 == buf16->utf16)
67             buf16->utf16 = (UChar *) xmalloc(sizeof(UChar) * capacity);
68         else
69             buf16->utf16
70                 = (UChar *) xrealloc(buf16->utf16, sizeof(UChar) * capacity);
71     }
72     else
73     {
74         xfree(buf16->utf16);
75         buf16->utf16 = 0;
76     }
77     buf16->utf16_cap = capacity;
78     return buf16;
79 }
80
81
82 struct icu_buf_utf16 *icu_buf_utf16_copy(struct icu_buf_utf16 *dest16,
83                                          const struct icu_buf_utf16 *src16)
84 {
85     if (!dest16 || !src16 || dest16 == src16)
86         return 0;
87
88     if (dest16->utf16_cap < src16->utf16_len)
89         icu_buf_utf16_resize(dest16, src16->utf16_len * 2);
90
91     u_strncpy(dest16->utf16, src16->utf16, src16->utf16_len);
92     dest16->utf16_len = src16->utf16_len;
93
94     return dest16;
95 }
96
97
98 struct icu_buf_utf16 *icu_buf_utf16_append(struct icu_buf_utf16 *dest16,
99                                            const struct icu_buf_utf16 *src16)
100 {
101     assert(dest16);
102     if (!src16)
103         return dest16;
104     if (dest16 == src16)
105         return 0;
106
107     if (dest16->utf16_cap <= src16->utf16_len + dest16->utf16_len)
108         icu_buf_utf16_resize(dest16, dest16->utf16_len + src16->utf16_len * 2);
109
110     u_strncpy(dest16->utf16 + dest16->utf16_len,
111               src16->utf16, src16->utf16_len);
112     dest16->utf16_len += src16->utf16_len;
113
114     return dest16;
115 }
116
117
118 void icu_buf_utf16_destroy(struct icu_buf_utf16 *buf16)
119 {
120     if (buf16)
121         xfree(buf16->utf16);
122     xfree(buf16);
123 }
124
125 void icu_buf_utf16_log(const char *lead, struct icu_buf_utf16 *src16)
126 {
127     if (src16)
128     {
129         struct icu_buf_utf8 *dst8 = icu_buf_utf8_create(0);
130         UErrorCode status = U_ZERO_ERROR;
131         icu_utf16_to_utf8(dst8, src16, &status);
132         yaz_log(YLOG_LOG, "%s=%s", lead, dst8->utf8);
133         icu_buf_utf8_destroy(dst8);
134     }
135     else
136     {
137         yaz_log(YLOG_LOG, "%s=NULL", lead);
138     }
139 }
140
141 #endif /* YAZ_HAVE_ICU */
142
143 /*
144  * Local variables:
145  * c-basic-offset: 4
146  * c-file-style: "Stroustrup"
147  * indent-tabs-mode: nil
148  * End:
149  * vim: shiftwidth=4 tabstop=8 expandtab
150  */
151