File: im_common.h

package info (click to toggle)
mlterm 3.9.4-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 25,340 kB
  • sloc: ansic: 154,713; sh: 5,302; cpp: 2,953; objc: 2,776; java: 2,472; makefile: 2,445; perl: 1,674; xml: 44
file content (64 lines) | stat: -rw-r--r-- 1,057 bytes parent folder | download
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
/* -*- c-basic-offset:2; tab-width:2; indent-tabs-mode:nil -*- */

#ifndef __IM_COMMON_H__
#define __IM_COMMON_H__

static inline u_int im_convert_encoding(ef_parser_t *parser, ef_conv_t *conv, const u_char *from,
                                        u_char **to, u_int from_len) {
  u_int len;
  u_int filled_len;

  if (from == NULL || parser == NULL || conv == NULL) {
    return 0;
  }

  *to = NULL;

  len = 0;

  (*parser->init)(parser);
  (*parser->set_str)(parser, from, from_len);
  (*conv->init)(conv);

#define UNIT__ 32

  while (1) {
    u_char *p;

    if (!(p = realloc(*to, len + UNIT__ + 1))) {
#ifdef DEBUG
      bl_warn_printf(BL_DEBUG_TAG " malloc failed.\n");
#endif

      if (*to) {
        free(*to);
      }

      return 0;
    }

    *to = p;

    p += len;

    filled_len = (*conv->convert)(conv, p, UNIT__, parser);

    len += filled_len;

    if (filled_len == 0 && parser->is_eos) {
      /* finished converting */

      break;
    }
  }

#undef UNIT__

  if (len) {
    (*to)[len] = '\0';
  }

  return len;
}

#endif