mkindex.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include <u.h>
  10. #include <libc.h>
  11. #include <bio.h>
  12. #include "dict.h"
  13. /*
  14. * Use this to start making an index for a new dictionary.
  15. * Get the dictionary-specific nextoff and printentry(_,'h')
  16. * commands working, add a record to the dicts[] array below,
  17. * and run this program to get a list of offset,headword
  18. * pairs
  19. */
  20. Biobuf boutbuf;
  21. Biobuf *bdict;
  22. Biobuf *bout = &boutbuf;
  23. int linelen;
  24. int breaklen = 2000;
  25. int outinhibit;
  26. int debug;
  27. Dict *dict; /* current dictionary */
  28. Entry getentry(int32_t);
  29. void
  30. main(int argc, char **argv)
  31. {
  32. int i;
  33. long a, ae;
  34. char *p;
  35. Entry e;
  36. Binit(&boutbuf, 1, OWRITE);
  37. dict = &dicts[0];
  38. ARGBEGIN {
  39. case 'd':
  40. p = ARGF();
  41. dict = 0;
  42. if(p) {
  43. for(i=0; dicts[i].name; i++)
  44. if(strcmp(p, dicts[i].name)==0) {
  45. dict = &dicts[i];
  46. break;
  47. }
  48. }
  49. if(!dict) {
  50. err("unknown dictionary: %s", p);
  51. exits("nodict");
  52. }
  53. break;
  54. case 'D':
  55. debug++;
  56. break;
  57. ARGEND }
  58. USED(argc,argv);
  59. bdict = Bopen(dict->path, OREAD);
  60. ae = Bseek(bdict, 0, 2);
  61. if(!bdict) {
  62. err("can't open dictionary %s", dict->path);
  63. exits("nodict");
  64. }
  65. for(a = 0; a < ae; a = (*dict->nextoff)(a+1)) {
  66. linelen = 0;
  67. e = getentry(a);
  68. Bprint(bout, "%ld\t", a);
  69. linelen = 4; /* only has to be approx right */
  70. (*dict->printentry)(e, 'h');
  71. }
  72. exits(0);
  73. }
  74. Entry
  75. getentry(int32_t b)
  76. {
  77. int32_t e, n, dtop;
  78. static Entry ans;
  79. static int anslen = 0;
  80. e = (*dict->nextoff)(b+1);
  81. ans.doff = b;
  82. if(e < 0) {
  83. dtop = Bseek(bdict, 0L, 2);
  84. if(b < dtop) {
  85. e = dtop;
  86. } else {
  87. err("couldn't seek to entry");
  88. ans.start = 0;
  89. ans.end = 0;
  90. }
  91. }
  92. n = e-b;
  93. if(n) {
  94. if(n > anslen) {
  95. ans.start = realloc(ans.start, n);
  96. if(!ans.start) {
  97. err("out of memory");
  98. exits("nomem");
  99. }
  100. anslen = n;
  101. }
  102. Bseek(bdict, b, 0);
  103. n = Bread(bdict, ans.start, n);
  104. ans.end = ans.start + n;
  105. }
  106. return ans;
  107. }