home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 3 / 3168 / idxtext.icn < prev    next >
Encoding:
Text File  |  1991-04-08  |  3.4 KB  |  121 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.11
  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 [-a] file1 [file2 [...]]," where file1,
  20. #  file2, etc are the names of gettext-format files that are to be
  21. #  (re-)indexed.  The -a flag tells idxtext to abort if an index file
  22. #  already exists.
  23. #
  24. #      Indexed files have a very simple format: keyname tab offset
  25. #  [tab offset [etc.]]\n.  The first line of the index file is a
  26. #  pointer to the last indexed byte of the text-base file it indexes.
  27. #
  28. #  BUGS: Index files are too large.  Also, I've yet to find a portable
  29. #  way of creating unique index names that are capable of being
  30. #  uniquely identified with their original text file.  It might be
  31. #  sensible to hard code the name into the index.  The chances of a
  32. #  conflict seem remote enough that I haven't bothered.  If you're
  33. #  worried, use the -a flag.
  34. #
  35. ############################################################################
  36. #
  37. #  Links: ./adjuncts.icn
  38. #  Requires: UNIX or MS-DOS
  39. #  See also: gettext.icn
  40. #
  41. ############################################################################
  42.  
  43.  
  44. # declared in adjuncts.icn
  45. # global _slash, _baselen
  46.  
  47. procedure main(a)
  48.  
  49.     local ABORT, idxfile_name, fname, infile, outfile
  50.     initial {
  51.     if find("UNIX", &features) then {
  52.         _slash := "/"
  53.         _baselen := 10
  54.     }
  55.     else if find("MS-DOS", &features) then {
  56.         _slash := "\\"
  57.         _baselen := 8
  58.     }
  59.     else stop("idxtext:  OS not supported")
  60.     }
  61.  
  62.     if \a[1] == "-a" then ABORT := pop(a)    
  63.  
  64.     # Check to see if we have any arguments.
  65.     *a = 0 & stop("usage: idxtext [-a] file1 [file2 [...]]")
  66.  
  67.     # Start popping filenames off of the argument list.
  68.     while fname := pop(a) do {
  69.  
  70.     # Open input file.
  71.     infile := open(fname) |
  72.         { write(&errout, "idxtext:  ",fname," not found"); next }
  73.     # Get index file name.
  74.     idxfile_name := Pathname(fname) || getidxname(fname)
  75.     if \ABORT then if close(open(idxfile_name)) then
  76.         stop("idxtext:  index file ",idxfile_name, " already exists")
  77.     outfile := open(idxfile_name, "w") |
  78.         stop("idxtext:  can't open ", idxfile_name)
  79.  
  80.     # Write index to index.IDX file.
  81.     write_index(infile, outfile)
  82.  
  83.     every close(infile | outfile)
  84.  
  85.     }
  86.  
  87. end
  88.  
  89.  
  90. procedure write_index(in, out)
  91.  
  92.     local key_offset_table, w, line, KEY
  93.  
  94.     # Write to out all keys in file "in," with their byte
  95.     # offsets.
  96.  
  97.     key_offset_table := table()
  98.  
  99.     while (w := where(in), line := read(in)) do {
  100.     line ? {
  101.         if ="::" then {
  102.         KEY := trim(tab(0))
  103.         if not (/key_offset_table[KEY] := KEY || "\t" || w)
  104.         then stop("idxtext:  duplicate key, ",KEY)
  105.         }
  106.     }
  107.     }
  108.  
  109.     # First line of index contains the offset of the last
  110.     # indexed byte in write_index, so that we can still
  111.     # search unindexed parts of in.
  112.     write(out, where(in))
  113.  
  114.     # Write sorted KEY\toffset lines.
  115.     if *key_offset_table > 0 then
  116.     every write(out, (!sort(key_offset_table))[2])
  117.  
  118.     return
  119.  
  120. end
  121.