| 1 |
# -*- coding: utf-8 -*- |
| 2 |
# Python interface to the Recode C library, for testing. |
| 3 |
# Copyright © 1996-2008 Free Software Foundation, Inc. |
| 4 |
# François Pinard <[email protected]>, 1988. |
| 5 |
|
| 6 |
# This program is free software; you can redistribute it and/or modify |
| 7 |
# it under the terms of the GNU General Public License as published by |
| 8 |
# the Free Software Foundation; either version 3, or (at your option) |
| 9 |
# any later version. |
| 10 |
|
| 11 |
# This program is distributed in the hope that it will be useful, |
| 12 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
# GNU General Public License for more details. |
| 15 |
|
| 16 |
# You should have received a copy of the GNU General Public License |
| 17 |
# along with this program; if not, see <https://www.gnu.org/licenses/>. |
| 18 |
|
| 19 |
from libcpp cimport bool |
| 20 |
from libc.stdlib cimport free |
| 21 |
from libc.stdio cimport FILE |
| 22 |
|
| 23 |
cdef extern from "common.h": |
| 24 |
|
| 25 |
## Forwarded. |
| 26 |
|
| 27 |
cdef struct recode_outer |
| 28 |
ctypedef recode_outer *RECODE_OUTER |
| 29 |
ctypedef recode_outer *RECODE_CONST_OUTER |
| 30 |
|
| 31 |
cdef struct recode_single |
| 32 |
ctypedef recode_single *RECODE_SINGLE |
| 33 |
|
| 34 |
## Symbols. |
| 35 |
|
| 36 |
enum recode_symbol_type: |
| 37 |
RECODE_NO_SYMBOL_TYPE |
| 38 |
RECODE_CHARSET |
| 39 |
RECODE_DATA_SURFACE |
| 40 |
|
| 41 |
enum recode_data_type: |
| 42 |
RECODE_NO_CHARSET_DATA |
| 43 |
RECODE_STRIP_DATA |
| 44 |
RECODE_EXPLODE_DATA |
| 45 |
|
| 46 |
struct recode_symbol: |
| 47 |
recode_symbol *next |
| 48 |
unsigned ordinal |
| 49 |
char *name |
| 50 |
char *iconv_name |
| 51 |
recode_data_type data_type |
| 52 |
void *data |
| 53 |
RECODE_SINGLE resurfacer |
| 54 |
RECODE_SINGLE unsurfacer |
| 55 |
recode_symbol_type type |
| 56 |
bool ignore |
| 57 |
ctypedef recode_symbol *RECODE_SYMBOL |
| 58 |
ctypedef recode_symbol *RECODE_CONST_SYMBOL |
| 59 |
|
| 60 |
struct recode_surface_list: |
| 61 |
RECODE_CONST_SYMBOL surface |
| 62 |
recode_surface_list *next |
| 63 |
|
| 64 |
struct recode_alias: |
| 65 |
char *name |
| 66 |
RECODE_SYMBOL symbol |
| 67 |
recode_surface_list *implied_surfaces |
| 68 |
ctypedef recode_alias *RECODE_ALIAS |
| 69 |
ctypedef recode_alias *RECODE_CONST_ALIAS |
| 70 |
|
| 71 |
## Steps. |
| 72 |
|
| 73 |
enum recode_size: |
| 74 |
RECODE_1 |
| 75 |
RECODE_2 |
| 76 |
RECODE_4 |
| 77 |
RECODE_N |
| 78 |
|
| 79 |
struct recode_quality: |
| 80 |
recode_size in_size |
| 81 |
recode_size out_size |
| 82 |
bool reversible |
| 83 |
bool slower |
| 84 |
bool faster |
| 85 |
|
| 86 |
struct recode_option_list: |
| 87 |
char *option |
| 88 |
recode_option_list *next |
| 89 |
ctypedef recode_option_list *RECODE_OPTION_LIST |
| 90 |
ctypedef recode_option_list *RECODE_CONST_OPTION_LIST |
| 91 |
|
| 92 |
ctypedef bool (*Recode_init)(RECODE_STEP, RECODE_CONST_REQUEST, |
| 93 |
RECODE_CONST_OPTION_LIST, |
| 94 |
RECODE_CONST_OPTION_LIST) |
| 95 |
ctypedef bool (*Recode_term)(RECODE_STEP, RECODE_CONST_REQUEST) |
| 96 |
ctypedef bool (*Recode_transform)(RECODE_SUBTASK) |
| 97 |
ctypedef bool (*Recode_fallback)(RECODE_SUBTASK, unsigned) |
| 98 |
|
| 99 |
struct recode_single: |
| 100 |
recode_single *next |
| 101 |
RECODE_SYMBOL before |
| 102 |
RECODE_SYMBOL after |
| 103 |
short conversion_cost |
| 104 |
void *initial_step_table |
| 105 |
recode_quality quality |
| 106 |
Recode_init init_routine |
| 107 |
Recode_transform transform_routine |
| 108 |
Recode_fallback fallback_routine |
| 109 |
|
| 110 |
enum recode_step_type: |
| 111 |
RECODE_NO_STEP_TABLE |
| 112 |
RECODE_BYTE_TO_BYTE |
| 113 |
RECODE_BYTE_TO_STRING |
| 114 |
RECODE_UCS2_TO_BYTE |
| 115 |
RECODE_UCS2_TO_STRING |
| 116 |
RECODE_STRING_TO_UCS2 |
| 117 |
RECODE_COMBINE_EXPLODE |
| 118 |
RECODE_COMBINE_STEP |
| 119 |
RECODE_EXPLODE_STEP |
| 120 |
|
| 121 |
struct recode_step: |
| 122 |
RECODE_SYMBOL before |
| 123 |
RECODE_SYMBOL after |
| 124 |
recode_quality quality |
| 125 |
recode_step_type step_type |
| 126 |
void *step_table |
| 127 |
void *local |
| 128 |
Recode_transform transform_routine |
| 129 |
Recode_fallback fallback_routine |
| 130 |
Recode_term term_routine |
| 131 |
ctypedef recode_step *RECODE_STEP |
| 132 |
ctypedef recode_step *RECODE_CONST_STEP |
| 133 |
|
| 134 |
## Requests. |
| 135 |
|
| 136 |
struct recode_request: |
| 137 |
RECODE_OUTER outer |
| 138 |
bool verbose_flag |
| 139 |
char diaeresis_char |
| 140 |
bool make_header_flag |
| 141 |
bool diacritics_only |
| 142 |
bool ascii_graphics |
| 143 |
RECODE_STEP sequence_array |
| 144 |
size_t sequence_allocated |
| 145 |
short sequence_length |
| 146 |
ctypedef recode_request *RECODE_REQUEST |
| 147 |
ctypedef recode_request *RECODE_CONST_REQUEST |
| 148 |
|
| 149 |
## Tasks. |
| 150 |
|
| 151 |
struct recode_read_only_text: |
| 152 |
char *name |
| 153 |
FILE *file |
| 154 |
char *buffer |
| 155 |
char *cursor |
| 156 |
char *limit |
| 157 |
|
| 158 |
struct recode_read_write_text: |
| 159 |
char *name |
| 160 |
FILE *file |
| 161 |
char *buffer |
| 162 |
char *cursor |
| 163 |
char *limit |
| 164 |
|
| 165 |
enum recode_swap_input: |
| 166 |
RECODE_SWAP_UNDECIDED |
| 167 |
RECODE_SWAP_NO |
| 168 |
RECODE_SWAP_YES |
| 169 |
|
| 170 |
enum recode_error_ 'recode_error': |
| 171 |
RECODE_NO_ERROR |
| 172 |
RECODE_NOT_CANONICAL |
| 173 |
RECODE_AMBIGUOUS_OUTPUT |
| 174 |
RECODE_UNTRANSLATABLE |
| 175 |
RECODE_INVALID_INPUT |
| 176 |
RECODE_SYSTEM_ERROR |
| 177 |
RECODE_USER_ERROR |
| 178 |
RECODE_INTERNAL_ERROR |
| 179 |
RECODE_MAXIMUM_ERROR |
| 180 |
|
| 181 |
struct recode_task: |
| 182 |
RECODE_CONST_REQUEST request |
| 183 |
recode_read_only_text input |
| 184 |
recode_read_write_text output |
| 185 |
bool byte_order_mark |
| 186 |
recode_swap_input swap_input |
| 187 |
recode_error_ fail_level |
| 188 |
recode_error_ abort_level |
| 189 |
recode_error_ error_so_far |
| 190 |
RECODE_CONST_STEP error_at_step |
| 191 |
ctypedef recode_task *RECODE_TASK |
| 192 |
ctypedef recode_task *RECODE_CONST_TASK |
| 193 |
|
| 194 |
struct recode_subtask: |
| 195 |
RECODE_TASK task |
| 196 |
RECODE_CONST_STEP step |
| 197 |
recode_read_only_text input |
| 198 |
recode_read_write_text output |
| 199 |
unsigned newline_count |
| 200 |
unsigned character_count |
| 201 |
ctypedef recode_subtask *RECODE_SUBTASK |
| 202 |
|
| 203 |
int recode_get_byte(RECODE_SUBTASK) |
| 204 |
void recode_put_byte(int, RECODE_SUBTASK) |
| 205 |
void SET_SUBTASK_ERROR(recode_error_, RECODE_SUBTASK) |
| 206 |
bool SUBTASK_RETURN(RECODE_SUBTASK) |
| 207 |
void RETURN_IF_NOGO(recode_error_, RECODE_SUBTASK) |
| 208 |
void GOT_CHARACTER(RECODE_SUBTASK) |
| 209 |
void GOT_NEWLINE(RECODE_SUBTASK) |
| 210 |
|
| 211 |
## Outers. |
| 212 |
|
| 213 |
struct recode_known_pair: |
| 214 |
unsigned char left |
| 215 |
unsigned char right |
| 216 |
|
| 217 |
struct recode_outer: |
| 218 |
bool auto_abort |
| 219 |
bool use_iconv |
| 220 |
# charset.c: |
| 221 |
recode_known_pair *pair_restriction |
| 222 |
unsigned pair_restrictions |
| 223 |
void *alias_table |
| 224 |
RECODE_SYMBOL symbol_list |
| 225 |
unsigned number_of_symbols |
| 226 |
char **argmatch_charset_array |
| 227 |
char **argmatch_surface_array |
| 228 |
char **realname_charset_array |
| 229 |
char **realname_surface_array |
| 230 |
# recode.c: |
| 231 |
recode_single *single_list |
| 232 |
unsigned number_of_singles |
| 233 |
unsigned char *one_to_same |
| 234 |
RECODE_SYMBOL data_symbol |
| 235 |
RECODE_SYMBOL ucs2_charset |
| 236 |
RECODE_SYMBOL iconv_pivot |
| 237 |
RECODE_SYMBOL crlf_surface |
| 238 |
RECODE_SYMBOL cr_surface |
| 239 |
recode_quality quality_byte_reversible |
| 240 |
recode_quality quality_byte_to_byte |
| 241 |
recode_quality quality_byte_to_ucs2 |
| 242 |
recode_quality quality_byte_to_variable |
| 243 |
recode_quality quality_ucs2_to_byte |
| 244 |
recode_quality quality_ucs2_to_variable |
| 245 |
recode_quality quality_variable_to_byte |
| 246 |
recode_quality quality_variable_to_ucs2 |
| 247 |
recode_quality quality_variable_to_variable |
| 248 |
|
| 249 |
## Miscellaneous. |
| 250 |
|
| 251 |
enum recode_list_format: |
| 252 |
RECODE_NO_FORMAT |
| 253 |
RECODE_DECIMAL_FORMAT |
| 254 |
RECODE_OCTAL_FORMAT |
| 255 |
RECODE_HEXADECIMAL_FORMAT |
| 256 |
RECODE_FULL_FORMAT |
| 257 |
|
| 258 |
enum recode_programming_language: |
| 259 |
RECODE_NO_LANGUAGE |
| 260 |
RECODE_LANGUAGE_C |
| 261 |
RECODE_LANGUAGE_PERL |
| 262 |
|
| 263 |
enum: |
| 264 |
NUL_ 'NUL' |
| 265 |
STRIP_SIZE_ 'STRIP_SIZE' |
| 266 |
|
| 267 |
ctypedef unsigned short recode_ucs2 |
| 268 |
|
| 269 |
struct strip_data: |
| 270 |
recode_ucs2 *pool |
| 271 |
short offset[256 // STRIP_SIZE_] |
| 272 |
|
| 273 |
struct ucs2_to_byte: |
| 274 |
recode_ucs2 code |
| 275 |
unsigned char byte |
| 276 |
|
| 277 |
struct ucs2_to_string: |
| 278 |
recode_ucs2 code |
| 279 |
unsigned short flags |
| 280 |
char *string |
| 281 |
|
| 282 |
## Per module declarations. |
| 283 |
|
| 284 |
# recode.c |
| 285 |
|
| 286 |
void recode_error(RECODE_OUTER, char *, ...) |
| 287 |
void recode_perror(RECODE_OUTER, char *, ...) |
| 288 |
void *recode_malloc(RECODE_OUTER, size_t) |
| 289 |
void *recode_realloc(RECODE_OUTER, void *, size_t) |
| 290 |
|
| 291 |
unsigned char *invert_table(RECODE_OUTER, unsigned char *) |
| 292 |
bool complete_pairs(RECODE_OUTER, RECODE_STEP, |
| 293 |
recode_known_pair *, unsigned, bool, bool) |
| 294 |
bool transform_byte_to_ucs2(RECODE_SUBTASK) |
| 295 |
bool init_ucs2_to_byte(RECODE_STEP, RECODE_CONST_REQUEST, |
| 296 |
RECODE_CONST_OPTION_LIST, RECODE_CONST_OPTION_LIST) |
| 297 |
bool transform_ucs2_to_byte(RECODE_SUBTASK) |
| 298 |
|
| 299 |
# charname.c and fr-charname.c |
| 300 |
|
| 301 |
char *ucs2_to_charname(int) |
| 302 |
char *ucs2_to_french_charname(int) |
| 303 |
|
| 304 |
# charset.c |
| 305 |
|
| 306 |
enum alias_find_type: |
| 307 |
SYMBOL_CREATE_CHARSET_ 'SYMBOL_CREATE_CHARSET' |
| 308 |
SYMBOL_CREATE_DATA_SURFACE_ 'SYMBOL_CREATE_DATA_SURFACE' |
| 309 |
ALIAS_FIND_AS_CHARSET_ 'ALIAS_FIND_AS_CHARSET' |
| 310 |
ALIAS_FIND_AS_SURFACE_ 'ALIAS_FIND_AS_SURFACE' |
| 311 |
ALIAS_FIND_AS_EITHER_ 'ALIAS_FIND_AS_EITHER' |
| 312 |
|
| 313 |
int code_to_ucs2(RECODE_CONST_SYMBOL, unsigned) |
| 314 |
bool prepare_for_aliases(RECODE_OUTER) |
| 315 |
RECODE_ALIAS recode_declare_alias(RECODE_OUTER, char *, char *) |
| 316 |
bool recode_declare_implied_surface(RECODE_OUTER, RECODE_ALIAS, |
| 317 |
RECODE_CONST_SYMBOL) |
| 318 |
bool make_argmatch_arrays(RECODE_OUTER) |
| 319 |
RECODE_ALIAS find_alias(RECODE_OUTER, char *, alias_find_type) |
| 320 |
bool find_and_report_subsets(RECODE_OUTER) |
| 321 |
bool decode_known_pairs(RECODE_OUTER, char *) |
| 322 |
|
| 323 |
# combine.c |
| 324 |
|
| 325 |
enum: |
| 326 |
DONE |
| 327 |
ELSE_ 'ELSE' |
| 328 |
|
| 329 |
bool init_explode(RECODE_STEP, RECODE_CONST_REQUEST, |
| 330 |
RECODE_CONST_OPTION_LIST, RECODE_CONST_OPTION_LIST) |
| 331 |
bool explode_byte_byte(RECODE_SUBTASK) |
| 332 |
bool explode_ucs2_byte(RECODE_SUBTASK) |
| 333 |
bool explode_byte_ucs2(RECODE_SUBTASK) |
| 334 |
bool explode_ucs2_ucs2(RECODE_SUBTASK) |
| 335 |
|
| 336 |
bool init_combine(RECODE_STEP, RECODE_CONST_REQUEST, |
| 337 |
RECODE_CONST_OPTION_LIST, RECODE_CONST_OPTION_LIST) |
| 338 |
bool combine_byte_byte(RECODE_SUBTASK) |
| 339 |
bool combine_ucs2_byte(RECODE_SUBTASK) |
| 340 |
bool combine_byte_ucs2(RECODE_SUBTASK) |
| 341 |
bool combine_ucs2_ucs2(RECODE_SUBTASK) |
| 342 |
|
| 343 |
# freeze.c |
| 344 |
|
| 345 |
void recode_freeze_tables(RECODE_OUTER) |
| 346 |
|
| 347 |
# iconv.c |
| 348 |
|
| 349 |
bool transform_with_iconv(RECODE_SUBTASK) |
| 350 |
|
| 351 |
# localcharset.c |
| 352 |
|
| 353 |
char *locale_charset() |
| 354 |
|
| 355 |
# names.c |
| 356 |
|
| 357 |
bool should_prefer_french() |
| 358 |
|
| 359 |
# mixed.c |
| 360 |
|
| 361 |
bool transform_c_source(RECODE_TASK) |
| 362 |
bool transform_po_source(RECODE_TASK) |
| 363 |
|
| 364 |
# outer.c |
| 365 |
|
| 366 |
ctypedef bool (*declare_single_Arg5)( |
| 367 |
RECODE_STEP, RECODE_CONST_REQUEST, |
| 368 |
RECODE_CONST_OPTION_LIST, RECODE_CONST_OPTION_LIST) |
| 369 |
ctypedef bool (*declare_single_Arg6)(RECODE_SUBTASK) |
| 370 |
|
| 371 |
bool reversibility(RECODE_SUBTASK, unsigned) |
| 372 |
RECODE_SINGLE declare_single( |
| 373 |
RECODE_OUTER, char *, char *, recode_quality, |
| 374 |
declare_single_Arg5, declare_single_Arg6) |
| 375 |
bool declare_iconv(RECODE_OUTER, char *, char *) |
| 376 |
bool recode_declare_explode_data(RECODE_OUTER, unsigned short *, char *, char *) |
| 377 |
bool recode_declare_strip_data(RECODE_OUTER, strip_data *, char *) |
| 378 |
|
| 379 |
# pool.c |
| 380 |
|
| 381 |
extern recode_ucs2 ucs2_data_pool[] |
| 382 |
|
| 383 |
# request.c |
| 384 |
|
| 385 |
char *edit_sequence(RECODE_REQUEST, bool) |
| 386 |
|
| 387 |
# rfc1345.c |
| 388 |
|
| 389 |
char *ucs2_to_rfc1345(recode_ucs2) |
| 390 |
|
| 391 |
# task.c |
| 392 |
|
| 393 |
int get_byte(RECODE_SUBTASK) |
| 394 |
void recode_put_byte(int, RECODE_SUBTASK) |
| 395 |
bool recode_if_nogo(recode_error_, RECODE_SUBTASK) |
| 396 |
bool transform_byte_to_byte(RECODE_SUBTASK) |
| 397 |
bool transform_byte_to_variable(RECODE_SUBTASK) |
| 398 |
|
| 399 |
# ucs.c |
| 400 |
|
| 401 |
enum: |
| 402 |
REPLACEMENT_CHARACTER_ 'REPLACEMENT_CHARACTER' |
| 403 |
NOT_A_CHARACTER_ 'NOT_A_CHARACTER' |
| 404 |
BYTE_ORDER_MARK_ 'BYTE_ORDER_MARK' |
| 405 |
BYTE_ORDER_MARK_SWAPPED_ 'BYTE_ORDER_MARK_SWAPPED' |
| 406 |
|
| 407 |
bool get_ucs2(unsigned *, RECODE_SUBTASK) |
| 408 |
bool get_ucs4(unsigned *, RECODE_SUBTASK) |
| 409 |
bool put_ucs2(unsigned, RECODE_SUBTASK) |
| 410 |
bool put_ucs4(unsigned, RECODE_SUBTASK) |
| 411 |
|
| 412 |
## Recode library at OUTER level. |
| 413 |
|
| 414 |
enum: |
| 415 |
RECODE_AUTO_ABORT_FLAG |
| 416 |
RECODE_NO_ICONV_FLAG |
| 417 |
RECODE_STRICT_MAPPING_FLAG |
| 418 |
|
| 419 |
RECODE_OUTER recode_new_outer(unsigned) |
| 420 |
bool recode_delete_outer(RECODE_OUTER) |
| 421 |
bool recode_list_all_symbols(RECODE_OUTER, RECODE_CONST_SYMBOL) |
| 422 |
bool recode_list_concise_charset(RECODE_OUTER, RECODE_CONST_SYMBOL, |
| 423 |
recode_list_format) |
| 424 |
bool recode_list_full_charset(RECODE_OUTER, RECODE_CONST_SYMBOL) |
| 425 |
|
| 426 |
# Recode library at REQUEST level. |
| 427 |
|
| 428 |
RECODE_REQUEST recode_new_request(RECODE_OUTER) |
| 429 |
bool recode_delete_request(RECODE_REQUEST) |
| 430 |
bool recode_scan_request(RECODE_REQUEST, char *) |
| 431 |
bool recode_format_table( |
| 432 |
RECODE_REQUEST, recode_programming_language, char *) |
| 433 |
bool recode_buffer_to_buffer( |
| 434 |
RECODE_CONST_REQUEST, char *, size_t, char **, size_t *, size_t *) |
| 435 |
bool recode_buffer_to_file( |
| 436 |
RECODE_CONST_REQUEST, char *, size_t, FILE *) |
| 437 |
bool recode_file_to_buffer( |
| 438 |
RECODE_CONST_REQUEST, FILE *, char **, size_t *, size_t *) |
| 439 |
bool recode_file_to_file(RECODE_CONST_REQUEST, FILE *, FILE *) |
| 440 |
|
| 441 |
# Recode library at TASK level. |
| 442 |
|
| 443 |
RECODE_TASK recode_new_task(RECODE_CONST_REQUEST) |
| 444 |
bool recode_delete_task(RECODE_TASK) |
| 445 |
bool recode_perform_task(RECODE_TASK) |
| 446 |
|
| 447 |
class error(Exception): |
| 448 |
pass |
| 449 |
|
| 450 |
## Enums repeated for Python. |
| 451 |
|
| 452 |
NO_SYMBOL_TYPE = RECODE_NO_SYMBOL_TYPE |
| 453 |
CHARSET = RECODE_CHARSET |
| 454 |
DATA_SURFACE = RECODE_DATA_SURFACE |
| 455 |
|
| 456 |
NO_CHARSET_DATA = RECODE_NO_CHARSET_DATA |
| 457 |
STRIP_DATA = RECODE_STRIP_DATA |
| 458 |
EXPLODE_DATA = RECODE_EXPLODE_DATA |
| 459 |
|
| 460 |
SIZE_1 = RECODE_1 |
| 461 |
SIZE_2 = RECODE_2 |
| 462 |
SIZE_4 = RECODE_4 |
| 463 |
SIZE_N = RECODE_N |
| 464 |
|
| 465 |
NO_STEP_TABLE = RECODE_NO_STEP_TABLE |
| 466 |
BYTE_TO_BYTE = RECODE_BYTE_TO_BYTE |
| 467 |
BYTE_TO_STRING = RECODE_BYTE_TO_STRING |
| 468 |
UCS2_TO_BYTE = RECODE_UCS2_TO_BYTE |
| 469 |
UCS2_TO_STRING = RECODE_UCS2_TO_STRING |
| 470 |
STRING_TO_UCS2 = RECODE_STRING_TO_UCS2 |
| 471 |
COMBINE_EXPLODE = RECODE_COMBINE_EXPLODE |
| 472 |
COMBINE_STEP = RECODE_COMBINE_STEP |
| 473 |
EXPLODE_STEP = RECODE_EXPLODE_STEP |
| 474 |
|
| 475 |
SWAP_UNDECIDED = RECODE_SWAP_UNDECIDED |
| 476 |
SWAP_NO = RECODE_SWAP_NO |
| 477 |
SWAP_YES = RECODE_SWAP_YES |
| 478 |
|
| 479 |
NO_ERROR = RECODE_NO_ERROR |
| 480 |
NOT_CANONICAL = RECODE_NOT_CANONICAL |
| 481 |
AMBIGUOUS_OUTPUT = RECODE_AMBIGUOUS_OUTPUT |
| 482 |
UNTRANSLATABLE = RECODE_UNTRANSLATABLE |
| 483 |
INVALID_INPUT = RECODE_INVALID_INPUT |
| 484 |
SYSTEM_ERROR = RECODE_SYSTEM_ERROR |
| 485 |
USER_ERROR = RECODE_USER_ERROR |
| 486 |
INTERNAL_ERROR = RECODE_INTERNAL_ERROR |
| 487 |
MAXIMUM_ERROR = RECODE_MAXIMUM_ERROR |
| 488 |
|
| 489 |
NO_FORMAT = RECODE_NO_FORMAT |
| 490 |
DECIMAL_FORMAT = RECODE_DECIMAL_FORMAT |
| 491 |
OCTAL_FORMAT = RECODE_OCTAL_FORMAT |
| 492 |
HEXADECIMAL_FORMAT = RECODE_HEXADECIMAL_FORMAT |
| 493 |
FULL_FORMAT = RECODE_FULL_FORMAT |
| 494 |
|
| 495 |
NO_LANGUAGE = RECODE_NO_LANGUAGE |
| 496 |
LANGUAGE_C = RECODE_LANGUAGE_C |
| 497 |
LANGUAGE_PERL = RECODE_LANGUAGE_PERL |
| 498 |
|
| 499 |
NUL = NUL_ |
| 500 |
STRIP_SIZE = STRIP_SIZE_ |
| 501 |
|
| 502 |
SYMBOL_CREATE_CHARSET = SYMBOL_CREATE_CHARSET_ |
| 503 |
SYMBOL_CREATE_DATA_SURFACE = SYMBOL_CREATE_DATA_SURFACE_ |
| 504 |
ALIAS_FIND_AS_CHARSET = ALIAS_FIND_AS_CHARSET_ |
| 505 |
ALIAS_FIND_AS_SURFACE = ALIAS_FIND_AS_SURFACE_ |
| 506 |
ALIAS_FIND_AS_EITHER = ALIAS_FIND_AS_EITHER_ |
| 507 |
|
| 508 |
xDONE = DONE |
| 509 |
xELSE = ELSE_ |
| 510 |
|
| 511 |
REPLACEMENT_CHARACTER = REPLACEMENT_CHARACTER_ |
| 512 |
NOT_A_CHARACTER = NOT_A_CHARACTER_ |
| 513 |
BYTE_ORDER_MARK = BYTE_ORDER_MARK_ |
| 514 |
BYTE_ORDER_MARK_SWAPPED = BYTE_ORDER_MARK_SWAPPED_ |
| 515 |
|
| 516 |
AUTO_ABORT_FLAG = RECODE_AUTO_ABORT_FLAG |
| 517 |
NO_ICONV_FLAG = RECODE_NO_ICONV_FLAG |
| 518 |
STRICT_MAPPING_FLAG = RECODE_STRICT_MAPPING_FLAG |
| 519 |
|
| 520 |
## Recode library at OUTER level. |
| 521 |
|
| 522 |
cdef class Outer: |
| 523 |
cdef RECODE_OUTER outer |
| 524 |
|
| 525 |
def __init__(self, auto_abort=False, iconv=False, strict=False): |
| 526 |
cdef int flags |
| 527 |
cdef RECODE_SINGLE single |
| 528 |
flags = 0 |
| 529 |
if auto_abort: |
| 530 |
flags = flags | RECODE_AUTO_ABORT_FLAG |
| 531 |
if not iconv: |
| 532 |
flags = flags | RECODE_NO_ICONV_FLAG |
| 533 |
if strict: |
| 534 |
flags = flags | RECODE_STRICT_MAPPING_FLAG |
| 535 |
self.outer = recode_new_outer(flags) |
| 536 |
if strict: |
| 537 |
single = self.outer.single_list |
| 538 |
while single is not NULL: |
| 539 |
single.fallback_routine = NULL |
| 540 |
single = single.next |
| 541 |
|
| 542 |
def __dealloc__(self): |
| 543 |
recode_delete_outer(self.outer) |
| 544 |
|
| 545 |
def default_charset(self): |
| 546 |
return locale_charset() |
| 547 |
|
| 548 |
def all_charsets(self): |
| 549 |
list = [] |
| 550 |
cdef RECODE_SYMBOL symbol |
| 551 |
symbol = self.outer.symbol_list |
| 552 |
while symbol is not NULL: |
| 553 |
if (symbol.type == RECODE_CHARSET |
| 554 |
and symbol is not self.outer.iconv_pivot |
| 555 |
and symbol is not self.outer.data_symbol): |
| 556 |
list.append(symbol.name) |
| 557 |
symbol = symbol.next |
| 558 |
return list |
| 559 |
|
| 560 |
def all_surfaces(self): |
| 561 |
list = [] |
| 562 |
cdef RECODE_SYMBOL symbol |
| 563 |
symbol = self.outer.symbol_list |
| 564 |
while symbol is not NULL: |
| 565 |
if symbol.type != RECODE_CHARSET: |
| 566 |
list.append(symbol.name) |
| 567 |
symbol = symbol.next |
| 568 |
return list |
| 569 |
|
| 570 |
def concise_charset(self, format=NO_FORMAT): |
| 571 |
ok = recode_list_concise_charset(self.outer, NULL, format) |
| 572 |
if not ok: |
| 573 |
raise error |
| 574 |
|
| 575 |
def full_charset(self): |
| 576 |
ok = recode_list_full_charset(self.outer, NULL) |
| 577 |
if not ok: |
| 578 |
raise error |
| 579 |
|
| 580 |
# Lazy, all in one call. |
| 581 |
def recode(self, char *command, char *input, verbose=False): |
| 582 |
request = Request(self) |
| 583 |
request.set_verbose(verbose) |
| 584 |
request.scan(command) |
| 585 |
return request.string(input) |
| 586 |
|
| 587 |
# Recode library at REQUEST level. |
| 588 |
|
| 589 |
cdef class Request: |
| 590 |
cdef RECODE_REQUEST request |
| 591 |
|
| 592 |
def __init__(self, Outer outer): |
| 593 |
self.request = recode_new_request(outer.outer) |
| 594 |
|
| 595 |
def __dealloc__(self): |
| 596 |
recode_delete_request(self.request) |
| 597 |
|
| 598 |
def set_verbose(self, flag): |
| 599 |
previous = self.request.verbose_flag != 0 |
| 600 |
self.request.verbose_flag = int(flag) |
| 601 |
return previous |
| 602 |
|
| 603 |
def scan(self, char *text): |
| 604 |
ok = recode_scan_request(self.request, text) |
| 605 |
if not ok: |
| 606 |
raise error |
| 607 |
|
| 608 |
def pair_sequence(self): |
| 609 |
list = [] |
| 610 |
cdef recode_step step |
| 611 |
cdef short counter |
| 612 |
for counter from 0 <= counter < self.request.sequence_length: |
| 613 |
step = self.request.sequence_array[counter] |
| 614 |
list.append((step.before.name, step.after.name)) |
| 615 |
return list |
| 616 |
|
| 617 |
def format_table(self, int language, char *charset): |
| 618 |
cdef RECODE_OUTER outer |
| 619 |
cdef bool saved |
| 620 |
outer = self.request.outer |
| 621 |
saved = outer.iconv_pivot.ignore |
| 622 |
outer.iconv_pivot.ignore = True |
| 623 |
ok = recode_format_table( |
| 624 |
self.request, <recode_programming_language> language, charset) |
| 625 |
outer.iconv_pivot.ignore = saved |
| 626 |
if not ok: |
| 627 |
raise error |
| 628 |
|
| 629 |
def string(self, text): |
| 630 |
cdef char *input = text |
| 631 |
cdef size_t input_len = len(text) |
| 632 |
cdef char *output = NULL |
| 633 |
cdef size_t output_len |
| 634 |
cdef size_t output_allocated = 0 |
| 635 |
result = recode_buffer_to_buffer(self.request, input, input_len, &output, &output_len, &output_allocated) |
| 636 |
if result is False or output is NULL: |
| 637 |
raise error |
| 638 |
try: |
| 639 |
py_string = output[:output_len] |
| 640 |
finally: |
| 641 |
free (output) |
| 642 |
return py_string |
| 643 |
|
| 644 |
# Unexposed APIs: |
| 645 |
|
| 646 |
# Don't expose recode_string; always check return value |
| 647 |
# |
| 648 |
#char *recode_string( |
| 649 |
# RECODE_CONST_REQUEST, char *) |
| 650 |
|
| 651 |
# Prefer buffer APIs, which allow NUL characters. |
| 652 |
# |
| 653 |
#bool recode_string_to_buffer( |
| 654 |
# RECODE_CONST_REQUEST, char *, char **, size_t *, size_t *) |
| 655 |
#bool recode_string_to_file( |
| 656 |
# RECODE_CONST_REQUEST, char *, FILE *) |
| 657 |
|
| 658 |
# FIXME: Expose these? |
| 659 |
# |
| 660 |
#bool recode_buffer_to_file( |
| 661 |
# RECODE_CONST_REQUEST, char *, size_t, FILE *) |
| 662 |
#bool recode_file_to_buffer( |
| 663 |
# RECODE_CONST_REQUEST, FILE *, char **, size_t *, size_t *) |
| 664 |
#bool recode_file_to_file(RECODE_CONST_REQUEST, FILE *, FILE *) |
| 665 |
|
| 666 |
# Recode library at TASK level. |
| 667 |
|
| 668 |
cdef class Task: |
| 669 |
cdef RECODE_TASK task |
| 670 |
|
| 671 |
def __init__(self, Request request): |
| 672 |
self.task = recode_new_task(request.request) |
| 673 |
|
| 674 |
def __dealloc__(self): |
| 675 |
free (self.task.output.buffer) |
| 676 |
recode_delete_task(self.task) |
| 677 |
|
| 678 |
def set_byte_order_mark(self, flag): |
| 679 |
previous = self.task.byte_order_mark != 0 |
| 680 |
self.task.byte_order_mark = int(flag) |
| 681 |
return previous |
| 682 |
|
| 683 |
def get_error(self): |
| 684 |
return self.task.error_so_far |
| 685 |
|
| 686 |
def set_fail_level(self, fail_level): |
| 687 |
previous = self.task.fail_level |
| 688 |
self.task.fail_level = fail_level |
| 689 |
return previous |
| 690 |
|
| 691 |
def set_abort_level(self, abort_level): |
| 692 |
previous = self.task.abort_level |
| 693 |
self.task.abort_level = abort_level |
| 694 |
return previous |
| 695 |
|
| 696 |
def set_input(self, text): |
| 697 |
cdef char *input = text |
| 698 |
cdef size_t input_len = len(text) |
| 699 |
self.task.input.buffer = input |
| 700 |
self.task.input.cursor = input |
| 701 |
self.task.input.limit = input + input_len |
| 702 |
|
| 703 |
def get_output(self): |
| 704 |
return self.task.output.buffer[:self.task.output.cursor - self.task.output.buffer] |
| 705 |
|
| 706 |
def perform(self): |
| 707 |
return recode_perform_task(self.task) |