CPU: Wrong CPU Load %.
[tomato.git] / release / src / router / libid3tag / ucs4.c
blob15dace86928f888fbdc33b333406aa40f6fa15dc
1 /*
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 $
22 # ifdef HAVE_CONFIG_H
23 # include "config.h"
24 # endif
26 # include "global.h"
28 # include <stdlib.h>
30 # include "id3tag.h"
31 # include "ucs4.h"
32 # include "latin1.h"
33 # include "utf16.h"
34 # include "utf8.h"
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;
46 while (*ptr)
47 ++ptr;
49 return ptr - ucs4;
53 * NAME: ucs4->size()
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;
78 while (*ucs4) {
79 ++size;
80 if (*ucs4 >= 0x00010000L &&
81 *ucs4 <= 0x0010ffffL)
82 ++size;
84 ++ucs4;
87 return size + 1;
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;
98 while (*ucs4) {
99 if (*ucs4 <= 0x0000007fL)
100 size += 1;
101 else if (*ucs4 <= 0x000007ffL)
102 size += 2;
103 else if (*ucs4 <= 0x0000ffffL)
104 size += 3;
105 else if (*ucs4 <= 0x001fffffL)
106 size += 4;
107 else if (*ucs4 <= 0x03ffffffL)
108 size += 5;
109 else if (*ucs4 <= 0x7fffffffL)
110 size += 6;
111 else
112 size += 2; /* based on U+00B7 replacement char */
114 ++ucs4;
117 return size + 1;
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));
129 if (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)
141 id3_utf16_t *utf16;
143 utf16 = malloc(id3_ucs4_utf16size(ucs4) * sizeof(*utf16));
144 if (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)
156 id3_utf8_t *utf8;
158 utf8 = malloc(id3_ucs4_utf8size(ucs4) * sizeof(*utf8));
159 if (utf8)
160 id3_utf8_encode(utf8, ucs4);
162 return release(utf8);
166 * NAME: ucs4->copy()
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)
181 id3_ucs4_t *ucs4;
183 ucs4 = malloc(id3_ucs4_size(src) * sizeof(*ucs4));
184 if (ucs4)
185 id3_ucs4_copy(ucs4, src);
187 return ucs4;
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;
198 digit = digits;
200 do {
201 *digit++ = number % 10;
202 number /= 10;
204 while (number);
206 while (digit != digits)
207 *ucs4++ = '0' + *--digit;
209 *ucs4 = 0;
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');
223 return number;