paul@380 | 1 | #!/usr/bin/env python |
paul@380 | 2 | |
paul@380 | 3 | """ |
paul@380 | 4 | POSIX character set conversion functions. |
paul@380 | 5 | |
paul@380 | 6 | Copyright (C) 2016 Paul Boddie <paul@boddie.org.uk> |
paul@380 | 7 | |
paul@380 | 8 | This program is free software; you can redistribute it and/or modify it under |
paul@380 | 9 | the terms of the GNU General Public License as published by the Free Software |
paul@380 | 10 | Foundation; either version 3 of the License, or (at your option) any later |
paul@380 | 11 | version. |
paul@380 | 12 | |
paul@380 | 13 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@380 | 14 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@380 | 15 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@380 | 16 | details. |
paul@380 | 17 | |
paul@380 | 18 | You should have received a copy of the GNU General Public License along with |
paul@380 | 19 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@380 | 20 | """ |
paul@380 | 21 | |
paul@380 | 22 | from __builtins__.types import check_int, check_string |
paul@380 | 23 | from native import iconv_close, iconv_open, iconv |
paul@380 | 24 | |
paul@380 | 25 | class ConverterError(Exception): |
paul@380 | 26 | |
paul@380 | 27 | "An error indicating a failure involving a character set converter." |
paul@380 | 28 | |
paul@380 | 29 | pass |
paul@380 | 30 | |
paul@380 | 31 | E2BIG = 7 |
paul@380 | 32 | EINVAL = 22 |
paul@380 | 33 | EILSEQ = 84 |
paul@380 | 34 | |
paul@380 | 35 | class Converter: |
paul@380 | 36 | |
paul@380 | 37 | "A character set converter." |
paul@380 | 38 | |
paul@380 | 39 | def __init__(self, from_encoding, to_encoding): |
paul@380 | 40 | |
paul@380 | 41 | "Initialise conversion between 'from_encoding' and 'to_encoding'." |
paul@380 | 42 | |
paul@380 | 43 | check_string(from_encoding) |
paul@380 | 44 | check_string(to_encoding) |
paul@380 | 45 | self.__data__ = iconv_open(to_encoding, from_encoding) |
paul@380 | 46 | |
paul@380 | 47 | def close(self): |
paul@380 | 48 | |
paul@380 | 49 | "Close this converter." |
paul@380 | 50 | |
paul@380 | 51 | iconv_close(self.__data__) |
paul@380 | 52 | self.__data__ = None |
paul@380 | 53 | |
paul@380 | 54 | def convert(self, s): |
paul@380 | 55 | |
paul@380 | 56 | "Convert 's' between the converter's encodings." |
paul@380 | 57 | |
paul@380 | 58 | if self.__data__ is None: |
paul@380 | 59 | raise ConverterError |
paul@380 | 60 | |
paul@380 | 61 | check_string(s) |
paul@380 | 62 | |
paul@380 | 63 | result = [] |
paul@380 | 64 | state = [0, len(s)] |
paul@380 | 65 | |
paul@380 | 66 | while True: |
paul@380 | 67 | |
paul@380 | 68 | # Obtain converted text and update the state. |
paul@380 | 69 | |
paul@380 | 70 | out = iconv(self.__data__, s, state) |
paul@380 | 71 | result.append(out) |
paul@380 | 72 | |
paul@380 | 73 | # Test for the end of the conversion. |
paul@380 | 74 | |
paul@380 | 75 | start, remaining = state |
paul@380 | 76 | if not remaining: |
paul@380 | 77 | return "".join(result) |
paul@380 | 78 | |
paul@380 | 79 | # vim: tabstop=4 expandtab shiftwidth=4 |