Debian Codegraph @

ipl/packs/euler/ebcdic.icn

raw
1 ############################################################################
2 #
3 # File: ebcdic.icn
4 #
5 # Subject: Procedures to convert between ASCII and EBCDIC
6 #
7 # Author: Alan Beale
8 #
9 # Date: March 31, 1990
10 #
11 ############################################################################
12 #
13 # These procedures assist in use of the ASCII and EBCDIC character sets,
14 # regardless of the native character set of the host:
15 #
16 # Ascii128() Returns a 128-byte string of ASCII characters in
17 # numerical order. Ascii128() should be used in
18 # preference to &ascii for applications which might
19 # run on an EBCDIC host.
20 #
21 # Ascii256() Returns a 256-byte string representing the 256-
22 # character ASCII character set. On an EBCDIC host,
23 # the order of the second 128 characters is essentially
24 # arbitrary.
25 #
26 # Ebcdic() Returns a 256-byte string of EBCDIC characters in
27 # numerical order.
28 #
29 # AsciiChar(i) Returns the character whose ASCII representation is i.
30 #
31 # AsciiOrd(c) Returns the position of the character c in the ASCII
32 # collating sequence.
33 #
34 # EbcdicChar(i) Returns the character whose EBCDIC representation is i.
35 #
36 # EbcdicOrd(c) Returns the position of the character c in the EBCDIC
37 # collating sequence.
38 #
39 # MapEtoA(s) Maps a string of EBCDIC characters to the equivalent
40 # ASCII string, according to a plausible mapping.
41 #
42 # MapAtoE(s) Maps a string of ASCII characters to the equivalent
43 # EBCDIC string, according to a plausible mapping.
44 #
45 # Control(c) Returns the "control character" associated with the
46 # character c. On an EBCDIC host, with $ representing
47 # an EBCDIC character with no 7-bit ASCII equivalent,
48 # Control("$") may not be identical to "\^$", as
49 # translated by ICONT (and neither result is particularly
50 # meaningful).
51 #
52 ############################################################################
53 #
54 # Notes:
55 #
56 # There is no universally accepted mapping between ASCII and EBCDIC.
57 # See the SHARE Inc. publication "ASCII and EBCDIC Character Set and
58 # Code Issues in Systems Application Architecture" for more information
59 # than you would ever want to have on this subject.
60 #
61 # The mapping of the first 128 characters defined below by Ascii128()
62 # is the most commonly accepted mapping, even though it probably
63 # is not exactly like the mapping used by your favorite PC to mainframe
64 # file transfer utility. The mapping of the second 128 characters
65 # is quite arbitrary, except that where an alternate translation of
66 # ASCII char(n) is popular, this translation is assigned to
67 # Ascii256()[n+129].
68 #
69 # The behavior of all functions in this package is controlled solely
70 # by the string literals in the _Eascii() procedure. Therefore you
71 # may modify these strings to taste, and still obtain consistent
72 # results, provided that each character appears exactly once in the
73 # result of _Eascii().
74 #
75 # Yes, it's really true that the EBCDIC "\n" (NL, char(16r15)) is not
76 # the same as "\l" (LF, char(16r25)). How can that be? "Don't blame
77 # me, man, I didn't do it."
78 #
79 ############################################################################
80
81 procedure _Eascii()
82 static EinAorder
83 initial
84 EinAorder :=
85 # NUL SOH STX ETX EOT ENQ ACK BEL BS HT NL VT FF CR SO SI
86 "\x00\x01\x02\x03\x37\x2d\x2e\x2f\x16\x05\x15\x0b\x0c\x0d\x0e\x0f"||
87 # DLE DC1 DC2 DC3 DC4 NAK SYN ETB CAN EM SUB ESC FS GS RS US
88 "\x10\x11\x12\x13\x3c\x3d\x32\x26\x18\x19\x3f\x27\x1c\x1d\x1e\x1f"||
89 # sp ! " # $ % & ' ( ) * + , - . /
90 "\x40\x5a\x7f\x7b\x5b\x6c\x50\x7d\x4d\x5d\x5c\x4e\x6b\x60\x4b\x61"||
91 # 0 1 2 3 4 5 6 7 8 9 : ; < = > ?
92 "\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\x7a\x5e\x4c\x7e\x6e\x6f"||
93 # @ A B C D E F G H I J K L M N O
94 "\x7c\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xd1\xd2\xd3\xd4\xd5\xd6"||
95 # P Q R S T U V W X Y Z $< \ $> ^ _
96 "\xd7\xd8\xd9\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xad\xe0\xbd\x5f\x6d"||
97 # ` a b c d e f g h i j k l m n o
98 "\x79\x81\x82\x83\x84\x85\x86\x87\x88\x89\x91\x92\x93\x94\x95\x96"||
99 # p q r s t u v w x y z $( | $) ~ DEL
100 "\x97\x98\x99\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xc0\x4f\xd0\xa1\x07"||
101 "\x04\x06\x08\x09\x0a\x14\x17\x1a\x1b\x20\x25\x21\x22\x23\x24\x28_
102 \x29\x2a\x2b\x2c\x30\x31\x33\x34\x35\x36\x38\x39\x3a\x3b\x3e\xff_
103 \x41\x42\x43\x44\x4a\x45\x46\x47\x48\x49\x51\x52\x53\x54\x55\x56_
104 \x57\x58\x59\x62\x63\x64\x65\x66\x67\x68\x69\x70\x71\x72\x73\x74_
105 \x75\x76\x77\x78\x80\x8a\x8c\x8d\x8e\x8f\x90\x9a\x9c\x9d\x9e\x9f_
106 \xa0\xaa\xab\xac\xae\xaf\xb0\xb1\xb2\xb3\xb4\xb5\xb6\xb7\xb8\xb9_
107 \xba\xbb\xbc\xbe\xbf\xca\xcb\xcc\xcd\xce\xcf\xda\xdb\xdc\xdd\xde_
108 \xdf\xe1\xea\xeb\xec\xed\xee\xef\xfa\xfb\xfc\x8b\x6a\x9b\xfd\xfe"
109 return EinAorder
110 end
111
112 procedure Ascii128()
113 if "\l" == "\n" then return string(&ascii)
114 return _Eascii()[1+:128]
115 end
116
117 procedure Ascii256()
118 if "\l" == "\n" then return string(&cset)
119 return _Eascii()
120 end
121
122 procedure Ebcdic()
123 if "\l" ~== "\n" then return &cset
124 return map(&cset, _Eascii(), &cset)
125 end
126
127 procedure AsciiChar(i)
128 if "\l" == "\n" then return char(i)
129 return _Eascii()[0 < i+1] | runerr(205,i)
130 end
131
132 procedure AsciiOrd(c)
133 if "\l" == "\n" then return ord(c)
134 return ord(MapEtoA(c))
135 end
136
137 procedure EbcdicChar(i)
138 if "\l" ~== "\n" then return char(i)
139 return map(char(i), _Eascii(), &cset)
140 end
141
142 procedure EbcdicOrd(c)
143 if "\l" ~== "\n" then return ord(c)
144 return ord(MapAtoE(c))
145 end
146
147 procedure MapEtoA(s)
148 return map(s, _Eascii(), &cset)
149 end
150
151 procedure MapAtoE(s)
152 return map(s, &cset, _Eascii())
153 end
154
155 procedure Control(c)
156 return AsciiChar(iand(AsciiOrd(c),16r1f))
157 end