home *** CD-ROM | disk | FTP | other *** search
/ ftp.cs.arizona.edu / ftp.cs.arizona.edu.tar / ftp.cs.arizona.edu / icon / historic / v941.tgz / icon.v941src.tar / icon.v941src / ipl / progs / morse.icn < prev    next >
Text File  |  2000-07-29  |  3KB  |  100 lines

  1. ############################################################################
  2. #
  3. #    File:     morse.icn
  4. #
  5. #    Subject:  Program to convert string to Morse code
  6. #
  7. #    Authors:  Ralph E. Griswold and Robert J. Alexander
  8. #
  9. #    Date:     August 14, 1996
  10. #
  11. ############################################################################
  12. #
  13. #   This file is in the public domain.
  14. #
  15. ############################################################################
  16. #
  17. #  If "morse" is invoked without arguments, a Morse code table is
  18. #  printed.  If words are entered as arguments, the Morse code
  19. #  conversion is printed in dots and dashes.  If the first character of
  20. #  the first argument is a dot or dash, the arguments are takes as Morse
  21. #  code and converted to a string.
  22. #
  23. ############################################################################
  24. #
  25. #  Links: colmize
  26. #
  27. ############################################################################
  28.  
  29. link colmize
  30.  
  31. procedure main(arg)
  32.    local lst, c, s
  33.    if *arg = 0 then {
  34.       lst := []
  35.       every c := !(&ucase || "    " || &digits) do {
  36.      put(lst,c || " " || morse(c))
  37.      }
  38.       every write(colmize(lst))
  39.       }
  40.    else {
  41.       s := ""
  42.       every s ||:= !arg || " "
  43.       s := trim(s)
  44.       write((if any('.-',s) then unmorse else morse)(s))
  45.       }
  46. end
  47.  
  48.  
  49. ############################################################################
  50. #
  51. #     This procedure converts the string s to its Morse code equivalent.
  52. #
  53. ############################################################################
  54.  
  55. procedure morse(s)
  56.    local i, t, c, x
  57.    static morsemeander, morseindex
  58.  
  59.    initial {
  60.       morsemeander :=
  61.       "....------.----..---.-.---...--.--.-..--..-.--....-.-.-...-..-....."
  62.       morseindex :=
  63.       "TMOT09TTT1T8TT2GQTTTJTZ7T3NKYTTCTTTTDXTTWPTB64EARTTLTVTIUFTSH5"
  64.       }
  65.  
  66.    x := ""
  67.    every c := !map(s,&lcase,&ucase) do
  68.       if not(i := find(c,morseindex)) then x ||:= "    "
  69.       else {
  70.      t := morsemeander[i+:6]
  71.      x ||:= t[find("-",t)+1:0] || " "
  72.      }
  73.    return x
  74. end
  75.  
  76.  
  77. ############################################################################
  78. #
  79. #     This procedure converts Morse code string s to its character string
  80. #     equivalent.
  81. #
  82. ############################################################################
  83.  
  84. procedure unmorse(s)
  85.    local x, t, c
  86.    x := ""
  87.    s ? {
  88.       until pos(0) do {
  89.      tab(many(' \t'))
  90.      t := tab(upto(' \t') | 0)
  91.      if t == "" then next
  92.      x ||:= (every c := !(&ucase || &digits) do {
  93.         if trim(morse(c)) == t then break c
  94.         }) | "?"
  95.      }
  96.       }
  97.    return x
  98. end
  99.  
  100.