home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / OL.LZH / PROCS.LZH / EBCDIC.ICN < prev    next >
Text File  |  1991-07-13  |  7KB  |  157 lines

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