1
# Copyright (c) 2008, Robert D. Cameron.
2
# Licensed to the public under the Open Software License 3.0.
3
# Licensed to International Characters, Inc., under the Academic
8
ord_template = "template<> struct Ord<%s,'%s'> {static uint8_t const value = 0x%x;};\n"
11
file.write(ord_template % ('ASCII', "\\0", 0))
12
for chr_val in range(0x20,0x7E):
14
if ch == "'" or ch == "\\": ch = "\\" + ch
15
file.write(ord_template % ('ASCII', ch, chr_val))
17
class EncodingFailure(Exception):
18
def __init__(self, value):
22
file.write(ord_template % ('EBCDIC', "\\0", 0))
23
encoder = codecs.getencoder('cp037')
24
for chr_val in range(0x20,0x7E):
26
(encoded, src_lgth) = encoder(ch)
27
if src_lgth != 1: raise EncodingFailure('input lgth')
28
if len(encoded) != 1: raise EncodingFailure('output lgth')
29
if ch == "'" or ch == "\\": ch = "\\" + ch
30
file.write(ord_template % ('EBCDIC', ch, ord(encoded[0])))
32
ASCII_EBCDIC_header = r"""/* ASCII_EBCDIC.h
33
Copyright (c) 2008, Robert D. Cameron.
34
Licensed to the public under the Open Software License 3.0.
35
Licensed to International Characters, Inc., under the Academic
38
This is a generated file using ASCII_EBCDIC.py. Do not edit.
46
#include "../../lib/stdint.h"
48
#include "../xmlmodel.h"
50
template<CodeUnit_Base C> struct CR;
51
template<> struct CR<ASCII> {static unsigned char const value = 0x0D;};
52
template<> struct CR<EBCDIC> {static unsigned char const value = 0x0D;};
54
template<CodeUnit_Base C> struct LF;
55
template<> struct LF<ASCII> {static unsigned char const value = 0x0A;};
56
template<> struct LF<EBCDIC> {static unsigned char const value = 0x25;};
58
template<CodeUnit_Base C> struct HT;
59
template<> struct HT<ASCII> {static unsigned char const value = 0x09;};
60
template<> struct HT<EBCDIC> {static unsigned char const value = 0x05;};
62
template<CodeUnit_Base C, unsigned char c> struct Ord;
65
UC2lc_base = "template <unsigned char _> struct UC2lc {static unsigned char const value = _;};\n"
66
UC2lc_template = "template <> struct UC2lc<'%s'> {static unsigned char const value = '%s';};\n"
67
lc2UC_base = "template <unsigned char _> struct lc2UC {static unsigned char const value = _;};\n"
68
lc2UC_template = "template <> struct lc2UC<'%s'> {static unsigned char const value = '%s';};\n"
73
f.write(UC2lc_template % (chr(ord('A')+v), chr(ord('a')+v)))
78
f.write(lc2UC_template % (chr(ord('a')+v), chr(ord('A')+v)))
80
def gen_ASCII_EBCDIC_h(filename):
81
define_sym = filename[:-2] + "_H"
82
f = open(filename, 'w')
83
f.write("#ifndef %s\n#define %s\n" % (define_sym, define_sym));
84
f.write(ASCII_EBCDIC_header + "\n")
96
if __name__ == "__main__": gen_ASCII_EBCDIC_h('ASCII_EBCDIC.h')