roget.c 2.8 KB

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