home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / OL.LZH / PROCS.LZH / FILENAME.ICN < prev    next >
Text File  |  1991-07-13  |  1KB  |  54 lines

  1. ############################################################################
  2. #
  3. #    Name:    filename.icn
  4. #
  5. #    Title:    Parse file names
  6. #
  7. #    Author:    Robert J. Alexander
  8. #
  9. #    Date:    December 5, 1989
  10. #
  11. ############################################################################
  12. #
  13. #  suffix() -- Parses a hierarchical file name, returning a 2-element
  14. #  list:  [prefix,suffix].  E.g. suffix("/a/b/c.d") -> ["/a/b/c","d"]
  15. #
  16. #
  17. #  tail() -- Parses a hierarchical file name, returning a 2-element
  18. #  list:  [head,tail].  E.g. tail("/a/b/c.d") -> ["/a/b","c.d"].
  19. #
  20. #  components() -- Parses a hierarchical file name, returning a list of
  21. #  all directory names in the file path, with the file name (tail) as
  22. #  the last element.
  23. #
  24. #  E.g.  components("/a/b/c.d") -> ["/","a","b","c.d"].
  25. #
  26. ############################################################################
  27.  
  28. procedure suffix(s,separator)
  29.    local i
  30.    /separator := "."
  31.    i := *s + 1
  32.    every i := find(separator,s)
  33.    return [s[1:i],s[(*s >= i) + 1:0] | &null]
  34. end
  35.  
  36. procedure tail(s,separator)
  37.    local i
  38.    /separator := "/"
  39.    i := 0
  40.    every i := find(separator,s)
  41.    return [s[1:i + (i <= 1 | 0)],"" ~== s[i + 1:0] | &null]
  42. end
  43.  
  44. procedure components(s,separator)
  45.    local x,head
  46.    /separator := "/"
  47.    x := tail(s,separator)
  48.    return case head := x[1] of {
  49.       separator: [separator]
  50.       "": []
  51.       default: components(head)
  52.       } ||| ([&null ~=== x[2]] | [])
  53. end
  54.