summaryrefslogtreecommitdiff
path: root/harnesses/dict_conv.py
blob: f7212939f549dcbfa9bf8b9ff6fb82924b643d38 (plain)
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
# -*- coding: utf-8 -*-
# dict_conv.py  (Python3 script)

import sys

ENC_UTF16_BE = 1
ENC_UTF16_LE = 2

def add_char(enc, s, c):
  if enc == ENC_UTF16_BE:
    s += "\\x00"

  s += c
  if enc == ENC_UTF16_LE:
    s += "\\x00"

  return s

def conv(enc, s):
  n = len(s)
  r = ""
  i = 0
  while i < n:
    c = s[i]
    if c == '\\':
      c = s[i+1]
      if c == '\\' or c == '"':
        r = add_char(enc, r, "\\" + c)
        i += 2
        continue
      else:
        raise("Unknown escape {0}".format(s))

    r = add_char(enc, r, c)
    i += 1

  return r

def main(enc):
  print("# This file was generated by dict_conv.py.")
  for line in sys.stdin:
    s = line.strip()
    if s[0] == '#':
      print(s)
      continue

    if s[0] == '"' and s[-1] == '"':
      s = conv(enc, s[1:-1])
      print("\"{0}\"".format(s))
    else:
      raise("Invalid format {0}".format(s))

def usage(argv):
  raise RuntimeError("Usage: python {0} utf16_be/utf16_le".format(argv[0]))


if __name__ == "__main__":
  argv = sys.argv
  argc = len(argv)

  if argc >= 2:
    s = argv[1]
    if s == 'utf16_be':
      enc = ENC_UTF16_BE
    elif s == 'utf16_le':
      enc = ENC_UTF16_LE
    else:
      usage(argv)
  else:
    usage(argv)

  main(enc)