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 / idxtext.icn < prev    next >
Text File  |  2000-07-29  |  5KB  |  156 lines

  1. ############################################################################
  2. #
  3. #    File:     idxtext.icn
  4. #
  5. #    Subject:  Program for creating indexed text-base
  6. #
  7. #    Author:   Richard L. Goerwitz
  8. #
  9. #      Date:     July 9, 1991
  10. #
  11. ############################################################################
  12. #
  13. #   This file is in the public domain.
  14. #
  15. ############################################################################
  16. #
  17. #    Version:  1.15
  18. #
  19. ############################################################################
  20. #
  21. #      idxtext turns a file associated with gettext() routine into an
  22. #  indexed text-base.  Though gettext() will work fine with files
  23. #  that haven't been indexed via idxtext(), access is faster if the
  24. #  indexing is done if the file is, say, over 10k (on my system the
  25. #  crossover point is actually about 5k).
  26. #
  27. #      Usage is simply "idxtext [-a] file1 [file2 [...]]," where file1,
  28. #  file2, etc are the names of gettext-format files that are to be
  29. #  (re-)indexed.  The -a flag tells idxtext to abort if an index file
  30. #  already exists.
  31. #
  32. #      Indexed files have a very simple format: keyname delimiter offset
  33. #  [delimiter offset [etc.]]\n.  The first line of the index file is a
  34. #  pointer to the last indexed byte of the text-base file it indexes.
  35. #
  36. #  BUGS: Index files are too large.  Also, I've yet to find a portable
  37. #  way of creating unique index names that are capable of being
  38. #  uniquely identified with their original text file.  It might be
  39. #  sensible to hard code the name into the index.  The chances of a
  40. #  conflict seem remote enough that I haven't bothered.  If you're
  41. #  worried, use the -a flag. (RLG)
  42. ############################################################################
  43. #
  44. #  Links: adjuncts
  45. #
  46. #  Tested with: MS-DOS, MS-DOS/386, OS/2, ProIcon, UNIX
  47. #
  48. #  See also: gettext.icn
  49. #
  50. #  Modified by Phillip Lee Thomas
  51. #  History: modified link and local statements.
  52. #           modified to run under OS/2 and ProIcon.
  53. #           Added exit() statement.
  54. #           Move OS declarations to Set_OS() in adjuncts.icn.
  55. #           Allow multiple indexed values.
  56. #
  57. #  Version 1.15 (August 5, 1995)
  58. #           Use preprocessor include statement rather than link.
  59. #           Allow multiple index keys for a stretch of text:
  60. #              Example:
  61. #                ::key one  ::key two   ::another key
  62. #                Multiple lines of text which are retrieved
  63. #                by searching for these three keys.
  64. #                ::key for another stretch of text
  65. #                A second bit of text.
  66. #
  67. #
  68. ############################################################################
  69. #
  70. #  Links:  adjuncts
  71. #
  72. ############################################################################
  73.  
  74. link adjuncts
  75.  
  76. # declared in adjuncts.icn
  77. # global _slash, _baselen, _delimiter
  78.  
  79. procedure main(a)
  80.  
  81.    local ABORT, idxfile_name, fname, infile, outfile
  82.  
  83.    Set_OS()
  84.    
  85.    if \a[1] == "-a" then ABORT := pop(a)  
  86.  
  87.    # Check to see if we have any arguments.
  88.  
  89.    if find("Macintosh", &features) then {
  90.       writes("Enter file name for indexing: ")
  91.       a := [read()]
  92.       }
  93.    else  {
  94.       *a = 0 & stop("usage: idxtext [-a] file1 [file2 [...]]")
  95.       }
  96.  
  97.    # Start popping filenames off of the argument list.
  98.  
  99.    while fname := pop(a) do {
  100.  
  101.       # Open input file.
  102.  
  103.       infile := open(fname) |
  104.          { write(&errout, "idxtext:  ",fname," not found"); next }
  105.  
  106.       # Get index file name.
  107.  
  108.       idxfile_name := Pathname(fname) || getidxname(fname)
  109.       if \ABORT then if close(open(idxfile_name)) then
  110.          stop("idxtext:  index file ",idxfile_name, " already exists")
  111.       outfile := open(idxfile_name, "w") |
  112.          stop("idxtext:  can't open ", idxfile_name)
  113.  
  114.       # Write index to index.IDX file.
  115.  
  116.       write_index(infile, outfile)
  117.       every close(infile | outfile)
  118.       }
  119.    exit()
  120. end
  121.  
  122.  
  123. procedure write_index(in, out)
  124.  
  125.    local key_offset_table, w, line, KEY
  126.  
  127.    # Write to out all keys in file "in," with their byte
  128.    # offsets.
  129.  
  130.    key_offset_table := table()
  131.  
  132.    while (w := where(in), line := read(in)) do {
  133.       line ? {
  134.          while ="::" do {
  135.             KEY := trim(tab(find("::") | 0))
  136.             if not (/key_offset_table[KEY] := KEY || _delimiter || w)
  137.             then key_offset_table[KEY] ||:= _delimiter || w
  138.             }
  139.          }
  140.       }
  141.  
  142.    # First line of index contains the offset of the last
  143.    # indexed byte in write_index, so that we can still
  144.    # search unindexed parts of in.
  145.  
  146.    write(out, where(in))
  147.  
  148.    # Write sorted KEY\toffset lines.
  149.  
  150.    if *key_offset_table > 0 then  {
  151.       every write(out, (!sort(key_offset_table))[2])
  152.       return
  153.       }
  154.    else stop("No indexed items found.")
  155. end
  156.