home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / OL.LZH / PROCS.LZH / ADLUTILS.ICN < prev    next >
Text File  |  1991-09-05  |  4KB  |  174 lines

  1. ############################################################################
  2. #
  3. #    Name:    adlutils.icn
  4. #
  5. #    Title:    Utility procedures for processing address lists
  6. #
  7. #    Author:    Ralph E. Griswold
  8. #
  9. #    Date:    September 2, 1991
  10. #
  11. ############################################################################
  12. #
  13. #     Procedures used by programs that process address lists:
  14. #
  15. #     nextadd()        get next address
  16. #     writeadd(add)        write address
  17. #     get_country(add)    get country
  18. #     get_state(add)        get state (U.S. addresses only)
  19. #     get_city(add)        get city (U.S. addresses only)
  20. #     get_zipcode(add)    get ZIP code (U.S. addresses only)
  21. #     get_lastname(add)    get last name
  22. #     get_namepfx(add)    get name prefix
  23. #     get_title(add)        get name title
  24. #     format_country(s)    format country name
  25. #
  26. ############################################################################
  27. #
  28. #  Links:  lastname, buffer, namepfx, title
  29. #
  30. ############################################################################
  31.  
  32. link lastname, buffer, namepfx, title
  33.  
  34. record label(header, text, comments)
  35.  
  36. procedure nextadd()
  37.    local comments, header, line, text
  38.  
  39.    initial {            # Get to first label.
  40.       while line := Read() do
  41.          line ? {
  42.             if ="#" then {
  43.                PutBack(line)
  44.                break
  45.                }
  46.             }
  47.       }
  48.  
  49.    header := Read() | fail
  50.  
  51.    comments := text := ""
  52.  
  53.    while line := Read() do
  54.       line ? {
  55.          if pos(0) then next    # Skip empty lines.
  56.          else if ="*" then comments ||:= "\n" || line
  57.          else if ="#" then {    # Header for next label.
  58.             PutBack(line)
  59.             break        # Done with current label.
  60.             }
  61.          else text ||:= "\n" || line
  62.          }
  63.    every text | comments ?:= {    # Strip off leading newline, if any.
  64.       move(1)
  65.       tab(0)
  66.       }
  67.  
  68.    return label(header, text, comments)
  69.  
  70. end
  71.  
  72. procedure writeadd(add)
  73.  
  74.    if *add.text + *add.comments = 0 then return
  75.    write(add.header)
  76.    if *add.text > 0 then write(add.text)
  77.    if *add.comments > 0 then write(add.comments)
  78.  
  79.    return
  80.  
  81. end
  82.  
  83. procedure get_country(add)
  84.  
  85.    trim(add.text) ? {
  86.       while tab(upto('\n')) do move(1)
  87.       if tab(0) ? {
  88.          tab(-1)
  89.          any(&digits)
  90.          } then return "U.S.A."
  91.       else return tab(0)
  92.       }
  93. end
  94.  
  95. procedure get_state(add)
  96.  
  97.    trim(add.text) ? {
  98.       while tab(upto('\n')) do move(1)
  99.       ="APO"
  100.       while tab(upto(',')) do move(1)
  101.       tab(many(' '))
  102.       return (tab(any(&ucase)) || tab(any(&ucase))) | "XX"
  103.       }
  104.  
  105. end
  106.  
  107. procedure get_city(add)        # only works for U.S. addresses
  108.    local result
  109.  
  110.    result := ""
  111.    trim(add.text) ? {
  112.       while tab(upto('\n')) do move(1)
  113.       result := ="APO"
  114.       result ||:= tab(upto(','))
  115.       return result
  116.       }
  117.  
  118. end
  119.  
  120.  
  121.  
  122. procedure get_zipcode(add)
  123.    local zip
  124.  
  125.    trim(add.text) ? {
  126.       while tab(upto('\n')) do move(1)        # get to last line
  127.       while tab(upto(' ')) do tab(many(' '))    # get to last field
  128.       zip := tab(0)
  129.       if *zip = 5 & integer(zip) then return zip
  130.       else if *zip = 10 & zip ? {
  131.          integer(move(5)) & ="-" & integer(tab(0))
  132.          }
  133.       then return zip
  134.       else return "9999999999"            # "to the end of the universe"
  135.       }
  136.  
  137. end
  138.  
  139. procedure get_lastname(add)
  140.  
  141.    return lastname(add.text ? tab(upto('\n') | 0))
  142.  
  143. end
  144.  
  145. procedure get_namepfx(add)
  146.  
  147.    return namepfx(add.text ? tab(upto('\n') | 0))
  148.  
  149. end
  150.  
  151. procedure get_title(add)
  152.  
  153.    return title(add.text ? tab(upto('\n') | 0))
  154.  
  155. end
  156.  
  157. procedure format_country(s)
  158.    local t
  159.  
  160.    s := map(s)
  161.    t := ""
  162.    s ? while tab(upto(&lcase)) do {
  163.       word := tab(many(&lcase))
  164.       if word == "of" then t ||:= word
  165.       else t ||:= {
  166.          word ? {
  167.             map(move(1),&lcase,&ucase) || tab(0)
  168.             }
  169.          }
  170.       t ||:= move(1)
  171.       }
  172.    return t
  173. end
  174.