tagged release 0.6.4
[parrot.git] / src / encoding.c
blobbc94e58852ffeca6154b784e8825212caa33ec91
1 /*
2 Copyright (C) 2004-2007, The Perl Foundation.
3 $Id$
5 =head1 NAME
7 src/encoding.c - global encoding functions
9 =head1 DESCRIPTION
11 These are parrot's generic encoding handling functions
13 =over 4
15 =cut
19 #define PARROT_NO_EXTERN_ENCODING_PTRS
20 #include "parrot/parrot.h"
22 /* HEADERIZER HFILE: include/parrot/encoding.h */
24 /* HEADERIZER BEGIN: static */
25 /* Don't modify between HEADERIZER BEGIN / HEADERIZER END. Your changes will be lost. */
27 static INTVAL register_encoding(PARROT_INTERP,
28 ARGIN(const char *encodingname),
29 ARGIN(ENCODING *encoding))
30 __attribute__nonnull__(1)
31 __attribute__nonnull__(2)
32 __attribute__nonnull__(3);
34 /* Don't modify between HEADERIZER BEGIN / HEADERIZER END. Your changes will be lost. */
35 /* HEADERIZER END: static */
37 ENCODING *Parrot_default_encoding_ptr;
38 ENCODING *Parrot_fixed_8_encoding_ptr;
39 ENCODING *Parrot_utf8_encoding_ptr;
40 ENCODING *Parrot_ucs2_encoding_ptr;
41 ENCODING *Parrot_utf16_encoding_ptr;
43 typedef struct One_encoding {
44 NOTNULL(ENCODING *encoding);
45 STRING *name;
46 } One_encoding;
48 typedef struct All_encodings {
49 int n_encodings;
50 One_encoding *enc;
51 } All_encodings;
53 static All_encodings *all_encodings;
57 =item C<void parrot_init_encodings_2>
59 RT#48260: Not yet documented!!!
61 =cut
65 void
66 parrot_init_encodings_2(void)
68 const int n = all_encodings->n_encodings;
69 int i;
71 for (i = 0; i < n; ++i) {
72 all_encodings->enc[i].name->charset = Parrot_default_charset_ptr;
78 =item C<void parrot_deinit_encodings>
80 RT#48260: Not yet documented!!!
82 =cut
86 void
87 parrot_deinit_encodings(void)
89 const int n = all_encodings->n_encodings;
90 int i;
92 for (i = 0; i < n; ++i) {
93 mem_sys_free(all_encodings->enc[i].encoding);
95 mem_sys_free(all_encodings->enc);
96 mem_sys_free(all_encodings);
97 all_encodings = NULL;
102 =item C<ENCODING * Parrot_new_encoding>
104 RT#48260: Not yet documented!!!
106 =cut
110 PARROT_API
111 PARROT_MALLOC
112 PARROT_CANNOT_RETURN_NULL
113 ENCODING *
114 Parrot_new_encoding(SHIM_INTERP)
116 return mem_allocate_typed(ENCODING);
121 =item C<const ENCODING * Parrot_find_encoding>
123 RT#48260: Not yet documented!!!
125 =cut
129 PARROT_API
130 PARROT_WARN_UNUSED_RESULT
131 PARROT_CAN_RETURN_NULL
132 const ENCODING *
133 Parrot_find_encoding(SHIM_INTERP, ARGIN(const char *encodingname))
135 const int n = all_encodings->n_encodings;
136 int i;
138 for (i = 0; i < n; ++i)
139 if (STREQ(all_encodings->enc[i].encoding->name, encodingname))
140 return all_encodings->enc[i].encoding;
141 return NULL;
146 =item C<const ENCODING * Parrot_load_encoding>
148 RT#48260: Not yet documented!!!
150 =cut
154 /* Yep, this needs to be a char * parameter -- it's tough to load in
155 encodings and such for strings if we can't be sure we've got enough
156 info set up to actually build strings... */
158 PARROT_API
159 PARROT_WARN_UNUSED_RESULT
160 PARROT_CANNOT_RETURN_NULL
161 const ENCODING *
162 Parrot_load_encoding(PARROT_INTERP, ARGIN(const char *encodingname))
164 UNUSED(encodingname);
165 real_exception(interp, NULL, UNIMPLEMENTED, "Can't load encodings yet");
170 =item C<INTVAL Parrot_encoding_number>
172 Return the number of the encoding or -1 if not found.
174 =cut
178 PARROT_API
179 PARROT_WARN_UNUSED_RESULT
180 INTVAL
181 Parrot_encoding_number(PARROT_INTERP, ARGIN(const STRING *encodingname))
183 const int n = all_encodings->n_encodings;
184 int i;
186 for (i = 0; i < n; ++i) {
187 if (!string_equal(interp, all_encodings->enc[i].name,
188 encodingname))
189 return i;
191 return -1;
196 =item C<INTVAL Parrot_encoding_number_of_str>
198 Return the number of the encoding of the given string or -1 if not found.
200 =cut
204 PARROT_API
205 PARROT_WARN_UNUSED_RESULT
206 INTVAL
207 Parrot_encoding_number_of_str(SHIM_INTERP, ARGIN(const STRING *src))
209 const int n = all_encodings->n_encodings;
210 int i;
212 for (i = 0; i < n; ++i) {
213 if (src->encoding == all_encodings->enc[i].encoding)
214 return i;
216 return -1;
221 =item C<STRING* Parrot_encoding_name>
223 RT#48260: Not yet documented!!!
225 =cut
229 PARROT_API
230 PARROT_WARN_UNUSED_RESULT
231 PARROT_CAN_RETURN_NULL
232 STRING*
233 Parrot_encoding_name(SHIM_INTERP, INTVAL number_of_encoding)
235 if (number_of_encoding >= all_encodings->n_encodings)
236 return NULL;
237 return all_encodings->enc[number_of_encoding].name;
242 =item C<const ENCODING* Parrot_get_encoding>
244 RT#48260: Not yet documented!!!
246 =cut
250 PARROT_API
251 PARROT_WARN_UNUSED_RESULT
252 PARROT_CAN_RETURN_NULL
253 const ENCODING*
254 Parrot_get_encoding(SHIM_INTERP, INTVAL number_of_encoding)
256 if (number_of_encoding >= all_encodings->n_encodings)
257 return NULL;
258 return all_encodings->enc[number_of_encoding].encoding;
263 =item C<const char * Parrot_encoding_c_name>
265 RT#48260: Not yet documented!!!
267 =cut
271 PARROT_API
272 PARROT_WARN_UNUSED_RESULT
273 PARROT_CAN_RETURN_NULL
274 const char *
275 Parrot_encoding_c_name(SHIM_INTERP, INTVAL number_of_encoding)
277 if (number_of_encoding >= all_encodings->n_encodings)
278 return NULL;
279 return all_encodings->enc[number_of_encoding].encoding->name;
284 =item C<static INTVAL register_encoding>
286 RT#48260: Not yet documented!!!
288 =cut
292 static INTVAL
293 register_encoding(PARROT_INTERP, ARGIN(const char *encodingname),
294 ARGIN(ENCODING *encoding))
296 const int n = all_encodings->n_encodings;
297 int i;
299 for (i = 0; i < n; ++i) {
300 if (STREQ(all_encodings->enc[i].encoding->name, encodingname))
301 return 0;
304 * TODO
305 * this needs either a LOCK or we just forbid dynamic
306 * loading of encodings from inside threads
308 if (!n)
309 all_encodings->enc = mem_allocate_typed(One_encoding);
310 else
311 all_encodings->enc = (One_encoding*)mem_sys_realloc(all_encodings->enc,
312 (n + 1) * sizeof (One_encoding));
313 all_encodings->n_encodings++;
314 all_encodings->enc[n].encoding = encoding;
315 all_encodings->enc[n].name = const_string(interp, encodingname);
317 return 1;
322 =item C<INTVAL Parrot_register_encoding>
324 RT#48260: Not yet documented!!!
326 =cut
330 PARROT_API
331 INTVAL
332 Parrot_register_encoding(PARROT_INTERP, ARGIN(const char *encodingname),
333 ARGIN(ENCODING *encoding))
335 if (!all_encodings) {
336 all_encodings = mem_allocate_typed(All_encodings);
337 all_encodings->n_encodings = 0;
338 all_encodings->enc = NULL;
340 if (STREQ("fixed_8", encodingname)) {
341 Parrot_fixed_8_encoding_ptr = encoding;
342 if (!Parrot_default_encoding_ptr) {
343 Parrot_default_encoding_ptr = encoding;
346 return register_encoding(interp, encodingname, encoding);
348 if (STREQ("utf8", encodingname)) {
349 Parrot_utf8_encoding_ptr = encoding;
350 return register_encoding(interp, encodingname, encoding);
352 if (STREQ("utf16", encodingname)) {
353 Parrot_utf16_encoding_ptr = encoding;
354 return register_encoding(interp, encodingname, encoding);
356 if (STREQ("ucs2", encodingname)) {
357 Parrot_ucs2_encoding_ptr = encoding;
358 return register_encoding(interp, encodingname, encoding);
360 return 0;
365 =item C<INTVAL Parrot_make_default_encoding>
367 RT#48260: Not yet documented!!!
369 =cut
373 PARROT_API
374 INTVAL
375 Parrot_make_default_encoding(SHIM_INTERP, SHIM(const char *encodingname),
376 ARGIN(ENCODING *encoding))
378 Parrot_default_encoding_ptr = encoding;
379 return 1;
384 =item C<const ENCODING * Parrot_default_encoding>
386 RT#48260: Not yet documented!!!
388 =cut
392 PARROT_API
393 PARROT_WARN_UNUSED_RESULT
394 PARROT_CANNOT_RETURN_NULL
395 const ENCODING *
396 Parrot_default_encoding(SHIM_INTERP)
398 return Parrot_default_encoding_ptr;
403 =item C<encoding_converter_t Parrot_find_encoding_converter>
405 RT#48260: Not yet documented!!!
407 =cut
411 PARROT_API
412 encoding_converter_t
413 Parrot_find_encoding_converter(PARROT_INTERP, ARGIN(ENCODING *lhs), ARGIN(ENCODING *rhs))
415 UNUSED(interp);
416 UNUSED(lhs);
417 UNUSED(rhs);
419 /* XXX Apparently unwritten */
421 return NULL;
426 * Local variables:
427 * c-file-style: "parrot"
428 * End:
429 * vim: expandtab shiftwidth=4: