Merge branch 'master' of ssh://git.indexdata.com/home/git/pub/yaz
[yaz-moved-to-github.git] / src / icu_utf16.c
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) 1995-2010 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
83     return buf16;
84 }
85
86
87 struct icu_buf_utf16 * icu_buf_utf16_copy(struct icu_buf_utf16 * dest16,
88                                           const struct icu_buf_utf16 * src16)
89 {
90     if (!dest16 || !src16 || dest16 == src16)
91         return 0;
92
93     if (dest16->utf16_cap < src16->utf16_len)
94         icu_buf_utf16_resize(dest16, src16->utf16_len * 2);
95
96     u_strncpy(dest16->utf16, src16->utf16, src16->utf16_len);
97     dest16->utf16_len = src16->utf16_len;
98
99     return dest16;
100 }
101
102 void icu_buf_utf16_destroy(struct icu_buf_utf16 * buf16)
103 {
104     if (buf16)
105         xfree(buf16->utf16);
106     xfree(buf16);
107 }
108
109
110 #endif /* YAZ_HAVE_ICU */
111
112 /*
113  * Local variables:
114  * c-basic-offset: 4
115  * c-file-style: "Stroustrup"
116  * indent-tabs-mode: nil
117  * End:
118  * vim: shiftwidth=4 tabstop=8 expandtab
119  */
120