1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507
|
/*
Copyright (C) 2004-2010, Parrot Foundation.
=head1 NAME
src/string/encoding.c - global encoding functions
=head1 DESCRIPTION
These are parrot's generic encoding handling functions
=over 4
=cut
*/
#include "parrot/encoding.h"
#include "encoding.str"
STR_VTABLE *Parrot_default_encoding_ptr = NULL;
STR_VTABLE *Parrot_platform_encoding_ptr = NULL;
static STR_VTABLE **encodings;
static int n_encodings;
static STRING *platform_str;
/* for backwards compatibility */
static STRING *unicode_str;
static STRING *fixed_8_str;
#define ENC_NAME_PLATFORM "platform"
#define ENC_NAME_UNICODE "unicode"
#define ENC_NAME_FIXED8 "fixed_8"
/* HEADERIZER HFILE: include/parrot/encoding.h */
/* HEADERIZER BEGIN: static */
/* Don't modify between HEADERIZER BEGIN / HEADERIZER END. Your changes will be lost. */
PARROT_PURE_FUNCTION
PARROT_WARN_UNUSED_RESULT
PARROT_CAN_RETURN_NULL
static const STR_VTABLE * find_encoding(PARROT_INTERP,
ARGIN(const STRING *encodingname))
__attribute__nonnull__(1)
__attribute__nonnull__(2);
#define ASSERT_ARGS_find_encoding __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
PARROT_ASSERT_ARG(interp) \
, PARROT_ASSERT_ARG(encodingname))
/* Don't modify between HEADERIZER BEGIN / HEADERIZER END. Your changes will be lost. */
/* HEADERIZER END: static */
/*
=item C<void Parrot_deinit_encodings(PARROT_INTERP)>
Deinitialize encodings and free all memory used by them.
=cut
*/
void
Parrot_deinit_encodings(PARROT_INTERP)
{
ASSERT_ARGS(Parrot_deinit_encodings)
mem_gc_free(interp, encodings);
encodings = NULL;
n_encodings = 0;
}
/*
=item C<STR_VTABLE * Parrot_new_encoding(PARROT_INTERP)>
Allocates the memory for a new string vtable from the system.
=cut
*/
PARROT_EXPORT
PARROT_MALLOC
PARROT_CANNOT_RETURN_NULL
STR_VTABLE *
Parrot_new_encoding(PARROT_INTERP)
{
ASSERT_ARGS(Parrot_new_encoding)
return mem_gc_allocate_typed(interp, STR_VTABLE);
}
/*
=item C<static const STR_VTABLE * find_encoding(PARROT_INTERP, const STRING
*encodingname)>
Finds an encoding with the STRING name C<encodingname>. Returns the encoding
if it is successfully found, returns NULL otherwise.
=cut
*/
PARROT_PURE_FUNCTION
PARROT_WARN_UNUSED_RESULT
PARROT_CAN_RETURN_NULL
static const STR_VTABLE *
find_encoding(PARROT_INTERP, ARGIN(const STRING *encodingname))
{
ASSERT_ARGS(find_encoding)
const int n = n_encodings;
int i;
for (i = 0; i < n; ++i)
if (STRING_equal(interp, encodings[i]->name_str, encodingname))
return encodings[i];
/* backwards compatibility */
if (STRING_equal(interp, encodingname, unicode_str))
return Parrot_utf8_encoding_ptr;
if (STRING_equal(interp, encodingname, platform_str))
return Parrot_platform_encoding_ptr;
if (STRING_equal(interp, encodingname, fixed_8_str))
return Parrot_ascii_encoding_ptr;
return NULL;
}
/*
=item C<const STR_VTABLE * Parrot_find_encoding(PARROT_INTERP, const char
*encodingname)>
Finds an encoding with the C string name C<encodingname>. Returns the encoding
if it is successfully found, returns NULL otherwise.
=cut
*/
PARROT_EXPORT
PARROT_PURE_FUNCTION
PARROT_WARN_UNUSED_RESULT
PARROT_CAN_RETURN_NULL
const STR_VTABLE *
Parrot_find_encoding(SHIM_INTERP, ARGIN(const char *encodingname))
{
ASSERT_ARGS(Parrot_find_encoding)
const int n = n_encodings;
int i;
for (i = 0; i < n; ++i)
if (STREQ(encodings[i]->name, encodingname))
return encodings[i];
/* backwards compatibility */
if (strcmp(encodingname, ENC_NAME_UNICODE) == 0)
return Parrot_utf8_encoding_ptr;
if (strcmp(encodingname, ENC_NAME_PLATFORM) == 0)
return Parrot_platform_encoding_ptr;
if (strcmp(encodingname, ENC_NAME_FIXED8) == 0)
return Parrot_ascii_encoding_ptr;
return NULL;
}
/*
=item C<const STR_VTABLE * Parrot_find_encoding_by_string(PARROT_INTERP, STRING
*encodingname)>
Finds an encoding with the STRING name C<encodingname>. Returns the encoding
if it is successfully found, throws an exception otherwise. Returns the
default encoding for the NULL string.
=cut
*/
PARROT_EXPORT
PARROT_PURE_FUNCTION
PARROT_WARN_UNUSED_RESULT
PARROT_CAN_RETURN_NULL
const STR_VTABLE *
Parrot_find_encoding_by_string(PARROT_INTERP, ARGIN(STRING *encodingname))
{
ASSERT_ARGS(Parrot_find_encoding_by_string)
if (STRING_IS_NULL(encodingname))
return Parrot_default_encoding_ptr;
else {
const STR_VTABLE * const result = find_encoding(interp, encodingname);
if (result)
return result;
}
Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_INVALID_ENCODING,
"invalid encoding '%Ss'", encodingname);
}
/*
=item C<const STR_VTABLE * Parrot_load_encoding(PARROT_INTERP, const char
*encodingname)>
Loads an encoding. Currently throws an exception because we cannot load
encodings. See https://trac.parrot.org/parrot/wiki/StringsTasklist.
=cut
*/
/* Yep, this needs to be a char * parameter -- it's tough to load in
encodings and such for strings if we can't be sure we've got enough
info set up to actually build strings...
Also remember to use PARROT_WARN_UNUSED_RESULT and
PARROT_CANNOT_RETURN_NULL when this actually works.
*/
PARROT_EXPORT
PARROT_DOES_NOT_RETURN
PARROT_CANNOT_RETURN_NULL
const STR_VTABLE *
Parrot_load_encoding(PARROT_INTERP, ARGIN(const char *encodingname))
{
ASSERT_ARGS(Parrot_load_encoding)
UNUSED(encodingname);
Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_UNIMPLEMENTED,
"Can't load encodings yet");
}
/*
=item C<INTVAL Parrot_encoding_number(PARROT_INTERP, const STRING
*encodingname)>
Return the number of the encoding or -1 if not found.
=cut
*/
PARROT_EXPORT
PARROT_PURE_FUNCTION
PARROT_WARN_UNUSED_RESULT
INTVAL
Parrot_encoding_number(PARROT_INTERP, ARGIN(const STRING *encodingname))
{
ASSERT_ARGS(Parrot_encoding_number)
const STR_VTABLE * const result = find_encoding(interp, encodingname);
return result ? result->num : -1;
}
/*
=item C<INTVAL Parrot_encoding_number_of_str(PARROT_INTERP, const STRING *src)>
Return the number of the encoding of the given string or -1 if not found.
This could be converted to a macro.
=cut
*/
PARROT_EXPORT
PARROT_PURE_FUNCTION
PARROT_WARN_UNUSED_RESULT
INTVAL
Parrot_encoding_number_of_str(SHIM_INTERP, ARGIN(const STRING *src))
{
ASSERT_ARGS(Parrot_encoding_number_of_str)
return src->encoding->num;
}
/*
=item C<STRING* Parrot_encoding_name(PARROT_INTERP, INTVAL number_of_encoding)>
Returns the name of a character encoding based on the INTVAL index
C<number_of_encoding> to the All_encodings array.
This could be converted to a macro.
=cut
*/
PARROT_EXPORT
PARROT_PURE_FUNCTION
PARROT_WARN_UNUSED_RESULT
PARROT_CAN_RETURN_NULL
STRING*
Parrot_encoding_name(SHIM_INTERP, INTVAL number_of_encoding)
{
ASSERT_ARGS(Parrot_encoding_name)
if (number_of_encoding >= n_encodings ||
number_of_encoding < 0)
return NULL;
return encodings[number_of_encoding]->name_str;
}
/*
=item C<const STR_VTABLE* Parrot_get_encoding(PARROT_INTERP, INTVAL
number_of_encoding)>
Returns the encoding given by the INTVAL index C<number_of_encoding>.
=cut
*/
PARROT_EXPORT
PARROT_PURE_FUNCTION
PARROT_WARN_UNUSED_RESULT
PARROT_CAN_RETURN_NULL
const STR_VTABLE*
Parrot_get_encoding(SHIM_INTERP, INTVAL number_of_encoding)
{
ASSERT_ARGS(Parrot_get_encoding)
if (number_of_encoding >= n_encodings ||
number_of_encoding < 0)
return NULL;
return encodings[number_of_encoding];
}
/*
=item C<const char * Parrot_encoding_c_name(PARROT_INTERP, INTVAL
number_of_encoding)>
Returns the NULL-terminated C string representation of the encodings name
given by the C<number_of_encoding>.
=cut
*/
PARROT_EXPORT
PARROT_PURE_FUNCTION
PARROT_WARN_UNUSED_RESULT
PARROT_CAN_RETURN_NULL
const char *
Parrot_encoding_c_name(SHIM_INTERP, INTVAL number_of_encoding)
{
ASSERT_ARGS(Parrot_encoding_c_name)
if (number_of_encoding >= n_encodings ||
number_of_encoding < 0)
return NULL;
return encodings[number_of_encoding]->name;
}
/*
=item C<void Parrot_str_internal_register_encoding_names(PARROT_INTERP)>
Helper function for initializing characterset encoding names. We can't create
the STRING names until the default encodings are already initted,
so the name generation is split into a second init stage.
=cut
*/
void
Parrot_str_internal_register_encoding_names(PARROT_INTERP)
{
ASSERT_ARGS(Parrot_str_internal_register_encoding_names)
int n;
for (n = 0; n < n_encodings; ++n)
encodings[n]->name_str =
Parrot_str_new_constant(interp, encodings[n]->name);
/* Can't use CONST_STRING here, not setup yet */
unicode_str = Parrot_str_new_constant(interp, ENC_NAME_UNICODE);
fixed_8_str = Parrot_str_new_constant(interp, ENC_NAME_FIXED8);
platform_str = Parrot_str_new_constant(interp, ENC_NAME_PLATFORM);
}
/*
=item C<INTVAL Parrot_register_encoding(PARROT_INTERP, STR_VTABLE *encoding)>
Registers a character encoding C<encoding> with name C<encodingname>.
Only allows one of 5 possibilities: fixed_8, utf8, utf16, ucs2 and ucs4.
=cut
*/
PARROT_EXPORT
INTVAL
Parrot_register_encoding(PARROT_INTERP, ARGIN(STR_VTABLE *encoding))
{
ASSERT_ARGS(Parrot_register_encoding)
int i;
int n = n_encodings;
for (i = 0; i < n_encodings; ++i) {
if (STREQ(encodings[i]->name, encoding->name))
return 0;
}
if (!n)
encodings = mem_gc_allocate_zeroed_typed(interp, STR_VTABLE *);
else
encodings = mem_gc_realloc_n_typed_zeroed(interp,
encodings, n + 1, n, STR_VTABLE *);
encoding->num = n;
encodings[n] = encoding;
++n_encodings;
return 1;
}
/*
=item C<void Parrot_encodings_init(PARROT_INTERP)>
Creates the initial encodings.
=cut
*/
PARROT_EXPORT
void
Parrot_encodings_init(PARROT_INTERP)
{
ASSERT_ARGS(Parrot_encodings_init)
Parrot_register_encoding(interp, Parrot_ascii_encoding_ptr);
Parrot_register_encoding(interp, Parrot_latin1_encoding_ptr);
Parrot_register_encoding(interp, Parrot_binary_encoding_ptr);
Parrot_register_encoding(interp, Parrot_utf8_encoding_ptr);
Parrot_register_encoding(interp, Parrot_utf16_encoding_ptr);
Parrot_register_encoding(interp, Parrot_ucs2_encoding_ptr);
Parrot_register_encoding(interp, Parrot_ucs4_encoding_ptr);
Parrot_default_encoding_ptr = Parrot_ascii_encoding_ptr;
Parrot_init_platform_encoding(interp);
/* Now that the plugins are registered, we can create STRING
* names for them. */
Parrot_str_internal_register_encoding_names(interp);
}
/*
=item C<INTVAL Parrot_make_default_encoding(PARROT_INTERP, const char
*encodingname, STR_VTABLE *encoding)>
Sets the default encoding to C<encoding> with name C<encodingname>.
=cut
*/
PARROT_EXPORT
INTVAL
Parrot_make_default_encoding(SHIM_INTERP, ARGIN(SHIM(const char *encodingname)),
ARGIN(STR_VTABLE *encoding))
{
ASSERT_ARGS(Parrot_make_default_encoding)
Parrot_default_encoding_ptr = encoding;
return 1;
}
/*
=item C<const STR_VTABLE * Parrot_default_encoding(PARROT_INTERP)>
Gets the default encoding.
=cut
*/
PARROT_EXPORT
PARROT_PURE_FUNCTION
PARROT_WARN_UNUSED_RESULT
PARROT_CANNOT_RETURN_NULL
const STR_VTABLE *
Parrot_default_encoding(SHIM_INTERP)
{
ASSERT_ARGS(Parrot_default_encoding)
return Parrot_default_encoding_ptr;
}
/*
* Local variables:
* c-file-style: "parrot"
* End:
* vim: expandtab shiftwidth=4 cinoptions='\:2=2' :
*/
|