home *** CD-ROM | disk | FTP | other *** search
- ############################################################################
- #
- # File: morse.icn
- #
- # Subject: Program to convert string to Morse code
- #
- # Author: Ralph E. Griswold and Robert J. Alexander
- #
- # Date: May 5, 1992
- #
- ###########################################################################
- #
- # If "morse" is invoked without arguments, a Morse code table is
- # printed. If words are entered as arguments, the Morse code
- # conversion is printed in dots and dashes. If the first character of
- # the first argument is a dot or dash, the arguments are takes as Morse
- # code and converted to a string.
- #
- ############################################################################
- #
- # Links: colmize
- #
- ############################################################################
-
- link colmize
-
- procedure main(arg)
- local lst, c, s
- if *arg = 0 then {
- lst := []
- every c := !(&ucase || " " || &digits) do {
- put(lst,c || " " || morse(c))
- }
- every write(colmize(lst))
- }
- else {
- s := ""
- every s ||:= !arg || " "
- s := trim(s)
- write((if any('.-',s) then unmorse else morse)(s))
- }
- end
-
-
- ############################################################################
- #
- # This procedure converts the string s to its Morse code equivalent.
- #
- ############################################################################
-
- procedure morse(s)
- local i, t, c, x
- static morsemeander, morseindex
-
- initial {
- morsemeander :=
- "....------.----..---.-.---...--.--.-..--..-.--....-.-.-...-..-....."
- morseindex :=
- "TMOT09TTT1T8TT2GQTTTJTZ7T3NKYTTCTTTTDXTTWPTB64EARTTLTVTIUFTSH5"
- }
-
- x := ""
- every c := !map(s,&lcase,&ucase) do
- if not(i := find(c,morseindex)) then x ||:= " "
- else {
- t := morsemeander[i+:6]
- x ||:= t[find("-",t)+1:0] || " "
- }
- return x
- end
-
-
- ############################################################################
- #
- # This procedure converts Morse code string s to its character string
- # equivalent.
- #
- ############################################################################
-
- procedure unmorse(s)
- local x, t, c
- x := ""
- s ? {
- until pos(0) do {
- tab(many(' \t'))
- t := tab(upto(' \t') | 0)
- if t == "" then next
- x ||:= (every c := !(&ucase || &digits) do {
- if trim(morse(c)) == t then break c
- }) | "?"
- }
- }
- return x
- end
-
-