summaryrefslogtreecommitdiff
path: root/ipl/progs/morse.icn
blob: dbfcaa78334dcc446b99d38fab2c7fb4e65bc45c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
############################################################################
#
#	File:     morse.icn
#
#	Subject:  Program to convert string to Morse code
#
#	Authors:  Ralph E. Griswold and Robert J. Alexander
#
#	Date:     August 14, 1996
#
############################################################################
#
#   This file is in the public domain.
#
############################################################################
#
#  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