fix codetest failure - ASSERT_ARGS does not have a ; after and
[parrot.git] / src / string / encoding.c
blob170c8f422c8775d4a73dfa79f5a2a33652d8d391
1 /*
2 Copyright (C) 2004-2010, Parrot Foundation.
3 $Id$
5 =head1 NAME
7 src/string/encoding.c - global encoding functions
9 =head1 DESCRIPTION
11 These are parrot's generic encoding handling functions
13 =over 4
15 =cut
19 #include "parrot/encoding.h"
21 STR_VTABLE *Parrot_default_encoding_ptr = NULL;
23 static STR_VTABLE **encodings;
24 static int n_encodings;
25 /* for backwards compatibility */
26 static STRING *unicode_str;
27 static STRING *fixed_8_str;
29 /* HEADERIZER HFILE: include/parrot/encoding.h */
31 /* HEADERIZER BEGIN: static */
32 /* Don't modify between HEADERIZER BEGIN / HEADERIZER END. Your changes will be lost. */
34 /* Don't modify between HEADERIZER BEGIN / HEADERIZER END. Your changes will be lost. */
35 /* HEADERIZER END: static */
40 =item C<void Parrot_deinit_encodings(PARROT_INTERP)>
42 Deinitialize encodings and free all memory used by them.
44 =cut
48 void
49 Parrot_deinit_encodings(PARROT_INTERP)
51 ASSERT_ARGS(Parrot_deinit_encodings)
53 mem_gc_free(interp, encodings);
54 encodings = NULL;
55 n_encodings = 0;
60 =item C<STR_VTABLE * Parrot_new_encoding(PARROT_INTERP)>
62 Allocates the memory for a new string vtable from the system.
64 =cut
68 PARROT_EXPORT
69 PARROT_MALLOC
70 PARROT_CANNOT_RETURN_NULL
71 STR_VTABLE *
72 Parrot_new_encoding(PARROT_INTERP)
74 ASSERT_ARGS(Parrot_new_encoding)
75 return mem_gc_allocate_typed(interp, STR_VTABLE);
80 =item C<const STR_VTABLE * Parrot_find_encoding(PARROT_INTERP, const char
81 *encodingname)>
83 Finds an encoding with the name C<encodingname>. Returns the encoding
84 if it is successfully found, returns NULL otherwise.
86 =cut
90 PARROT_EXPORT
91 PARROT_PURE_FUNCTION
92 PARROT_WARN_UNUSED_RESULT
93 PARROT_CAN_RETURN_NULL
94 const STR_VTABLE *
95 Parrot_find_encoding(SHIM_INTERP, ARGIN(const char *encodingname))
97 ASSERT_ARGS(Parrot_find_encoding)
98 const int n = n_encodings;
99 int i;
101 for (i = 0; i < n; ++i)
102 if (STREQ(encodings[i]->name, encodingname))
103 return encodings[i];
105 /* backwards compatibility */
106 if (strcmp(encodingname, "unicode") == 0)
107 return Parrot_utf8_encoding_ptr;
109 return NULL;
114 =item C<const STR_VTABLE * Parrot_load_encoding(PARROT_INTERP, const char
115 *encodingname)>
117 Loads an encoding. Currently throws an exception because we cannot load
118 encodings. See https://trac.parrot.org/parrot/wiki/StringsTasklist.
120 =cut
124 /* Yep, this needs to be a char * parameter -- it's tough to load in
125 encodings and such for strings if we can't be sure we've got enough
126 info set up to actually build strings...
128 Also remember to use PARROT_WARN_UNUSED_RESULT and
129 PARROT_CANNOT_RETURN_NULL when this actually works.
132 PARROT_EXPORT
133 PARROT_DOES_NOT_RETURN
134 PARROT_CANNOT_RETURN_NULL
135 const STR_VTABLE *
136 Parrot_load_encoding(PARROT_INTERP, ARGIN(const char *encodingname))
138 ASSERT_ARGS(Parrot_load_encoding)
139 UNUSED(encodingname);
140 Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_UNIMPLEMENTED,
141 "Can't load encodings yet");
146 =item C<INTVAL Parrot_encoding_number(PARROT_INTERP, const STRING
147 *encodingname)>
149 Return the number of the encoding or -1 if not found.
151 =cut
155 PARROT_EXPORT
156 PARROT_PURE_FUNCTION
157 PARROT_WARN_UNUSED_RESULT
158 INTVAL
159 Parrot_encoding_number(PARROT_INTERP, ARGIN(const STRING *encodingname))
161 ASSERT_ARGS(Parrot_encoding_number)
162 const int n = n_encodings;
163 int i;
165 for (i = 0; i < n; ++i) {
166 if (Parrot_str_equal(interp, encodings[i]->name_str, encodingname))
167 return i;
170 /* backwards compatibility */
171 if (Parrot_str_equal(interp, encodingname, unicode_str)) {
172 for (i = 0; i < n; ++i) {
173 if (STREQ(encodings[i]->name, "utf8"))
174 return i;
177 else if (Parrot_str_equal(interp, encodingname, fixed_8_str)) {
178 for (i = 0; i < n; ++i) {
179 if (STREQ(encodings[i]->name, "ascii"))
180 return i;
184 return -1;
189 =item C<INTVAL Parrot_encoding_number_of_str(PARROT_INTERP, const STRING *src)>
191 Return the number of the encoding of the given string or -1 if not found.
193 This could be converted to a macro.
195 =cut
199 PARROT_EXPORT
200 PARROT_PURE_FUNCTION
201 PARROT_WARN_UNUSED_RESULT
202 INTVAL
203 Parrot_encoding_number_of_str(SHIM_INTERP, ARGIN(const STRING *src))
205 ASSERT_ARGS(Parrot_encoding_number_of_str)
207 return src->encoding->num;
212 =item C<STRING* Parrot_encoding_name(PARROT_INTERP, INTVAL number_of_encoding)>
214 Returns the name of a character encoding based on the INTVAL index
215 C<number_of_encoding> to the All_encodings array.
217 This could be converted to a macro.
219 =cut
223 PARROT_EXPORT
224 PARROT_PURE_FUNCTION
225 PARROT_WARN_UNUSED_RESULT
226 PARROT_CAN_RETURN_NULL
227 STRING*
228 Parrot_encoding_name(SHIM_INTERP, INTVAL number_of_encoding)
230 ASSERT_ARGS(Parrot_encoding_name)
231 if (number_of_encoding >= n_encodings ||
232 number_of_encoding < 0)
233 return NULL;
234 return encodings[number_of_encoding]->name_str;
239 =item C<const STR_VTABLE* Parrot_get_encoding(PARROT_INTERP, INTVAL
240 number_of_encoding)>
242 Returns the encoding given by the INTVAL index C<number_of_encoding>.
244 =cut
248 PARROT_EXPORT
249 PARROT_PURE_FUNCTION
250 PARROT_WARN_UNUSED_RESULT
251 PARROT_CAN_RETURN_NULL
252 const STR_VTABLE*
253 Parrot_get_encoding(SHIM_INTERP, INTVAL number_of_encoding)
255 ASSERT_ARGS(Parrot_get_encoding)
256 if (number_of_encoding >= n_encodings ||
257 number_of_encoding < 0)
258 return NULL;
259 return encodings[number_of_encoding];
264 =item C<const char * Parrot_encoding_c_name(PARROT_INTERP, INTVAL
265 number_of_encoding)>
267 Returns the NULL-terminated C string representation of the encodings name
268 given by the C<number_of_encoding>.
270 =cut
274 PARROT_EXPORT
275 PARROT_PURE_FUNCTION
276 PARROT_WARN_UNUSED_RESULT
277 PARROT_CAN_RETURN_NULL
278 const char *
279 Parrot_encoding_c_name(SHIM_INTERP, INTVAL number_of_encoding)
281 ASSERT_ARGS(Parrot_encoding_c_name)
282 if (number_of_encoding >= n_encodings ||
283 number_of_encoding < 0)
284 return NULL;
285 return encodings[number_of_encoding]->name;
290 =item C<void Parrot_str_internal_register_encoding_names(PARROT_INTERP)>
292 Helper function for initializing characterset encoding names. We can't create
293 the STRING names until the default encodings are already initted,
294 so the name generation is split into a second init stage.
296 =cut
301 void
302 Parrot_str_internal_register_encoding_names(PARROT_INTERP)
304 ASSERT_ARGS(Parrot_str_internal_register_encoding_names)
305 int n;
306 for (n = 0; n < n_encodings; ++n)
307 encodings[n]->name_str =
308 Parrot_str_new_constant(interp, encodings[n]->name);
309 unicode_str = Parrot_str_new_constant(interp, "unicode");
310 fixed_8_str = Parrot_str_new_constant(interp, "fixed_8");
315 =item C<INTVAL Parrot_register_encoding(PARROT_INTERP, STR_VTABLE *encoding)>
317 Registers a character encoding C<encoding> with name C<encodingname>.
318 Only allows one of 5 possibilities: fixed_8, utf8, utf16, ucs2 and ucs4.
320 =cut
324 PARROT_EXPORT
325 INTVAL
326 Parrot_register_encoding(PARROT_INTERP, ARGIN(STR_VTABLE *encoding))
328 ASSERT_ARGS(Parrot_register_encoding)
329 int i;
330 int n = n_encodings;
332 for (i = 0; i < n_encodings; ++i) {
333 if (STREQ(encodings[i]->name, encoding->name))
334 return 0;
337 if (!n)
338 encodings = mem_gc_allocate_zeroed_typed(interp, STR_VTABLE *);
339 else
340 encodings = mem_gc_realloc_n_typed_zeroed(interp,
341 encodings, n + 1, n, STR_VTABLE *);
343 encoding->num = n;
344 encodings[n] = encoding;
345 ++n_encodings;
347 return 1;
352 =item C<void Parrot_encodings_init(PARROT_INTERP)>
354 Creates the initial encodings.
356 =cut
360 PARROT_EXPORT
361 void
362 Parrot_encodings_init(PARROT_INTERP)
364 ASSERT_ARGS(Parrot_encodings_init)
366 Parrot_register_encoding(interp, Parrot_ascii_encoding_ptr);
367 Parrot_register_encoding(interp, Parrot_latin1_encoding_ptr);
368 Parrot_register_encoding(interp, Parrot_binary_encoding_ptr);
369 Parrot_register_encoding(interp, Parrot_utf8_encoding_ptr);
370 Parrot_register_encoding(interp, Parrot_utf16_encoding_ptr);
371 Parrot_register_encoding(interp, Parrot_ucs2_encoding_ptr);
372 Parrot_register_encoding(interp, Parrot_ucs4_encoding_ptr);
374 Parrot_default_encoding_ptr = Parrot_ascii_encoding_ptr;
376 /* Now that the plugins are registered, we can create STRING
377 * names for them. */
378 Parrot_str_internal_register_encoding_names(interp);
383 =item C<INTVAL Parrot_make_default_encoding(PARROT_INTERP, const char
384 *encodingname, STR_VTABLE *encoding)>
386 Sets the default encoding to C<encoding> with name C<encodingname>.
388 =cut
392 PARROT_EXPORT
393 INTVAL
394 Parrot_make_default_encoding(SHIM_INTERP, SHIM(const char *encodingname),
395 ARGIN(STR_VTABLE *encoding))
397 ASSERT_ARGS(Parrot_make_default_encoding)
398 Parrot_default_encoding_ptr = encoding;
399 return 1;
404 =item C<const STR_VTABLE * Parrot_default_encoding(PARROT_INTERP)>
406 Gets the default encoding.
408 =cut
412 PARROT_EXPORT
413 PARROT_PURE_FUNCTION
414 PARROT_WARN_UNUSED_RESULT
415 PARROT_CANNOT_RETURN_NULL
416 const STR_VTABLE *
417 Parrot_default_encoding(SHIM_INTERP)
419 ASSERT_ARGS(Parrot_default_encoding)
420 return Parrot_default_encoding_ptr;
425 * Local variables:
426 * c-file-style: "parrot"
427 * End:
428 * vim: expandtab shiftwidth=4: