File: ui_selection_encoding.c

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 (80 lines) | stat: -rw-r--r-- 1,814 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/* -*- c-basic-offset:2; tab-width:2; indent-tabs-mode:nil -*- */

#include "../ui_selection_encoding.h"

#include <stdio.h> /* NULL */
#include <pobl/bl_locale.h>
#include <vt_char_encoding.h>
#include <mef/ef_utf16_conv.h>
#include <mef/ef_utf16_parser.h>

/* --- static varaiables --- */

static ef_parser_t *utf_parser; /* leaked */
static ef_parser_t *parser; /* leaked */
static ef_conv_t *utf_conv; /* leaked */
static ef_conv_t *conv; /* leaked */

/* --- static functions --- */

/* See utf8_illegal_char() in vt_char_encoding.c */
static size_t utf16le_illegal_char(ef_conv_t *conv, u_char *dst, size_t dst_size, int *is_full,
                                   ef_char_t *ch) {
  *is_full = 0;

  if (ch->cs == DEC_SPECIAL) {
    u_int16_t utf16;

    if (dst_size < 2) {
      *is_full = 1;
    } else if ((utf16 = vt_convert_decsp_to_ucs(ef_char_to_int(ch)))) {
      memcpy(dst, (u_char*)&utf16, 2); /* little endian */
      return 2;
    }
  }

  return 0;
}

/* --- global functions --- */

ef_parser_t *ui_get_selection_parser(int utf) {
  if (utf) {
    if (!utf_parser) {
      if (!(utf_parser = ef_utf16le_parser_new())) {
        return NULL;
      }
    }

    return utf_parser;
  } else {
    if (!parser) {
      if (!(parser = vt_char_encoding_parser_new(vt_get_char_encoding(bl_get_codeset_win32())))) {
        return NULL;
      }
    }

    return parser;
  }
}

ef_conv_t *ui_get_selection_conv(int utf) {
  if (utf) {
    if (!utf_conv) {
      if (!(utf_conv = ef_utf16le_conv_new())) {
        return NULL;
      }
      utf_conv->illegal_char = utf16le_illegal_char;
    }

    return utf_conv;
  } else {
    if (!conv) {
      if (!(conv = vt_char_encoding_conv_new(vt_get_char_encoding(bl_get_codeset_win32())))) {
        return NULL;
      }
    }

    return conv;
  }
}