mkindex.c 1.8 KB

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