1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/templates/native/iconv.c Mon Dec 12 00:40:54 2016 +0100
1.3 @@ -0,0 +1,151 @@
1.4 +/* Native functions for character set conversion.
1.5 +
1.6 +Copyright (C) 2016 Paul Boddie <paul@boddie.org.uk>
1.7 +
1.8 +This program is free software; you can redistribute it and/or modify it under
1.9 +the terms of the GNU General Public License as published by the Free Software
1.10 +Foundation; either version 3 of the License, or (at your option) any later
1.11 +version.
1.12 +
1.13 +This program is distributed in the hope that it will be useful, but WITHOUT
1.14 +ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
1.15 +FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
1.16 +details.
1.17 +
1.18 +You should have received a copy of the GNU General Public License along with
1.19 +this program. If not, see <http://www.gnu.org/licenses/>.
1.20 +*/
1.21 +
1.22 +#include <iconv.h> /* iconv, iconv_close, iconv_open */
1.23 +#include <string.h> /* memcpy */
1.24 +#include <errno.h> /* errno */
1.25 +#include "native/common.h"
1.26 +#include "types.h"
1.27 +#include "exceptions.h"
1.28 +#include "ops.h"
1.29 +#include "progconsts.h"
1.30 +#include "progops.h"
1.31 +#include "progtypes.h"
1.32 +#include "main.h"
1.33 +
1.34 +static const size_t OUTBUFSIZE_MIN = 16;
1.35 +
1.36 +/* Character set conversion. */
1.37 +
1.38 +__attr __fn_native_iconv_iconv(__attr __args[])
1.39 +{
1.40 + __attr * const cd = &__args[1];
1.41 + __attr * const instr = &__args[2];
1.42 + __attr * const state = &__args[3];
1.43 + /* cd interpreted as iconv_t */
1.44 + iconv_t c = (iconv_t) cd->datavalue;
1.45 + /* instr.__data__ interpreted as string */
1.46 + char *inbuf = __load_via_object(instr->value, __pos___data__).strvalue;
1.47 + /* state.__data__ interpreted as list */
1.48 + __fragment *f = __load_via_object(state->value, __pos___data__).seqvalue;
1.49 +
1.50 + /* Obtain the start position from the state. */
1.51 +
1.52 + int start = __load_via_object(f->attrs[0].value, __pos___data__).intvalue;
1.53 + int remaining = __load_via_object(f->attrs[1].value, __pos___data__).intvalue;
1.54 +
1.55 + /* Allocate a string for the output buffer using the remaining input size
1.56 + as a guide. */
1.57 +
1.58 + size_t outbufsize = remaining < OUTBUFSIZE_MIN ? OUTBUFSIZE_MIN : remaining;
1.59 + size_t outbytesleft = outbufsize;
1.60 + size_t inbytesleft = remaining;
1.61 +
1.62 + char buf[outbytesleft];
1.63 + char *outbuf = buf, *outbufstart = outbuf, *resultbuf;
1.64 + size_t result, outbytestotal;
1.65 +
1.66 + /* Convert from the start point. */
1.67 +
1.68 + inbuf += start;
1.69 +
1.70 + errno = 0;
1.71 + result = iconv(c, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
1.72 +
1.73 + /* Return any string. */
1.74 +
1.75 + if ((result != -1) || (errno == E2BIG))
1.76 + {
1.77 + outbytestotal = outbufsize - outbytesleft;
1.78 + resultbuf = __ALLOCATE(outbytestotal + 1, sizeof(char));
1.79 + memcpy(resultbuf, outbufstart, outbytestotal);
1.80 +
1.81 + /* Mutate the state to indicate the next input buffer position. */
1.82 +
1.83 + f->attrs[0] = __new_int(start + remaining - inbytesleft);
1.84 + f->attrs[1] = __new_int(inbytesleft);
1.85 + return __new_str(resultbuf, outbytestotal);
1.86 + }
1.87 +
1.88 + /* Invalid sequence. */
1.89 +
1.90 + if (errno == EILSEQ)
1.91 + {
1.92 + resultbuf = __ALLOCATE(inbytesleft + 1, sizeof(char));
1.93 + memcpy(resultbuf, inbuf, inbytesleft);
1.94 + __raise_os_error(__new_int(errno), __new_str(resultbuf, inbytesleft));
1.95 + }
1.96 +
1.97 + /* Incomplete sequence. */
1.98 +
1.99 + else if (errno == EINVAL)
1.100 + {
1.101 + resultbuf = __ALLOCATE(inbytesleft + 1, sizeof(char));
1.102 + memcpy(resultbuf, inbuf, inbytesleft);
1.103 + __raise_os_error(__new_int(errno), __new_str(resultbuf, inbytesleft));
1.104 + }
1.105 +
1.106 + /* General failure. */
1.107 +
1.108 + else
1.109 + __raise_os_error(__new_int(errno), __builtins___none_None);
1.110 +}
1.111 +
1.112 +__attr __fn_native_iconv_iconv_close(__attr __args[])
1.113 +{
1.114 + __attr * const cd = &__args[1];
1.115 + /* cd interpreted as iconv_t */
1.116 + iconv_t c = (iconv_t) cd->datavalue;
1.117 +
1.118 + errno = 0;
1.119 +
1.120 + if (iconv_close(c) == -1)
1.121 + __raise_os_error(__new_int(errno), __builtins___none_None);
1.122 +
1.123 + return __builtins___none_None;
1.124 +}
1.125 +
1.126 +__attr __fn_native_iconv_iconv_open(__attr __args[])
1.127 +{
1.128 + __attr * const tocode = &__args[1];
1.129 + __attr * const fromcode = &__args[2];
1.130 + /* tocode.__data__ interpreted as string */
1.131 + char *t = __load_via_object(tocode->value, __pos___data__).strvalue;
1.132 + /* fromcode.__data__ interpreted as string */
1.133 + char *f = __load_via_object(fromcode->value, __pos___data__).strvalue;
1.134 + iconv_t result;
1.135 + __attr attr;
1.136 +
1.137 + errno = 0;
1.138 + result = iconv_open(t, f);
1.139 +
1.140 + if (result == (iconv_t) -1)
1.141 + __raise_os_error(__new_int(errno), __builtins___none_None);
1.142 +
1.143 + /* Return the descriptor as an opaque value. */
1.144 +
1.145 + attr.context = 0;
1.146 + attr.datavalue = (void *) result;
1.147 + return attr;
1.148 +}
1.149 +
1.150 +/* Module initialisation. */
1.151 +
1.152 +void __main_native_iconv()
1.153 +{
1.154 +}