2 * libid3tag - ID3 tag manipulation library
3 * Copyright (C) 2000-2004 Underbit Technologies, Inc.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 * $Id: ucs4.c,v 1.13 2004/01/23 09:41:32 rob Exp $
36 id3_ucs4_t
const id3_ucs4_empty
[] = { 0 };
39 * NAME: ucs4->length()
40 * DESCRIPTION: return the number of ucs4 chars represented by a ucs4 string
42 id3_length_t
id3_ucs4_length(id3_ucs4_t
const *ucs4
)
44 id3_ucs4_t
const *ptr
= ucs4
;
54 * DESCRIPTION: return the encoding size of a ucs4 string
56 id3_length_t
id3_ucs4_size(id3_ucs4_t
const *ucs4
)
58 return id3_ucs4_length(ucs4
) + 1;
62 * NAME: ucs4->latin1size()
63 * DESCRIPTION: return the encoding size of a latin1-encoded ucs4 string
65 id3_length_t
id3_ucs4_latin1size(id3_ucs4_t
const *ucs4
)
67 return id3_ucs4_size(ucs4
);
71 * NAME: ucs4->utf16size()
72 * DESCRIPTION: return the encoding size of a utf16-encoded ucs4 string
74 id3_length_t
id3_ucs4_utf16size(id3_ucs4_t
const *ucs4
)
76 id3_length_t size
= 0;
80 if (*ucs4
>= 0x00010000L
&&
91 * NAME: ucs4->utf8size()
92 * DESCRIPTION: return the encoding size of a utf8-encoded ucs4 string
94 id3_length_t
id3_ucs4_utf8size(id3_ucs4_t
const *ucs4
)
96 id3_length_t size
= 0;
99 if (*ucs4
<= 0x0000007fL
)
101 else if (*ucs4
<= 0x000007ffL
)
103 else if (*ucs4
<= 0x0000ffffL
)
105 else if (*ucs4
<= 0x001fffffL
)
107 else if (*ucs4
<= 0x03ffffffL
)
109 else if (*ucs4
<= 0x7fffffffL
)
112 size
+= 2; /* based on U+00B7 replacement char */
121 * NAME: ucs4->latin1duplicate()
122 * DESCRIPTION: duplicate and encode a ucs4 string into latin1
124 id3_latin1_t
*id3_ucs4_latin1duplicate(id3_ucs4_t
const *ucs4
)
126 id3_latin1_t
*latin1
;
128 latin1
= malloc(id3_ucs4_latin1size(ucs4
) * sizeof(*latin1
));
130 id3_latin1_encode(latin1
, ucs4
);
132 return release(latin1
);
136 * NAME: ucs4->utf16duplicate()
137 * DESCRIPTION: duplicate and encode a ucs4 string into utf16
139 id3_utf16_t
*id3_ucs4_utf16duplicate(id3_ucs4_t
const *ucs4
)
143 utf16
= malloc(id3_ucs4_utf16size(ucs4
) * sizeof(*utf16
));
145 id3_utf16_encode(utf16
, ucs4
);
147 return release(utf16
);
151 * NAME: ucs4->utf8duplicate()
152 * DESCRIPTION: duplicate and encode a ucs4 string into utf8
154 id3_utf8_t
*id3_ucs4_utf8duplicate(id3_ucs4_t
const *ucs4
)
158 utf8
= malloc(id3_ucs4_utf8size(ucs4
) * sizeof(*utf8
));
160 id3_utf8_encode(utf8
, ucs4
);
162 return release(utf8
);
167 * DESCRIPTION: copy a ucs4 string
169 void id3_ucs4_copy(id3_ucs4_t
*dest
, id3_ucs4_t
const *src
)
171 while ((*dest
++ = *src
++))
176 * NAME: ucs4->duplicate()
177 * DESCRIPTION: duplicate a ucs4 string
179 id3_ucs4_t
*id3_ucs4_duplicate(id3_ucs4_t
const *src
)
183 ucs4
= malloc(id3_ucs4_size(src
) * sizeof(*ucs4
));
185 id3_ucs4_copy(ucs4
, src
);
191 * NAME: ucs4->putnumber()
192 * DESCRIPTION: write a ucs4 string containing a (positive) decimal number
194 void id3_ucs4_putnumber(id3_ucs4_t
*ucs4
, unsigned long number
)
196 int digits
[10], *digit
;
201 *digit
++ = number
% 10;
206 while (digit
!= digits
)
207 *ucs4
++ = '0' + *--digit
;
213 * NAME: ucs4->getnumber()
214 * DESCRIPTION: read a ucs4 string containing a (positive) decimal number
216 unsigned long id3_ucs4_getnumber(id3_ucs4_t
const *ucs4
)
218 unsigned long number
= 0;
220 while (*ucs4
>= '0' && *ucs4
<= '9')
221 number
= 10 * number
+ (*ucs4
++ - '0');