/xmlbench/trunk

To get this branch, use:
bzr branch http://darksoft.org/webbzr/xmlbench/trunk

« back to all changes in this revision

Viewing changes to parse/parabix.20090922/src/charsets/ASCII_EBCDIC.py

  • Committer: Suren A. Chilingaryan
  • Date: 2009-09-23 17:13:04 UTC
  • Revision ID: csa@dside.dyndns.org-20090923171304-osvtr4zqb29h11kd
Intel, Tango, Phobos, and RapidXML parsers; Memory benchmark scripts

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
4
#    Free License 3.0.
 
5
 
 
6
import codecs;
 
7
 
 
8
ord_template = "template<> struct Ord<%s,'%s'> {static uint8_t const value = 0x%x;};\n"
 
9
 
 
10
def gen_ASCII(file):
 
11
        file.write(ord_template % ('ASCII', "\\0", 0))
 
12
        for chr_val in range(0x20,0x7E):
 
13
                ch = chr(chr_val)
 
14
                if ch == "'" or ch == "\\": ch = "\\" + ch
 
15
                file.write(ord_template % ('ASCII', ch, chr_val))
 
16
 
 
17
class EncodingFailure(Exception):
 
18
        def __init__(self, value):
 
19
                self.value = value
 
20
 
 
21
def gen_EBCDIC(file):
 
22
        file.write(ord_template % ('EBCDIC', "\\0", 0))
 
23
        encoder = codecs.getencoder('cp037')
 
24
        for chr_val in range(0x20,0x7E):
 
25
                ch = chr(chr_val)
 
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])))
 
31
 
 
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
 
36
    Free License 3.0.
 
37
    
 
38
    This is a generated file using ASCII_EBCDIC.py.  Do not edit.
 
39
 
 
40
*/
 
41
 
 
42
#ifndef _MSC_VER
 
43
#include <stdint.h>
 
44
#endif
 
45
#ifdef _MSC_VER
 
46
#include "../../lib/stdint.h"
 
47
#endif
 
48
#include "../xmlmodel.h"
 
49
 
 
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;};
 
53
 
 
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;};
 
57
 
 
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;};
 
61
 
 
62
template<CodeUnit_Base C, unsigned char c> struct Ord;
 
63
 
 
64
"""
 
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"
 
69
 
 
70
def gen_UC2lc(f):
 
71
        f.write(UC2lc_base)
 
72
        for v in range(26):
 
73
                f.write(UC2lc_template % (chr(ord('A')+v), chr(ord('a')+v)))
 
74
 
 
75
def gen_lc2UC(f):
 
76
        f.write(lc2UC_base)
 
77
        for v in range(26):
 
78
                f.write(lc2UC_template % (chr(ord('a')+v), chr(ord('A')+v)))
 
79
        
 
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")
 
85
        gen_ASCII(f)
 
86
        f.write("\n")
 
87
        gen_EBCDIC(f)
 
88
        f.write("\n")
 
89
        gen_UC2lc(f)
 
90
        f.write("\n")
 
91
        gen_lc2UC(f)
 
92
        f.write("\n#endif\n")
 
93
        f.close()
 
94
 
 
95
 
 
96
if __name__ == "__main__": gen_ASCII_EBCDIC_h('ASCII_EBCDIC.h')