home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 2 / 2296 / stripunb.icn < prev   
Encoding:
Text File  |  1990-12-28  |  3.2 KB  |  125 lines

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