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

  1. ############################################################################
  2. #
  3. #    Name:     stripunb.icn
  4. #
  5. #    Title:     Strip unbalanced material
  6. #
  7. #    Author:     Richard L. Goerwitz
  8. #
  9. #    Version: 1.6
  10. #
  11. #    Date:     June 1, 1991
  12. #
  13. ############################################################################
  14. #  
  15. #     This procedure strips material from a line which is unbalanced with
  16. #  respect to the characters defined in arguments 1 and 2 (unbalanced
  17. #  being defined as bal() defines it, except that characters preceded
  18. #  by a backslash are counted as regular characters, and are not taken
  19. #  into account by the balancing algorithm).
  20. #
  21. #     One little bit of weirdness I added in is a table argument. Put
  22. #  simply, if you call stripunb() as follows,
  23. #
  24. #      stripunb('<','>',s,&null,&null,t)
  25. #
  26. #  and if t is a table having the form,
  27. #
  28. #      key:  "bold"        value: outstr("\e[2m", "\e1m")
  29. #      key:  "underline"   value: outstr("\e[4m", "\e1m")
  30. #      etc.
  31. #
  32. #  then every instance of "<bold>" in string s will be mapped to
  33. #  "\e2m," and every instance of "</bold>" will be mapped to "\e[1m."
  34. #  Values in table t must be records of type output(on, off).  When
  35. #  "</>" is encountered, stripunb will output the .off value for the
  36. #  preceding .on string encountered.
  37. #
  38. ############################################################################
  39. #
  40. #  Links: slashbal.icn
  41. #
  42. ############################################################################
  43.  
  44. link slashbal
  45.  
  46. global last_k
  47. record outstr(on, off)
  48.  
  49.  
  50. procedure stripunb(c1,c2,s,i,j,t)
  51.  
  52.     # NB:  Stripunb() returns a string - not an integer (like find,
  53.     # upto).
  54.  
  55.     local lookinfor, bothcs, s2, k, new_s
  56.     #global last_k
  57.     initial last_k := list()
  58.  
  59.     /c1 := '<'
  60.     /c2 := '>'
  61.     bothcs := c1 ++ c2
  62.     lookinfor := c1 ++ '\\'
  63.     c := &cset -- c1 -- c2
  64.  
  65.     /s := &subject
  66.     if \i then {
  67.     if i < 1 then
  68.         i := *s + (i+1)
  69.     }
  70.     else i := \&pos | 1
  71.     if \j then {
  72.     if j < 1 then
  73.         j := *s + (j+1)
  74.     }
  75.     else j := *s + 1
  76.  
  77.     s2 := ""
  78.     s ? {
  79.     while s2 ||:= tab(upto(lookinfor)) do {
  80.         if ="\\" then {
  81.         if not any(bothcs) then
  82.             s2 ||:= "\\"
  83.         &pos+1 > j & (return s2)
  84.         s2 ||:= move(1)
  85.         next
  86.         }
  87.         else {
  88.         &pos > j & (return s2)
  89.         any(c1) |
  90.             stop("stripunb:  Unbalanced string, pos(",&pos,").\n",s)
  91.         if not (k := tab(slashbal(c,c1,c2)))
  92.         then {
  93.             # If the last char on the line is the right-delim...
  94.             if (.&subject[&pos:0]||" ") ? slashbal(c,c1,c2)
  95.             # ...then, naturally, the rest of the line is the tag.
  96.             then k := tab(0)
  97.             else {
  98.             # BUT, if it's not the right-delim, then we have a
  99.             # tag split by a line break.  Blasted things.
  100.             return stripunb(c1,c2,&subject||read(&input),
  101.                     *.&subject,,t) |
  102.             # Can't find the right delimiter.  Parsing error.
  103.             stop("stripunb:  Incomplete tag\n",s[1:80] | s)
  104.             }
  105.         }
  106.         # T is the maptable.
  107.         if \t then {
  108.             k ?:= 2(tab(any(c1)), tab(upto(c2)), move(1), pos(0))
  109.             if k ?:= (="/", tab(0)) then {
  110.             compl:= pop(last_k) | stop("Incomplete tag, ",&subject) 
  111.             if k == ""
  112.             then k := compl
  113.             else k == compl | stop("Incorrectly paired tag,/tag.")
  114.             s2 ||:= \(\t[k]).off
  115.             }
  116.             else {
  117.             s2 ||:= \(\t[k]).on
  118.             push(last_k, k)
  119.             }
  120.         }
  121.         }
  122.     }
  123.     s2 ||:= tab(0)
  124.     }
  125.  
  126.     return s2
  127.  
  128. end
  129.