Esempio n. 1
0
CAMLprim value ml_text_encode_bigarray(value cd_val, value buf_val, value pos_val, value len_val, value code_val)
{
    CAMLparam5(cd_val, buf_val, pos_val, len_val, code_val);

    uint32_t code = Int_val(code_val);
    size_t len = Long_val(len_val);
    size_t in_left = 4;
    char *in_bytes = (char*)&code;
    size_t out_left = len;
    char *out_bytes = (char*)Caml_ba_data_val(buf_val) + Long_val(pos_val);

    iconv(Iconv_val(cd_val), &in_bytes, &in_left, &out_bytes, &out_left);

    if (in_left == 0) {
        value result = caml_alloc_tuple(1);
        Store_field(result, 0, Val_int(len - out_left));
        CAMLreturn(result);
    } else if (errno == E2BIG)
        CAMLreturn(Val_need_more);
    else
        CAMLreturn(Val_error);
}
Esempio n. 2
0
CAMLprim value ml_text_decode(value cd_val, value buf_val, value pos_val, value len_val)
{
  CAMLparam4(cd_val, buf_val, pos_val, len_val);

  uint32 code;
  size_t len = Long_val(len_val);
  size_t in_left = len;
  char *in_bytes = String_val(buf_val) + Long_val(pos_val);
  size_t out_left = 4;
  char *out_bytes = (char*)&code;

  iconv(Iconv_val(cd_val), &in_bytes, &in_left, &out_bytes, &out_left);

  if (out_left == 0) {
    value result = caml_alloc_tuple(2);
    Store_field(result, 0, Val_int(code));
    Store_field(result, 1, Val_int(len - in_left));
    CAMLreturn(result);
  } else if (errno == EINVAL)
    CAMLreturn(Val_need_more);
  else
    CAMLreturn(Val_error);
}
Esempio n. 3
0
long ml_iconv_hash(value v)
{
  return (long)Iconv_val(v);
}
Esempio n. 4
0
int ml_iconv_compare(value v1, value v2)
{
  return (int)((long)Iconv_val(v1) - (long)Iconv_val(v2));
}
Esempio n. 5
0
void ml_iconv_finalize(value cd)
{
  iconv_close(Iconv_val(cd));
}