home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 3 / 3001 / idxtext.icn < prev    next >
Encoding:
Text File  |  1991-03-07  |  2.7 KB  |  110 lines

  1. ############################################################################
  2. #
  3. #    Name:     idxtext.icn
  4. #
  5. #    Title:     idxtext (index text-base for gettext() routine)
  6. #
  7. #    Author:     Richard L. Goerwitz
  8. #
  9. #    Version: 1.9
  10. #
  11. ############################################################################
  12. #
  13. #      Idxtext turns a file associated with gettext() routine into an
  14. #  indexed text- base.  Though gettext() will work fine with files
  15. #  that haven't been indexed via idxtext(), access is faster if the
  16. #  indexing is done if the file is, say, over 10k (on my system the
  17. #  crossover point is actually about 5k).
  18. #
  19. #      Usage is simply "idxtext file1 [file2 [...]]," where file1,
  20. #  file2, etc are the names of gettext-format files that are to be
  21. #  (re-)indexed.
  22. #
  23. #      Indexed files have a very simple format: keyname tab offset
  24. #  [tab offset [etc.]]\n.  The first line of the index file is a
  25. #  pointer to the last indexed byte of the text-base file it indexes.
  26. #
  27. #  BUGS:  Index files are too large.
  28. #
  29. ############################################################################
  30. #
  31. #  Links: ./adjuncts.icn
  32. #
  33. #  Requires: UNIX or MS-DOS
  34. #
  35. #  See also: gettext.icn
  36. #
  37. ############################################################################
  38.  
  39.  
  40. global _slash, _baselen
  41.  
  42. procedure main(a)
  43.  
  44.     local temp_name, fname, infile, outfile
  45.     initial {
  46.     if find("UNIX", &features) then {
  47.         _slash := "/"
  48.         _baselen := 10
  49.     }
  50.     else if find("MS-DOS", &features) then {
  51.         _slash := "\\"
  52.         _baselen := 8
  53.     }
  54.     else stop("idxtext:  OS not supported")
  55.     }
  56.  
  57.     # Check to see if we have any arguments.
  58.     *a = 0 & stop("usage:  idxtext file1 [file2 [...]]")
  59.  
  60.     # Start popping filenames off of the argument list.
  61.     while fname := pop(a) do {
  62.  
  63.     # Open input file.
  64.     infile := open(fname) | stop("idxtext:  ",fname," not found")
  65.     # Get index file name.
  66.     outfile := open(temp_name := Pathname(fname)||getidxname(fname),"w") |
  67.         stop("idxtext:  ",temp_name," not found")
  68.  
  69.     # Write index to index.IDX file.
  70.     write_index(infile, outfile)
  71.  
  72.     every close(infile | outfile)
  73.  
  74.     }
  75.  
  76. end
  77.  
  78.  
  79. procedure write_index(in, out)
  80.  
  81.     local key_offset_table, w, line, KEY
  82.  
  83.     # Write to out all keys in file "in," with their byte
  84.     # offsets.
  85.  
  86.     key_offset_table := table()
  87.  
  88.     while (w := where(in), line := read(in)) do {
  89.     line ? {
  90.         if ="::" then {
  91.         KEY := trim(tab(0))
  92.         if not (/key_offset_table[KEY] := KEY || "\t" || w)
  93.         then stop("idxtext:  duplicate key, ",KEY)
  94.         }
  95.     }
  96.     }
  97.  
  98.     # First line of index contains the offset of the last
  99.     # indexed byte in write_index, so that we can still
  100.     # search unindexed parts of in.
  101.     write(out, where(in))
  102.  
  103.     # Write sorted KEY\toffset lines.
  104.     if *key_offset_table > 0 then
  105.     every write(out, (!sort(key_offset_table))[2])
  106.  
  107.     return
  108.  
  109. end
  110.