dict.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /******************************************************************************
  2. * This file is part of TinTin++ *
  3. * *
  4. * Copyright 2004-2020 Igor van den Hoven *
  5. * *
  6. * TinTin++ is free software; you can redistribute it and/or modify *
  7. * it under the terms of the GNU General Public License as published by *
  8. * the Free Software Foundation; either version 3 of the License, or *
  9. * (at your option) any later version. *
  10. * *
  11. * This program is distributed in the hope that it will be useful, *
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  14. * GNU General Public License for more details. *
  15. * *
  16. * You should have received a copy of the GNU General Public License *
  17. * along with TinTin++. If not, see https://www.gnu.org/licenses. *
  18. ******************************************************************************/
  19. /******************************************************************************
  20. * T I N T I N + + *
  21. * *
  22. * coded by Igor van den Hoven 2019 *
  23. ******************************************************************************/
  24. #include "tintin.h"
  25. #include "dict.h"
  26. int dictionary_search(char **array, int array_size, char *key)
  27. {
  28. // Copyright 2014-2020 Igor van den Hoven
  29. register int mid, i, bot;
  30. register char val;
  31. bot = 0;
  32. i = array_size - 1;
  33. mid = i / 2;
  34. while (mid)
  35. {
  36. val = strcmp(key, array[i - mid]);
  37. if (val < 0)
  38. {
  39. i -= mid + 1;
  40. }
  41. else if (val > 0)
  42. {
  43. bot = i - mid;
  44. }
  45. else
  46. {
  47. return i - mid;
  48. }
  49. mid = (i - bot) / 2;
  50. }
  51. if (i > bot)
  52. {
  53. val = strcmp(key, array[i]);
  54. if (val > 0)
  55. {
  56. return -1;
  57. }
  58. else if (val < 0)
  59. {
  60. --i;
  61. }
  62. else
  63. {
  64. return i;
  65. }
  66. }
  67. if (!strcmp(key, array[i]))
  68. {
  69. return i;
  70. }
  71. return -1;
  72. }
  73. void dictionary_lowerstring(char *in, char *out)
  74. {
  75. char *pti, *pto;
  76. pti = in;
  77. pto = out;
  78. while (*pti)
  79. {
  80. if (isalpha(*pti))
  81. {
  82. *pto++ = tolower(*pti++);
  83. }
  84. else
  85. {
  86. pti++;
  87. }
  88. }
  89. *pto = 0;
  90. }
  91. int spellcheck_count(struct session *ses, char *in)
  92. {
  93. char *arg, arg1[BUFFER_SIZE], arg2[BUFFER_SIZE];
  94. int cnt, hash, index;
  95. cnt = 0;
  96. arg = in;
  97. while (*arg)
  98. {
  99. arg = get_arg_in_braces(ses, arg, arg1, GET_ONE);
  100. dictionary_lowerstring(arg1, arg2);
  101. if (isalpha(*arg2))
  102. {
  103. hash = *arg2 - 'a';
  104. index = dictionary_search(wordlist[hash], wordlist_size[hash], arg2 + 1);
  105. if (index == -1)
  106. {
  107. cnt++;
  108. }
  109. }
  110. if (*arg == COMMAND_SEPARATOR)
  111. {
  112. arg++;
  113. }
  114. }
  115. return cnt;
  116. }
  117. DO_COMMAND(do_dictionary)
  118. {
  119. char arg1[BUFFER_SIZE], arg2[BUFFER_SIZE], arg3[BUFFER_SIZE];
  120. int hash, size, index;
  121. sub_arg_in_braces(ses, arg, arg1, GET_ALL, SUB_VAR|SUB_FUN);
  122. if (*arg1 == 0 || !isalpha(*arg1))
  123. {
  124. show_message(ses, LIST_COMMAND, "#SYNTAX: #DICTIONARY {WORD}");
  125. return ses;
  126. }
  127. arg = arg1;
  128. while (*arg)
  129. {
  130. arg = get_arg_in_braces(ses, arg, arg2, GET_ONE);
  131. dictionary_lowerstring(arg2, arg3);
  132. if (isalpha(*arg3))
  133. {
  134. hash = *arg3 - 'a';
  135. size = wordlist_size[hash];
  136. index = dictionary_search(wordlist[hash], size, arg3 + 1);
  137. if (index == -1)
  138. {
  139. tintin_printf2(ses, "\e[1;31m%s", arg2);
  140. }
  141. else
  142. {
  143. tintin_printf2(ses, "\e[1;32m%s", arg2);
  144. }
  145. }
  146. if (*arg == COMMAND_SEPARATOR)
  147. {
  148. arg++;
  149. }
  150. }
  151. return ses;
  152. }