Reformat: delete trailing whitespace
[yaz-moved-to-github.git] / src / icu_utf16.c
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) 1995-2012 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
26 #include <unicode/ustring.h>  /* some more string fcns*/
27 #include <unicode/uchar.h>    /* char names           */
28
29 struct icu_buf_utf16 * icu_buf_utf16_create(size_t capacity)
30 {
31     struct icu_buf_utf16 * buf16
32         = (struct icu_buf_utf16 *) xmalloc(sizeof(struct icu_buf_utf16));
33
34     buf16->utf16 = 0;
35     buf16->utf16_len = 0;
36     buf16->utf16_cap = 0;
37
38     if (capacity > 0)
39     {
40         buf16->utf16 = (UChar *) xmalloc(sizeof(UChar) * capacity);
41         buf16->utf16[0] = (UChar) 0;
42         buf16->utf16_cap = capacity;
43     }
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         icu_buf_utf16_clear(buf16);
73         buf16->utf16_cap = capacity;
74     }
75     else
76     {
77         xfree(buf16->utf16);
78         buf16->utf16 = 0;
79         buf16->utf16_len = 0;
80         buf16->utf16_cap = 0;
81     }
82     return buf16;
83 }
84
85
86 struct icu_buf_utf16 * icu_buf_utf16_copy(struct icu_buf_utf16 * dest16,
87                                           const struct icu_buf_utf16 * src16)
88 {
89     if (!dest16 || !src16 || dest16 == src16)
90         return 0;
91
92     if (dest16->utf16_cap < src16->utf16_len)
93         icu_buf_utf16_resize(dest16, src16->utf16_len * 2);
94
95     u_strncpy(dest16->utf16, src16->utf16, src16->utf16_len);
96     dest16->utf16_len = src16->utf16_len;
97
98     return dest16;
99 }
100
101 void icu_buf_utf16_destroy(struct icu_buf_utf16 * buf16)
102 {
103     if (buf16)
104         xfree(buf16->utf16);
105     xfree(buf16);
106 }
107
108
109 #endif /* YAZ_HAVE_ICU */
110
111 /*
112  * Local variables:
113  * c-basic-offset: 4
114  * c-file-style: "Stroustrup"
115  * indent-tabs-mode: nil
116  * End:
117  * vim: shiftwidth=4 tabstop=8 expandtab
118  */
119