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