roget.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <bio.h>
  4. #include <ctype.h>
  5. #include "dict.h"
  6. /* Roget's Thesaurus from project Gutenberg */
  7. static long Last = 0;
  8. void
  9. rogetprintentry(Entry e, int cmd)
  10. {
  11. int spc;
  12. char c, *p;
  13. spc = 0;
  14. p = e.start;
  15. if(cmd == 'h'){
  16. while(!isspace(*p) && p < e.end)
  17. p++;
  18. while(strncmp(p, " -- ", 4) != 0 && p < e.end){
  19. while(isspace(*p) && p < e.end)
  20. p++;
  21. if (*p == '[' || *p == '{'){
  22. c = (*p == '[')? ']': '}';
  23. while(*p != c && p < e.end)
  24. p++;
  25. p++;
  26. continue;
  27. }
  28. if (isdigit(*p) || ispunct(*p)){
  29. while(!isspace(*p) && p < e.end)
  30. p++;
  31. continue;
  32. }
  33. if (isspace(*p))
  34. spc = 1;
  35. else
  36. if (spc){
  37. outchar(' ');
  38. spc = 0;
  39. }
  40. while(!isspace(*p) && p < e.end)
  41. outchar(*p++);
  42. }
  43. return;
  44. }
  45. while(p < e.end && !isspace(*p))
  46. p++;
  47. while(p < e.end && isspace(*p))
  48. p++;
  49. while (p < e.end){
  50. if (p < e.end -4 && strncmp(p, " -- ", 4) == 0){ /* first line */
  51. outnl(2);
  52. p += 4;
  53. spc = 0;
  54. }
  55. if (p < e.end -2 && strncmp(p, "[ ", 4) == 0){ /* twiddle layout */
  56. outchars(" [");
  57. continue;
  58. }
  59. if (p < e.end -4 && strncmp(p, "&c (", 4) == 0){ /* usefull xref */
  60. if (spc)
  61. outchar(' ');
  62. outchar('/');
  63. while(p < e.end && *p != '(')
  64. p++;
  65. p++;
  66. while(p < e.end && *p != ')')
  67. outchar(*p++);
  68. p++;
  69. while(p < e.end && isspace(*p))
  70. p++;
  71. while(p < e.end && isdigit(*p))
  72. p++;
  73. outchar('/');
  74. continue;
  75. }
  76. if (p < e.end -3 && strncmp(p, "&c ", 3) == 0){ /* less usefull xref */
  77. while(p < e.end && !isdigit(*p))
  78. p++;
  79. while(p < e.end && isdigit(*p))
  80. p++;
  81. continue;
  82. }
  83. if (*p == '\n' && p < (e.end -1)){ /* their newlines */
  84. spc = 0;
  85. p++;
  86. if (isspace(*p)){ /* their continuation line */
  87. while (isspace(*p))
  88. p++;
  89. p--;
  90. }
  91. else{
  92. outnl(2);
  93. }
  94. }
  95. if (spc && *p != ';' && *p != '.' &&
  96. *p != ',' && !isspace(*p)){ /* drop spaces before punct */
  97. spc = 0;
  98. outchar(' ');
  99. }
  100. if (isspace(*p))
  101. spc = 1;
  102. else
  103. outchar(*p);
  104. p++;
  105. }
  106. outnl(0);
  107. }
  108. long
  109. rogetnextoff(long fromoff)
  110. {
  111. int i;
  112. vlong l;
  113. char *p;
  114. Bseek(bdict, fromoff, 0);
  115. Brdline(bdict, '\n');
  116. while ((p = Brdline(bdict, '\n')) != nil){
  117. l = Blinelen(bdict);
  118. if (!isdigit(*p))
  119. continue;
  120. for (i = 0; i < l-4; i++)
  121. if (strncmp(p+i, " -- ", 4) == 0)
  122. return Boffset(bdict)-l;
  123. }
  124. return Boffset(bdict);
  125. }
  126. void
  127. rogetprintkey(void)
  128. {
  129. Bprint(bout, "No pronunciation key.\n");
  130. }